// File: 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
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0x221(%rbp)
movq -0x30(%rbp), %rax
movswl 0x2(%rax), %eax
addl $0x3f, %eax
movl $0x40, %ecx
cltd
idivl %ecx
movb %al, -0x222(%rbp)
movsbl -0x222(%rbp), %eax
subl $0x1, %eax
cltq
movq $0x0, -0xf0(%rbp,%rax,8)
movsbl -0x222(%rbp), %eax
subl $0x1, %eax
cltq
movq $0x0, -0x200(%rbp,%rax,8)
movsbl -0x222(%rbp), %eax
subl $0x1, %eax
cltq
movq $0x0, -0x220(%rbp,%rax,8)
leaq -0x1e0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %edx
callq 0x1d90
leaq -0x1e0(%rbp), %rdi
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %eax
cltq
addq %rax, %rsi
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %edx
callq 0x1d90
leaq -0x200(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %edx
callq 0x1d90
leaq -0x220(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %eax
cltq
addq %rax, %rsi
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %edx
callq 0x1d90
leaq -0x200(%rbp), %rdi
movsbl -0x221(%rbp), %esi
callq 0x22e0
cmpq $0x0, %rax
jne 0x33d7
leaq -0x220(%rbp), %rdi
movsbl -0x221(%rbp), %esi
callq 0x22e0
cmpq $0x0, %rax
je 0x33e3
movl $0x0, -0x4(%rbp)
jmp 0x39b3
movq -0x30(%rbp), %rdi
addq $0x28, %rdi
leaq -0x200(%rbp), %rsi
movsbl -0x222(%rbp), %edx
callq 0x2640
movsbl %al, %eax
cmpl $0x1, %eax
jne 0x3429
movq -0x30(%rbp), %rdi
addq $0x28, %rdi
leaq -0x220(%rbp), %rsi
movsbl -0x222(%rbp), %edx
callq 0x2640
movsbl %al, %eax
cmpl $0x1, %eax
je 0x3435
movl $0x0, -0x4(%rbp)
jmp 0x39b3
leaq -0x90(%rbp), %rdi
leaq -0x220(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq $0x28, %rdx
movsbl -0x222(%rbp), %ecx
callq 0x39c0
movsbl -0x222(%rbp), %eax
subl $0x1, %eax
cltq
movq $0x0, -0x50(%rbp,%rax,8)
leaq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x30(%rbp), %rcx
callq 0x3c10
leaq -0x50(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movq -0x30(%rbp), %rcx
addq $0x28, %rcx
movsbl -0x222(%rbp), %r8d
callq 0x3d60
leaq -0x70(%rbp), %rdi
leaq -0x200(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movq -0x30(%rbp), %rcx
addq $0x28, %rcx
movsbl -0x222(%rbp), %r8d
callq 0x3d60
leaq -0xd0(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0xd0(%rbp), %rdi
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rdi
leaq -0x1e0(%rbp), %rsi
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0x130(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x48, %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0x150(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x48, %rsi
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0x90(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
leaq -0x130(%rbp), %rdx
movq -0x30(%rbp), %rcx
addq $0x8, %rcx
movsbl -0x221(%rbp), %r8d
callq 0x3e00
leaq -0x130(%rbp), %rdi
leaq -0x150(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
leaq -0xd0(%rbp), %rcx
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rcx
movq -0x30(%rbp), %r8
callq 0x3e60
leaq -0x90(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq $0x8, %rdx
movsbl -0x221(%rbp), %ecx
callq 0x39c0
leaq -0xd0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rsi
leaq -0x90(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x3ff0
movq $0x0, -0x190(%rbp)
movq -0x30(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x188(%rbp)
leaq -0x1e0(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x50(%rbp), %rdi
movsbl -0x222(%rbp), %esi
callq 0x40a0
movw %ax, -0x236(%rbp)
leaq -0x70(%rbp), %rdi
movsbl -0x222(%rbp), %esi
callq 0x40a0
movw -0x236(%rbp), %cx
movswl %cx, %edi
movswl %ax, %esi
callq 0x4060
movw %ax, -0x19a(%rbp)
leaq -0x50(%rbp), %rdi
movswl -0x19a(%rbp), %eax
subl $0x1, %eax
movswl %ax, %esi
callq 0x4130
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x234(%rbp)
leaq -0x70(%rbp), %rdi
movswl -0x19a(%rbp), %eax
subl $0x1, %eax
movswl %ax, %esi
callq 0x4130
movq %rax, %rcx
movl -0x234(%rbp), %eax
cmpq $0x0, %rcx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
shll %ecx
orl %ecx, %eax
cltq
movq -0x190(%rbp,%rax,8), %rax
movq %rax, -0x198(%rbp)
leaq -0xf0(%rbp), %rdi
movq -0x198(%rbp), %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0x110(%rbp), %rdi
movq -0x198(%rbp), %rsi
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0x90(%rbp), %rdi
movsbl -0x221(%rbp), %esi
callq 0x4170
movq $0x1, -0x90(%rbp)
movswl -0x19a(%rbp), %eax
subl $0x2, %eax
movw %ax, -0x19c(%rbp)
movswl -0x19c(%rbp), %eax
cmpl $0x0, %eax
jl 0x3911
movq -0x30(%rbp), %rax
movq 0xa8(%rax), %rax
leaq -0xf0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq *%rax
leaq -0x50(%rbp), %rdi
movswl -0x19c(%rbp), %esi
callq 0x4130
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x23c(%rbp)
leaq -0x70(%rbp), %rdi
movswl -0x19c(%rbp), %esi
callq 0x4130
movq %rax, %rcx
movl -0x23c(%rbp), %eax
cmpq $0x0, %rcx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
shll %ecx
orl %ecx, %eax
cltq
movq %rax, -0x230(%rbp)
movq -0x230(%rbp), %rax
movq -0x190(%rbp,%rax,8), %rax
movq %rax, -0x198(%rbp)
cmpq $0x0, -0x198(%rbp)
je 0x38f8
leaq -0x130(%rbp), %rdi
movq -0x198(%rbp), %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0x150(%rbp), %rdi
movq -0x198(%rbp), %rsi
movsbl -0x221(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rsi
movsbl -0x221(%rbp), %edx
callq 0x3db0
leaq -0x130(%rbp), %rdi
leaq -0x150(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x3ff0
leaq -0x170(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
leaq -0x130(%rbp), %rdx
movq -0x30(%rbp), %rcx
addq $0x8, %rcx
movsbl -0x221(%rbp), %r8d
callq 0x3e00
leaq -0x130(%rbp), %rdi
leaq -0x150(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
leaq -0x110(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x3e60
leaq -0x90(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x170(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x41b0
jmp 0x38fa
movw -0x19c(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x19c(%rbp)
jmp 0x3770
leaq -0x90(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq $0x8, %rdx
movsbl -0x221(%rbp), %ecx
callq 0x39c0
leaq -0xf0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x3ff0
movq -0x30(%rbp), %rdi
addq $0x28, %rdi
leaq -0xf0(%rbp), %rsi
movsbl -0x222(%rbp), %edx
callq 0x2640
movsbl %al, %eax
cmpl $0x1, %eax
je 0x3996
leaq -0xf0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq $0x28, %rdx
movsbl -0x222(%rbp), %ecx
callq 0x24b0
leaq -0xf0(%rbp), %rdi
leaq -0x200(%rbp), %rsi
movsbl -0x221(%rbp), %edx
callq 0x26f0
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nop