1
0
mirror of https://github.com/henrydcase/nobs.git synced 2024-11-22 23:28:57 +00:00
This commit is contained in:
Henry Case 2019-02-19 14:43:36 +00:00
parent 2f234154e4
commit fc84db2c0f
5 changed files with 40 additions and 25 deletions

View File

@ -13,7 +13,7 @@ V ?= 0
GOCACHE ?= off GOCACHE ?= off
GOARCH ?= GOARCH ?=
ETC_DIR = $(PRJ_DIR)/etc ETC_DIR = $(PRJ_DIR)/etc
BENCH_NAME = . BENCH_NAME = BenchmarkXMul
DBG = 1 DBG = 1
OPTS_ENV = OPTS_ENV =
ifeq ($(NOASM),1) ifeq ($(NOASM),1)
@ -21,9 +21,9 @@ ifeq ($(NOASM),1)
endif endif
ifeq ($(DBG),1) ifeq ($(DBG),1)
DBG_FLAGS+= -m -m # escape analysis DBG_FLAGS+= #-m # escape analysis
DBG_FLAGS+= -l # no inline DBG_FLAGS+= -l # no inline
DBG_FLAGS+= -N # debug symbols DBG_FLAGS+= -N # debug symbols
#OPTS+=-gcflags=all="$(DBG_FLAGS)" #OPTS+=-gcflags=all="$(DBG_FLAGS)"
OPTS+=-gcflags "$(DBG_FLAGS)" OPTS+=-gcflags "$(DBG_FLAGS)"
OPTS_ENV+= GOTRACEBACK=crash # enable core dumps OPTS_ENV+= GOTRACEBACK=crash # enable core dumps
@ -68,3 +68,15 @@ vendor-sidh-for-tls: clean
bench: clean $(addprefix prep-,$(TARGETS)) bench: clean $(addprefix prep-,$(TARGETS))
cd $(GOPATH_LOCAL); $(OPTS_ENV) GOCACHE=$(GOCACHE) GOPATH=$(GOPATH_LOCAL) GOMAXPROCS=1 $(GO) test \ cd $(GOPATH_LOCAL); $(OPTS_ENV) GOCACHE=$(GOCACHE) GOPATH=$(GOPATH_LOCAL) GOMAXPROCS=1 $(GO) test \
$(BENCH_OPTS) ./... $(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

View File

@ -10,6 +10,10 @@ type PublicKey struct {
// Defines operations on private key // Defines operations on private key
type PrivateKey struct { type PrivateKey struct {
e [37]int8 e [37]int8
// Temporary buffer used during key generation. Placed
// here to avoid heap memory allocation
tmp [64]byte
} }
// PrivateKey // PrivateKey

View File

@ -3,12 +3,15 @@ package csidh
import "io" import "io"
import "crypto/rand" import "crypto/rand"
// OZAPTF
var buf [8 * limbByteSize]byte
// TODO: this is weird. How do I know loop will end? // TODO: this is weird. How do I know loop will end?
func randFp(fp *Fp) { func randFp(fp *Fp) {
// var buf [len(fp) * limbByteSize]byte
mask := uint64(1<<(pbits%limbBitSize)) - 1 mask := uint64(1<<(pbits%limbBitSize)) - 1
for { for {
*fp = Fp{} *fp = Fp{}
var buf [len(fp) * limbByteSize]byte
if _, err := io.ReadFull(rand.Reader, buf[:]); err != nil { if _, err := io.ReadFull(rand.Reader, buf[:]); err != nil {
// OZAPTF: to be re-done (AES_CTR) // OZAPTF: to be re-done (AES_CTR)
panic("Can't read random number") panic("Can't read random number")
@ -60,15 +63,14 @@ func (c *PrivateKey) Generate(rand io.Reader) error {
} }
for i := 0; i < len(primes); { for i := 0; i < len(primes); {
var buf [64]byte _, err := io.ReadFull(rand, c.tmp[:])
_, err := io.ReadFull(rand, buf[:])
if err != nil { if err != nil {
return err return err
} }
for j, _ := range buf { for j, _ := range c.tmp {
if int8(buf[j]) <= expMax && int8(buf[j]) >= -expMax { if int8(c.tmp[j]) <= expMax && int8(c.tmp[j]) >= -expMax {
c.e[i>>1] |= int8((buf[j] & 0xf) << uint((i%2)*4)) c.e[i>>1] |= int8((c.tmp[j] & 0xf) << uint((i%2)*4))
i = i + 1 i = i + 1
if i == len(primes) { if i == len(primes) {
break break

View File

@ -272,31 +272,31 @@ func testProcessTestVectors(t *testing.T) {
func TestProcessTestVectors(t *testing.T) { testProcessTestVectors(t) } func TestProcessTestVectors(t *testing.T) { testProcessTestVectors(t) }
var prv1, prv2 PrivateKey
// Private key generation // Private key generation
func BenchmarkGeneratePrivate(b *testing.B) { func BenchmarkGeneratePrivate(b *testing.B) {
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
var prv PrivateKey prv1.Generate(rng)
prv.Generate(rng)
} }
} }
// Public key generation from private (group action on empty key) // Public key generation from private (group action on empty key)
func BenchmarkGeneratePublic(b *testing.B) { func BenchmarkGeneratePublic(b *testing.B) {
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
var prv PrivateKey
var pub PublicKey var pub PublicKey
prv.Generate(rng) prv1.Generate(rng)
pub.Generate(&prv) pub.Generate(&prv1)
} }
} }
// Benchmark validation on same key multiple times // Benchmark validation on same key multiple times
func BenchmarkValidate(b *testing.B) { func BenchmarkValidate(b *testing.B) {
var pub PublicKey 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}
var prv PrivateKey prv1.Import(prvBytes)
prv.Generate(rng) var pub PublicKey
pub.Generate(&prv) pub.Generate(&prv1)
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
pub.Validate() pub.Validate()
@ -320,18 +320,15 @@ func BenchmarkValidateRandom(b *testing.B) {
// Benchmark validation on different keys // Benchmark validation on different keys
func BenchmarkValidateGenerated(b *testing.B) { func BenchmarkValidateGenerated(b *testing.B) {
var pub PublicKey var pub PublicKey
var prv PrivateKey
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
prv.Generate(rng) prv1.Generate(rng)
pub.Generate(&prv) pub.Generate(&prv1)
pub.Validate() pub.Validate()
} }
} }
func BenchmarkDeriveGenerated(b *testing.B) { func BenchmarkDeriveGenerated(b *testing.B) {
var ss [64]uint8 var ss [64]uint8
var prv1, prv2 PrivateKey
var pub1, pub2 PublicKey var pub1, pub2 PublicKey
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
prv1.Generate(rng) prv1.Generate(rng)
@ -346,7 +343,6 @@ func BenchmarkDeriveGenerated(b *testing.B) {
func BenchmarkDerive(b *testing.B) { func BenchmarkDerive(b *testing.B) {
var ss [64]uint8 var ss [64]uint8
var prv1, prv2 PrivateKey
var pub1, pub2 PublicKey var pub1, pub2 PublicKey
prv1.Generate(rng) prv1.Generate(rng)

View File

@ -79,6 +79,7 @@ func cswapPoint(P1, P2 *Point, choice uint8) {
// see this: https://eprint.iacr.org/2017/264.pdf // see this: https://eprint.iacr.org/2017/264.pdf
func xMul512(kP, P *Point, co *Coeff, k *Fp) { func xMul512(kP, P *Point, co *Coeff, k *Fp) {
var A24 Coeff var A24 Coeff
R := *P R := *P
// P = P-Q // P = P-Q
PdQ := *P PdQ := *P