// File: /workspace/github_cpp_makefile_star2_O2/msotoodeh[P]asymmetric-passwords/common/curve25519/ed25519_sign.c
// Function: ed25519_SignMessage
void ed25519_SignMessage(
    unsigned char *signature,           /* OUT: [64 bytes] signature (R,S) */
    const unsigned char *privKey,       /*  IN: [64 bytes] private key (sk,pk) */
    const void *blinding,               /*  IN: [optional] null or blinding context */
    const unsigned char *msg,           /*  IN: [msg_size bytes] message to sign */
    size_t msg_size)
{
    SHA512_CTX H;
    Affine_POINT R;
    U_WORD a[K_WORDS], t[K_WORDS], r[K_WORDS];
    U8 md[SHA512_DIGEST_LENGTH];

    /* [a:b] = H(sk) */
    SHA512_Init(&H);
    SHA512_Update(&H, privKey, 32);
    SHA512_Final(md, &H);
    ecp_TrimSecretKey(md);              /* a = first 32 bytes */
    ecp_BytesToWords(a, md);

    /* r = H(b + m) mod BPO */
    SHA512_Init(&H);
    SHA512_Update(&H, md+32, 32);
    SHA512_Update(&H, msg, msg_size);
    SHA512_Final(md, &H);
    eco_DigestToWords(r, md);
    eco_Mod(r);                         /* r mod BPO */

    /* R = r*P */
    edp_BasePointMultiply(&R, r, blinding);
    ed25519_PackPoint(signature, R.y, R.x[0]); /* R part of signature */

    /* S = r + H(encoded(R) + pk + m) * a  mod BPO */
    SHA512_Init(&H);
    SHA512_Update(&H, signature, 32);   /* encoded(R) */
    SHA512_Update(&H, privKey+32, 32);  /* pk */
    SHA512_Update(&H, msg, msg_size);   /* m */
    SHA512_Final(md, &H);
    eco_DigestToWords(t, md);

    eco_MulReduce(t, t, a);             /* h()*a */
    eco_AddReduce(t, t, r);
    eco_Mod(t);
    ecp_WordsToBytes(signature+32, t);  /* S part of signature */

    /* Clear sensitive data */
    ecp_SetValue(a, 0);
    ecp_SetValue(r, 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %r8, %r13
movq %r8, 0x10(%rsp)
movq %rcx, %r15
movq %rcx, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x100(%rsp), %r12
movq %r12, %rdi
callq 0x407abc
pushq $0x20
popq %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x409252
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x407b44
movq %rbp, %rdi
callq 0x40a2ac
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x40a2d7
movq %r12, %rdi
callq 0x407abc
leaq 0x60(%rsp), %rsi
movq %r12, %rdi
pushq $0x20
popq %rdx
callq 0x409252
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x409252
movq %rbp, %rdi
movq %r12, %rsi
callq 0x407b44
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x40a1f3
movq %r15, %rdi
callq 0x40a19c
leaq 0xc0(%rsp), %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
callq 0x406ccf
leaq 0xe0(%rsp), %rsi
movl -0x20(%rsi), %edx
andl $0x1, %edx
movq %rbx, %rdi
callq 0x40a306
movq %r12, %rdi
callq 0x407abc
movq %r12, %rdi
movq %rbx, %rsi
pushq $0x20
popq %r13
movq %r13, %rdx
callq 0x409252
addq $0x20, %r14
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x409252
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x409252
movq %rbp, %rdi
movq %r12, %rsi
callq 0x407b44
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x40a1f3
movq %r14, %rdi
movq %r14, %rsi
leaq 0x20(%rsp), %r12
movq %r12, %rdx
callq 0x40a0f8
movq %r14, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x40a1dc
movq %r14, %rdi
callq 0x40a19c
addq $0x20, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40a2ee
movq %r12, %rdi
xorl %esi, %esi
callq 0x409494
movq %r15, %rdi
xorl %esi, %esi
callq 0x409494
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)