// File: /workspace/github_cpp_makefile_star2_O2/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 $0x218, %rsp            # imm = 0x218
movq %r8, %rbp
movq %rcx, %r13
movl %edx, 0xc(%rsp)
movq %rsi, 0x20(%rsp)
movsbl (%r8), %r14d
movswl 0x2(%r8), %eax
addl $0x3f, %eax
pushq $0x40
popq %rcx
cltd
idivl %ecx
movq %rax, 0x10(%rsp)
movsbq %al, %r12
andq $0x0, 0x88(%rsp,%r12,8)
movq %rdi, %rbx
andq $0x0, 0x68(%rsp,%r12,8)
andq $0x0, 0x48(%rsp,%r12,8)
movsbq 0x1(%r8), %r15
leaq 0x150(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x19d5
movl %r14d, 0x8(%rsp)
movslq %r14d, %r14
leaq (%rsp,%r14,8), %rdi
addq $0x150, %rdi            # imm = 0x150
addq %r15, %rbx
movq %rdi, 0x18(%rsp)
movq %rbx, %rsi
movl %r15d, %edx
callq 0x19d5
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x19d5
addq %r15, %r13
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x19d5
movq %rbx, %rdi
movl %r14d, %esi
callq 0x1d5d
xorl %r13d, %r13d
testq %rax, %rax
jne 0x2be6
movq 0x10(%rsp), %r15
movq %r14, 0x28(%rsp)
leaq 0x50(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0x1d5d
testq %rax, %rax
jne 0x2be6
movq %rbp, %r14
leaq 0x28(%rbp), %rbx
movsbl %r15b, %ebp
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x1f1e
cmpb $0x1, %al
jne 0x2be6
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x1f1e
cmpb $0x1, %al
jne 0x2be6
decq %r12
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %rdi, %r13
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x2bfb
andq $0x0, 0xb0(%rsp,%r12,8)
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %edx
movq %r14, %rcx
callq 0x2da6
movq %r12, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %ebp, %r8d
callq 0x2e82
leaq 0xf0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %r13, %rdx
movq %rbx, 0x20(%rsp)
movq %rbx, %rcx
movl %ebp, %r8d
callq 0x2e82
movl %ebp, 0xc(%rsp)
leaq 0x1d0(%rsp), %r12
movq %r12, %rdi
leaq 0x150(%rsp), %rsi
movl 0x8(%rsp), %ebx
movl %ebx, %edx
callq 0x3017
movq 0x28(%rsp), %rax
leaq (%rsp,%rax,8), %r13
addq $0x1d0, %r13            # imm = 0x1D0
movq %rax, %r15
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movl %ebx, %edx
callq 0x3017
leaq 0x48(%r14), %rsi
movq %rsi, 0x10(%rsp)
leaq 0x1b0(%rsp), %rbp
movq %rbp, %rdi
movl %ebx, %edx
callq 0x3017
leaq (%r14,%r15,8), %rsi
addq $0x48, %rsi
leaq 0x110(%rsp), %rdi
movl %ebx, %edx
callq 0x3017
leaq 0x8(%r14), %r15
leaq 0xd0(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movl %ebx, %r8d
callq 0x3034
movq %rbp, %rdi
leaq 0x110(%rsp), %rsi
movq %r12, %rdx
movq %r12, %rbp
movq %r13, %rcx
movq %r14, %r8
callq 0x3069
leaq 0xd0(%rsp), %rdi
movq %rdi, %rsi
movq %rdi, %r12
movq %r15, 0x18(%rsp)
movq %r15, %rdx
movl %ebx, %ecx
callq 0x2bfb
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x31b8
andq $0x0, 0x30(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x150(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rbp, 0x48(%rsp)
leaq 0xb0(%rsp), %rdi
movl 0xc(%rsp), %ebp
movl %ebp, %esi
callq 0x3223
movl %eax, %ebx
leaq 0xf0(%rsp), %rdi
movl %ebp, %esi
callq 0x3223
movl %eax, %ebp
cmpw %ax, %bx
cmovgl %ebx, %ebp
leal -0x1(%rbp), %eax
movswl %ax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0xb0(%rsp,%rcx,8), %rdx
xorl %esi, %esi
btq %rax, %rdx
setb %sil
movq 0xf0(%rsp,%rcx,8), %rcx
xorl %edx, %edx
btq %rax, %rcx
setb %dl
shll $0x4, %edx
leal (%rdx,%rsi,8), %eax
movq 0x30(%rsp,%rax), %rbx
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movl 0x8(%rsp), %edx
callq 0x3017
movq 0x28(%rsp), %rax
leaq (%rbx,%rax,8), %rsi
movl 0x8(%rsp), %ebx
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movl %ebx, %edx
callq 0x3017
movq %r12, %rdi
movl %ebx, %esi
callq 0x3272
movq $0x1, (%r12)
addl $-0x2, %ebp
testw %bp, %bp
js 0x2b64
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq *0xa8(%r14)
movzwl %bp, %eax
movq %rax, %rcx
shrq $0x6, %rcx
movq 0xb0(%rsp,%rcx,8), %rdx
xorl %esi, %esi
btq %rax, %rdx
setb %sil
movq 0xf0(%rsp,%rcx,8), %rcx
xorl %edx, %edx
btq %rax, %rcx
setb %dl
shll $0x4, %edx
leal (%rdx,%rsi,8), %eax
movq %r15, %rbx
movq 0x30(%rsp,%rax), %r15
testq %r15, %r15
je 0x2b5a
movq %r14, %rbx
movq %r13, %r14
leaq 0x1b0(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, 0x10(%rsp)
movl 0x8(%rsp), %ebp
movl %ebp, %edx
callq 0x3017
movq 0x28(%rsp), %rax
leaq (%r15,%rax,8), %rsi
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movl %ebp, %edx
callq 0x3017
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x31b8
leaq 0x190(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
movl %ebp, %r8d
movq 0x10(%rsp), %rbp
callq 0x3034
movq %r13, %rdi
movq %r14, %r13
movq %rbx, %r14
leaq 0x130(%rsp), %rbx
movq %r15, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r14, %r8
callq 0x3069
movq %r12, %rdi
movq %r12, %rsi
leaq 0x190(%rsp), %rdx
movq %r14, %rcx
callq 0x328d
decl %ebp
movq %rbx, %r15
jmp 0x2a52
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
movl 0x8(%rsp), %ecx
callq 0x2bfb
leaq 0x90(%rsp), %r15
leaq 0x130(%rsp), %rsi
movq %r15, %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x31b8
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movl 0xc(%rsp), %ebp
movl %ebp, %edx
callq 0x1f1e
cmpb $0x1, %al
je 0x2bcd
leaq 0x90(%rsp), %rdi
movq %rdi, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x1e48
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl 0x8(%rsp), %edx
callq 0x1f59
movq %rax, %r13
movl %r13d, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq