mv of babyjub.PublicKey to babyjub.PublicKeyComp

Update usage of `*babyjub.PublicKey` to `babyjub.PublicKeyComp`
- when the key is not defined, internally is used `babyjub.EmptyBJJComp`, which is a `[32]byte` of zeroes of type `babyjub.PublicKeyComp`
- the API continues returning `nil` when the key is not defined
This commit is contained in:
arnaucube
2020-12-18 17:19:07 +01:00
parent b9943182b8
commit 4b10549822
47 changed files with 761 additions and 733 deletions

View File

@@ -143,14 +143,13 @@ func (s *StrHezEthAddr) UnmarshalText(text []byte) error {
return nil
}
// HezBJJ is used to scan/value *babyjub.PublicKey directly into strings that follow the BJJ public key hez fotmat (^hez:[A-Za-z0-9_-]{44}$) from/to sql DBs.
// It assumes that *babyjub.PublicKey are inserted/fetched to/from the DB using the default Scan/Value interface
// HezBJJ is used to scan/value *babyjub.PublicKeyComp directly into strings that follow the BJJ public key hez fotmat (^hez:[A-Za-z0-9_-]{44}$) from/to sql DBs.
// It assumes that *babyjub.PublicKeyComp are inserted/fetched to/from the DB using the default Scan/Value interface
type HezBJJ string
// NewHezBJJ creates a HezBJJ from a *babyjub.PublicKey.
// NewHezBJJ creates a HezBJJ from a *babyjub.PublicKeyComp.
// Calling this method with a nil bjj causes panic
func NewHezBJJ(bjj *babyjub.PublicKey) HezBJJ {
pkComp := [32]byte(bjj.Compress())
func NewHezBJJ(pkComp babyjub.PublicKeyComp) HezBJJ {
sum := pkComp[0]
for i := 1; i < len(pkComp); i++ {
sum += pkComp[i]
@@ -159,20 +158,20 @@ func NewHezBJJ(bjj *babyjub.PublicKey) HezBJJ {
return HezBJJ("hez:" + base64.RawURLEncoding.EncodeToString(bjjSum))
}
func hezStrToBJJ(s string) (*babyjub.PublicKey, error) {
func hezStrToBJJ(s string) (babyjub.PublicKeyComp, error) {
const decodedLen = 33
const encodedLen = 44
formatErr := errors.New("invalid BJJ format. Must follow this regex: ^hez:[A-Za-z0-9_-]{44}$")
encoded := strings.TrimPrefix(s, "hez:")
if len(encoded) != encodedLen {
return nil, formatErr
return common.EmptyBJJComp, formatErr
}
decoded, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil {
return nil, formatErr
return common.EmptyBJJComp, formatErr
}
if len(decoded) != decodedLen {
return nil, formatErr
return common.EmptyBJJComp, formatErr
}
bjjBytes := [decodedLen - 1]byte{}
copy(bjjBytes[:decodedLen-1], decoded[:decodedLen-1])
@@ -181,27 +180,27 @@ func hezStrToBJJ(s string) (*babyjub.PublicKey, error) {
sum += bjjBytes[i]
}
if decoded[decodedLen-1] != sum {
return nil, tracerr.Wrap(errors.New("checksum verification failed"))
return common.EmptyBJJComp, tracerr.Wrap(errors.New("checksum verification failed"))
}
bjjComp := babyjub.PublicKeyComp(bjjBytes)
return bjjComp.Decompress()
return bjjComp, nil
}
// ToBJJ returns a *babyjub.PublicKey created from HezBJJ
func (b HezBJJ) ToBJJ() (*babyjub.PublicKey, error) {
// ToBJJ returns a babyjub.PublicKeyComp created from HezBJJ
func (b HezBJJ) ToBJJ() (babyjub.PublicKeyComp, error) {
return hezStrToBJJ(string(b))
}
// Scan implements Scanner for database/sql
func (b *HezBJJ) Scan(src interface{}) error {
bjj := &babyjub.PublicKey{}
bjj := &babyjub.PublicKeyComp{}
if err := bjj.Scan(src); err != nil {
return tracerr.Wrap(err)
}
if bjj == nil {
return nil
}
*b = NewHezBJJ(bjj)
*b = NewHezBJJ(*bjj)
return nil
}
@@ -214,8 +213,8 @@ func (b HezBJJ) Value() (driver.Value, error) {
return bjj.Value()
}
// StrHezBJJ is used to unmarshal HezBJJ directly into an alias of babyjub.PublicKey
type StrHezBJJ babyjub.PublicKey
// StrHezBJJ is used to unmarshal HezBJJ directly into an alias of babyjub.PublicKeyComp
type StrHezBJJ babyjub.PublicKeyComp
// UnmarshalText unmarshals a StrHezBJJ
func (s *StrHezBJJ) UnmarshalText(text []byte) error {
@@ -223,7 +222,7 @@ func (s *StrHezBJJ) UnmarshalText(text []byte) error {
if err != nil {
return tracerr.Wrap(err)
}
*s = StrHezBJJ(*bjj)
*s = StrHezBJJ(bjj)
return nil
}

View File

@@ -157,11 +157,11 @@ func TestStrHezBJJ(t *testing.T) {
I StrHezBJJ
}
priv := babyjub.NewRandPrivKey()
hezBjj := NewHezBJJ(priv.Public())
hezBjj := NewHezBJJ(priv.Public().Compress())
from := []byte(`{"I":"` + hezBjj + `"}`)
to := &testStrHezBJJ{}
assert.NoError(t, json.Unmarshal(from, to))
assert.Equal(t, priv.Public(), (*babyjub.PublicKey)(&to.I))
assert.Equal(t, priv.Public().Compress(), (babyjub.PublicKeyComp)(to.I))
}
func TestStrHezIdx(t *testing.T) {
@@ -262,20 +262,23 @@ func TestHezBJJ(t *testing.T) {
assert.NoError(t, err)
// Example structs
type bjjStruct struct {
I *babyjub.PublicKey `meddler:"i"`
I babyjub.PublicKeyComp `meddler:"i"`
}
type hezBJJStruct struct {
I HezBJJ `meddler:"i"`
}
type bjjStructNil struct {
I *babyjub.PublicKeyComp `meddler:"i"`
}
type hezBJJStructNil struct {
I *HezBJJ `meddler:"i"`
}
// Not nil case
// Insert into DB using *babyjub.PublicKey Scan/Value
// Insert into DB using *babyjub.PublicKeyComp Scan/Value
priv := babyjub.NewRandPrivKey()
fromBJJ := bjjStruct{
I: priv.Public(),
I: priv.Public().Compress(),
}
err = meddler.Insert(db, "test", &fromBJJ)
assert.NoError(t, err)
@@ -289,11 +292,11 @@ func TestHezBJJ(t *testing.T) {
assert.NoError(t, err)
// Insert into DB using HezBJJ Scan/Value
fromHezBJJ := hezBJJStruct{
I: NewHezBJJ(priv.Public()),
I: NewHezBJJ(priv.Public().Compress()),
}
err = meddler.Insert(db, "test", &fromHezBJJ)
assert.NoError(t, err)
// Read from DB using *babyjub.PublicKey Scan/Value
// Read from DB using *babyjub.PublicKeyComp Scan/Value
toBJJ := bjjStruct{}
err = meddler.QueryRow(db, &toBJJ, "select * from test")
assert.NoError(t, err)
@@ -303,8 +306,8 @@ func TestHezBJJ(t *testing.T) {
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using *babyjub.PublicKey Scan/Value
fromBJJNil := bjjStruct{
// Insert into DB using *babyjub.PublicKeyComp Scan/Value
fromBJJNil := bjjStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromBJJNil)
@@ -326,9 +329,10 @@ func TestHezBJJ(t *testing.T) {
}
err = meddler.Insert(db, "test", &fromHezBJJNil)
assert.NoError(t, err)
// Read from DB using *babyjub.PublicKey Scan/Value
toBJJNil := bjjStruct{
I: priv.Public(), // check that this will be set to nil, not because of not being initialized
// Read from DB using *babyjub.PublicKeyComp Scan/Value
bjjComp := priv.Public().Compress()
toBJJNil := bjjStructNil{
I: &bjjComp, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toBJJNil, "select * from test")
assert.NoError(t, err)