Browse Source

Merge pull request #33 from iden3/feature/pkcomp-scanvalue

Add scanner/valuer interface to babyjub.PublicKeyComp
feature/exp-comppoint-signy
Eduard S 4 years ago
committed by GitHub
parent
commit
0a5c6acba3
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions
  1. +18
    -0
      babyjub/eddsa.go
  2. +16
    -1
      babyjub/eddsa_test.go

+ 18
- 0
babyjub/eddsa.go

@ -354,3 +354,21 @@ func (pk PublicKey) Value() (driver.Value, error) {
comp := pk.Compress() comp := pk.Compress()
return comp[:], nil return comp[:], nil
} }
// Scan implements Scanner for database/sql.
func (pkComp *PublicKeyComp) Scan(src interface{}) error {
srcB, ok := src.([]byte)
if !ok {
return fmt.Errorf("can't scan %T into PublicKeyComp", src)
}
if len(srcB) != 32 {
return fmt.Errorf("can't scan []byte of len %d into PublicKeyComp, want %d", len(srcB), 32)
}
copy(pkComp[:], srcB)
return nil
}
// Value implements valuer for database/sql.
func (pkComp PublicKeyComp) Value() (driver.Value, error) {
return pkComp[:], nil
}

+ 16
- 1
babyjub/eddsa_test.go

@ -160,7 +160,7 @@ func TestSignatureScannerValuer(t *testing.T) {
assert.Equal(t, value, scan) assert.Equal(t, value, scan)
} }
func TestPubKeyScannerValuer(t *testing.T) {
func TestPublicKeyScannerValuer(t *testing.T) {
privKValue := NewRandPrivKey() privKValue := NewRandPrivKey()
pubKValue := privKValue.Public() pubKValue := privKValue.Public()
privKScan := NewRandPrivKey() privKScan := NewRandPrivKey()
@ -175,6 +175,21 @@ func TestPubKeyScannerValuer(t *testing.T) {
assert.Equal(t, value, scan) assert.Equal(t, value, scan)
} }
func TestPublicKeyCompScannerValuer(t *testing.T) {
privKValue := NewRandPrivKey()
pubKCompValue := privKValue.Public().Compress()
privKScan := NewRandPrivKey()
pubKCompScan := privKScan.Public().Compress()
var value driver.Valuer
var scan sql.Scanner
value = &pubKCompValue
scan = &pubKCompScan
fromDB, err := value.Value()
assert.Nil(t, err)
assert.Nil(t, scan.Scan(fromDB))
assert.Equal(t, value, scan)
}
func BenchmarkBabyjubEddsa(b *testing.B) { func BenchmarkBabyjubEddsa(b *testing.B) {
var k PrivateKey var k PrivateKey
_, err := hex.Decode(k[:], _, err := hex.Decode(k[:],

Loading…
Cancel
Save