// File: /workspace/github_cpp_makefile_star2_O2/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
movq %r9, %r14
movq %r8, 0x10(%rsp)
movq %rcx, %r15
movl %edx, 0xc(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %rbp
leaq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x38(%rsp)
movb (%r9), %cl
movswl 0x2(%r9), %ebx
leal 0x3f(%rbx), %eax
pushq $0x40
popq %rsi
cltd
idivl %esi
movl %eax, %r13d
movsbl %cl, %r12d
movq %r15, %rdi
movl %r12d, %esi
callq 0x1d5d
xorl %ecx, %ecx
testq %rax, %rax
jne 0x223d
movl %r12d, 0x8(%rsp)
movq %rbp, 0x20(%rsp)
leaq 0x28(%r14), %r12
movq %r13, 0x18(%rsp)
movsbl %r13b, %r13d
movq %r12, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x2096
xorl %ecx, %ecx
cmpb $0x1, %al
jne 0x223d
leaq 0x60(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1a25
leaq 0x48(%r14), %rsi
xorl $0x1, %eax
movq 0x30(%rsp,%rax,8), %rdx
incl %ebx
movswl %bx, %r8d
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
xorl %ecx, %ecx
movq %r14, %r9
callq 0x1ab8
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0x1d5d
testq %rax, %rax
je 0x2251
xorl %ecx, %ecx
movl %ecx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x5e67(%rip)      # 0x80c0
je 0x2271
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x1776
testl %eax, %eax
jne 0x2288
jmp 0x223b
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x3272
movq $0x1, (%rbx)
leaq 0x60(%rsp), %rbx
movq %r15, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movl %r13d, %r8d
callq 0x2e82
movq %r15, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x2bfb
movq %r15, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movl %r13d, %r8d
callq 0x2e82
movsbl 0x1(%r14), %esi
leaq 0x80(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x1887
movswl 0x2(%r14), %eax
addl $0x7, %eax
pushq $0x8
popq %rcx
cltd
idivl %ecx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movl %eax, %edx
callq 0x19d5
movsbq 0x18(%rsp), %rax
andq $0x0, 0x38(%rsp,%rax,8)
movl %r13d, %ebp
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
movl 0x8(%rsp), %edx
callq 0x3017
movq %r13, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movl %ebp, %r8d
callq 0x2e82
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movl 0xc(%rsp), %edx
movq %r14, %rcx
callq 0x2da6
movq %r13, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movl %ebp, %r8d
callq 0x371b
movq %r13, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl %ebp, %r8d
callq 0x2e82
movq %r13, %rdi
movl %ebp, %esi
callq 0x3223
cwtl
movsbl 0x1(%r14), %esi
leal (,%rsi,8), %ecx
cmpl %eax, %ecx
movl $0x0, %ecx
jl 0x223d
movslq %esi, %rax
movq 0x10(%rsp), %rdi
addq %rax, %rdi
leaq 0x40(%rsp), %rdx
callq 0x1887
pushq $0x1
popq %rcx
jmp 0x223d