// File: 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
subq $0x80, %rsp
movb %r9b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
testb $0x1, -0x29(%rbp)
je 0x15df
movq 0x14a0c(%rip), %rdi     # 0x15fd0
leaq 0x11a55(%rip), %rsi     # 0x13020
callq 0x10d0
movq %rax, %rdi
movq 0x149fe(%rip), %rsi     # 0x15fd8
callq 0x1100
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1b20
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x44(%rbp)
leaq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x50b0
testb $0x1, -0x29(%rbp)
je 0x167e
movq 0x1497c(%rip), %rdi     # 0x15fd0
movq 0x14965(%rip), %rsi     # 0x15fc0
callq 0x1100
movq %rax, %rdi
leaq 0x119cf(%rip), %rsi     # 0x13039
callq 0x10d0
movq %rax, %rdi
movq 0x1495f(%rip), %rsi     # 0x15fd8
callq 0x1100
movq -0x18(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4e30
leaq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x50b0
movl $0x0, -0x54(%rbp)
cmpl $0x10, -0x54(%rbp)
jge 0x16dc
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x169f
movq -0x20(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x10(%rax)
movq -0x20(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x8000, %ecx           # imm = 0x8000
movl %ecx, 0x2c(%rax)
movq -0x20(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x38(%rax)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
movl 0x2c(%rax), %ecx
subl $0x8000, %ecx           # imm = 0x8000
movl %ecx, 0x2c(%rax)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x38(%rax)
leaq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x50b0
leaq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x50b0
movq 0x14873(%rip), %rdi     # 0x15fd0
leaq 0x118ef(%rip), %rsi     # 0x13053
callq 0x10d0
movq $0x0, -0x60(%rbp)
cmpq $0x10, -0x60(%rbp)
jae 0x17b9
movq 0x14851(%rip), %rdi     # 0x15fd0
leaq 0x25a(%rip), %rsi       # 0x19e0
callq 0x1040
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x1130
movq %rax, %rdi
movl $0x20, %esi
callq 0x1110
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1771
movq 0x14810(%rip), %rdi     # 0x15fd0
movq 0x147f9(%rip), %rsi     # 0x15fc0
callq 0x1100
movq 0x147fd(%rip), %rdi     # 0x15fd0
leaq 0x11887(%rip), %rsi     # 0x13061
callq 0x10d0
movq %rax, %rdi
movq 0x147d7(%rip), %rsi     # 0x15fc0
callq 0x1100
movq $0x0, -0x68(%rbp)
cmpq $0x10, -0x68(%rbp)
jae 0x183e
movq 0x147cc(%rip), %rdi     # 0x15fd0
leaq 0x1d5(%rip), %rsi       # 0x19e0
callq 0x1040
movq %rax, %rdi
movq -0x18(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x1130
movq %rax, %rdi
movl $0x20, %esi
callq 0x1110
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x17f6
movq 0x1478b(%rip), %rdi     # 0x15fd0
movq 0x14774(%rip), %rsi     # 0x15fc0
callq 0x1100
movq 0x14778(%rip), %rdi     # 0x15fd0
leaq 0x1180e(%rip), %rsi     # 0x1306d
callq 0x10d0
movq $0x0, -0x70(%rbp)
cmpq $0x10, -0x70(%rbp)
jae 0x18b4
movq 0x14756(%rip), %rdi     # 0x15fd0
leaq 0x15f(%rip), %rsi       # 0x19e0
callq 0x1040
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq -0x70(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x1130
movq %rax, %rdi
movl $0x20, %esi
callq 0x1110
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x186c
movq 0x14715(%rip), %rdi     # 0x15fd0
movq 0x146fe(%rip), %rsi     # 0x15fc0
callq 0x1100
movq 0x14702(%rip), %rdi     # 0x15fd0
leaq 0x117a6(%rip), %rsi     # 0x1307b
callq 0x10d0
movq %rax, %rdi
movq 0x146dc(%rip), %rsi     # 0x15fc0
callq 0x1100
movq $0x0, -0x78(%rbp)
cmpq $0x10, -0x78(%rbp)
jae 0x1939
movq 0x146d1(%rip), %rdi     # 0x15fd0
leaq 0xda(%rip), %rsi        # 0x19e0
callq 0x1040
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x1130
movq %rax, %rdi
movl $0x20, %esi
callq 0x1110
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x18f1
movq 0x14690(%rip), %rdi     # 0x15fd0
movq 0x14679(%rip), %rsi     # 0x15fc0
callq 0x1100
movq 0x1467d(%rip), %rdi     # 0x15fd0
leaq 0x1172d(%rip), %rsi     # 0x13087
callq 0x10d0
movq $0x0, -0x80(%rbp)
cmpq $0x4, -0x80(%rbp)
jae 0x19b7
movq -0x80(%rbp), %rax
movl -0x40(%rbp,%rax,4), %esi
movq 0x14653(%rip), %rdi     # 0x15fd0
callq 0x1130
movq -0x80(%rbp), %rax
movl -0x40(%rbp,%rax,4), %eax
movq -0x80(%rbp), %rcx
cmpl -0x50(%rbp,%rcx,4), %eax
je 0x19a7
movq 0x14635(%rip), %rdi     # 0x15fd0
leaq 0x116f8(%rip), %rsi     # 0x1309a
callq 0x10d0
jmp 0x19a9
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x1967
movq 0x14612(%rip), %rdi     # 0x15fd0
leaq 0x116e2(%rip), %rsi     # 0x130a7
callq 0x10d0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)