// File: /workspace/github_cpp_makefile_star2_O3/simap[P]ecctools/lib/uECC.c
// Function: uECC_verify
int uECC_verify(const uint8_t *public_key,
                const uint8_t *message_hash,
                unsigned hash_size,
                const uint8_t *signature,
                uECC_Curve curve) {
    uECC_word_t u1[uECC_MAX_WORDS], u2[uECC_MAX_WORDS];
    uECC_word_t z[uECC_MAX_WORDS];
    uECC_word_t sum[uECC_MAX_WORDS * 2];
    uECC_word_t rx[uECC_MAX_WORDS];
    uECC_word_t ry[uECC_MAX_WORDS];
    uECC_word_t tx[uECC_MAX_WORDS];
    uECC_word_t ty[uECC_MAX_WORDS];
    uECC_word_t tz[uECC_MAX_WORDS];
    const uECC_word_t *points[4];
    const uECC_word_t *point;
    bitcount_t num_bits;
    bitcount_t i;
#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    uECC_word_t *_public = (uECC_word_t *)public_key;
#else
    uECC_word_t _public[uECC_MAX_WORDS * 2];
#endif    
    uECC_word_t r[uECC_MAX_WORDS], s[uECC_MAX_WORDS];
    wordcount_t num_words = curve->num_words;
    wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);

    rx[num_n_words - 1] = 0;
    r[num_n_words - 1] = 0;
    s[num_n_words - 1] = 0;

#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    bcopy((uint8_t *) r, signature, curve->num_bytes);
    bcopy((uint8_t *) s, signature + curve->num_bytes, curve->num_bytes);
#else
    uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
    uECC_vli_bytesToNative(
        _public + num_words, public_key + curve->num_bytes, curve->num_bytes);
    uECC_vli_bytesToNative(r, signature, curve->num_bytes);
    uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
#endif

    /* r, s must not be 0. */
    if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
        return 0;
    }

    /* r, s must be < n. */
    if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
            uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
        return 0;
    }

    /* Calculate u1 and u2. */
    uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
    u1[num_n_words - 1] = 0;
    bits2int(u1, message_hash, hash_size, curve);
    uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
    uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */

    /* Calculate sum = G + Q. */
    uECC_vli_set(sum, _public, num_words);
    uECC_vli_set(sum + num_words, _public + num_words, num_words);
    uECC_vli_set(tx, curve->G, num_words);
    uECC_vli_set(ty, curve->G + num_words, num_words);
    uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
    XYcZ_add(tx, ty, sum, sum + num_words, curve);
    uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
    apply_z(sum, sum + num_words, z, curve);

    /* Use Shamir's trick to calculate u1*G + u2*Q */
    points[0] = 0;
    points[1] = curve->G;
    points[2] = _public;
    points[3] = sum;
    num_bits = smax(uECC_vli_numBits(u1, num_n_words),
                    uECC_vli_numBits(u2, num_n_words));

    point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
                   ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
    uECC_vli_set(rx, point, num_words);
    uECC_vli_set(ry, point + num_words, num_words);
    uECC_vli_clear(z, num_words);
    z[0] = 1;

    for (i = num_bits - 2; i >= 0; --i) {
        uECC_word_t index;
        curve->double_jacobian(rx, ry, z, curve);

        index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
        point = points[index];
        if (point) {
            uECC_vli_set(tx, point, num_words);
            uECC_vli_set(ty, point + num_words, num_words);
            apply_z(tx, ty, z, curve);
            uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
            XYcZ_add(tx, ty, rx, ry, curve);
            uECC_vli_modMult_fast(z, z, tz, curve);
        }
    }

    uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
    apply_z(rx, ry, z, curve);

    /* v = x1 (mod n) */
    if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
        uECC_vli_sub(rx, rx, curve->n, num_n_words);
    }

    /* Accept only if v == r. */
    return (int)(uECC_vli_equal(rx, r, num_words));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rcx, %r15
