190 lines
3.7 KiB
Go
190 lines
3.7 KiB
Go
package sike
|
|
|
|
// Fp implementation
|
|
|
|
// Compute z = x + y (mod 2*p).
|
|
func fpAddRdc(z, x, y *Fp) {
|
|
var carry uint64
|
|
|
|
// z=x+y % p503
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], carry = addc64(carry, x[i], y[i])
|
|
}
|
|
|
|
// z = z - p503x2
|
|
carry = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], carry = subc64(carry, z[i], p503x2[i])
|
|
}
|
|
|
|
// if z<0 add p503x2 back
|
|
mask := uint64(0 - carry)
|
|
carry = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], carry = addc64(carry, z[i], p503x2[i]&mask)
|
|
}
|
|
}
|
|
|
|
// Compute z = x - y (mod 2*p).
|
|
func fpSubRdc(z, x, y *Fp) {
|
|
var borrow uint64
|
|
|
|
// z = z - p503x2
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], borrow = subc64(borrow, x[i], y[i])
|
|
}
|
|
|
|
// if z<0 add p503x2 back
|
|
mask := uint64(0 - borrow)
|
|
borrow = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], borrow = addc64(borrow, z[i], p503x2[i]&mask)
|
|
}
|
|
}
|
|
|
|
// Reduce a field element in [0, 2*p) to one in [0,p).
|
|
func fpRdcP(x *Fp) {
|
|
var borrow, mask uint64
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
x[i], borrow = subc64(borrow, x[i], p503[i])
|
|
}
|
|
|
|
// Sets all bits if borrow = 1
|
|
mask = 0 - borrow
|
|
borrow = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
x[i], borrow = addc64(borrow, x[i], p503[i]&mask)
|
|
}
|
|
}
|
|
|
|
// Implementation doesn't actually depend on a prime field.
|
|
func fpSwapCond(x, y *Fp, mask uint8) {
|
|
if mask != 0 {
|
|
var tmp Fp
|
|
copy(tmp[:], y[:])
|
|
copy(y[:], x[:])
|
|
copy(x[:], tmp[:])
|
|
}
|
|
}
|
|
|
|
// Compute z = x * y.
|
|
func fpMul(z *FpX2, x, y *Fp) {
|
|
var u, v, t uint64
|
|
var carry uint64
|
|
var uv uint128
|
|
|
|
for i := uint64(0); i < FP_WORDS; i++ {
|
|
for j := uint64(0); j <= i; j++ {
|
|
uv = mul64(x[j], y[i-j])
|
|
v, carry = addc64(0, uv.L, v)
|
|
u, carry = addc64(carry, uv.H, u)
|
|
t += carry
|
|
}
|
|
z[i] = v
|
|
v = u
|
|
u = t
|
|
t = 0
|
|
}
|
|
|
|
for i := FP_WORDS; i < (2*FP_WORDS)-1; i++ {
|
|
for j := i - FP_WORDS + 1; j < FP_WORDS; j++ {
|
|
uv = mul64(x[j], y[i-j])
|
|
v, carry = addc64(0, uv.L, v)
|
|
u, carry = addc64(carry, uv.H, u)
|
|
t += carry
|
|
}
|
|
z[i] = v
|
|
v = u
|
|
u = t
|
|
t = 0
|
|
}
|
|
z[2*FP_WORDS-1] = v
|
|
}
|
|
|
|
// Perform Montgomery reduction: set z = x R^{-1} (mod 2*p)
|
|
// with R=2^512. Destroys the input value.
|
|
func fpMontRdc(z *Fp, x *FpX2) {
|
|
var carry, t, u, v uint64
|
|
var uv uint128
|
|
var count int
|
|
|
|
count = 3 // number of 0 digits in the least significat part of p503 + 1
|
|
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
for j := 0; j < i; j++ {
|
|
if j < (i - count + 1) {
|
|
uv = mul64(z[j], p503p1[i-j])
|
|
v, carry = addc64(0, uv.L, v)
|
|
u, carry = addc64(carry, uv.H, u)
|
|
t += carry
|
|
}
|
|
}
|
|
v, carry = addc64(0, v, x[i])
|
|
u, carry = addc64(carry, u, 0)
|
|
t += carry
|
|
|
|
z[i] = v
|
|
v = u
|
|
u = t
|
|
t = 0
|
|
}
|
|
|
|
for i := FP_WORDS; i < 2*FP_WORDS-1; i++ {
|
|
if count > 0 {
|
|
count--
|
|
}
|
|
for j := i - FP_WORDS + 1; j < FP_WORDS; j++ {
|
|
if j < (FP_WORDS - count) {
|
|
uv = mul64(z[j], p503p1[i-j])
|
|
v, carry = addc64(0, uv.L, v)
|
|
u, carry = addc64(carry, uv.H, u)
|
|
t += carry
|
|
}
|
|
}
|
|
v, carry = addc64(0, v, x[i])
|
|
u, carry = addc64(carry, u, 0)
|
|
|
|
t += carry
|
|
z[i-FP_WORDS] = v
|
|
v = u
|
|
u = t
|
|
t = 0
|
|
}
|
|
v, carry = addc64(0, v, x[2*FP_WORDS-1])
|
|
z[FP_WORDS-1] = v
|
|
}
|
|
|
|
// Compute z = x + y, without reducing mod p.
|
|
func fp2Add(z, x, y *FpX2) {
|
|
var carry uint64
|
|
for i := 0; i < 2*FP_WORDS; i++ {
|
|
z[i], carry = addc64(carry, x[i], y[i])
|
|
}
|
|
}
|
|
|
|
// Compute z = x - y, without reducing mod p.
|
|
func fp2Sub(z, x, y *FpX2) {
|
|
var borrow, mask uint64
|
|
for i := 0; i < 2*FP_WORDS; i++ {
|
|
z[i], borrow = subc64(borrow, x[i], y[i])
|
|
}
|
|
|
|
// Sets all bits if borrow = 1
|
|
mask = 0 - borrow
|
|
borrow = 0
|
|
for i := FP_WORDS; i < 2*FP_WORDS; i++ {
|
|
z[i], borrow = addc64(borrow, z[i], p503[i-FP_WORDS]&mask)
|
|
}
|
|
}
|
|
|
|
// Montgomery multiplication. Input values must be already
|
|
// in Montgomery domain.
|
|
func fpMulRdc(dest, lhs, rhs *Fp) {
|
|
a := lhs // = a*R
|
|
b := rhs // = b*R
|
|
|
|
var ab FpX2
|
|
fpMul(&ab, a, b) // = a*b*R*R
|
|
fpMontRdc(dest, &ab) // = a*b*R mod p
|
|
}
|