diff options
Diffstat (limited to 'go')
-rw-r--r-- | go/bls/bls.go | 58 | ||||
-rw-r--r-- | go/bls/bls_test.go | 135 |
2 files changed, 127 insertions, 66 deletions
diff --git a/go/bls/bls.go b/go/bls/bls.go index 45408ac..c66f46b 100644 --- a/go/bls/bls.go +++ b/go/bls/bls.go @@ -27,7 +27,7 @@ const CurveFp382_2 = 2 func Init(curve int) error { err := C.blsInit(C.int(curve), C.BLS_MAX_OP_UNIT_SIZE) if err != 0 { - return fmt.Errorf("ERR Init curve=%d\n", curve) + return fmt.Errorf("ERR Init curve=%d", curve) } return nil } @@ -75,7 +75,7 @@ func (id *ID) getPointer() (p *C.blsId) { return (*C.blsId)(unsafe.Pointer(&id.v[0])) } -// GetByte +// GetByte -- func (id *ID) GetByte(ioMode int) []byte { buf := make([]byte, 1024) // #nosec @@ -96,32 +96,32 @@ func (id *ID) SetByte(buf []byte, ioMode int) error { return nil } -// Serialize +// Serialize -- func (id *ID) Serialize() []byte { return id.GetByte(C.blsIoEcComp) } -// Deserialize +// Deserialize -- func (id *ID) Deserialize(b []byte) error { return id.SetByte(b, C.blsIoEcComp) } -// GetHexString +// GetHexString -- func (id *ID) GetHexString() string { return string(id.GetByte(16)) } -// GetDecString +// GetDecString -- func (id *ID) GetDecString() string { return string(id.GetByte(10)) } -// SetHexString +// SetHexString -- func (id *ID) SetHexString(s string) error { return id.SetByte([]byte(s), 16) } -// SetDecString +// SetDecString -- func (id *ID) SetDecString(s string) error { return id.SetByte([]byte(s), 10) } @@ -152,7 +152,7 @@ func (sec *SecretKey) getPointer() (p *C.blsSecretKey) { return (*C.blsSecretKey)(unsafe.Pointer(&sec.v[0])) } -// GetByte +// GetByte -- func (sec *SecretKey) GetByte(ioMode int) []byte { buf := make([]byte, 1024) // #nosec @@ -173,32 +173,32 @@ func (sec *SecretKey) SetByte(buf []byte, ioMode int) error { return nil } -// Serialize +// Serialize -- func (sec *SecretKey) Serialize() []byte { return sec.GetByte(C.blsIoEcComp) } -// Deserialize +// Deserialize -- func (sec *SecretKey) Deserialize(b []byte) error { return sec.SetByte(b, C.blsIoEcComp) } -// GetHexString +// GetHexString -- func (sec *SecretKey) GetHexString() string { return string(sec.GetByte(16)) } -// GetDecString +// GetDecString -- func (sec *SecretKey) GetDecString() string { return string(sec.GetByte(10)) } -// SetHexString +// SetHexString -- func (sec *SecretKey) SetHexString(s string) error { return sec.SetByte([]byte(s), 16) } -// SetDecString +// SetDecString -- func (sec *SecretKey) SetDecString(s string) error { return sec.SetByte([]byte(s), 10) } @@ -284,10 +284,10 @@ func (pub *PublicKey) getPointer() (p *C.blsPublicKey) { return (*C.blsPublicKey)(unsafe.Pointer(&pub.v[0])) } -// GetByte +// GetByte -- func (pub *PublicKey) GetByte(ioMode int) []byte { buf := make([]byte, 1024) - // #nopub + // #nosec n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) if n == 0 { panic("implementation err. size of buf is small") @@ -297,7 +297,7 @@ func (pub *PublicKey) GetByte(ioMode int) []byte { // SetByte -- func (pub *PublicKey) SetByte(buf []byte, ioMode int) error { - // #nopub + // #nosec err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) if err != 0 { return fmt.Errorf("bad byte:%x", buf) @@ -305,22 +305,22 @@ func (pub *PublicKey) SetByte(buf []byte, ioMode int) error { return nil } -// Serialize +// Serialize -- func (pub *PublicKey) Serialize() []byte { return pub.GetByte(C.blsIoEcComp) } -// Deserialize +// Deserialize -- func (pub *PublicKey) Deserialize(b []byte) error { return pub.SetByte(b, C.blsIoEcComp) } -// GetHexString +// GetHexString -- func (pub *PublicKey) GetHexString() string { return fmt.Sprintf("%x", pub.Serialize()) } -// SetHexString +// SetHexString -- func (pub *PublicKey) SetHexString(s string) error { b, err := hex.DecodeString(s) if err != nil { @@ -368,10 +368,10 @@ func (sign *Sign) getPointer() (p *C.blsSign) { return (*C.blsSign)(unsafe.Pointer(&sign.v[0])) } -// GetByte +// GetByte -- func (sign *Sign) GetByte(ioMode int) []byte { buf := make([]byte, 1024) - // #nosign + // #nosec n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) if n == 0 { panic("implementation err. size of buf is small") @@ -381,7 +381,7 @@ func (sign *Sign) GetByte(ioMode int) []byte { // SetByte -- func (sign *Sign) SetByte(buf []byte, ioMode int) error { - // #nosign + // #nosec err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) if err != 0 { return fmt.Errorf("bad byte:%x", buf) @@ -389,22 +389,22 @@ func (sign *Sign) SetByte(buf []byte, ioMode int) error { return nil } -// Serialize +// Serialize -- func (sign *Sign) Serialize() []byte { return sign.GetByte(C.blsIoEcComp) } -// Deserialize +// Deserialize -- func (sign *Sign) Deserialize(b []byte) error { return sign.SetByte(b, C.blsIoEcComp) } -// GetHexString +// GetHexString -- func (sign *Sign) GetHexString() string { return fmt.Sprintf("%x", sign.Serialize()) } -// SetHexString +// SetHexString -- func (sign *Sign) SetHexString(s string) error { b, err := hex.DecodeString(s) if err != nil { diff --git a/go/bls/bls_test.go b/go/bls/bls_test.go index 4e79f08..361b662 100644 --- a/go/bls/bls_test.go +++ b/go/bls/bls_test.go @@ -20,14 +20,14 @@ func testPre(t *testing.T) { t.Fatal(err) } if !id.IsSame(&id2) { - t.Errorf("not same id", id.GetHexString(), id2.GetHexString()) + t.Errorf("not same id\n%s\n%s", id.GetHexString(), id2.GetHexString()) } err = id2.SetDecString(id.GetDecString()) if err != nil { t.Fatal(err) } if !id.IsSame(&id2) { - t.Errorf("not same id", id.GetDecString(), id2.GetDecString()) + t.Errorf("not same id\n%s\n%s", id.GetDecString(), id2.GetDecString()) } } { @@ -101,32 +101,23 @@ func testRecoverSecretKey(t *testing.T) { idVec := make([]ID, n) for i := 0; i < n; i++ { idVec[i].Set([]uint64{uint64(i), 1, 2, 3, 4, 5}[0:unitN]) - secVec[i].Set(msk, &idVec[i]) + err := secVec[i].Set(msk, &idVec[i]) + if err != nil { + t.Errorf("%s\n", err) + } } // recover sec2 from secVec and idVec var sec2 SecretKey - sec2.Recover(secVec, idVec) + err := sec2.Recover(secVec, idVec) + if err != nil { + t.Errorf("err%s\n", err) + } if !sec.IsSame(&sec2) { t.Errorf("Mismatch in recovered secret key:\n %s\n %s.", sec.GetHexString(), sec2.GetHexString()) } } -func testSign(t *testing.T) { - m := "testSign" - t.Log(m) - - var sec0 SecretKey - sec0.Init() - pub0 := sec0.GetPublicKey() - s0 := sec0.Sign(m) - if !s0.Verify(pub0, m) { - t.Error("Signature does not verify") - } - - k := 3 - msk := sec0.GetMasterSecretKey(k) - mpk := GetMasterPublicKey(msk) - +func testEachSign(t *testing.T, m string, msk []SecretKey, mpk []PublicKey) ([]ID, []SecretKey, []PublicKey, []Sign) { idTbl := []uint64{3, 5, 193, 22, 15} n := len(idTbl) @@ -139,9 +130,15 @@ func testSign(t *testing.T) { idVec[i].Set([]uint64{idTbl[i], 0, 0, 0, 0, 0}[0:unitN]) t.Logf("idVec[%d]=%s\n", i, idVec[i].GetHexString()) - secVec[i].Set(msk, &idVec[i]) + err := secVec[i].Set(msk, &idVec[i]) + if err != nil { + t.Error(err) + } - pubVec[i].Set(mpk, &idVec[i]) + err = pubVec[i].Set(mpk, &idVec[i]) + if err != nil { + t.Error(err) + } t.Logf("pubVec[%d]=%s\n", i, pubVec[i].GetHexString()) if !pubVec[i].IsSame(secVec[i].GetPublicKey()) { @@ -153,18 +150,46 @@ func testSign(t *testing.T) { t.Error("Pubkey derivation does not match") } } + return idVec, secVec, pubVec, signVec +} +func testSign(t *testing.T) { + m := "testSign" + t.Log(m) + + var sec0 SecretKey + sec0.Init() + pub0 := sec0.GetPublicKey() + s0 := sec0.Sign(m) + if !s0.Verify(pub0, m) { + t.Error("Signature does not verify") + } + + k := 3 + msk := sec0.GetMasterSecretKey(k) + mpk := GetMasterPublicKey(msk) + idVec, secVec, pubVec, signVec := testEachSign(t, m, msk, mpk) + var sec1 SecretKey - sec1.Recover(secVec, idVec) + err := sec1.Recover(secVec, idVec) + if err != nil { + t.Error(err) + } if !sec0.IsSame(&sec1) { t.Error("Mismatch in recovered seckey.") } var pub1 PublicKey - pub1.Recover(pubVec, idVec) + err = pub1.Recover(pubVec, idVec) + if err != nil { + t.Error(err) + } if !pub0.IsSame(&pub1) { t.Error("Mismatch in recovered pubkey.") } var s1 Sign - s1.Recover(signVec, idVec) + err = s1.Recover(signVec, idVec) + if err != nil { + t.Error(err) + } if !s0.IsSame(&s1) { t.Error("Mismatch in recovered signature.") } @@ -268,7 +293,10 @@ func testOrder(t *testing.T, c int) { } func test(t *testing.T, c int) { - Init(c) + err := Init(c) + if err != nil { + t.Fatal(err) + } unitN = GetOpUnitSize() t.Logf("unitN=%d\n", unitN) testPre(t) @@ -301,7 +329,10 @@ var curve = CurveFp382_1 func BenchmarkPubkeyFromSeckey(b *testing.B) { b.StopTimer() - Init(curve) + err := Init(curve) + if err != nil { + b.Fatal(err) + } var sec SecretKey for n := 0; n < b.N; n++ { sec.Init() @@ -313,7 +344,10 @@ func BenchmarkPubkeyFromSeckey(b *testing.B) { func BenchmarkSigning(b *testing.B) { b.StopTimer() - Init(curve) + err := Init(curve) + if err != nil { + b.Fatal(err) + } var sec SecretKey for n := 0; n < b.N; n++ { sec.Init() @@ -325,7 +359,10 @@ func BenchmarkSigning(b *testing.B) { func BenchmarkValidation(b *testing.B) { b.StopTimer() - Init(curve) + err := Init(curve) + if err != nil { + b.Fatal(err) + } var sec SecretKey for n := 0; n < b.N; n++ { sec.Init() @@ -340,7 +377,10 @@ func BenchmarkValidation(b *testing.B) { func benchmarkDeriveSeckeyShare(k int, b *testing.B) { b.StopTimer() - Init(curve) + err := Init(curve) + if err != nil { + b.Fatal(err) + } var sec SecretKey sec.Init() msk := sec.GetMasterSecretKey(k) @@ -348,8 +388,11 @@ func benchmarkDeriveSeckeyShare(k int, b *testing.B) { for n := 0; n < b.N; n++ { id.Set([]uint64{1, 2, 3, 4, 5, uint64(n)}) b.StartTimer() - sec.Set(msk, &id) + err := sec.Set(msk, &id) b.StopTimer() + if err != nil { + b.Errorf("%s\n", err) + } } } @@ -361,7 +404,10 @@ func BenchmarkDeriveSeckeyShare500(b *testing.B) { benchmarkDeriveSeckeyShare(50 func benchmarkRecoverSeckey(k int, b *testing.B) { b.StopTimer() - Init(curve) + err := Init(curve) + if err != nil { + b.Fatal(err) + } var sec SecretKey sec.Init() msk := sec.GetMasterSecretKey(k) @@ -372,14 +418,20 @@ func benchmarkRecoverSeckey(k int, b *testing.B) { idVec := make([]ID, n) for i := 0; i < n; i++ { idVec[i].Set([]uint64{1, 2, 3, 4, 5, uint64(i)}) - secVec[i].Set(msk, &idVec[i]) + err := secVec[i].Set(msk, &idVec[i]) + if err != nil { + b.Errorf("%s\n", err) + } } // recover from secVec and idVec var sec2 SecretKey b.StartTimer() for n := 0; n < b.N; n++ { - sec2.Recover(secVec, idVec) + err := sec2.Recover(secVec, idVec) + if err != nil { + b.Errorf("%s\n", err) + } } } @@ -390,7 +442,10 @@ func BenchmarkRecoverSeckey1000(b *testing.B) { benchmarkRecoverSeckey(1000, b) func benchmarkRecoverSignature(k int, b *testing.B) { b.StopTimer() - Init(curve) + err := Init(curve) + if err != nil { + b.Fatal(err) + } var sec SecretKey sec.Init() msk := sec.GetMasterSecretKey(k) @@ -402,7 +457,10 @@ func benchmarkRecoverSignature(k int, b *testing.B) { signVec := make([]Sign, n) for i := 0; i < n; i++ { idVec[i].Set([]uint64{1, 2, 3, 4, 5, uint64(i)}) - secVec[i].Set(msk, &idVec[i]) + err := secVec[i].Set(msk, &idVec[i]) + if err != nil { + b.Errorf("%s\n", err) + } signVec[i] = *secVec[i].Sign("test message") } @@ -410,7 +468,10 @@ func benchmarkRecoverSignature(k int, b *testing.B) { var sig Sign b.StartTimer() for n := 0; n < b.N; n++ { - sig.Recover(signVec, idVec) + err := sig.Recover(signVec, idVec) + if err != nil { + b.Errorf("%s\n", err) + } } } |