// File: /workspace/github_cpp_makefile_star2_O2/Finsenty54[P]fast_collision/my_first_try.cpp
// Function: find_collision(unsigned int const*, unsigned int*, unsigned int*, unsigned int*, unsigned int*, bool)
void find_collision(const uint32 IV[], uint32 msg1block0[], uint32 msg1block1[], uint32 msg2block0[], uint32 msg2block1[], bool verbose)
{
	if (verbose)
		cout << "Generating first block: " << flush;
	find_block0(msg1block0, IV);

	uint32 IHV[4] = { IV[0], IV[1], IV[2], IV[3] };
    uint32 IHV_TE[4] = { IV[0], IV[1], IV[2], IV[3] };
    //计算得到新的IHV
	md5_compress(IHV, msg1block0);

	if (verbose)
		cout << endl << "Generating second block: " << flush;
	find_block1(msg1block1, IHV);

    md5_compress(IHV, msg1block1);

	for (int t = 0; t < 16; ++t)
	{
		msg2block0[t] = msg1block0[t];
		msg2block1[t] = msg1block1[t];
	}

	//加上差分，得到m2
	msg2block0[4] += 1 << 31; msg2block0[11] += 1 << 15; msg2block0[14] += 1 << 31;
	msg2block1[4] += 1 << 31; msg2block1[11] -= 1 << 15; msg2block1[14] += 1 << 31;
	
    md5_compress(IHV_TE,msg2block0);
    md5_compress(IHV_TE,msg2block1);

    cout<<"\n\nmsg1block0\n";
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg1block0[i]<<' ';
    }
    cout<<endl;
    cout<<"\nmsg1block1"<<endl;
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg1block1[i]<<' ';
    }
    cout<<endl;

    cout<<"\n\nmsg2block0\n";
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg2block0[i]<<' ';
    }
    cout<<endl;
    cout<<"\nmsg2block1"<<endl;
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg2block1[i]<<' ';
    }
    cout<<endl;

    //验证消息是否碰撞
    cout<<"\n碰撞hash值： ";
    for (size_t i = 0; i < 4; i++)
    {
        cout<<IHV[i];
        if (IHV[i]!=IHV_TE[i])
        {
            cout<<"not success\n";
        }
        
    }
    cout<<"\n\n时间：\n";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r12d
