mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
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:
@@ -115,13 +115,13 @@ func NonceFromBytes(b [5]byte) Nonce {
|
||||
|
||||
// Account is a struct that gives information of the holdings of an address and a specific token. Is the data structure that generates the Value stored in the leaf of the MerkleTree
|
||||
type Account struct {
|
||||
Idx Idx `meddler:"idx"`
|
||||
TokenID TokenID `meddler:"token_id"`
|
||||
BatchNum BatchNum `meddler:"batch_num"`
|
||||
PublicKey *babyjub.PublicKey `meddler:"bjj"`
|
||||
EthAddr ethCommon.Address `meddler:"eth_addr"`
|
||||
Nonce Nonce `meddler:"-"` // max of 40 bits used
|
||||
Balance *big.Int `meddler:"-"` // max of 192 bits used
|
||||
Idx Idx `meddler:"idx"`
|
||||
TokenID TokenID `meddler:"token_id"`
|
||||
BatchNum BatchNum `meddler:"batch_num"`
|
||||
PublicKey babyjub.PublicKeyComp `meddler:"bjj"`
|
||||
EthAddr ethCommon.Address `meddler:"eth_addr"`
|
||||
Nonce Nonce `meddler:"-"` // max of 40 bits used
|
||||
Balance *big.Int `meddler:"-"` // max of 192 bits used
|
||||
}
|
||||
|
||||
func (a *Account) String() string {
|
||||
@@ -158,15 +158,13 @@ func (a *Account) Bytes() ([32 * NLeafElems]byte, error) {
|
||||
copy(b[28:32], a.TokenID.Bytes())
|
||||
copy(b[23:28], nonceBytes[:])
|
||||
|
||||
if a.PublicKey == nil {
|
||||
return b, tracerr.Wrap(fmt.Errorf("Account.PublicKey can not be nil"))
|
||||
}
|
||||
if babyjub.PointCoordSign(a.PublicKey.X) {
|
||||
pkSign, pkY := babyjub.UnpackSignY(a.PublicKey)
|
||||
if pkSign {
|
||||
b[22] = 1
|
||||
}
|
||||
balanceBytes := a.Balance.Bytes()
|
||||
copy(b[64-len(balanceBytes):64], balanceBytes)
|
||||
ayBytes := a.PublicKey.Y.Bytes()
|
||||
ayBytes := pkY.Bytes()
|
||||
copy(b[96-len(ayBytes):96], ayBytes)
|
||||
copy(b[108:128], a.EthAddr.Bytes())
|
||||
|
||||
@@ -234,11 +232,7 @@ func AccountFromBytes(b [32 * NLeafElems]byte) (*Account, error) {
|
||||
return nil, tracerr.Wrap(fmt.Errorf("%s Balance", ErrNumOverflow))
|
||||
}
|
||||
ay := new(big.Int).SetBytes(b[64:96])
|
||||
pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
publicKey := babyjub.PublicKey(*pkPoint)
|
||||
publicKeyComp := babyjub.PackSignY(sign, ay)
|
||||
ethAddr := ethCommon.BytesToAddress(b[108:128])
|
||||
|
||||
if !cryptoUtils.CheckBigIntInField(balance) {
|
||||
@@ -252,7 +246,7 @@ func AccountFromBytes(b [32 * NLeafElems]byte) (*Account, error) {
|
||||
TokenID: TokenID(tokenID),
|
||||
Nonce: nonce,
|
||||
Balance: balance,
|
||||
PublicKey: &publicKey,
|
||||
PublicKey: publicKeyComp,
|
||||
EthAddr: ethAddr,
|
||||
}
|
||||
return &a, nil
|
||||
|
||||
@@ -22,11 +22,11 @@ import (
|
||||
func TestIdxParser(t *testing.T) {
|
||||
i := Idx(1)
|
||||
iBytes, err := i.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 6, len(iBytes))
|
||||
assert.Equal(t, "000000000001", hex.EncodeToString(iBytes[:]))
|
||||
i2, err := IdxFromBytes(iBytes[:])
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, i, i2)
|
||||
|
||||
i = Idx(100)
|
||||
@@ -35,11 +35,11 @@ func TestIdxParser(t *testing.T) {
|
||||
// value before overflow
|
||||
i = Idx(281474976710655)
|
||||
iBytes, err = i.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 6, len(iBytes))
|
||||
assert.Equal(t, "ffffffffffff", hex.EncodeToString(iBytes[:]))
|
||||
i2, err = IdxFromBytes(iBytes[:])
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, i, i2)
|
||||
|
||||
// expect value overflow
|
||||
@@ -52,7 +52,7 @@ func TestIdxParser(t *testing.T) {
|
||||
func TestNonceParser(t *testing.T) {
|
||||
n := Nonce(1)
|
||||
nBytes, err := n.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5, len(nBytes))
|
||||
assert.Equal(t, "0000000001", hex.EncodeToString(nBytes[:]))
|
||||
n2 := NonceFromBytes(nBytes)
|
||||
@@ -61,7 +61,7 @@ func TestNonceParser(t *testing.T) {
|
||||
// value before overflow
|
||||
n = Nonce(1099511627775)
|
||||
nBytes, err = n.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5, len(nBytes))
|
||||
assert.Equal(t, "ffffffffff", hex.EncodeToString(nBytes[:]))
|
||||
n2 = NonceFromBytes(nBytes)
|
||||
@@ -77,25 +77,25 @@ func TestNonceParser(t *testing.T) {
|
||||
func TestAccount(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
pk := sk.Public()
|
||||
|
||||
account := &Account{
|
||||
TokenID: TokenID(1),
|
||||
Nonce: Nonce(1234),
|
||||
Balance: big.NewInt(1000),
|
||||
PublicKey: pk,
|
||||
PublicKey: pk.Compress(),
|
||||
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
b, err := account.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, byte(1), b[22])
|
||||
a1, err := AccountFromBytes(b)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, account, a1)
|
||||
|
||||
e, err := account.BigInts()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
|
||||
@@ -106,7 +106,7 @@ func TestAccount(t *testing.T) {
|
||||
assert.Equal(t, new(big.Int).SetBytes(account.EthAddr.Bytes()).String(), e[3].String())
|
||||
|
||||
a2, err := AccountFromBigInts(e)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, account, a2)
|
||||
assert.Equal(t, a1, a2)
|
||||
}
|
||||
@@ -116,35 +116,35 @@ func TestAccountLoop(t *testing.T) {
|
||||
for i := 0; i < 256; i++ {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
pk := sk.Public()
|
||||
|
||||
key, err := ethCrypto.GenerateKey()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
address := ethCrypto.PubkeyToAddress(key.PublicKey)
|
||||
|
||||
account := &Account{
|
||||
TokenID: TokenID(i),
|
||||
Nonce: Nonce(i),
|
||||
Balance: big.NewInt(1000),
|
||||
PublicKey: pk,
|
||||
PublicKey: pk.Compress(),
|
||||
EthAddr: address,
|
||||
}
|
||||
b, err := account.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
a1, err := AccountFromBytes(b)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, account, a1)
|
||||
|
||||
e, err := account.BigInts()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
|
||||
|
||||
a2, err := AccountFromBigInts(e)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, account, a2)
|
||||
}
|
||||
}
|
||||
@@ -157,31 +157,31 @@ func TestAccountLoopRandom(t *testing.T) {
|
||||
pk := sk.Public()
|
||||
|
||||
key, err := ethCrypto.GenerateKey()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
address := ethCrypto.PubkeyToAddress(key.PublicKey)
|
||||
|
||||
account := &Account{
|
||||
TokenID: TokenID(i),
|
||||
Nonce: Nonce(i),
|
||||
Balance: big.NewInt(1000),
|
||||
PublicKey: pk,
|
||||
PublicKey: pk.Compress(),
|
||||
EthAddr: address,
|
||||
}
|
||||
b, err := account.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
a1, err := AccountFromBytes(b)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, account, a1)
|
||||
|
||||
e, err := account.BigInts()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
|
||||
assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
|
||||
|
||||
a2, err := AccountFromBigInts(e)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, account, a2)
|
||||
}
|
||||
}
|
||||
@@ -200,18 +200,18 @@ func bigFromStr(h string, u int) *big.Int {
|
||||
func TestAccountHashValue(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
pk := sk.Public()
|
||||
|
||||
account := &Account{
|
||||
TokenID: TokenID(1),
|
||||
Nonce: Nonce(1234),
|
||||
Balance: big.NewInt(1000),
|
||||
PublicKey: pk,
|
||||
PublicKey: pk.Compress(),
|
||||
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
v, err := account.HashValue()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "16297758255249203915951182296472515138555043617458222397753168518282206850764", v.String())
|
||||
}
|
||||
|
||||
@@ -219,67 +219,70 @@ func TestAccountHashValueTestVectors(t *testing.T) {
|
||||
// values from js test vectors
|
||||
ay := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(253), nil), big.NewInt(1))
|
||||
assert.Equal(t, "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", (hex.EncodeToString(ay.Bytes())))
|
||||
bjj, err := babyjub.PointFromSignAndY(true, ay)
|
||||
require.Nil(t, err)
|
||||
bjjPoint, err := babyjub.PointFromSignAndY(true, ay)
|
||||
require.NoError(t, err)
|
||||
bjj := babyjub.PublicKey(*bjjPoint)
|
||||
|
||||
account := &Account{
|
||||
Idx: 1,
|
||||
TokenID: 0xFFFFFFFF,
|
||||
PublicKey: (*babyjub.PublicKey)(bjj),
|
||||
PublicKey: bjj.Compress(),
|
||||
EthAddr: ethCommon.HexToAddress("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
|
||||
Nonce: Nonce(0xFFFFFFFFFF),
|
||||
Balance: bigFromStr("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16),
|
||||
}
|
||||
|
||||
e, err := account.BigInts()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "9444732965739290427391", e[0].String())
|
||||
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", e[1].String())
|
||||
assert.Equal(t, "14474011154664524427946373126085988481658748083205070504932198000989141204991", e[2].String())
|
||||
assert.Equal(t, "1461501637330902918203684832716283019655932542975", e[3].String())
|
||||
|
||||
h, err := poseidon.Hash(e[:])
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "4550823210217540218403400309533329186487982452461145263910122718498735057257", h.String())
|
||||
|
||||
v, err := account.HashValue()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "4550823210217540218403400309533329186487982452461145263910122718498735057257", v.String())
|
||||
|
||||
// second account
|
||||
ay = big.NewInt(0)
|
||||
bjj, err = babyjub.PointFromSignAndY(false, ay)
|
||||
require.Nil(t, err)
|
||||
bjjPoint, err = babyjub.PointFromSignAndY(false, ay)
|
||||
require.NoError(t, err)
|
||||
bjj = babyjub.PublicKey(*bjjPoint)
|
||||
account = &Account{
|
||||
TokenID: 0,
|
||||
PublicKey: (*babyjub.PublicKey)(bjj),
|
||||
PublicKey: bjj.Compress(),
|
||||
EthAddr: ethCommon.HexToAddress("0x00"),
|
||||
Nonce: Nonce(0),
|
||||
Balance: big.NewInt(0),
|
||||
}
|
||||
v, err = account.HashValue()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "7750253361301235345986002241352365187241910378619330147114280396816709365657", v.String())
|
||||
|
||||
// third account
|
||||
ay = bigFromStr("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7", 16)
|
||||
bjj, err = babyjub.PointFromSignAndY(false, ay)
|
||||
require.Nil(t, err)
|
||||
bjjPoint, err = babyjub.PointFromSignAndY(false, ay)
|
||||
require.NoError(t, err)
|
||||
bjj = babyjub.PublicKey(*bjjPoint)
|
||||
account = &Account{
|
||||
TokenID: 3,
|
||||
PublicKey: (*babyjub.PublicKey)(bjj),
|
||||
PublicKey: bjj.Compress(),
|
||||
EthAddr: ethCommon.HexToAddress("0xA3C88ac39A76789437AED31B9608da72e1bbfBF9"),
|
||||
Nonce: Nonce(129),
|
||||
Balance: bigFromStr("42000000000000000000", 10),
|
||||
}
|
||||
e, err = account.BigInts()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "554050781187", e[0].String())
|
||||
assert.Equal(t, "42000000000000000000", e[1].String())
|
||||
assert.Equal(t, "15238403086306505038849621710779816852318505119327426213168494964113886299863", e[2].String())
|
||||
assert.Equal(t, "935037732739828347587684875151694054123613453305", e[3].String())
|
||||
v, err = account.HashValue()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "10565754214047872850889045989683221123564392137456000481397520902594455245517", v.String())
|
||||
}
|
||||
|
||||
@@ -290,7 +293,7 @@ func TestAccountErrNotInFF(t *testing.T) {
|
||||
r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
|
||||
e := [NLeafElems]*big.Int{z, z, r, r}
|
||||
_, err := AccountFromBigInts(e)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Q should give error
|
||||
r = cryptoConstants.Q
|
||||
@@ -310,7 +313,7 @@ func TestAccountErrNotInFF(t *testing.T) {
|
||||
func TestAccountErrNumOverflowNonce(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
pk := sk.Public()
|
||||
|
||||
// check limit
|
||||
@@ -318,11 +321,11 @@ func TestAccountErrNumOverflowNonce(t *testing.T) {
|
||||
TokenID: TokenID(1),
|
||||
Nonce: Nonce(math.Pow(2, 40) - 1),
|
||||
Balance: big.NewInt(1000),
|
||||
PublicKey: pk,
|
||||
PublicKey: pk.Compress(),
|
||||
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
_, err = account.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// force value overflow
|
||||
account.Nonce = Nonce(math.Pow(2, 40))
|
||||
@@ -331,13 +334,13 @@ func TestAccountErrNumOverflowNonce(t *testing.T) {
|
||||
assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), tracerr.Unwrap(err))
|
||||
|
||||
_, err = AccountFromBytes(b)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestAccountErrNumOverflowBalance(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
pk := sk.Public()
|
||||
|
||||
// check limit
|
||||
@@ -345,13 +348,13 @@ func TestAccountErrNumOverflowBalance(t *testing.T) {
|
||||
TokenID: TokenID(1),
|
||||
Nonce: Nonce(math.Pow(2, 40) - 1),
|
||||
Balance: new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil), big.NewInt(1)),
|
||||
PublicKey: pk,
|
||||
PublicKey: pk.Compress(),
|
||||
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", account.Balance.String())
|
||||
|
||||
_, err = account.Bytes()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// force value overflow
|
||||
account.Balance = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
|
||||
@@ -361,7 +364,7 @@ func TestAccountErrNumOverflowBalance(t *testing.T) {
|
||||
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), tracerr.Unwrap(err))
|
||||
|
||||
_, err = AccountFromBytes(b)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
b[39] = 1
|
||||
_, err = AccountFromBytes(b)
|
||||
|
||||
@@ -11,17 +11,17 @@ import (
|
||||
|
||||
// AccountCreationAuth authorizations sent by users to the L2DB, to be used for account creations when necessary
|
||||
type AccountCreationAuth struct {
|
||||
EthAddr ethCommon.Address `meddler:"eth_addr"`
|
||||
BJJ *babyjub.PublicKey `meddler:"bjj"`
|
||||
Signature []byte `meddler:"signature"`
|
||||
Timestamp time.Time `meddler:"timestamp,utctime"`
|
||||
EthAddr ethCommon.Address `meddler:"eth_addr"`
|
||||
BJJ babyjub.PublicKeyComp `meddler:"bjj"`
|
||||
Signature []byte `meddler:"signature"`
|
||||
Timestamp time.Time `meddler:"timestamp,utctime"`
|
||||
}
|
||||
|
||||
// HashToSign builds the hash to be signed using BJJ pub key and the constant message
|
||||
func (a *AccountCreationAuth) HashToSign() ([]byte, error) {
|
||||
// Calculate message to be signed
|
||||
const msg = "I authorize this babyjubjub key for hermez rollup account creation"
|
||||
comp, err := a.BJJ.Compress().MarshalText()
|
||||
comp, err := a.BJJ.MarshalText()
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -23,27 +23,27 @@ func TestFeePercentage(t *testing.T) {
|
||||
func TestCalcFeeAmount(t *testing.T) {
|
||||
v := big.NewInt(1000)
|
||||
feeAmount, err := CalcFeeAmount(v, FeeSelector(195)) // 800%
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "8000", feeAmount.String())
|
||||
|
||||
feeAmount, err = CalcFeeAmount(v, FeeSelector(192)) // 100%
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1000", feeAmount.String())
|
||||
|
||||
feeAmount, err = CalcFeeAmount(v, FeeSelector(172)) // 50%
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "500", feeAmount.String())
|
||||
|
||||
feeAmount, err = CalcFeeAmount(v, FeeSelector(126)) // 10.2%
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "101", feeAmount.String())
|
||||
|
||||
feeAmount, err = CalcFeeAmount(v, FeeSelector(60)) // 1.03%
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "10", feeAmount.String())
|
||||
|
||||
feeAmount, err = CalcFeeAmount(v, FeeSelector(31)) // 0.127%
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1", feeAmount.String())
|
||||
}
|
||||
|
||||
|
||||
@@ -28,16 +28,16 @@ type L1Tx struct {
|
||||
// where type:
|
||||
// - L1UserTx: 0
|
||||
// - L1CoordinatorTx: 1
|
||||
TxID TxID `meddler:"id"`
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
Position int `meddler:"position"`
|
||||
UserOrigin bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromIdx Idx `meddler:"from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.DepositAmount (deposit)
|
||||
FromEthAddr ethCommon.Address `meddler:"from_eth_addr,zeroisnull"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj,zeroisnull"`
|
||||
ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
|
||||
TokenID TokenID `meddler:"token_id"`
|
||||
Amount *big.Int `meddler:"amount,bigint"`
|
||||
TxID TxID `meddler:"id"`
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
Position int `meddler:"position"`
|
||||
UserOrigin bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromIdx Idx `meddler:"from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.DepositAmount (deposit)
|
||||
FromEthAddr ethCommon.Address `meddler:"from_eth_addr,zeroisnull"`
|
||||
FromBJJ babyjub.PublicKeyComp `meddler:"from_bjj,zeroisnull"`
|
||||
ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
|
||||
TokenID TokenID `meddler:"token_id"`
|
||||
Amount *big.Int `meddler:"amount,bigint"`
|
||||
// EffectiveAmount only applies to L1UserTx.
|
||||
EffectiveAmount *big.Int `meddler:"effective_amount,bigintnull"`
|
||||
DepositAmount *big.Int `meddler:"deposit_amount,bigint"`
|
||||
@@ -261,8 +261,8 @@ func L1TxFromDataAvailability(b []byte, nLevels uint32) (*L1Tx, error) {
|
||||
func (tx *L1Tx) BytesGeneric() ([]byte, error) {
|
||||
var b [L1UserTxBytesLen]byte
|
||||
copy(b[0:20], tx.FromEthAddr.Bytes())
|
||||
if tx.FromBJJ != nil {
|
||||
pkCompL := tx.FromBJJ.Compress()
|
||||
if tx.FromBJJ != EmptyBJJComp {
|
||||
pkCompL := tx.FromBJJ
|
||||
pkCompB := SwapEndianness(pkCompL[:])
|
||||
copy(b[20:52], pkCompB[:])
|
||||
}
|
||||
@@ -310,7 +310,7 @@ func (tx *L1Tx) BytesCoordinatorTx(compressedSignatureBytes []byte) ([]byte, err
|
||||
b[0] = v
|
||||
copy(b[1:33], s)
|
||||
copy(b[33:65], r)
|
||||
pkCompL := tx.FromBJJ.Compress()
|
||||
pkCompL := tx.FromBJJ
|
||||
pkCompB := SwapEndianness(pkCompL[:])
|
||||
copy(b[65:97], pkCompB[:])
|
||||
copy(b[97:101], tx.TokenID.Bytes())
|
||||
@@ -331,12 +331,7 @@ func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
|
||||
|
||||
pkCompB := b[20:52]
|
||||
pkCompL := SwapEndianness(pkCompB)
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
copy(pkComp[:], pkCompL)
|
||||
tx.FromBJJ, err = pkComp.Decompress()
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
copy(tx.FromBJJ[:], pkCompL)
|
||||
fromIdx, err := IdxFromBytes(b[52:58])
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
@@ -378,12 +373,7 @@ func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommo
|
||||
r := b[33:65]
|
||||
pkCompB := b[65:97]
|
||||
pkCompL := SwapEndianness(pkCompB)
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
copy(pkComp[:], pkCompL)
|
||||
tx.FromBJJ, err = pkComp.Decompress()
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
copy(tx.FromBJJ[:], pkCompL)
|
||||
tx.TokenID, err = TokenIDFromBytes(b[97:101])
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestNewL1UserTx(t *testing.T) {
|
||||
FromIdx: Idx(300),
|
||||
}
|
||||
l1Tx, err := NewL1Tx(l1Tx)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "0x00000000000001e240004700", l1Tx.TxID.String())
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func TestNewL1CoordinatorTx(t *testing.T) {
|
||||
BatchNum: &batchNum,
|
||||
}
|
||||
l1Tx, err := NewL1Tx(l1Tx)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "0x01000000000000cafe005800", l1Tx.TxID.String())
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestL1TxCompressedData(t *testing.T) {
|
||||
TokenID: 5,
|
||||
}
|
||||
txCompressedData, err := tx.TxCompressedData()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test vector value generated from javascript implementation
|
||||
expectedStr := "7307597389635308713748674793997299267459594577423"
|
||||
@@ -73,7 +73,7 @@ func TestBytesDataAvailability(t *testing.T) {
|
||||
TokenID: 5,
|
||||
}
|
||||
txCompressedData, err := tx.BytesDataAvailability(32)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "0000000200000003000000", hex.EncodeToString(txCompressedData))
|
||||
|
||||
tx = L1Tx{
|
||||
@@ -83,7 +83,7 @@ func TestBytesDataAvailability(t *testing.T) {
|
||||
TokenID: 5,
|
||||
}
|
||||
txCompressedData, err = tx.BytesDataAvailability(32)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "0000000200000003000400", hex.EncodeToString(txCompressedData))
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ func TestL1TxFromDataAvailability(t *testing.T) {
|
||||
Amount: big.NewInt(4),
|
||||
}
|
||||
txCompressedData, err := tx.BytesDataAvailability(32)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
l1tx, err := L1TxFromDataAvailability(txCompressedData, 32)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
|
||||
@@ -106,7 +106,7 @@ func TestL1TxFromDataAvailability(t *testing.T) {
|
||||
EffectiveAmount: big.NewInt(4),
|
||||
}
|
||||
txCompressedData, err = tx.BytesDataAvailability(32)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
l1tx, err = L1TxFromDataAvailability(txCompressedData, 32)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
|
||||
@@ -118,10 +118,7 @@ func TestL1userTxByteParsers(t *testing.T) {
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
pkCompL := []byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
|
||||
err := pkComp.UnmarshalText(pkCompL)
|
||||
require.Nil(t, err)
|
||||
|
||||
pk, err := pkComp.Decompress()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
l1Tx := &L1Tx{
|
||||
UserOrigin: true,
|
||||
@@ -130,17 +127,17 @@ func TestL1userTxByteParsers(t *testing.T) {
|
||||
Amount: big.NewInt(1),
|
||||
DepositAmount: big.NewInt(2),
|
||||
FromIdx: 2,
|
||||
FromBJJ: pk,
|
||||
FromBJJ: pkComp,
|
||||
FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
|
||||
encodedData, err := l1Tx.BytesUser()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
decodedData, err := L1UserTxFromBytes(encodedData)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, l1Tx, decodedData)
|
||||
encodedData2, err := decodedData.BytesUser()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, encodedData, encodedData2)
|
||||
|
||||
// expect error if length!=68
|
||||
@@ -156,13 +153,10 @@ func TestL1TxByteParsersCompatibility(t *testing.T) {
|
||||
// Data from compatibility test
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
pkCompB, err := hex.DecodeString("0dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
pkCompL := SwapEndianness(pkCompB)
|
||||
err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
|
||||
require.Nil(t, err)
|
||||
|
||||
pk, err := pkComp.Decompress()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
depositAmount := new(big.Int)
|
||||
depositAmount.SetString("100000000000000000000", 10)
|
||||
@@ -172,16 +166,16 @@ func TestL1TxByteParsersCompatibility(t *testing.T) {
|
||||
Amount: big.NewInt(2400000000000000000),
|
||||
DepositAmount: depositAmount,
|
||||
FromIdx: Idx(29767899),
|
||||
FromBJJ: pk,
|
||||
FromBJJ: pkComp,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x85dab5b9e2e361d0c208d77be90efcc0439b0a53"),
|
||||
UserOrigin: true,
|
||||
}
|
||||
|
||||
expected, err := utils.HexDecode("85dab5b9e2e361d0c208d77be90efcc0439b0a530dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a000001c638db8be880f00020039c0000053cb88d")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
encodedData, err := l1Tx.BytesUser()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, expected, encodedData)
|
||||
}
|
||||
|
||||
@@ -191,7 +185,7 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
||||
chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
|
||||
|
||||
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
publicKey := privateKey.Public()
|
||||
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
|
||||
@@ -200,19 +194,16 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
||||
}
|
||||
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
|
||||
pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
fromEthAddr := crypto.PubkeyToAddress(*pubKey)
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
|
||||
err = pkComp.UnmarshalText(pkCompL)
|
||||
require.Nil(t, err)
|
||||
pk, err := pkComp.Decompress()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
|
||||
bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
|
||||
|
||||
babyjub := pk.Compress()
|
||||
babyjubB := SwapEndianness(babyjub[:])
|
||||
babyjubB := SwapEndianness(pkComp[:])
|
||||
var data []byte
|
||||
data = append(data, bytesMessage1...)
|
||||
data = append(data, bytesMessage2...)
|
||||
@@ -221,26 +212,26 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
||||
data = append(data, hermezAddress.Bytes()...)
|
||||
hash := crypto.Keccak256Hash(data)
|
||||
signature, err := crypto.Sign(hash.Bytes(), privateKey)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
// Ethereum adds 27 to v
|
||||
v := int(signature[64])
|
||||
signature[64] = byte(v + 27)
|
||||
|
||||
l1Tx := &L1Tx{
|
||||
TokenID: 231,
|
||||
FromBJJ: pk,
|
||||
FromBJJ: pkComp,
|
||||
FromEthAddr: fromEthAddr,
|
||||
Amount: big.NewInt(0),
|
||||
DepositAmount: big.NewInt(0),
|
||||
}
|
||||
|
||||
bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, l1Tx, l1txDecoded)
|
||||
bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
|
||||
|
||||
// expect error if length!=68
|
||||
@@ -256,11 +247,11 @@ func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
|
||||
// Data from compatibility test
|
||||
var signature []byte
|
||||
r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
v, err := hex.DecodeString("1b")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
signature = append(signature, r[:]...)
|
||||
signature = append(signature, s[:]...)
|
||||
@@ -268,24 +259,22 @@ func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
|
||||
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
pkCompL := SwapEndianness(pkCompB)
|
||||
err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
// Data from the compatibility test
|
||||
require.Nil(t, err)
|
||||
pk, err := pkComp.Decompress()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
l1Tx := &L1Tx{
|
||||
TokenID: 231,
|
||||
FromBJJ: pk,
|
||||
FromBJJ: pkComp,
|
||||
}
|
||||
|
||||
encodeData, err := l1Tx.BytesCoordinatorTx(signature)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expected, encodeData)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestNewL2Tx(t *testing.T) {
|
||||
Nonce: 144,
|
||||
}
|
||||
l2Tx, err := NewL2Tx(l2Tx)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "0x020000000156660000000090", l2Tx.TxID.String())
|
||||
}
|
||||
|
||||
@@ -33,10 +33,10 @@ func TestL2TxByteParsers(t *testing.T) {
|
||||
// Data from the compatibility test
|
||||
expected := "00000101000001002b16c9"
|
||||
encodedData, err := l2Tx.BytesDataAvailability(32)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, expected, hex.EncodeToString(encodedData))
|
||||
|
||||
decodedData, err := L2TxFromBytesDataAvailability(encodedData, 32)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, l2Tx, decodedData)
|
||||
}
|
||||
|
||||
@@ -12,19 +12,28 @@ import (
|
||||
"github.com/iden3/go-iden3-crypto/poseidon"
|
||||
)
|
||||
|
||||
// PoolL2Tx is a struct that represents a L2Tx sent by an account to the coordinator hat is waiting to be forged
|
||||
// EmptyBJJComp contains the 32 byte array of a empty BabyJubJub PublicKey
|
||||
// Compressed. It is a valid point in the BabyJubJub curve, so does not give
|
||||
// errors when being decompressed.
|
||||
var EmptyBJJComp = babyjub.PublicKeyComp([32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
|
||||
|
||||
// PoolL2Tx is a struct that represents a L2Tx sent by an account to the
|
||||
// coordinator that is waiting to be forged
|
||||
type PoolL2Tx struct {
|
||||
// Stored in DB: mandatory fileds
|
||||
|
||||
// TxID (12 bytes) for L2Tx is:
|
||||
// bytes: | 1 | 6 | 5 |
|
||||
// values: | type | FromIdx | Nonce |
|
||||
TxID TxID `meddler:"tx_id"`
|
||||
FromIdx Idx `meddler:"from_idx"`
|
||||
ToIdx Idx `meddler:"to_idx,zeroisnull"`
|
||||
AuxToIdx Idx `meddler:"-"` // AuxToIdx is only used internally at the StateDB to avoid repeated computation when processing transactions (from Synchronizer, TxSelector, BatchBuilder)
|
||||
TxID TxID `meddler:"tx_id"`
|
||||
FromIdx Idx `meddler:"from_idx"`
|
||||
ToIdx Idx `meddler:"to_idx,zeroisnull"`
|
||||
// AuxToIdx is only used internally at the StateDB to avoid repeated
|
||||
// computation when processing transactions (from Synchronizer,
|
||||
// TxSelector, BatchBuilder)
|
||||
AuxToIdx Idx `meddler:"-"`
|
||||
ToEthAddr ethCommon.Address `meddler:"to_eth_addr,zeroisnull"`
|
||||
ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
|
||||
ToBJJ babyjub.PublicKeyComp `meddler:"to_bjj,zeroisnull"`
|
||||
TokenID TokenID `meddler:"token_id"`
|
||||
Amount *big.Int `meddler:"amount,bigint"` // TODO: change to float16
|
||||
Fee FeeSelector `meddler:"fee"`
|
||||
@@ -33,17 +42,17 @@ type PoolL2Tx struct {
|
||||
Signature babyjub.SignatureComp `meddler:"signature"` // tx signature
|
||||
Timestamp time.Time `meddler:"timestamp,utctime"` // time when added to the tx pool
|
||||
// Stored in DB: optional fileds, may be uninitialized
|
||||
RqFromIdx Idx `meddler:"rq_from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.DepositAmount (deposit)
|
||||
RqToIdx Idx `meddler:"rq_to_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.DepositAmount (deposit)
|
||||
RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr,zeroisnull"`
|
||||
RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"` // TODO: stop using json, use scanner/valuer
|
||||
RqTokenID TokenID `meddler:"rq_token_id,zeroisnull"`
|
||||
RqAmount *big.Int `meddler:"rq_amount,bigintnull"` // TODO: change to float16
|
||||
RqFee FeeSelector `meddler:"rq_fee,zeroisnull"`
|
||||
RqNonce Nonce `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
|
||||
AbsoluteFee float64 `meddler:"fee_usd,zeroisnull"`
|
||||
AbsoluteFeeUpdate time.Time `meddler:"usd_update,utctimez"`
|
||||
Type TxType `meddler:"tx_type"`
|
||||
RqFromIdx Idx `meddler:"rq_from_idx,zeroisnull"`
|
||||
RqToIdx Idx `meddler:"rq_to_idx,zeroisnull"`
|
||||
RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr,zeroisnull"`
|
||||
RqToBJJ babyjub.PublicKeyComp `meddler:"rq_to_bjj,zeroisnull"`
|
||||
RqTokenID TokenID `meddler:"rq_token_id,zeroisnull"`
|
||||
RqAmount *big.Int `meddler:"rq_amount,bigintnull"` // TODO: change to float16
|
||||
RqFee FeeSelector `meddler:"rq_fee,zeroisnull"`
|
||||
RqNonce Nonce `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
|
||||
AbsoluteFee float64 `meddler:"fee_usd,zeroisnull"`
|
||||
AbsoluteFeeUpdate time.Time `meddler:"usd_update,utctimez"`
|
||||
Type TxType `meddler:"tx_type"`
|
||||
// Extra metadata, may be uninitialized
|
||||
RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
|
||||
}
|
||||
@@ -81,7 +90,7 @@ func (tx *PoolL2Tx) SetType() error {
|
||||
} else if tx.ToIdx == 1 {
|
||||
tx.Type = TxTypeExit
|
||||
} else if tx.ToIdx == 0 {
|
||||
if tx.ToBJJ != nil && tx.ToEthAddr == FFAddr {
|
||||
if tx.ToBJJ != EmptyBJJComp && tx.ToEthAddr == FFAddr {
|
||||
tx.Type = TxTypeTransferToBJJ
|
||||
} else if tx.ToEthAddr != FFAddr && tx.ToEthAddr != EmptyAddr {
|
||||
tx.Type = TxTypeTransferToEthAddr
|
||||
@@ -125,10 +134,13 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
var b [31]byte
|
||||
|
||||
toBJJSign := byte(0)
|
||||
if tx.ToBJJ != nil && babyjub.PointCoordSign(tx.ToBJJ.X) {
|
||||
pkSign, _ := babyjub.UnpackSignY(tx.ToBJJ)
|
||||
if pkSign {
|
||||
toBJJSign = byte(1)
|
||||
}
|
||||
|
||||
b[0] = toBJJSign
|
||||
b[1] = byte(tx.Fee)
|
||||
nonceBytes, err := tx.Nonce.Bytes()
|
||||
@@ -174,8 +186,11 @@ func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
|
||||
}
|
||||
var b [25]byte
|
||||
toBJJSign := byte(0)
|
||||
if tx.ToBJJ != nil && babyjub.PointCoordSign(tx.ToBJJ.X) {
|
||||
toBJJSign = byte(1)
|
||||
if tx.ToBJJ != EmptyBJJComp {
|
||||
sign, _ := babyjub.UnpackSignY(tx.ToBJJ)
|
||||
if sign {
|
||||
toBJJSign = byte(1)
|
||||
}
|
||||
}
|
||||
b[0] = toBJJSign
|
||||
b[1] = byte(tx.Fee)
|
||||
@@ -224,11 +239,14 @@ func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
var b [25]byte
|
||||
toBJJSign := byte(0)
|
||||
if tx.RqToBJJ != nil && babyjub.PointCoordSign(tx.RqToBJJ.X) {
|
||||
toBJJSign = byte(1)
|
||||
rqToBJJSign := byte(0)
|
||||
if tx.RqToBJJ != EmptyBJJComp {
|
||||
sign, _ := babyjub.UnpackSignY(tx.RqToBJJ)
|
||||
if sign {
|
||||
rqToBJJSign = byte(1)
|
||||
}
|
||||
}
|
||||
b[0] = toBJJSign
|
||||
b[0] = rqToBJJSign
|
||||
b[1] = byte(tx.RqFee)
|
||||
nonceBytes, err := tx.RqNonce.Bytes()
|
||||
if err != nil {
|
||||
@@ -260,24 +278,21 @@ func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
|
||||
}
|
||||
toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
|
||||
rqToEthAddr := EthAddrToBigInt(tx.RqToEthAddr)
|
||||
toBJJY := big.NewInt(0)
|
||||
if tx.ToBJJ != nil {
|
||||
toBJJY = tx.ToBJJ.Y
|
||||
}
|
||||
|
||||
_, toBJJY := babyjub.UnpackSignY(tx.ToBJJ)
|
||||
|
||||
rqTxCompressedDataV2, err := tx.RqTxCompressedDataV2()
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
rqToBJJY := big.NewInt(0)
|
||||
if tx.RqToBJJ != nil {
|
||||
rqToBJJY = tx.RqToBJJ.Y
|
||||
}
|
||||
|
||||
_, rqToBJJY := babyjub.UnpackSignY(tx.RqToBJJ)
|
||||
|
||||
return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJY, rqTxCompressedDataV2, rqToEthAddr, rqToBJJY})
|
||||
}
|
||||
|
||||
// VerifySignature returns true if the signature verification is correct for the given PublicKey
|
||||
func (tx *PoolL2Tx) VerifySignature(pk *babyjub.PublicKey) bool {
|
||||
// VerifySignature returns true if the signature verification is correct for the given PublicKeyComp
|
||||
func (tx *PoolL2Tx) VerifySignature(pkComp babyjub.PublicKeyComp) bool {
|
||||
h, err := tx.HashToSign()
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -286,6 +301,10 @@ func (tx *PoolL2Tx) VerifySignature(pk *babyjub.PublicKey) bool {
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
pk, err := pkComp.Decompress()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return pk.VerifyPoseidon(h, s)
|
||||
}
|
||||
|
||||
@@ -325,13 +344,15 @@ func PoolL2TxsToL2Txs(txs []PoolL2Tx) ([]L2Tx, error) {
|
||||
return l2Txs, nil
|
||||
}
|
||||
|
||||
// PoolL2TxState is a struct that represents the status of a L2 transaction
|
||||
// PoolL2TxState is a string that represents the status of a L2 transaction
|
||||
type PoolL2TxState string
|
||||
|
||||
const (
|
||||
// PoolL2TxStatePending represents a valid L2Tx that hasn't started the forging process
|
||||
// PoolL2TxStatePending represents a valid L2Tx that hasn't started the
|
||||
// forging process
|
||||
PoolL2TxStatePending PoolL2TxState = "pend"
|
||||
// PoolL2TxStateForging represents a valid L2Tx that has started the forging process
|
||||
// PoolL2TxStateForging represents a valid L2Tx that has started the
|
||||
// forging process
|
||||
PoolL2TxStateForging PoolL2TxState = "fing"
|
||||
// PoolL2TxStateForged represents a L2Tx that has already been forged
|
||||
PoolL2TxStateForged PoolL2TxState = "fged"
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewPoolL2Tx(t *testing.T) {
|
||||
@@ -19,24 +20,24 @@ func TestNewPoolL2Tx(t *testing.T) {
|
||||
Nonce: 144,
|
||||
}
|
||||
poolL2Tx, err := NewPoolL2Tx(poolL2Tx)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "0x020000000156660000000090", poolL2Tx.TxID.String())
|
||||
}
|
||||
|
||||
func TestTxCompressedData(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
tx := PoolL2Tx{
|
||||
FromIdx: 2,
|
||||
ToIdx: 3,
|
||||
Amount: big.NewInt(4),
|
||||
TokenID: 5,
|
||||
Nonce: 6,
|
||||
ToBJJ: sk.Public(),
|
||||
ToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
txCompressedData, err := tx.TxCompressedData()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
// test vector value generated from javascript implementation
|
||||
expectedStr := "1766847064778421992193717128424891165872736891548909569553540445094274575"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
@@ -48,10 +49,10 @@ func TestTxCompressedData(t *testing.T) {
|
||||
RqTokenID: 10,
|
||||
RqNonce: 11,
|
||||
RqFee: 12,
|
||||
RqToBJJ: sk.Public(),
|
||||
RqToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
txCompressedData, err = tx.RqTxCompressedDataV2()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
// test vector value generated from javascript implementation
|
||||
expectedStr = "6571340879233176732837827812956721483162819083004853354503"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
@@ -61,7 +62,7 @@ func TestTxCompressedData(t *testing.T) {
|
||||
func TestTxCompressedDataV2(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
tx := PoolL2Tx{
|
||||
FromIdx: 7,
|
||||
ToIdx: 8,
|
||||
@@ -69,10 +70,10 @@ func TestTxCompressedDataV2(t *testing.T) {
|
||||
TokenID: 10,
|
||||
Nonce: 11,
|
||||
Fee: 12,
|
||||
ToBJJ: sk.Public(),
|
||||
ToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
txCompressedData, err := tx.TxCompressedDataV2()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
// test vector value generated from javascript implementation
|
||||
expectedStr := "6571340879233176732837827812956721483162819083004853354503"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
@@ -86,7 +87,7 @@ func TestTxCompressedDataV2(t *testing.T) {
|
||||
func TestRqTxCompressedDataV2(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
tx := PoolL2Tx{
|
||||
RqFromIdx: 7,
|
||||
RqToIdx: 8,
|
||||
@@ -94,10 +95,10 @@ func TestRqTxCompressedDataV2(t *testing.T) {
|
||||
RqTokenID: 10,
|
||||
RqNonce: 11,
|
||||
RqFee: 12,
|
||||
RqToBJJ: sk.Public(),
|
||||
RqToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
txCompressedData, err := tx.RqTxCompressedDataV2()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
// test vector value generated from javascript implementation
|
||||
expectedStr := "6571340879233176732837827812956721483162819083004853354503"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
@@ -110,7 +111,7 @@ func TestRqTxCompressedDataV2(t *testing.T) {
|
||||
func TestHashToSign(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
tx := PoolL2Tx{
|
||||
FromIdx: 2,
|
||||
ToIdx: 3,
|
||||
@@ -120,29 +121,37 @@ func TestHashToSign(t *testing.T) {
|
||||
ToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
toSign, err := tx.HashToSign()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1469900657138253851938022936440971384682713995864967090251961124784132925291", toSign.String())
|
||||
}
|
||||
|
||||
func TestVerifyTxSignature(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
tx := PoolL2Tx{
|
||||
FromIdx: 2,
|
||||
ToIdx: 3,
|
||||
Amount: big.NewInt(4),
|
||||
TokenID: 5,
|
||||
Nonce: 6,
|
||||
ToBJJ: sk.Public(),
|
||||
ToBJJ: sk.Public().Compress(),
|
||||
RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
RqToBJJ: sk.Public(),
|
||||
RqToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
toSign, err := tx.HashToSign()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "18645218094210271622244722988708640202588315450486586312909439859037906375295", toSign.String())
|
||||
|
||||
sig := sk.SignPoseidon(toSign)
|
||||
tx.Signature = sig.Compress()
|
||||
assert.True(t, tx.VerifySignature(sk.Public()))
|
||||
assert.True(t, tx.VerifySignature(sk.Public().Compress()))
|
||||
}
|
||||
|
||||
func TestDecompressEmptyBJJComp(t *testing.T) {
|
||||
pkComp := EmptyBJJComp
|
||||
pk, err := pkComp.Decompress()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "2957874849018779266517920829765869116077630550401372566248359756137677864698", pk.X.String())
|
||||
assert.Equal(t, "0", pk.Y.String())
|
||||
}
|
||||
|
||||
14
common/tx.go
14
common/tx.go
@@ -140,13 +140,13 @@ type Tx struct {
|
||||
BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
|
||||
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
||||
// L1
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
|
||||
DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
|
||||
DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
|
||||
DepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
|
||||
FromBJJ babyjub.PublicKeyComp `meddler:"from_bjj"`
|
||||
DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
|
||||
DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
|
||||
DepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
|
||||
// L2
|
||||
Fee *FeeSelector `meddler:"fee"`
|
||||
FeeUSD *float64 `meddler:"fee_usd"`
|
||||
|
||||
@@ -40,16 +40,16 @@ func TestTxIDMarshalers(t *testing.T) {
|
||||
h := []byte("0x00000000000001e240004700")
|
||||
var txid TxID
|
||||
err := txid.UnmarshalText(h)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, h, []byte(txid.String()))
|
||||
|
||||
h2, err := txid.MarshalText()
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, h, h2)
|
||||
|
||||
var txid2 TxID
|
||||
err = txid2.UnmarshalText(h2)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, h2, []byte(txid2.String()))
|
||||
assert.Equal(t, h, h2)
|
||||
}
|
||||
|
||||
@@ -27,15 +27,15 @@ func EthAddrToBigInt(a ethCommon.Address) *big.Int {
|
||||
// the Hermez checksum at the last byte, and is encoded in BigEndian) and
|
||||
// returns the corresponding *babyjub.PublicKey. This method is not part of the
|
||||
// spec, is used for importing javascript test vectors data.
|
||||
func BJJFromStringWithChecksum(s string) (*babyjub.PublicKey, error) {
|
||||
func BJJFromStringWithChecksum(s string) (babyjub.PublicKeyComp, error) {
|
||||
b, err := hex.DecodeString(s)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
return EmptyBJJComp, tracerr.Wrap(err)
|
||||
}
|
||||
pkBytes := SwapEndianness(b)
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
copy(pkComp[:], pkBytes[:])
|
||||
return pkComp.Decompress()
|
||||
return pkComp, nil
|
||||
}
|
||||
|
||||
// CopyBigInt returns a copy of the big int
|
||||
|
||||
@@ -9,11 +9,14 @@ import (
|
||||
|
||||
func TestBJJFromStringWithChecksum(t *testing.T) {
|
||||
s := "21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7"
|
||||
pk, err := BJJFromStringWithChecksum(s)
|
||||
assert.Nil(t, err)
|
||||
pkComp, err := BJJFromStringWithChecksum(s)
|
||||
assert.NoError(t, err)
|
||||
sBytes, err := hex.DecodeString(s)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, hex.EncodeToString(SwapEndianness(sBytes)), pk.Compress().String())
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, hex.EncodeToString(SwapEndianness(sBytes)), pkComp.String())
|
||||
|
||||
pk, err := pkComp.Decompress()
|
||||
assert.NoError(t, err)
|
||||
|
||||
// expected values computed with js implementation
|
||||
assert.Equal(t, "2492816973395423007340226948038371729989170225696553239457870892535792679622", pk.X.String())
|
||||
|
||||
@@ -11,6 +11,6 @@ import (
|
||||
func TestZKInputs(t *testing.T) {
|
||||
zki := NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
|
||||
_, err := json.Marshal(zki)
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
// fmt.Println(string(s))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user