movl %edx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
movswl 0x2(%r8), %ecx
leal 0x3f(%rcx), %eax
addl $0x7e, %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movsbq (%r8), %rbx
sarl $0x6, %ecx
movq %rcx, 0x20(%rsp)
movsbq %cl, %r14
xorl %ebp, %ebp
movq %rbp, 0x28(%rsp,%r14,8)
movq %rbp, 0x78(%rsp,%r14,8)
movq %rbp, 0xb8(%rsp,%r14,8)
movq %r8, (%rsp)
movsbq 0x1(%r8), %r12
leaq 0x180(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x1b04
leaq (%rsp,%rbx,8), %rdi
addq $0x180, %rdi            # imm = 0x180
addq %r12, %r13
movq %rdi, 0x8(%rsp)
movq %r13, %rsi
movl %r12d, %edx
callq 0x1b04
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x1b04
addq %r12, %r15
leaq 0xc0(%rsp), %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x1b04
testq %rbx, %rbx
jle 0x2c40
decq %r14
xorl %eax, %eax
xorl %ecx, %ecx
orq 0x80(%rsp,%rax,8), %rcx
incq %rax
cmpq %rax, %rbx
jne 0x2bd6
testq %rcx, %rcx
je 0x2c3e
xorl %eax, %eax
xorl %ecx, %ecx
orq 0xc0(%rsp,%rax,8), %rcx
incq %rax
cmpq %rax, %rbx
jne 0x2bef
xorl %ebp, %ebp
testq %rcx, %rcx
je 0x2c40
movq (%rsp), %rax
leaq 0x28(%rax), %r15
movzbl 0x20(%rsp), %esi
leaq -0x1(%rsi), %rax
testb %al, %al
js 0x2c40
movl %eax, %edi
andl $0x7f, %edi
shll $0x3, %edi
movq (%r15,%rdi), %rcx
movq 0x80(%rsp,%rdi), %rdi
cmpq %rdi, %rcx
ja 0x2c54
decq %rax
cmpq %rdi, %rcx
jae 0x2c17
jmp 0x2c40
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x1(%rsi), %rax
testb %al, %al
js 0x2c40
movl %eax, %edi
andl $0x7f, %edi
shll $0x3, %edi
movq (%r15,%rdi), %rcx
movq 0xc0(%rsp,%rdi), %rdi
cmpq %rdi, %rcx
ja 0x2c7f
decq %rax
cmpq %rdi, %rcx
jae 0x2c58
jmp 0x2c40
movq %rsi, 0x58(%rsp)
movsbl 0x20(%rsp), %ebp
leaq 0x60(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x30c7
xorl %r13d, %r13d
movq %r13, 0x120(%rsp,%r14,8)
leaq 0x120(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movl 0x18(%rsp), %edx
movq (%rsp), %r12
movq %r12, %rcx
callq 0x3420
movq %r14, %rdi
movq %r14, %rsi
leaq 0x60(%rsp), %r14
movq %r14, %rdx
movq %r15, %rcx
movl %ebp, 0x2c(%rsp)
movl %ebp, %r8d
callq 0x353a
leaq 0x160(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %r14, %rdx
movq %r15, 0x10(%rsp)
movq %r15, %rcx
movl %ebp, %r8d
callq 0x353a
leaq (,%rbx,8), %rbp
leaq 0x1e0(%rsp), %r15
movq %r15, %rdi
leaq 0x180(%rsp), %rsi
movq %rbp, %rdx
callq 0x1070
leaq (%rsp,%rbx,8), %r14
addq $0x1e0, %r14            # imm = 0x1E0
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
callq 0x1070
leaq 0x48(%r12), %rsi
movq %rsi, 0x50(%rsp)
leaq 0x100(%rsp), %rdi
movq %rbp, %rdx
callq 0x1070
leaq (%r12,%rbx,8), %rsi
addq $0x48, %rsi
leaq 0xe0(%rsp), %rdi
movq %rbp, 0x18(%rsp)
movq %rbp, %rdx
callq 0x1070
leaq 0x8(%r12), %rbp
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
leaq 0x100(%rsp), %rdx
movq %rbp, %rcx
movl %ebx, %r8d
callq 0x37ab
leaq 0x100(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %r12, %r8
callq 0x3817
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movq %rdi, %r15
movq %rbp, 0x8(%rsp)
movq %rbp, %rdx
movl %ebx, %ecx
callq 0x30c7
leaq 0x1e0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x3a02
movq %r13, 0xa0(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0x180(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq %rbp, 0xb8(%rsp)
leaq 0x120(%rsp), %rdi
movl 0x2c(%rsp), %ebp
movl %ebp, %esi
callq 0x3aad
movl %eax, %r12d
leaq 0x160(%rsp), %rdi
movl %ebp, %esi
callq 0x3aad
movl %eax, %ebp
cmpw %ax, %r12w
cmovgl %r12d, %ebp
movl %ebp, %eax
decl %eax
movswl %ax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0x120(%rsp,%rcx,8), %rdx
xorl %esi, %esi
btq %rax, %rdx
setb %sil
movq 0x160(%rsp,%rcx,8), %rcx
xorl %edx, %edx
btq %rax, %rcx
setb %dl
shll $0x4, %edx
leal (%rdx,%rsi,8), %eax
movq 0xa0(%rsp,%rax), %rax
movq (%rax,%r13,8), %rcx
movq %rcx, 0x30(%rsp,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x2e8b
leaq (%rax,%rbx,8), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x140(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x2ea2
leaq 0x60(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x1050
movq $0x1, (%r12)
addw $-0x2, %bp
js 0x300f
movzwl %bp, %r14d
leaq 0x60(%rsp), %r12
movq (%rsp), %rbp
leaq 0x30(%rsp), %rdi
leaq 0x140(%rsp), %rsi
movq %r12, %rdx
movq (%rsp), %rcx
callq *0xa8(%rcx)
movl %r14d, %eax
shrl $0x6, %eax
movq 0x120(%rsp,%rax,8), %rcx
xorl %edx, %edx
btq %r14, %rcx
setb %dl
movq 0x160(%rsp,%rax,8), %rax
xorl %ecx, %ecx
btq %r14, %rax
setb %cl
shll $0x4, %ecx
leal (%rcx,%rdx,8), %eax
movq 0xa0(%rsp,%rax), %rax
testq %rax, %rax
je 0x2fff
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x100(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x2f44
leaq (%rax,%rbx,8), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0xe0(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x2f5e
leaq 0x100(%rsp), %r13
movq %r13, %rdi
leaq 0xe0(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x3a02
leaq 0x1c0(%rsp), %rdi
movq %r12, %r15
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r13, %rdx
movq 0x8(%rsp), %rcx
movl %ebx, %r8d
callq 0x37ab
movq %r13, %rdi
leaq 0xe0(%rsp), %rsi
movq %r12, %rdx
movq %r15, %r12
leaq 0x140(%rsp), %rcx
movq %rbp, %r8
callq 0x3817
movsbl (%rbp), %ecx
leaq 0x220(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
leaq 0x1c0(%rsp), %rdx
callq 0x561c
movq %r12, %rdi
movq %r15, %rsi
callq *0xc0(%rbp)
leal -0x1(%r14), %eax
testl %r14d, %r14d
movl %eax, %r14d
jg 0x2ee9
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movl %ebx, %ecx
callq 0x30c7
leaq 0x30(%rsp), %rdi
leaq 0x140(%rsp), %rsi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x3a02
movq 0x58(%rsp), %rdi
leaq -0x1(%rdi), %rax
movq 0x10(%rsp), %r8
testb %al, %al
js 0x306f
movl %eax, %edx
andl $0x7f, %edx
shll $0x3, %edx
movq (%r8,%rdx), %rcx
movq 0x30(%rsp,%rdx), %rdx
cmpq %rdx, %rcx
ja 0x309b
decq %rax
cmpq %rdx, %rcx
jae 0x304d
cmpb $0x0, 0x20(%rsp)
jle 0x309b
xorl %eax, %eax
xorl %ecx, %ecx
movq (%r8,%rax,8), %rdx
addq %rcx, %rdx
xorl %esi, %esi
subq %rdx, 0x30(%rsp,%rax,8)
setb %sil
testq %rdx, %rdx
cmovneq %rsi, %rcx
incq %rax
cmpq %rax, %rdi
jne 0x307a
leal -0x1(%rbx), %eax
movzbl %al, %ecx
xorl %eax, %eax
movq 0x80(%rsp,%rcx,8), %rdx
xorq 0x30(%rsp,%rcx,8), %rdx
orq %rdx, %rax
addq $-0x1, %rcx
jb 0x30a3
xorl %ebp, %ebp
testq %rax, %rax
sete %bpl
jmp 0x2c40