// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1c8, %rsp            # imm = 0x1C8
movq %r8, %r13
movq %rcx, %r15
movq %rcx, (%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
callq 0x40827c
movl $0x20, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x409a57
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x408304
movq %rbp, %rdi
callq 0x40aafc
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x40ab25
movq %r12, %rdi
callq 0x40827c
leaq 0x50(%rsp), %rsi
movl $0x20, %edx
movq %r12, %rdi
callq 0x409a57
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x409a57
movq %rbp, %rdi
movq %r12, %rsi
callq 0x408304
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x40aa42
movq %r15, %rdi
callq 0x40a9eb
leaq 0xb0(%rsp), %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
callq 0x407448
leaq 0xd0(%rsp), %rsi
movl -0x20(%rsi), %edx
andl $0x1, %edx
movq %rbx, %rdi
callq 0x40ab57
movq %r12, %rdi
callq 0x40827c
movl $0x20, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x409a57
addq $0x20, %r14
movl $0x20, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x409a57
movq %r12, %rdi
movq (%rsp), %rsi
movq %r13, %rdx
callq 0x409a57
movq %rbp, %rdi
movq %r12, %rsi
callq 0x408304
leaq 0x90(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x40aa42
movq %r14, %rdi
movq %r14, %rsi
leaq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x40a947
movq %r14, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x40aa2b
movq %r14, %rdi
callq 0x40a9eb
addq $0x20, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40ab3a
movq %r12, %rdi
xorl %esi, %esi
callq 0x409cec
movq %r15, %rdi
xorl %esi, %esi
callq 0x409cec
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop