diff --git a/Makefile b/Makefile index 8274a58..d31611a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ V ?= 0 GOCACHE ?= off GOARCH ?= ETC_DIR = $(PRJ_DIR)/etc -BENCH_NAME = . +BENCH_NAME = BenchmarkXMul DBG = 1 OPTS_ENV = ifeq ($(NOASM),1) @@ -21,9 +21,9 @@ ifeq ($(NOASM),1) endif ifeq ($(DBG),1) - DBG_FLAGS+= -m -m # escape analysis - DBG_FLAGS+= -l # no inline - DBG_FLAGS+= -N # debug symbols + DBG_FLAGS+= #-m # escape analysis + DBG_FLAGS+= -l # no inline + DBG_FLAGS+= -N # debug symbols #OPTS+=-gcflags=all="$(DBG_FLAGS)" OPTS+=-gcflags "$(DBG_FLAGS)" OPTS_ENV+= GOTRACEBACK=crash # enable core dumps @@ -68,3 +68,15 @@ vendor-sidh-for-tls: clean bench: clean $(addprefix prep-,$(TARGETS)) cd $(GOPATH_LOCAL); $(OPTS_ENV) GOCACHE=$(GOCACHE) GOPATH=$(GOPATH_LOCAL) GOMAXPROCS=1 $(GO) test \ $(BENCH_OPTS) ./... + +bench_csidh: clean make_dirs $(addprefix prep-,$(TARGETS)) + cd $(GOPATH_LOCAL); $(OPTS_ENV) GOCACHE=$(GOCACHE) GOPATH=$(GOPATH_LOCAL) GOMAXPROCS=1 $(GO) test \ + $(OPTS) -run="^_" -bench=$(BENCH_NAME) -memprofile mem.prof -benchmem github.com/henrydcase/nobs/dh/csidh + +test_drbg: clean make_dirs $(addprefix prep-,$(TARGETS)) + cd $(GOPATH_LOCAL); $(OPTS_ENV) GOCACHE=$(GOCACHE) GOPATH=$(GOPATH_LOCAL) GOMAXPROCS=1 $(GO) test \ + $(OPTS) -c -run=. github.com/henrydcase/nobs/drbg + +bench_drbg: clean make_dirs $(addprefix prep-,$(TARGETS)) + cd $(GOPATH_LOCAL); $(OPTS_ENV) GOCACHE=$(GOCACHE) GOPATH=$(GOPATH_LOCAL) GOMAXPROCS=1 $(GO) test \ + $(OPTS) -run="XXX" -bench=. -benchmem -memprofile=drbg_prof github.com/henrydcase/nobs/drbg diff --git a/dh/csidh/api.go b/dh/csidh/api.go index e935d83..290e381 100644 --- a/dh/csidh/api.go +++ b/dh/csidh/api.go @@ -10,6 +10,10 @@ type PublicKey struct { // Defines operations on private key type PrivateKey struct { e [37]int8 + + // Temporary buffer used during key generation. Placed + // here to avoid heap memory allocation + tmp [64]byte } // PrivateKey diff --git a/dh/csidh/csidh.go b/dh/csidh/csidh.go index 6bb9a83..220e1e9 100644 --- a/dh/csidh/csidh.go +++ b/dh/csidh/csidh.go @@ -3,12 +3,15 @@ package csidh import "io" import "crypto/rand" +// OZAPTF +var buf [8 * limbByteSize]byte + // TODO: this is weird. How do I know loop will end? func randFp(fp *Fp) { + // var buf [len(fp) * limbByteSize]byte mask := uint64(1<<(pbits%limbBitSize)) - 1 for { *fp = Fp{} - var buf [len(fp) * limbByteSize]byte if _, err := io.ReadFull(rand.Reader, buf[:]); err != nil { // OZAPTF: to be re-done (AES_CTR) panic("Can't read random number") @@ -60,15 +63,14 @@ func (c *PrivateKey) Generate(rand io.Reader) error { } for i := 0; i < len(primes); { - var buf [64]byte - _, err := io.ReadFull(rand, buf[:]) + _, err := io.ReadFull(rand, c.tmp[:]) if err != nil { return err } - for j, _ := range buf { - if int8(buf[j]) <= expMax && int8(buf[j]) >= -expMax { - c.e[i>>1] |= int8((buf[j] & 0xf) << uint((i%2)*4)) + for j, _ := range c.tmp { + if int8(c.tmp[j]) <= expMax && int8(c.tmp[j]) >= -expMax { + c.e[i>>1] |= int8((c.tmp[j] & 0xf) << uint((i%2)*4)) i = i + 1 if i == len(primes) { break diff --git a/dh/csidh/csidh_test.go b/dh/csidh/csidh_test.go index 6ee9294..855a8c4 100644 --- a/dh/csidh/csidh_test.go +++ b/dh/csidh/csidh_test.go @@ -272,31 +272,31 @@ func testProcessTestVectors(t *testing.T) { func TestProcessTestVectors(t *testing.T) { testProcessTestVectors(t) } +var prv1, prv2 PrivateKey + // Private key generation func BenchmarkGeneratePrivate(b *testing.B) { for n := 0; n < b.N; n++ { - var prv PrivateKey - prv.Generate(rng) + prv1.Generate(rng) } } // Public key generation from private (group action on empty key) func BenchmarkGeneratePublic(b *testing.B) { for n := 0; n < b.N; n++ { - var prv PrivateKey var pub PublicKey - prv.Generate(rng) - pub.Generate(&prv) + prv1.Generate(rng) + pub.Generate(&prv1) } } // Benchmark validation on same key multiple times func BenchmarkValidate(b *testing.B) { - var pub PublicKey - var prv PrivateKey + prvBytes := []byte{0xaa, 0x54, 0xe4, 0xd4, 0xd0, 0xbd, 0xee, 0xcb, 0xf4, 0xd0, 0xc2, 0xbc, 0x52, 0x44, 0x11, 0xee, 0xe1, 0x14, 0xd2, 0x24, 0xe5, 0x0, 0xcc, 0xf5, 0xc0, 0xe1, 0x1e, 0xb3, 0x43, 0x52, 0x45, 0xbe, 0xfb, 0x54, 0xc0, 0x55, 0xb2} + prv1.Import(prvBytes) - prv.Generate(rng) - pub.Generate(&prv) + var pub PublicKey + pub.Generate(&prv1) for n := 0; n < b.N; n++ { pub.Validate() @@ -320,18 +320,15 @@ func BenchmarkValidateRandom(b *testing.B) { // Benchmark validation on different keys func BenchmarkValidateGenerated(b *testing.B) { var pub PublicKey - var prv PrivateKey - for n := 0; n < b.N; n++ { - prv.Generate(rng) - pub.Generate(&prv) + prv1.Generate(rng) + pub.Generate(&prv1) pub.Validate() } } func BenchmarkDeriveGenerated(b *testing.B) { var ss [64]uint8 - var prv1, prv2 PrivateKey var pub1, pub2 PublicKey for n := 0; n < b.N; n++ { prv1.Generate(rng) @@ -346,7 +343,6 @@ func BenchmarkDeriveGenerated(b *testing.B) { func BenchmarkDerive(b *testing.B) { var ss [64]uint8 - var prv1, prv2 PrivateKey var pub1, pub2 PublicKey prv1.Generate(rng) diff --git a/dh/csidh/curve.go b/dh/csidh/curve.go index de65607..f93bcf5 100644 --- a/dh/csidh/curve.go +++ b/dh/csidh/curve.go @@ -79,6 +79,7 @@ func cswapPoint(P1, P2 *Point, choice uint8) { // see this: https://eprint.iacr.org/2017/264.pdf func xMul512(kP, P *Point, co *Coeff, k *Fp) { var A24 Coeff + R := *P // P = P-Q PdQ := *P