movq %r8, %r14
movq %rcx, -0x30(%rbp)
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
testl %r9d, %r9d
je 0x152d
movq 0x9abf(%rip), %rdi      # 0xafd8
leaq 0x7b00(%rip), %rsi      # 0x9020
callq 0x10e0
movq %rax, %rdi
callq 0x10a0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x17f4
movl (%rbx), %eax
leaq -0x50(%rbp), %rdi
movl %eax, (%rdi)
movl 0x4(%rbx), %ecx
movl %ecx, 0x4(%rdi)
movl 0x8(%rbx), %edx
movl %edx, 0x8(%rdi)
movl 0xc(%rbx), %esi
movl %esi, 0xc(%rdi)
movl %eax, -0x40(%rbp)
movl %ecx, -0x3c(%rbp)
movl %edx, -0x38(%rbp)
movl %esi, -0x34(%rbp)
movq %r13, %rsi
callq 0x2c40
testb %r12b, %r12b
je 0x158e
movq 0x9a66(%rip), %rdi      # 0xafd8
callq 0x1070
leaq 0x7abb(%rip), %rsi      # 0x9039
movq %rax, %rdi
callq 0x10e0
movq %rax, %rdi
callq 0x10a0
leaq -0x50(%rbp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2af0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2c40
xorl %eax, %eax
movq -0x30(%rbp), %rdx
cmpq $0x10, %rax
je 0x15c9
movl (%r13,%rax,4), %ecx
movl %ecx, (%rdx,%rax,4)
movl (%r15,%rax,4), %ecx
movl %ecx, (%r14,%rax,4)
incq %rax
jmp 0x15ae
movq -0x30(%rbp), %rsi
xorb $-0x80, 0x13(%rsi)
addl $0x8000, 0x2c(%rsi)     # imm = 0x8000
xorb $-0x80, 0x3b(%rsi)
xorb $-0x80, 0x13(%r14)
addl $0xffff8000, 0x2c(%r14) # imm = 0xFFFF8000
xorb $-0x80, 0x3b(%r14)
leaq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x2c40
movq %rbx, %rdi
movq %r14, -0x58(%rbp)
movq %r14, %rsi
callq 0x2c40
movq 0x99c8(%rip), %r12      # 0xafd8
leaq 0x7a3c(%rip), %rsi      # 0x9053
movq %r12, %rdi
callq 0x10e0
xorl %r14d, %r14d
pushq $0x20
popq %rbx
cmpq $0x10, %r14
je 0x165f
movq (%r12), %rax
movq -0x18(%rax), %rax
movl 0x18(%r12,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r12,%rax)
movl (%r13,%r14,4), %esi
movq %r12, %rdi
callq 0x1090
movq %rax, %rdi
movl %ebx, %esi
callq 0x1110
incq %r14
jmp 0x1625
movq %r12, %rdi
callq 0x1070
leaq 0x79f3(%rip), %rsi      # 0x9061
movq %r12, %rdi
callq 0x10e0
movq %rax, %rdi
callq 0x1070
xorl %r14d, %r14d
pushq $0x20
popq %rbx
cmpq $0x10, %r14
je 0x16bd
movq (%r12), %rax
movq -0x18(%rax), %rax
movl 0x18(%r12,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r12,%rax)
movl (%r15,%r14,4), %esi
movq %r12, %rdi
callq 0x1090
movq %rax, %rdi
movl %ebx, %esi
callq 0x1110
incq %r14
jmp 0x1684
movq %r12, %rdi
callq 0x1070
leaq 0x79a1(%rip), %rsi      # 0x906d
movq %r12, %rdi
callq 0x10e0
xorl %r14d, %r14d
pushq $0x20
popq %rbx
cmpq $0x10, %r14
je 0x1717
movq (%r12), %rax
movq -0x18(%rax), %rax
movl 0x18(%r12,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r12,%rax)
movq -0x30(%rbp), %rax
movl (%rax,%r14,4), %esi
movq %r12, %rdi
callq 0x1090
movq %rax, %rdi
movl %ebx, %esi
callq 0x1110
incq %r14
jmp 0x16da
movq %r12, %rdi
callq 0x1070
leaq 0x7955(%rip), %rsi      # 0x907b
movq %r12, %rdi
callq 0x10e0
movq %rax, %rdi
callq 0x1070
xorl %r14d, %r14d
pushq $0x20
popq %rbx
cmpq $0x10, %r14
je 0x1779
movq (%r12), %rax
movq -0x18(%rax), %rax
movl 0x18(%r12,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r12,%rax)
movq -0x58(%rbp), %rax
movl (%rax,%r14,4), %esi
movq %r12, %rdi
callq 0x1090
movq %rax, %rdi
movl %ebx, %esi
callq 0x1110
incq %r14
jmp 0x173c
movq 0x9858(%rip), %rbx      # 0xafd8
movq %rbx, %rdi
callq 0x1070
leaq 0x78f8(%rip), %rsi      # 0x9087
movq %rbx, %rdi
callq 0x10e0
xorl %r15d, %r15d
leaq 0x78f9(%rip), %r14      # 0x909a
cmpq $0x4, %r15
je 0x17d0
movl -0x50(%rbp,%r15,4), %esi
movq %rbx, %rdi
callq 0x1090
movl -0x50(%rbp,%r15,4), %eax
cmpl -0x40(%rbp,%r15,4), %eax
je 0x17cb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10e0
incq %r15
jmp 0x17a1
movq 0x9801(%rip), %rdi      # 0xafd8
leaq 0x78c9(%rip), %rsi      # 0x90a7
callq 0x10e0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop