@@ -14,25 +14,25 @@ var p503 = Fp{ | |||||
} | } | ||||
// 2*503 | // 2*503 | ||||
var p503x2 = Fp{ | |||||
var pX2 = Fp{ | |||||
0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x57FFFFFFFFFFFFFF, | 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x57FFFFFFFFFFFFFF, | ||||
0x2610B7B44423CF41, 0x3737ED90F6FCFB5E, 0xC08B8D7BB4EF49A0, 0x0080CDEA83023C3C, | 0x2610B7B44423CF41, 0x3737ED90F6FCFB5E, 0xC08B8D7BB4EF49A0, 0x0080CDEA83023C3C, | ||||
} | } | ||||
// p503 + 1 | // p503 + 1 | ||||
var p503p1 = Fp{ | |||||
var p1 = Fp{ | |||||
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0xAC00000000000000, | 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0xAC00000000000000, | ||||
0x13085BDA2211E7A0, 0x1B9BF6C87B7E7DAF, 0x6045C6BDDA77A4D0, 0x004066F541811E1E, | 0x13085BDA2211E7A0, 0x1B9BF6C87B7E7DAF, 0x6045C6BDDA77A4D0, 0x004066F541811E1E, | ||||
} | } | ||||
// R^2=(2^512)^2 mod p | // R^2=(2^512)^2 mod p | ||||
var p503R2 = Fp{ | |||||
var pR2 = Fp{ | |||||
0x5289A0CF641D011F, 0x9B88257189FED2B9, 0xA3B365D58DC8F17A, 0x5BC57AB6EFF168EC, | 0x5289A0CF641D011F, 0x9B88257189FED2B9, 0xA3B365D58DC8F17A, 0x5BC57AB6EFF168EC, | ||||
0x9E51998BD84D4423, 0xBF8999CBAC3B5695, 0x46E9127BCE14CDB6, 0x003F6CFCE8B81771, | 0x9E51998BD84D4423, 0xBF8999CBAC3B5695, 0x46E9127BCE14CDB6, 0x003F6CFCE8B81771, | ||||
} | } | ||||
// p503 + 1 left-shifted by 8, assuming little endianness | // p503 + 1 left-shifted by 8, assuming little endianness | ||||
var p503p1s8 = Fp{ | |||||
var p1s8 = Fp{ | |||||
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, | 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, | ||||
0x085BDA2211E7A0AC, 0x9BF6C87B7E7DAF13, 0x45C6BDDA77A4D01B, 0x4066F541811E1E60, | 0x085BDA2211E7A0AC, 0x9BF6C87B7E7DAF13, 0x45C6BDDA77A4D01B, 0x4066F541811E1E60, | ||||
} | } |
@@ -36,9 +36,9 @@ func toMontDomain(x *Fp2) { | |||||
var aRR FpX2 | var aRR FpX2 | ||||
// convert to montgomery domain | // convert to montgomery domain | ||||
fpMul(&aRR, &x.A, &p503R2) // = a*R*R | |||||
fpMontRdc(&x.A, &aRR) // = a*R mod p | |||||
fpMul(&aRR, &x.B, &p503R2) | |||||
fpMul(&aRR, &x.A, &pR2) // = a*R*R | |||||
fpMontRdc(&x.A, &aRR) // = a*R mod p | |||||
fpMul(&aRR, &x.B, &pR2) | |||||
fpMontRdc(&x.B, &aRR) | fpMontRdc(&x.B, &aRR) | ||||
} | } | ||||
@@ -15,17 +15,17 @@ func fpAddRdc(z, x, y *Fp) { | |||||
z[i], carry = bits.Add64(x[i], y[i], carry) | z[i], carry = bits.Add64(x[i], y[i], carry) | ||||
} | } | ||||
// z = z - p503x2 | |||||
// z = z - pX2 | |||||
carry = 0 | carry = 0 | ||||
for i := 0; i < FP_WORDS; i++ { | for i := 0; i < FP_WORDS; i++ { | ||||
z[i], carry = bits.Sub64(z[i], p503x2[i], carry) | |||||
z[i], carry = bits.Sub64(z[i], pX2[i], carry) | |||||
} | } | ||||
// if z<0 add p503x2 back | |||||
// if z<0 add pX2 back | |||||
mask := uint64(0 - carry) | mask := uint64(0 - carry) | ||||
carry = 0 | carry = 0 | ||||
for i := 0; i < FP_WORDS; i++ { | for i := 0; i < FP_WORDS; i++ { | ||||
z[i], carry = bits.Add64(z[i], p503x2[i]&mask, carry) | |||||
z[i], carry = bits.Add64(z[i], pX2[i]&mask, carry) | |||||
} | } | ||||
} | } | ||||
@@ -33,16 +33,16 @@ func fpAddRdc(z, x, y *Fp) { | |||||
func fpSubRdc(z, x, y *Fp) { | func fpSubRdc(z, x, y *Fp) { | ||||
var borrow uint64 | var borrow uint64 | ||||
// z = z - p503x2 | |||||
// z = z - pX2 | |||||
for i := 0; i < FP_WORDS; i++ { | for i := 0; i < FP_WORDS; i++ { | ||||
z[i], borrow = bits.Sub64(x[i], y[i], borrow) | z[i], borrow = bits.Sub64(x[i], y[i], borrow) | ||||
} | } | ||||
// if z<0 add p503x2 back | |||||
// if z<0 add pX2 back | |||||
mask := uint64(0 - borrow) | mask := uint64(0 - borrow) | ||||
borrow = 0 | borrow = 0 | ||||
for i := 0; i < FP_WORDS; i++ { | for i := 0; i < FP_WORDS; i++ { | ||||
z[i], borrow = bits.Add64(z[i], p503x2[i]&mask, borrow) | |||||
z[i], borrow = bits.Add64(z[i], pX2[i]&mask, borrow) | |||||
} | } | ||||
} | } | ||||
@@ -116,7 +116,7 @@ func fpMontRdc(z *Fp, x *FpX2) { | |||||
for i := 0; i < FP_WORDS; i++ { | for i := 0; i < FP_WORDS; i++ { | ||||
for j := 0; j < i; j++ { | for j := 0; j < i; j++ { | ||||
if j < (i - count + 1) { | if j < (i - count + 1) { | ||||
hi, lo = bits.Mul64(z[j], p503p1[i-j]) | |||||
hi, lo = bits.Mul64(z[j], p1[i-j]) | |||||
v, carry = bits.Add64(lo, v, 0) | v, carry = bits.Add64(lo, v, 0) | ||||
u, carry = bits.Add64(hi, u, carry) | u, carry = bits.Add64(hi, u, carry) | ||||
t += carry | t += carry | ||||
@@ -138,7 +138,7 @@ func fpMontRdc(z *Fp, x *FpX2) { | |||||
} | } | ||||
for j := i - FP_WORDS + 1; j < FP_WORDS; j++ { | for j := i - FP_WORDS + 1; j < FP_WORDS; j++ { | ||||
if j < (FP_WORDS - count) { | if j < (FP_WORDS - count) { | ||||
hi, lo = bits.Mul64(z[j], p503p1[i-j]) | |||||
hi, lo = bits.Mul64(z[j], p1[i-j]) | |||||
v, carry = bits.Add64(lo, v, 0) | v, carry = bits.Add64(lo, v, 0) | ||||
u, carry = bits.Add64(hi, u, carry) | u, carry = bits.Add64(hi, u, carry) | ||||
t += carry | t += carry |