In the process of Elliptic curve operation, the results of 256-bit addition and subtraction are often outside the interval [0,p], and the operation of +p or-p is required.
The 256-bit NIST domain Elliptic curve parameter p is generated in the following formula:
p = 2^256? 2^224 + 2^192 + 2^96? 1
The formula is broken down by the symbol:
p = (2^256 + 2^192 + 2^96)-(2^224 + 1)
Convert to 16 and press the 64-bit section to change to the following form:
+: 0000000000000001 0000000000000000 0000000100000000 0000000000000000-: 0000000100000000 0000000000000000 0000000000000000 0000000000000001=: ffffffff00000001 0000000000000000 00000000ffffffff ffffffffffffffff
Careful observation reveals that only one 64-bit immediate number is present in the 64-bit addition and subtraction involved:
0000000100000000
Assuming that a 256-bit subtraction eventually occurs borrow, its low 256-bit value is stored in the register R8:r11, and now requires the +p operation to correct it, if the 4 immediate numbers that constitute p are directly calculated, the code is as follows:
ADDQ $0xffffffffffffffff,%r8adcq $0xffffffff,%r9adcq $0x0,%r10adcq $0xffffffff00000001,%r11
By the x64 directive characteristic, this writing is not allowed, only the operation register Rax can use the immediate number greater than 32 bits, through the preceding analysis, we can convert 4 addition to 3 addition and 4 subtraction to achieve, the code is as follows:
Movq $0x100000000,%raxaddq%rax,%r9adcq $0x0,%r10adcq $0x1,%r11#---subq $0x1,%R8SBBQ $,%R9SBBQ $%R10SBBQ,%rax,%r11
Is there any other way? Of course, for example, you can advance the number of prime p 4 64-bit values in the register R12:R15, the code is as follows:
ADDQ%r12,%r8adcq%r13,%r9adcq%r14,%r10adcq%r15,%r11
To sacrifice 4 general-purpose registers for a 256-bit constant is a last resort choice, unless this operation occupies a large part of the overall operation, it is worthwhile to do so, such as the number of p multiplication inverse, more than 80% of the operation is +/-p, it is necessary to save prime p directly in the general register for use at any time.
256-bit NIST optimization details of elliptic curve operations (addition and subtraction of single prime number p)