// File: /workspace/github_c_makefile_star2_O1/cyb70289[P]tt/tests/number.c
// Function: test_mont
void test_mont(int nl)
{
	struct tt_int *n = rand_int(nl);
	n->buf[0] |= 1;

	int l = _tt_rand() % nl;
	if (l == 0)
		l = 1;
	struct tt_int *a = rand_int(l);
	l = _tt_rand() % nl;
	if (l == 0)
		l = 1;
	struct tt_int *b = rand_int(l);

	/* lambda = (2^31)^(n->msb), (2^63)^(n->msb) */
	struct tt_int *lambda = tt_int_alloc();
	_tt_int_realloc(lambda, n->msb+1);
	lambda->buf[n->msb] = 1;
	lambda->msb = n->msb+1;

	/* u = -1/n % lambda */
	struct tt_int *u = tt_int_alloc();
	tt_int_mod_inv(u, n, lambda);
	if (u->sign == 0)
		tt_int_sub(u, lambda, u);

	/* c = (a * b * lambda) % n */
	struct tt_int *c = tt_int_alloc();
	tt_int_mul(c, a, b);
	tt_int_mul(c, c, lambda);
	tt_int_div(NULL, c, c, n);

	/* a = (a * lambda) % n, b = (b * lambda) % n */
	tt_int_mul(a, a, lambda);
	tt_int_div(NULL, a, a, n);
	tt_int_mul(b, b, lambda);
	tt_int_div(NULL, b, b, n);

	/* d = montgomery(a * b) */
	struct tt_int *d = tt_int_alloc();
	tt_int_mul(d, a, b);
	_tt_int_realloc(d, n->msb*2+1);

	_tt_word *t = malloc((n->msb+1)*3*_tt_word_sz);
	_tt_int_mont_reduce(d->buf, &d->msb, d->buf, d->msb,
			u->buf, u->msb, n->buf, n->msb, t);

	/* c == d? */
	if (_tt_int_cmp_buf(c->buf, c->msb, d->buf, d->msb))
		tt_error("Montgomery failed!");

	free(t);
	tt_int_free(a);
	tt_int_free(b);
	tt_int_free(n);
	tt_int_free(lambda);
	tt_int_free(u);
	tt_int_free(c);
	tt_int_free(d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, %ebp
callq 0x1eeb
movq %rax, %rbx
movq 0x10(%rax), %rax
orq $0x1, (%rax)
callq 0x90d2
xorl %edx, %edx
divl %ebp
movl %edx, %edi
cmpl $0x1, %edx
adcl $0x0, %edi
callq 0x1eeb
movq %rax, %r14
callq 0x90d2
xorl %edx, %edx
divl %ebp
movl %edx, %edi
cmpl $0x1, %edx
adcl $0x0, %edi
callq 0x1eeb
movq %rax, %r12
callq 0x48dc
movq %rax, %r15
movl 0x8(%rbx), %esi
incl %esi
movq %rax, %rdi
callq 0x4952
movq 0x10(%r15), %rax
movslq 0x8(%rbx), %rcx
movq $0x1, (%rax,%rcx,8)
leal 0x1(%rcx), %eax
movl %eax, 0x8(%r15)
callq 0x48dc
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x8118
cmpl $0x0, (%r13)
jne 0x2188
movq %r13, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x7004
callq 0x48dc
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x700e
movq %rbp, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x700e
xorl %edi, %edi
movq %rbp, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x711c
movq %r14, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x700e
xorl %edi, %edi
movq %r14, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x711c
movq %r12, %rdi
movq %r12, %rsi
movq %r15, 0x8(%rsp)
movq %r15, %rdx
callq 0x700e
xorl %edi, %edi
movq %r12, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x711c
callq 0x48dc
movq %rax, %r15
movq %rax, %rdi
movq %r14, 0x20(%rsp)
movq %r14, %rsi
movq %r12, 0x18(%rsp)
movq %r12, %rdx
callq 0x700e
movl 0x8(%rbx), %eax
leal 0x1(,%rax,2), %esi
movq %r15, %rdi
callq 0x4952
movl 0x8(%rbx), %r12d
leal (%r12,%r12,2), %eax
leal 0x18(,%rax,8), %eax
movslq %eax, %rdi
callq 0x1140
movq %rax, %r14
movq 0x10(%r15), %rdi
leaq 0x8(%r15), %rsi
movl 0x8(%r15), %ecx
movq 0x10(%r13), %r8
movq %r13, 0x10(%rsp)
movl 0x8(%r13), %r9d
subq $0x8, %rsp
movq %rdi, %rdx
pushq %rax
pushq %r12
pushq 0x10(%rbx)
callq 0x815c
addq $0x20, %rsp
movq 0x10(%rbp), %rdi
movl 0x8(%rbp), %esi
movq 0x10(%r15), %rdx
movl 0x8(%r15), %ecx
callq 0x59b9
testl %eax, %eax
je 0x22a7
leaq 0x8de0(%rip), %rsi      # 0xb077
leaq 0x8de3(%rip), %rdx      # 0xb081
xorl %edi, %edi
xorl %eax, %eax
callq 0x8c46
movq %r14, %rdi
callq 0x1030
movq 0x20(%rsp), %rdi
callq 0x493c
movq 0x18(%rsp), %rdi
callq 0x493c
movq %rbx, %rdi
callq 0x493c
movq 0x8(%rsp), %rdi
callq 0x493c
movq 0x10(%rsp), %rdi
callq 0x493c
movq %rbp, %rdi
callq 0x493c
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x493c