Yazar | SHA1 | Mesaj | Tarih |
---|---|---|---|
Kris Kwiatkowski | 3479e45558 | cleanup: removes PrimeFieldElement inversion | 6 yıl önce |
Henry Case | 571611c9f7 | makefile: improvements | 6 yıl önce |
Henry Case | 1c2c27017e |
cleanup: removes PrimeFieldElement::square method
The method is implemented in exactly same way as multiplication. Currently there is no explenation for it's existance. Benchmarking shows no change |
6 yıl önce |
Henry Case | 0e6db56864 | cleanup: removes unused PrimeFieldElement methods | 6 yıl önce |
Henry Case | 1336c8ff50 | cleanup: removes ProjectivePrimeFieldPoint | 6 yıl önce |
@@ -8,6 +8,7 @@ matrix: | |||
fast_finish: true | |||
script: | |||
- make test | |||
- make bench | |||
- make cover | |||
- NOASM=1 make cover-sidh | |||
@@ -3,37 +3,49 @@ MK_FILE_PATH = $(lastword $(MAKEFILE_LIST)) | |||
PRJ_DIR = $(abspath $(dir $(MK_FILE_PATH))) | |||
GOPATH_LOCAL = $(PRJ_DIR)/build | |||
GOPATH_DIR = github.com/cloudflare/p751sidh | |||
CSHAKE_PKG = github.com/henrydcase/nobs/hash/sha3 | |||
CSHAKE_PKG ?= github.com/henrydcase/nobs/hash/sha3 | |||
TARGETS = p751toolbox sidh sike | |||
GOARCH ?= | |||
OPTS_GCCGO ?= -compiler gccgo -O2 -g | |||
OPTS_TAGS ?= -tags=noasm | |||
OPTS ?= | |||
OPTS_TAGS ?= -tags=noasm | |||
NOASM ?= | |||
# -run="NonExistent" is set to make sure tests are not run before benchmarking | |||
BENCH_OPTS ?= -bench=. -run="NonExistent" | |||
# whether to be verbose | |||
V ?= 1 | |||
ifeq ($(NOASM),1) | |||
OPTS+=$(OPTS_TAGS) | |||
endif | |||
ifeq ($(V),1) | |||
OPTS += -v # Be verbose | |||
OPTS += -gcflags=-m # Show results from inlining | |||
endif | |||
clean: | |||
rm -rf $(GOPATH_LOCAL) | |||
rm -rf coverage*.txt | |||
prep: | |||
build_env: | |||
GOPATH=$(GOPATH_LOCAL) go get $(CSHAKE_PKG) | |||
mkdir -p $(GOPATH_LOCAL)/src/$(GOPATH_DIR) | |||
cp -rf p751toolbox $(GOPATH_LOCAL)/src/$(GOPATH_DIR) | |||
cp -rf sidh $(GOPATH_LOCAL)/src/$(GOPATH_DIR) | |||
cp -rf sike $(GOPATH_LOCAL)/src/$(GOPATH_DIR) | |||
cp -rf etc $(GOPATH_LOCAL)/src/$(GOPATH_DIR) | |||
test-%: prep | |||
GOPATH=$(GOPATH_LOCAL) go test -v $(OPTS) $(GOPATH_DIR)/$* | |||
copy-target-%: | |||
cp -rf $* $(GOPATH_LOCAL)/src/$(GOPATH_DIR) | |||
prep_targets: build_env $(addprefix copy-target-, $(TARGETS)) | |||
test-%: prep_targets | |||
GOPATH=$(GOPATH_LOCAL) go test $(OPTS) $(GOPATH_DIR)/$* | |||
bench-%: prep | |||
cd $*; GOPATH=$(GOPATH_LOCAL) go test -v $(OPTS) -bench=. | |||
bench-%: prep_targets | |||
cd $*; GOPATH=$(GOPATH_LOCAL) go test $(OPTS) $(BENCH_OPTS) | |||
cover-%: prep | |||
cover-%: prep_targets | |||
GOPATH=$(GOPATH_LOCAL) go test \ | |||
-race -coverprofile=coverage_$*.txt -covermode=atomic $(OPTS) $(GOPATH_DIR)/$* | |||
cat coverage_$*.txt >> coverage.txt | |||
@@ -28,15 +28,6 @@ type ProjectivePoint struct { | |||
Z ExtensionFieldElement | |||
} | |||
// A point on the projective line P^1(F_p). | |||
// | |||
// This represents a point on the (Kummer line) of the prime-field subgroup of | |||
// the base curve E_0(F_p), defined by E_0 : y^2 = x^3 + x. | |||
type ProjectivePrimeFieldPoint struct { | |||
X PrimeFieldElement | |||
Z PrimeFieldElement | |||
} | |||
func (params *ProjectiveCurveParameters) FromAffine(a *ExtensionFieldElement) { | |||
params.A = *a | |||
params.C.One() | |||
@@ -158,34 +149,17 @@ func (cparams *ProjectiveCurveParameters) RecoverCurveCoefficients4(coefEq *Curv | |||
return | |||
} | |||
func (point *ProjectivePoint) FromAffinePrimeField(x *PrimeFieldElement) { | |||
point.X.A = x.A | |||
point.X.B = zeroExtensionField.B | |||
point.Z = oneExtensionField | |||
} | |||
func (point *ProjectivePoint) FromAffine(x *ExtensionFieldElement) { | |||
point.X = *x | |||
point.Z = oneExtensionField | |||
} | |||
func (point *ProjectivePrimeFieldPoint) FromAffine(x *PrimeFieldElement) { | |||
point.X = *x | |||
point.Z = onePrimeField | |||
} | |||
func (point *ProjectivePoint) ToAffine() *ExtensionFieldElement { | |||
affine_x := new(ExtensionFieldElement) | |||
affine_x.Inv(&point.Z).Mul(affine_x, &point.X) | |||
return affine_x | |||
} | |||
func (point *ProjectivePrimeFieldPoint) ToAffine() *PrimeFieldElement { | |||
affine_x := new(PrimeFieldElement) | |||
affine_x.Inv(&point.Z).Mul(affine_x, &point.X) | |||
return affine_x | |||
} | |||
func (lhs *ProjectivePoint) VartimeEq(rhs *ProjectivePoint) bool { | |||
var t0, t1 ExtensionFieldElement | |||
t0.Mul(&lhs.X, &rhs.Z) | |||
@@ -193,23 +167,11 @@ func (lhs *ProjectivePoint) VartimeEq(rhs *ProjectivePoint) bool { | |||
return t0.VartimeEq(&t1) | |||
} | |||
func (lhs *ProjectivePrimeFieldPoint) VartimeEq(rhs *ProjectivePrimeFieldPoint) bool { | |||
var t0, t1 PrimeFieldElement | |||
t0.Mul(&lhs.X, &rhs.Z) | |||
t1.Mul(&lhs.Z, &rhs.X) | |||
return t0.VartimeEq(&t1) | |||
} | |||
func ProjectivePointConditionalSwap(xP, xQ *ProjectivePoint, choice uint8) { | |||
ExtensionFieldConditionalSwap(&xP.X, &xQ.X, choice) | |||
ExtensionFieldConditionalSwap(&xP.Z, &xQ.Z, choice) | |||
} | |||
func ProjectivePrimeFieldPointConditionalSwap(xP, xQ *ProjectivePrimeFieldPoint, choice uint8) { | |||
PrimeFieldConditionalSwap(&xP.X, &xQ.X, choice) | |||
PrimeFieldConditionalSwap(&xP.Z, &xQ.Z, choice) | |||
} | |||
// Combined coordinate doubling and differential addition. Takes projective points | |||
// P,Q,Q-P and (A+2C)/4C curve E coefficient. Returns 2*P and P+Q calculated on E. | |||
// Function is used only by RightToLeftLadder. Corresponds to Algorithm 5 of SIKE | |||
@@ -105,16 +105,6 @@ func (dest *ExtensionFieldElement) Mul(lhs, rhs *ExtensionFieldElement) *Extensi | |||
return dest | |||
} | |||
// Set dest = -x | |||
// | |||
// Allowed to overlap dest with x. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *ExtensionFieldElement) Neg(x *ExtensionFieldElement) *ExtensionFieldElement { | |||
dest.Sub(&zeroExtensionField, x) | |||
return dest | |||
} | |||
// Set dest = 1/x | |||
// | |||
// Allowed to overlap dest with x. | |||
@@ -142,18 +132,21 @@ func (dest *ExtensionFieldElement) Inv(x *ExtensionFieldElement) *ExtensionField | |||
fp751MontgomeryReduce(&asq_plus_bsq.A, &asq) // = (a^2 + b^2)*R mod p | |||
// Now asq_plus_bsq = a^2 + b^2 | |||
var asq_plus_bsq_inv PrimeFieldElement | |||
asq_plus_bsq_inv.Inv(&asq_plus_bsq) | |||
c := &asq_plus_bsq_inv.A | |||
// Invert asq_plus_bsq | |||
inv := asq_plus_bsq | |||
inv.Mul(&asq_plus_bsq, &asq_plus_bsq) | |||
inv.P34(&inv) | |||
inv.Mul(&inv, &inv) | |||
inv.Mul(&inv, &asq_plus_bsq) | |||
var ac fp751X2 | |||
fp751Mul(&ac, a, c) | |||
fp751Mul(&ac, a, &inv.A) | |||
fp751MontgomeryReduce(&dest.A, &ac) | |||
var minus_b Fp751Element | |||
fp751SubReduced(&minus_b, &minus_b, b) | |||
var minus_bc fp751X2 | |||
fp751Mul(&minus_bc, &minus_b, c) | |||
fp751Mul(&minus_bc, &minus_b, &inv.A) | |||
fp751MontgomeryReduce(&dest.B, &minus_bc) | |||
return dest | |||
@@ -277,35 +270,6 @@ var onePrimeField = PrimeFieldElement{ | |||
A: Fp751Element{0x249ad, 0x0, 0x0, 0x0, 0x0, 0x8310000000000000, 0x5527b1e4375c6c66, 0x697797bf3f4f24d0, 0xc89db7b2ac5c4e2e, 0x4ca4b439d2076956, 0x10f7926c7512c7e9, 0x2d5b24bce5e2}, | |||
} | |||
// Set dest = 0. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Zero() *PrimeFieldElement { | |||
*dest = zeroPrimeField | |||
return dest | |||
} | |||
// Set dest = 1. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) One() *PrimeFieldElement { | |||
*dest = onePrimeField | |||
return dest | |||
} | |||
// Set dest to x. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) SetUint64(x uint64) *PrimeFieldElement { | |||
var xRR fp751X2 | |||
dest.A = Fp751Element{} // = 0 | |||
dest.A[0] = x // = x | |||
fp751Mul(&xRR, &dest.A, &montgomeryRsq) // = x*R*R | |||
fp751MontgomeryReduce(&dest.A, &xRR) // = x*R mod p | |||
return dest | |||
} | |||
// Set dest = lhs * rhs. | |||
// | |||
// Allowed to overlap lhs or rhs with dest. | |||
@@ -328,104 +292,14 @@ func (dest *PrimeFieldElement) Mul(lhs, rhs *PrimeFieldElement) *PrimeFieldEleme | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Pow2k(x *PrimeFieldElement, k uint8) *PrimeFieldElement { | |||
dest.Square(x) | |||
dest.Mul(x, x) | |||
for i := uint8(1); i < k; i++ { | |||
dest.Square(dest) | |||
dest.Mul(dest, dest) | |||
} | |||
return dest | |||
} | |||
// Set dest = x^2 | |||
// | |||
// Allowed to overlap x with dest. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Square(x *PrimeFieldElement) *PrimeFieldElement { | |||
a := &x.A // = a*R | |||
b := &x.A // = b*R | |||
var ab fp751X2 | |||
fp751Mul(&ab, a, b) // = a*b*R*R | |||
fp751MontgomeryReduce(&dest.A, &ab) // = a*b*R mod p | |||
return dest | |||
} | |||
// Set dest = -x | |||
// | |||
// Allowed to overlap x with dest. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Neg(x *PrimeFieldElement) *PrimeFieldElement { | |||
dest.Sub(&zeroPrimeField, x) | |||
return dest | |||
} | |||
// Set dest = lhs + rhs. | |||
// | |||
// Allowed to overlap lhs or rhs with dest. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Add(lhs, rhs *PrimeFieldElement) *PrimeFieldElement { | |||
fp751AddReduced(&dest.A, &lhs.A, &rhs.A) | |||
return dest | |||
} | |||
// Set dest = lhs - rhs. | |||
// | |||
// Allowed to overlap lhs or rhs with dest. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Sub(lhs, rhs *PrimeFieldElement) *PrimeFieldElement { | |||
fp751SubReduced(&dest.A, &lhs.A, &rhs.A) | |||
return dest | |||
} | |||
// Returns true if lhs = rhs. Takes variable time. | |||
func (lhs *PrimeFieldElement) VartimeEq(rhs *PrimeFieldElement) bool { | |||
return lhs.A.vartimeEq(rhs.A) | |||
} | |||
// If choice = 1u8, set (x,y) = (y,x). If choice = 0u8, set (x,y) = (x,y). | |||
// | |||
// Returns dest to allow chaining operations. | |||
func PrimeFieldConditionalSwap(x, y *PrimeFieldElement, choice uint8) { | |||
fp751ConditionalSwap(&x.A, &y.A, choice) | |||
} | |||
// Set dest = sqrt(x), if x is a square. If x is nonsquare dest is undefined. | |||
// | |||
// Allowed to overlap x with dest. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Sqrt(x *PrimeFieldElement) *PrimeFieldElement { | |||
tmp_x := *x // Copy x in case dest == x | |||
// Since x is assumed to be square, x = y^2 | |||
dest.P34(x) // dest = (y^2)^((p-3)/4) = y^((p-3)/2) | |||
dest.Mul(dest, &tmp_x) // dest = y^2 * y^((p-3)/2) = y^((p+1)/2) | |||
// Now dest^2 = y^(p+1) = y^2 = x, so dest = sqrt(x) | |||
return dest | |||
} | |||
// Set dest = 1/x. | |||
// | |||
// Allowed to overlap x with dest. | |||
// | |||
// Returns dest to allow chaining operations. | |||
func (dest *PrimeFieldElement) Inv(x *PrimeFieldElement) *PrimeFieldElement { | |||
tmp_x := *x // Copy x in case dest == x | |||
dest.Square(x) // dest = x^2 | |||
dest.P34(dest) // dest = (x^2)^((p-3)/4) = x^((p-3)/2) | |||
dest.Square(dest) // dest = x^(p-3) | |||
dest.Mul(dest, &tmp_x) // dest = x^(p-2) | |||
return dest | |||
} | |||
// Set dest = x^((p-3)/4). If x is square, this is 1/sqrt(x). | |||
// | |||
// Allowed to overlap x with dest. | |||
@@ -448,7 +322,7 @@ func (dest *PrimeFieldElement) P34(x *PrimeFieldElement) *PrimeFieldElement { | |||
// Build a lookup table of odd multiples of x. | |||
lookup := [16]PrimeFieldElement{} | |||
xx := &PrimeFieldElement{} | |||
xx.Square(x) // Set xx = x^2 | |||
xx.Mul(x, x) // Set xx = x^2 | |||
lookup[0] = *x | |||
for i := 1; i < 16; i++ { | |||
lookup[i].Mul(&lookup[i-1], xx) | |||
@@ -35,6 +35,10 @@ func radix64ToBigInt(x []uint64) *big.Int { | |||
return val | |||
} | |||
func VartimeEq(x,y *PrimeFieldElement) bool { | |||
return x.A.vartimeEq(y.A) | |||
} | |||
func (x *PrimeFieldElement) toBigInt() *big.Int { | |||
// Convert from Montgomery form | |||
return x.A.toBigIntFromMontgomeryForm() | |||
@@ -249,91 +253,6 @@ func TestExtensionFieldElementBatch3Inv(t *testing.T) { | |||
//------------------------------------------------------------------------------ | |||
// Prime Field | |||
//------------------------------------------------------------------------------ | |||
func TestPrimeFieldElementSetUint64VersusBigInt(t *testing.T) { | |||
setUint64RoundTrips := func(x uint64) bool { | |||
z := new(PrimeFieldElement).SetUint64(x).toBigInt().Uint64() | |||
return x == z | |||
} | |||
if err := quick.Check(setUint64RoundTrips, quickCheckConfig); err != nil { | |||
t.Error(err) | |||
} | |||
} | |||
func TestPrimeFieldElementAddVersusBigInt(t *testing.T) { | |||
addMatchesBigInt := func(x, y PrimeFieldElement) bool { | |||
z := new(PrimeFieldElement) | |||
z.Add(&x, &y) | |||
check := new(big.Int) | |||
check.Add(x.toBigInt(), y.toBigInt()) | |||
check.Mod(check, cln16prime) | |||
return check.Cmp(z.toBigInt()) == 0 | |||
} | |||
if err := quick.Check(addMatchesBigInt, quickCheckConfig); err != nil { | |||
t.Error(err) | |||
} | |||
} | |||
func TestPrimeFieldElementSubVersusBigInt(t *testing.T) { | |||
subMatchesBigInt := func(x, y PrimeFieldElement) bool { | |||
z := new(PrimeFieldElement) | |||
z.Sub(&x, &y) | |||
check := new(big.Int) | |||
check.Sub(x.toBigInt(), y.toBigInt()) | |||
check.Mod(check, cln16prime) | |||
return check.Cmp(z.toBigInt()) == 0 | |||
} | |||
if err := quick.Check(subMatchesBigInt, quickCheckConfig); err != nil { | |||
t.Error(err) | |||
} | |||
} | |||
func TestPrimeFieldElementInv(t *testing.T) { | |||
inverseIsCorrect := func(x PrimeFieldElement) bool { | |||
z := new(PrimeFieldElement) | |||
z.Inv(&x) | |||
// Now z = (1/x), so (z * x) * x == x | |||
z.Mul(z, &x).Mul(z, &x) | |||
return z.VartimeEq(&x) | |||
} | |||
// This is more expensive; run fewer tests | |||
var quickCheckConfig = &quick.Config{MaxCount: (1 << (8 + quickCheckScaleFactor))} | |||
if err := quick.Check(inverseIsCorrect, quickCheckConfig); err != nil { | |||
t.Error(err) | |||
} | |||
} | |||
func TestPrimeFieldElementSqrt(t *testing.T) { | |||
inverseIsCorrect := func(x PrimeFieldElement) bool { | |||
// Construct y = x^2 so we're sure y is square. | |||
y := new(PrimeFieldElement) | |||
y.Square(&x) | |||
z := new(PrimeFieldElement) | |||
z.Sqrt(y) | |||
// Now z = sqrt(y), so z^2 == y | |||
z.Square(z) | |||
return z.VartimeEq(y) | |||
} | |||
// This is more expensive; run fewer tests | |||
var quickCheckConfig = &quick.Config{MaxCount: (1 << (8 + quickCheckScaleFactor))} | |||
if err := quick.Check(inverseIsCorrect, quickCheckConfig); err != nil { | |||
t.Error(err) | |||
} | |||
} | |||
func TestPrimeFieldElementMulVersusBigInt(t *testing.T) { | |||
mulMatchesBigInt := func(x, y PrimeFieldElement) bool { | |||
z := new(PrimeFieldElement) | |||
@@ -370,26 +289,6 @@ func TestPrimeFieldElementP34VersusBigInt(t *testing.T) { | |||
} | |||
} | |||
func TestFp751ElementConditionalSwap(t *testing.T) { | |||
var one = Fp751Element{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} | |||
var two = Fp751Element{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2} | |||
var x = one | |||
var y = two | |||
fp751ConditionalSwap(&x, &y, 0) | |||
if !(x == one && y == two) { | |||
t.Error("Found", x, "expected", one) | |||
} | |||
fp751ConditionalSwap(&x, &y, 1) | |||
if !(x == two && y == one) { | |||
t.Error("Found", x, "expected", two) | |||
} | |||
} | |||
// Package-level storage for this field element is intended to deter | |||
// compiler optimizations. | |||
var benchmarkFp751Element Fp751Element | |||
@@ -452,51 +351,6 @@ func BenchmarkPrimeFieldElementMul(b *testing.B) { | |||
} | |||
} | |||
func BenchmarkPrimeFieldElementInv(b *testing.B) { | |||
z := &PrimeFieldElement{A: bench_x} | |||
w := new(PrimeFieldElement) | |||
for n := 0; n < b.N; n++ { | |||
w.Inv(z) | |||
} | |||
} | |||
func BenchmarkPrimeFieldElementSqrt(b *testing.B) { | |||
z := &PrimeFieldElement{A: bench_x} | |||
w := new(PrimeFieldElement) | |||
for n := 0; n < b.N; n++ { | |||
w.Sqrt(z) | |||
} | |||
} | |||
func BenchmarkPrimeFieldElementSquare(b *testing.B) { | |||
z := &PrimeFieldElement{A: bench_x} | |||
w := new(PrimeFieldElement) | |||
for n := 0; n < b.N; n++ { | |||
w.Square(z) | |||
} | |||
} | |||
func BenchmarkPrimeFieldElementAdd(b *testing.B) { | |||
z := &PrimeFieldElement{A: bench_x} | |||
w := new(PrimeFieldElement) | |||
for n := 0; n < b.N; n++ { | |||
w.Add(z, z) | |||
} | |||
} | |||
func BenchmarkPrimeFieldElementSub(b *testing.B) { | |||
z := &PrimeFieldElement{A: bench_x} | |||
w := new(PrimeFieldElement) | |||
for n := 0; n < b.N; n++ { | |||
w.Sub(z, z) | |||
} | |||
} | |||
// --- field operation functions | |||
func BenchmarkFp751Multiply(b *testing.B) { | |||
@@ -38,10 +38,6 @@ func (point ProjectivePoint) String() string { | |||
return fmt.Sprintf("X:\n%sZ:\n%s", point.X.String(), point.Z.String()) | |||
} | |||
func (point ProjectivePrimeFieldPoint) String() string { | |||
return fmt.Sprintf("X:\n%sZ:\n%s", point.X.String(), point.Z.String()) | |||
} | |||
func (point Fp751Element) PrintHex() { | |||
fmt.Printf("\t") | |||
for i := 0; i < fp751NumWords/2; i++ { | |||