// File: /workspace/github_cpp_makefile_star2_O1/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 $0x278, %rsp            # imm = 0x278
movq %rcx, %r15
movl %edx, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
movswl 0x2(%r8), %ecx
leal 0x3f(%rcx), %eax
addl $0x7e, %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movsbq (%r8), %r13
sarl $0x6, %ecx
movq %rcx, 0x28(%rsp)
movsbq %cl, %r14
xorl %ebp, %ebp
movq %rbp, 0x28(%rsp,%r14,8)
movq %rbp, 0x88(%rsp,%r14,8)
movq %rbp, 0xc8(%rsp,%r14,8)
movq %r8, 0x68(%rsp)
movsbq 0x1(%r8), %rbx
leaq 0x190(%rsp), %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x1b19
leaq (%rsp,%r13,8), %rdi
addq $0x190, %rdi            # imm = 0x190
addq %rbx, %r12
movq %rdi, 0x18(%rsp)
movq %r12, %rsi
movl %ebx, %edx
callq 0x1b19
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x1b19
addq %rbx, %r15
leaq 0xd0(%rsp), %rdi
movq %r15, %rsi
movl %ebx, %edx
movq %r13, %rbx
callq 0x1b19
testq %r13, %r13
jle 0x3156
decq %r14
movzbl %bl, %r12d
xorl %ecx, %ecx
xorl %eax, %eax
orq 0x90(%rsp,%rcx,8), %rax
incq %rcx
cmpq %rcx, %r12
jne 0x2bfc
xorl %ebp, %ebp
testb %r12b, %r12b
jle 0x3156
testq %rax, %rax
je 0x3156
xorl %eax, %eax
xorl %ecx, %ecx
orq 0xd0(%rsp,%rax,8), %rcx
incq %rax
cmpq %rax, %r12
jne 0x2c24
xorl %ebp, %ebp
testq %rcx, %rcx
movq 0x68(%rsp), %r13
je 0x3156
leaq 0x28(%r13), %r15
movzbl 0x28(%rsp), %esi
leaq -0x1(%rsi), %rax
testb %al, %al
js 0x3156
movl %eax, %edi
andl $0x7f, %edi
shll $0x3, %edi
movq (%r15,%rdi), %rcx
movq 0x90(%rsp,%rdi), %rdi
cmpq %rdi, %rcx
ja 0x2c7f
decq %rax
cmpq %rdi, %rcx
jae 0x2c51
jmp 0x3156
leaq -0x1(%rsi), %rax
testb %al, %al
js 0x3156
movl %eax, %edi
andl $0x7f, %edi
shll $0x3, %edi
movq (%r15,%rdi), %rcx
movq 0xd0(%rsp,%rdi), %rdi
cmpq %rdi, %rcx
ja 0x2cb1
decq %rax
cmpq %rdi, %rcx
jae 0x2c83
jmp 0x3156
movq %rsi, 0x60(%rsp)
movq %rbx, 0x8(%rsp)
movsbl 0x28(%rsp), %ebx
leaq 0x70(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x316a
movq $0x0, 0x110(%rsp,%r14,8)
leaq 0x110(%rsp), %rbp
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movl 0x10(%rsp), %edx
movq %r13, %rcx
callq 0x3555
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0x70(%rsp), %r14
movq %r14, %rdx
movq %r15, %rcx
movl %ebx, %r8d
callq 0x367a
leaq 0x170(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq %r14, %rdx
movq %r15, 0x58(%rsp)
movq %r15, %rcx
movl %ebx, 0x20(%rsp)
movl %ebx, %r8d
callq 0x367a
testb %r12b, %r12b
jle 0x2d61
leal (,%r12,8), %edx
leaq 0x1f0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x1070
movq 0x8(%rsp), %rax
leaq (%rsp,%rax,8), %rbx
addq $0x1f0, %rbx            # imm = 0x1F0
testb %r12b, %r12b
jle 0x2d8b
leal (,%r12,8), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x1070
movq %rbx, 0x50(%rsp)
leaq 0x48(%r13), %r14
testb %r12b, %r12b
jle 0x2dcd
leal (,%r12,8), %ebx
leaq 0x130(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1070
movq 0x8(%rsp), %rax
leaq (%r14,%rax,8), %rsi
leaq 0xf0(%rsp), %rdi
movq %rbx, %rdx
callq 0x1070
movq %r14, 0x18(%rsp)
leaq 0x8(%r13), %r14
movsbl %r12b, %r8d
leaq 0x70(%rsp), %rbx
leaq 0x1f0(%rsp), %r15
leaq 0x130(%rsp), %rbp
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x38f2
leaq 0xf0(%rsp), %rsi
movq %rbp, %rdi
movq %r15, %rdx
movq 0x50(%rsp), %rbp
movq %rbp, %rcx
movq %r13, %r8
callq 0x3970
movsbl %r12b, %ecx
movq %rbx, %rdi
movq %rbx, %rsi
movq %r14, 0x10(%rsp)
movq %r14, %rdx
callq 0x316a
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x3b5b
movq $0x0, 0xb0(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x190(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq %r15, 0xc8(%rsp)
leaq 0x110(%rsp), %rdi
movl 0x20(%rsp), %ebp
movl %ebp, %esi
callq 0x3c06
movl %eax, %ebx
leaq 0x170(%rsp), %rdi
movl %ebp, %esi
callq 0x3c06
movl %eax, %ebp
cmpw %ax, %bx
cmovgl %ebx, %ebp
leal -0x1(%rbp), %eax
movswl %ax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0x110(%rsp,%rcx,8), %rdx
xorl %esi, %esi
btq %rax, %rdx
setb %sil
movq 0x170(%rsp,%rcx,8), %rcx
xorl %edx, %edx
btq %rax, %rcx
setb %dl
shll $0x4, %edx
leal (%rdx,%rsi,8), %eax
movq 0xb0(%rsp,%rax), %rax
testb %r12b, %r12b
jle 0x2ef8
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x30(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x2ee7
testb %r12b, %r12b
movq 0x8(%rsp), %rcx
jle 0x2f35
leaq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x150(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x2f08
testb %r12b, %r12b
jle 0x2f35
leal (,%r12,8), %edx
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x1050
movq $0x1, 0x70(%rsp)
addw $-0x2, %bp
js 0x308e
movzwl %bp, %r14d
leaq 0x70(%rsp), %rbx
leaq 0x30(%rsp), %rdi
leaq 0x150(%rsp), %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq *0xa8(%r13)
movl %r14d, %eax
shrl $0x6, %eax
movq 0x110(%rsp,%rax,8), %rcx
xorl %edx, %edx
btq %r14, %rcx
setb %dl
movq 0x170(%rsp,%rax,8), %rax
xorl %ecx, %ecx
btq %r14, %rax
setb %cl
shll $0x4, %ecx
leal (%rcx,%rdx,8), %eax
movq 0xb0(%rsp,%rax), %rax
testq %rax, %rax
je 0x307e
testb %r12b, %r12b
jle 0x2fc5
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x130(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x2fb1
testb %r12b, %r12b
jle 0x2fe9
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0xf0(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x2fd5
leaq 0x130(%rsp), %rbp
movq %rbp, %rdi
leaq 0xf0(%rsp), %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x3b5b
movsbl %r12b, %r8d
leaq 0x1d0(%rsp), %rdi
movq %r13, %r15
movq %rbx, %r13
leaq 0x30(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
callq 0x38f2
movq %rbp, %rdi
leaq 0xf0(%rsp), %rsi
movq %rbx, %rdx
movq %r13, %rbx
movq %r15, %r13
leaq 0x150(%rsp), %rcx
movq %r15, %r8
callq 0x3970
movsbl (%r15), %ecx
leaq 0x230(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x1d0(%rsp), %rdx
callq 0x5802
movq %rbx, %rdi
movq %r15, %rsi
callq *0xc0(%r13)
leal -0x1(%r14), %eax
testl %r14d, %r14d
movl %eax, %r14d
jg 0x2f51
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
callq 0x316a
leaq 0x30(%rsp), %rdi
leaq 0x150(%rsp), %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x3b5b
movq 0x60(%rsp), %rdi
leaq -0x1(%rdi), %rcx
movq 0x58(%rsp), %r8
movb $0x1, %al
testb %cl, %cl
js 0x30f6
movl %ecx, %esi
andl $0x7f, %esi
shll $0x3, %esi
movq (%r8,%rsi), %rdx
movq 0x30(%rsp,%rsi), %rsi
cmpq %rsi, %rdx
ja 0x30f4
decq %rcx
cmpq %rsi, %rdx
jae 0x30ce
jmp 0x30f6
xorl %eax, %eax
cmpb $0x0, 0x28(%rsp)
setle %cl
xorb $0x1, %al
orb %cl, %al
jne 0x3129
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 0x3108
movq 0x8(%rsp), %rax
decb %al
movzbl %al, %eax
incq %rax
xorl %ecx, %ecx
movq 0x88(%rsp,%rax,8), %rdx
xorq 0x28(%rsp,%rax,8), %rdx
orq %rdx, %rcx
decq %rax
jg 0x3138
xorl %ebp, %ebp
testq %rcx, %rcx
sete %bpl
movl %ebp, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq