// File: /workspace/github_cpp_makefile_star2_O2/occivink[P]pyct/monocypher.cpp
// Function: crypto_x25519_inverse
void crypto_x25519_inverse(u8 blind_salt [32], const u8 private_key[32],
                           const u8 curve_point[32])
{
    static const  u8 Lm2[32] = { // L - 2
        0xeb, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2,
        0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, };
    // 1 in Montgomery form
    u32 m_inv [8] = {0x8d98951d, 0xd6ec3174, 0x737dcf70, 0xc6ef5bf4,
                     0xfffffffe, 0xffffffff, 0xffffffff, 0x0fffffff,};

    u8 scalar[32];
    COPY(scalar, private_key, 32);
    trim_scalar(scalar);

    // Convert the scalar in Montgomery form
    // m_scl = scalar * 2^256 (modulo L)
    u32 m_scl[8];
    {
        u32 tmp[16];
        ZERO(tmp, 8);
        load32_le_buf(tmp+8, scalar, 8);
        mod_l(scalar, tmp);
        load32_le_buf(m_scl, scalar, 8);
        WIPE_BUFFER(tmp); // Wipe ASAP to save stack space
    }

    // Compute the inverse
    u32 product[16];
    for (int i = 252; i >= 0; i--) {
        ZERO(product, 16);
        multiply(product, m_inv, m_inv);
        redc(m_inv, product);
        if (scalar_bit(Lm2, i)) {
            ZERO(product, 16);
            multiply(product, m_inv, m_scl);
            redc(m_inv, product);
        }
    }
    // Convert the inverse *out* of Montgomery form
    // scalar = m_inv / 2^256 (modulo L)
    COPY(product, m_inv, 8);
    ZERO(product + 8, 8);
    redc(m_inv, product);
    store32_le_buf(scalar, m_inv, 8); // the *inverse* of the scalar

    // Clear the cofactor of scalar:
    //   cleared = scalar * (3*L + 1)      (modulo 8*L)
    //   cleared = scalar + scalar * 3 * L (modulo 8*L)
    // Note that (scalar * 3) is reduced modulo 8, so we only need the
    // first byte.
    add_xl(scalar, scalar[0] * 3);

    // Recall that 8*L < 2^256. However it is also very close to
    // 2^255. If we spanned the ladder over 255 bits, random tests
    // wouldn't catch the off-by-one error.
    scalarmult(blind_salt, scalar, curve_point, 256);

    WIPE_BUFFER(scalar);   WIPE_BUFFER(m_scl);
    WIPE_BUFFER(product);  WIPE_BUFFER(m_inv);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, 0x8(%rsp)
movq %rdi, (%rsp)
movaps 0x4bca(%rip), %xmm0     # 0xced0
movaps %xmm0, 0x40(%rsp)
movaps 0x4bae(%rip), %xmm0     # 0xcec0
movaps %xmm0, 0x30(%rsp)
xorl %eax, %eax
cmpq $0x20, %rax
je 0x832b
movb (%rsi,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x8319
andb $-0x8, 0x10(%rsp)
movb 0x2f(%rsp), %al
andb $0x3f, %al
orb $0x40, %al
movb %al, 0x2f(%rsp)
xorl %eax, %eax
cmpq $0x8, %rax
je 0x834e
andl $0x0, 0x50(%rsp,%rax,4)
incq %rax
jmp 0x833e
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %rbp
pushq $0x8
popq %r15
movq %rbp, %rsi
movq %r15, %rdx
callq 0x264b
leaq 0x50(%rsp), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x84d1
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x264b
pushq $0x40
popq %rsi
movq %r12, %rdi
callq 0x25a4
movl $0xfc, %ebx
leaq 0x30(%rsp), %rbp
leaq 0x4af7(%rip), %r14      # 0xcea0
xorl %eax, %eax
testl %ebx, %ebx
jns 0x83b9
jmp 0x8426
andl $0x0, 0x50(%rsp,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x83b1
movq %r12, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x860a
movq %rbp, %rdi
movq %r12, %rsi
callq 0x8655
movl %ebx, %eax
shrl $0x3, %eax
movzbl (%rax,%r14), %eax
movl %ebx, %ecx
andl $0x7, %ecx
btl %ecx, %eax
jae 0x8417
xorl %eax, %eax
cmpq $0x10, %rax
je 0x83fe
andl $0x0, 0x50(%rsp,%rax,4)
incq %rax
jmp 0x83ee
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x860a
movq %rbp, %rdi
movq %r12, %rsi
callq 0x8655
decl %ebx
jmp 0x83a9
movl 0x30(%rsp,%rax,4), %ecx
movl %ecx, 0x50(%rsp,%rax,4)
incq %rax
cmpq $0x8, %rax
jne 0x841b
jmp 0x8437
andl $0x0, 0x50(%rsp,%r15,4)
incq %r15
cmpq $0x10, %r15
jne 0x842e
leaq 0x30(%rsp), %r15
leaq 0x50(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x8655
leaq 0x10(%rsp), %r13
pushq $0x8
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x287d
imull $0x3, (%r13), %eax
movzbl %al, %esi
movq %r13, %rdi
callq 0x6f95
movq (%rsp), %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movl $0x100, %ecx            # imm = 0x100
callq 0x57ad
pushq $0x20
popq %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x25a4
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x25a4
pushq $0x40
popq %rsi
movq %r12, %rdi
callq 0x25a4
movq %r15, %rdi
movq %rbx, %rsi
callq 0x25a4
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq