193 líneas
3.8 KiB
Go
193 líneas
3.8 KiB
Go
package sike
|
|
|
|
import (
|
|
"math/bits"
|
|
)
|
|
|
|
// 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 = bits.Add64(x[i], y[i], carry)
|
|
}
|
|
|
|
// z = z - pX2
|
|
carry = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], carry = bits.Sub64(z[i], pX2[i], carry)
|
|
}
|
|
|
|
// if z<0 add pX2 back
|
|
mask := uint64(0 - carry)
|
|
carry = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], carry = bits.Add64(z[i], pX2[i]&mask, carry)
|
|
}
|
|
}
|
|
|
|
// Compute z = x - y (mod 2*p).
|
|
func fpSubRdc(z, x, y *Fp) {
|
|
var borrow uint64
|
|
|
|
// z = z - pX2
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], borrow = bits.Sub64(x[i], y[i], borrow)
|
|
}
|
|
|
|
// if z<0 add pX2 back
|
|
mask := uint64(0 - borrow)
|
|
borrow = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
z[i], borrow = bits.Add64(z[i], pX2[i]&mask, borrow)
|
|
}
|
|
}
|
|
|
|
// 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 = bits.Sub64(x[i], p[i], borrow)
|
|
}
|
|
|
|
// Sets all bits if borrow = 1
|
|
mask = 0 - borrow
|
|
borrow = 0
|
|
for i := 0; i < FP_WORDS; i++ {
|
|
x[i], borrow = bits.Add64(x[i], p[i]&mask, borrow)
|
|
}
|
|
}
|
|
|
|
// 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 carry, t, u, v uint64
|
|
var hi, lo uint64
|
|
|
|
for i := uint64(0); i < FP_WORDS; i++ {
|
|
for j := uint64(0); j <= i; j++ {
|
|
hi, lo = bits.Mul64(x[j], y[i-j])
|
|
v, carry = bits.Add64(lo, v, 0)
|
|
u, carry = bits.Add64(hi, u, carry)
|
|
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++ {
|
|
hi, lo = bits.Mul64(x[j], y[i-j])
|
|
v, carry = bits.Add64(lo, v, 0)
|
|
u, carry = bits.Add64(hi, u, carry)
|
|
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 hi, lo uint64
|
|
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) {
|
|
hi, lo = bits.Mul64(z[j], p1[i-j])
|
|
v, carry = bits.Add64(lo, v, 0)
|
|
u, carry = bits.Add64(hi, u, carry)
|
|
t += carry
|
|
}
|
|
}
|
|
v, carry = bits.Add64(v, x[i], 0)
|
|
u, carry = bits.Add64(u, 0, carry)
|
|
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) {
|
|
hi, lo = bits.Mul64(z[j], p1[i-j])
|
|
v, carry = bits.Add64(lo, v, 0)
|
|
u, carry = bits.Add64(hi, u, carry)
|
|
t += carry
|
|
}
|
|
}
|
|
v, carry = bits.Add64(v, x[i], 0)
|
|
u, carry = bits.Add64(u, 0, carry)
|
|
|
|
t += carry
|
|
z[i-FP_WORDS] = v
|
|
v = u
|
|
u = t
|
|
t = 0
|
|
}
|
|
v, carry = bits.Add64(v, x[2*FP_WORDS-1], 0)
|
|
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 = bits.Add64(x[i], y[i], carry)
|
|
}
|
|
}
|
|
|
|
// 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 = bits.Sub64(x[i], y[i], borrow)
|
|
}
|
|
|
|
// Sets all bits if borrow = 1
|
|
mask = 0 - borrow
|
|
borrow = 0
|
|
for i := FP_WORDS; i < 2*FP_WORDS; i++ {
|
|
z[i], borrow = bits.Add64(z[i], p[i-FP_WORDS]&mask, borrow)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|