// File: /workspace/github_cpp_makefile_star2_O3/simap[P]ecctools/lib/uECC.c
// Function: uECC_sign_with_k(unsigned char const*, unsigned char const*, unsigned int, unsigned long*, unsigned char*, uECC_Curve_t const*)
static int uECC_sign_with_k(const uint8_t *private_key,
                            const uint8_t *message_hash,
                            unsigned hash_size,
                            uECC_word_t *k,
                            uint8_t *signature,
                            uECC_Curve curve) {

    uECC_word_t tmp[uECC_MAX_WORDS];
    uECC_word_t s[uECC_MAX_WORDS];
    uECC_word_t *k2[2] = {tmp, s};
#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    uECC_word_t *p = (uECC_word_t *)signature;
#else
    uECC_word_t p[uECC_MAX_WORDS * 2];
#endif
    uECC_word_t carry;
    wordcount_t num_words = curve->num_words;
    wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
    bitcount_t num_n_bits = curve->num_n_bits;

    /* Make sure 0 < k < curve_n */
    if (uECC_vli_isZero(k, num_words) || uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
        return 0;
    }

    carry = regularize_k(k, tmp, s, curve);
    EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
    if (uECC_vli_isZero(p, num_words)) {
        return 0;
    }

    /* If an RNG function was specified, get a random number
       to prevent side channel analysis of k. */
    if (!g_rng_function) {
        uECC_vli_clear(tmp, num_n_words);
        tmp[0] = 1;
    } else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
        return 0;
    }

    /* Prevent side channel analysis of uECC_vli_modInv() to determine
       bits of k / the private key by premultiplying by a random number */
    uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
    uECC_vli_modInv(k, k, curve->n, num_n_words);       /* k = 1 / k' */
    uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */

#if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
    uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
#endif

#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    bcopy((uint8_t *) tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
#else
    uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits)); /* tmp = d */
#endif

    s[num_n_words - 1] = 0;
    uECC_vli_set(s, p, num_words);
    uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */

    bits2int(tmp, message_hash, hash_size, curve);
    uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
    uECC_vli_modMult(s, s, k, curve->n, num_n_words);  /* s = (e + r*d) / k */
    if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
        return 0;
    }
#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    bcopy((uint8_t *) signature + curve->num_bytes, (uint8_t *) s, curve->num_bytes);
#else
    uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
#endif    
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
leaq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x58(%rsp)
movsbq (%r9), %r12
movswl 0x2(%r9), %ebp
leal 0x3f(%rbp), %eax
leal 0x7e(%rbp), %r13d
testl %eax, %eax
cmovnsl %eax, %r13d
testq %r12, %r12
jle 0x2596
movq %r9, %r14
movq %r8, %rbx
movq %rcx, %r15
sarl $0x6, %r13d
xorl %eax, %eax
xorl %ecx, %ecx
orq (%r15,%rax,8), %rcx
incq %rax
cmpq %rax, %r12
jne 0x24d8
xorl %r9d, %r9d
testq %rcx, %rcx
je 0x2599
movq %rdi, 0x40(%rsp)
movq %rsi, 0x48(%rsp)
movl %edx, 0x1c(%rsp)
leaq 0x28(%r14), %rdi
movsbl %r13b, %edx
movq %rdi, 0x10(%rsp)
movq %r15, %rsi
movl %edx, 0xc(%rsp)
callq 0x2399
xorl %r9d, %r9d
cmpb $0x1, %al
jne 0x2599
leaq 0x20(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1b68
leaq 0x48(%r14), %rsi
xorl $0x1, %eax
movq 0x50(%rsp,%rax,8), %rdx
incl %ebp
movswl %bp, %r8d
xorl %ebp, %ebp
leaq 0x80(%rsp), %rdi
xorl %ecx, %ecx
movq %r14, %r9
callq 0x1c27
xorl %eax, %eax
orq 0x80(%rsp,%rbp,8), %rax
incq %rbp
cmpq %rbp, %r12
jne 0x255b
xorl %r9d, %r9d
testq %rax, %rax
movq 0x10(%rsp), %rsi
je 0x2599
cmpq $0x0, 0x6b30(%rip)      # 0x90b0
je 0x25ae
leaq 0x20(%rsp), %rdi
movl 0xc(%rsp), %ebp
movl %ebp, %edx
callq 0x182b
testl %eax, %eax
jne 0x25d5
xorl %r9d, %r9d
movl %r9d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %r13b, %r13b
movl 0xc(%rsp), %ebp
jle 0x25cc
movl %r13d, %edx
andl $0x7f, %edx
shll $0x3, %edx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x1050
movq $0x1, 0x20(%rsp)
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
callq 0x353a
movq %r15, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movl %ebp, %ecx
callq 0x30c7
movq %r15, %rdi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
callq 0x353a
movsbl 0x1(%r14), %eax
testl %eax, %eax
jle 0x2654
movslq %eax, %rdx
leal -0x8(,%rax,8), %esi
decl %eax
xorl %edi, %edi
movl %eax, %ecx
andl $-0x8, %ecx
movq 0x80(%rsp,%rcx), %r8
movl %esi, %ecx
andb $0x38, %cl
shrq %cl, %r8
movb %r8b, (%rbx,%rdi)
incq %rdi
addl $-0x8, %esi
decl %eax
cmpq %rdi, %rdx
jne 0x262e
movswl 0x2(%r14), %edx
leal 0x7(%rdx), %eax
addl $0xe, %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x3, %edx
leaq 0x20(%rsp), %rdi
movq %rdi, %rbp
movq 0x40(%rsp), %rsi
callq 0x1b04
movsbq %r13b, %rax
movq $0x0, 0x58(%rsp,%rax,8)
shlq $0x3, %r12
leaq 0x60(%rsp), %r13
leaq 0x80(%rsp), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x1070
movq %r13, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq 0x10(%rsp), %r12
movq %r12, %rcx
movl 0xc(%rsp), %ebp
movl %ebp, %r8d
callq 0x353a
leaq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl 0x1c(%rsp), %edx
movq %r14, %rcx
callq 0x3420
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
movl %ebp, %r8d
callq 0x4143
movq %r13, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl %ebp, %r8d
callq 0x353a
movq %r13, %rdi
movl %ebp, %esi
callq 0x3aad
movswl %ax, %ecx
movsbl 0x1(%r14), %eax
leal (,%rax,8), %edx
cmpl %ecx, %edx
movl $0x0, %r9d
jl 0x2599
movslq %eax, %rsi
movl $0x1, %r9d
testb %sil, %sil
jle 0x2599
addq %rsi, %rbx
addl $-0x8, %edx
decl %eax
xorl %edi, %edi
movl %eax, %ecx
andl $-0x8, %ecx
movq 0x60(%rsp,%rcx), %r8
movl %edx, %ecx
andb $0x38, %cl
shrq %cl, %r8
movb %r8b, (%rbx,%rdi)
incq %rdi
addl $-0x8, %edx
decl %eax
cmpq %rdi, %rsi
jne 0x2742
jmp 0x2599