|
@@ -1,5 +1,9 @@ |
|
|
package sike |
|
|
package sike |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"math/bits" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
// Fp implementation |
|
|
// Fp implementation |
|
|
|
|
|
|
|
|
// Compute z = x + y (mod 2*p). |
|
|
// Compute z = x + y (mod 2*p). |
|
@@ -8,20 +12,20 @@ func fpAddRdc(z, x, y *Fp) { |
|
|
|
|
|
|
|
|
// z=x+y % p503 |
|
|
// z=x+y % p503 |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
z[i], carry = addc64(carry, x[i], y[i]) |
|
|
|
|
|
|
|
|
z[i], carry = bits.Add64(x[i], y[i], carry) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// z = z - p503x2 |
|
|
// z = z - p503x2 |
|
|
carry = 0 |
|
|
carry = 0 |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
z[i], carry = subc64(carry, z[i], p503x2[i]) |
|
|
|
|
|
|
|
|
z[i], carry = bits.Sub64(z[i], p503x2[i], carry) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// if z<0 add p503x2 back |
|
|
// if z<0 add p503x2 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 = addc64(carry, z[i], p503x2[i]&mask) |
|
|
|
|
|
|
|
|
z[i], carry = bits.Add64(z[i], p503x2[i]&mask, carry) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -31,14 +35,14 @@ func fpSubRdc(z, x, y *Fp) { |
|
|
|
|
|
|
|
|
// z = z - p503x2 |
|
|
// z = z - p503x2 |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
z[i], borrow = subc64(borrow, x[i], y[i]) |
|
|
|
|
|
|
|
|
z[i], borrow = bits.Sub64(x[i], y[i], borrow) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// if z<0 add p503x2 back |
|
|
// if z<0 add p503x2 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 = addc64(borrow, z[i], p503x2[i]&mask) |
|
|
|
|
|
|
|
|
z[i], borrow = bits.Add64(z[i], p503x2[i]&mask, borrow) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -46,14 +50,14 @@ func fpSubRdc(z, x, y *Fp) { |
|
|
func fpRdcP(x *Fp) { |
|
|
func fpRdcP(x *Fp) { |
|
|
var borrow, mask uint64 |
|
|
var borrow, mask uint64 |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
x[i], borrow = subc64(borrow, x[i], p503[i]) |
|
|
|
|
|
|
|
|
x[i], borrow = bits.Sub64(x[i], p503[i], borrow) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Sets all bits if borrow = 1 |
|
|
// Sets all bits if borrow = 1 |
|
|
mask = 0 - borrow |
|
|
mask = 0 - borrow |
|
|
borrow = 0 |
|
|
borrow = 0 |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
for i := 0; i < FP_WORDS; i++ { |
|
|
x[i], borrow = addc64(borrow, x[i], p503[i]&mask) |
|
|
|
|
|
|
|
|
x[i], borrow = bits.Add64(x[i], p503[i]&mask, borrow) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -69,15 +73,14 @@ func fpSwapCond(x, y *Fp, mask uint8) { |
|
|
|
|
|
|
|
|
// Compute z = x * y. |
|
|
// Compute z = x * y. |
|
|
func fpMul(z *FpX2, x, y *Fp) { |
|
|
func fpMul(z *FpX2, x, y *Fp) { |
|
|
var u, v, t uint64 |
|
|
|
|
|
var carry uint64 |
|
|
|
|
|
var uv uint128 |
|
|
|
|
|
|
|
|
var carry, t, u, v uint64 |
|
|
|
|
|
var hi, lo uint64 |
|
|
|
|
|
|
|
|
for i := uint64(0); i < FP_WORDS; i++ { |
|
|
for i := uint64(0); i < FP_WORDS; i++ { |
|
|
for j := uint64(0); j <= i; j++ { |
|
|
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) |
|
|
|
|
|
|
|
|
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 |
|
|
t += carry |
|
|
} |
|
|
} |
|
|
z[i] = v |
|
|
z[i] = v |
|
@@ -88,9 +91,9 @@ func fpMul(z *FpX2, x, y *Fp) { |
|
|
|
|
|
|
|
|
for i := FP_WORDS; i < (2*FP_WORDS)-1; i++ { |
|
|
for i := FP_WORDS; i < (2*FP_WORDS)-1; i++ { |
|
|
for j := i - FP_WORDS + 1; j < FP_WORDS; j++ { |
|
|
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) |
|
|
|
|
|
|
|
|
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 |
|
|
t += carry |
|
|
} |
|
|
} |
|
|
z[i] = v |
|
|
z[i] = v |
|
@@ -105,7 +108,7 @@ func fpMul(z *FpX2, x, y *Fp) { |
|
|
// with R=2^512. Destroys the input value. |
|
|
// with R=2^512. Destroys the input value. |
|
|
func fpMontRdc(z *Fp, x *FpX2) { |
|
|
func fpMontRdc(z *Fp, x *FpX2) { |
|
|
var carry, t, u, v uint64 |
|
|
var carry, t, u, v uint64 |
|
|
var uv uint128 |
|
|
|
|
|
|
|
|
var hi, lo uint64 |
|
|
var count int |
|
|
var count int |
|
|
|
|
|
|
|
|
count = 3 // number of 0 digits in the least significat part of p503 + 1 |
|
|
count = 3 // number of 0 digits in the least significat part of p503 + 1 |
|
@@ -113,14 +116,14 @@ 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) { |
|
|
uv = mul64(z[j], p503p1[i-j]) |
|
|
|
|
|
v, carry = addc64(0, uv.L, v) |
|
|
|
|
|
u, carry = addc64(carry, uv.H, u) |
|
|
|
|
|
|
|
|
hi, lo = bits.Mul64(z[j], p503p1[i-j]) |
|
|
|
|
|
v, carry = bits.Add64(lo, v, 0) |
|
|
|
|
|
u, carry = bits.Add64(hi, u, carry) |
|
|
t += carry |
|
|
t += carry |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
v, carry = addc64(0, v, x[i]) |
|
|
|
|
|
u, carry = addc64(carry, u, 0) |
|
|
|
|
|
|
|
|
v, carry = bits.Add64(v, x[i], 0) |
|
|
|
|
|
u, carry = bits.Add64(u, 0, carry) |
|
|
t += carry |
|
|
t += carry |
|
|
|
|
|
|
|
|
z[i] = v |
|
|
z[i] = v |
|
@@ -135,14 +138,14 @@ 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) { |
|
|
uv = mul64(z[j], p503p1[i-j]) |
|
|
|
|
|
v, carry = addc64(0, uv.L, v) |
|
|
|
|
|
u, carry = addc64(carry, uv.H, u) |
|
|
|
|
|
|
|
|
hi, lo = bits.Mul64(z[j], p503p1[i-j]) |
|
|
|
|
|
v, carry = bits.Add64(lo, v, 0) |
|
|
|
|
|
u, carry = bits.Add64(hi, u, carry) |
|
|
t += carry |
|
|
t += carry |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
v, carry = addc64(0, v, x[i]) |
|
|
|
|
|
u, carry = addc64(carry, u, 0) |
|
|
|
|
|
|
|
|
v, carry = bits.Add64(v, x[i], 0) |
|
|
|
|
|
u, carry = bits.Add64(u, 0, carry) |
|
|
|
|
|
|
|
|
t += carry |
|
|
t += carry |
|
|
z[i-FP_WORDS] = v |
|
|
z[i-FP_WORDS] = v |
|
@@ -150,7 +153,7 @@ func fpMontRdc(z *Fp, x *FpX2) { |
|
|
u = t |
|
|
u = t |
|
|
t = 0 |
|
|
t = 0 |
|
|
} |
|
|
} |
|
|
v, carry = addc64(0, v, x[2*FP_WORDS-1]) |
|
|
|
|
|
|
|
|
v, carry = bits.Add64(v, x[2*FP_WORDS-1], 0) |
|
|
z[FP_WORDS-1] = v |
|
|
z[FP_WORDS-1] = v |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -158,7 +161,7 @@ func fpMontRdc(z *Fp, x *FpX2) { |
|
|
func fp2Add(z, x, y *FpX2) { |
|
|
func fp2Add(z, x, y *FpX2) { |
|
|
var carry uint64 |
|
|
var carry uint64 |
|
|
for i := 0; i < 2*FP_WORDS; i++ { |
|
|
for i := 0; i < 2*FP_WORDS; i++ { |
|
|
z[i], carry = addc64(carry, x[i], y[i]) |
|
|
|
|
|
|
|
|
z[i], carry = bits.Add64(x[i], y[i], carry) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -166,14 +169,14 @@ func fp2Add(z, x, y *FpX2) { |
|
|
func fp2Sub(z, x, y *FpX2) { |
|
|
func fp2Sub(z, x, y *FpX2) { |
|
|
var borrow, mask uint64 |
|
|
var borrow, mask uint64 |
|
|
for i := 0; i < 2*FP_WORDS; i++ { |
|
|
for i := 0; i < 2*FP_WORDS; i++ { |
|
|
z[i], borrow = subc64(borrow, x[i], y[i]) |
|
|
|
|
|
|
|
|
z[i], borrow = bits.Sub64(x[i], y[i], borrow) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Sets all bits if borrow = 1 |
|
|
// Sets all bits if borrow = 1 |
|
|
mask = 0 - borrow |
|
|
mask = 0 - borrow |
|
|
borrow = 0 |
|
|
borrow = 0 |
|
|
for i := FP_WORDS; i < 2*FP_WORDS; i++ { |
|
|
for i := FP_WORDS; i < 2*FP_WORDS; i++ { |
|
|
z[i], borrow = addc64(borrow, z[i], p503[i-FP_WORDS]&mask) |
|
|
|
|
|
|
|
|
z[i], borrow = bits.Add64(z[i], p503[i-FP_WORDS]&mask, borrow) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|