Browse Source

Add scanner/valuer interface to PublicKey

feature/signature-sql-interface
a_bennassar 4 years ago
parent
commit
a86308cb0b
2 changed files with 41 additions and 1 deletions
  1. +25
    -0
      babyjub/eddsa.go
  2. +16
    -1
      babyjub/eddsa_test.go

+ 25
- 0
babyjub/eddsa.go

@ -283,3 +283,28 @@ func (p *PublicKey) VerifyPoseidon(msg *big.Int, sig *Signature) bool {
right.Add(sig.R8, right) // right = 8 * R + 8 * hm * A right.Add(sig.R8, right) // right = 8 * R + 8 * hm * A
return (left.X.Cmp(right.X) == 0) && (left.Y.Cmp(right.Y) == 0) return (left.X.Cmp(right.X) == 0) && (left.Y.Cmp(right.Y) == 0)
} }
// Scan implements Scanner for database/sql.
func (p *PublicKey) Scan(src interface{}) error {
srcB, ok := src.([]byte)
if !ok {
return fmt.Errorf("can't scan %T into PublicKey", src)
}
if len(srcB) != 32 {
return fmt.Errorf("can't scan []byte of len %d into PublicKey, want %d", len(srcB), 32)
}
var comp PublicKeyComp
copy(comp[:], srcB)
decomp, err := comp.Decompress()
if err != nil {
return err
}
*p = *decomp
return nil
}
// Value implements valuer for database/sql.
func (p *PublicKey) Value() (driver.Value, error) {
comp := p.Compress()
return comp[:], nil
}

+ 16
- 1
babyjub/eddsa_test.go

@ -135,7 +135,7 @@ func TestCompressDecompress(t *testing.T) {
} }
} }
func TestScannerValuer(t *testing.T) {
func TestSignatureScannerValuer(t *testing.T) {
privK := NewRandPrivKey() privK := NewRandPrivKey()
var value driver.Valuer var value driver.Valuer
var scan sql.Scanner var scan sql.Scanner
@ -147,6 +147,21 @@ func TestScannerValuer(t *testing.T) {
assert.Equal(t, value, scan) assert.Equal(t, value, scan)
} }
func TestPubKeyScannerValuer(t *testing.T) {
privKValue := NewRandPrivKey()
pubKValue := privKValue.Public()
privKScan := NewRandPrivKey()
pubKScan := privKScan.Public()
var value driver.Valuer
var scan sql.Scanner
value = pubKValue
scan = pubKScan
fromDB, err := value.Value()
assert.NoError(t, err)
assert.NoError(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[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))

Loading…
Cancel
Save