Browse Source

Update Leaf to new spec (Sign+Ay)

feature/sql-semaphore1
arnaucube 4 years ago
parent
commit
16555b51e3
4 changed files with 74 additions and 75 deletions
  1. +7
    -2
      batchbuilder/batchbuilder.go
  2. +2
    -2
      batchbuilder/state.go
  3. +23
    -22
      common/leaf.go
  4. +42
    -49
      common/leaf_test.go

+ 7
- 2
batchbuilder/batchbuilder.go

@ -3,17 +3,20 @@ package batchbuilder
import ( import (
ethCommon "github.com/ethereum/go-ethereum/common" ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/common"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-merkletree" "github.com/iden3/go-merkletree"
"github.com/iden3/go-merkletree/db" "github.com/iden3/go-merkletree/db"
"github.com/iden3/go-merkletree/db/memory" "github.com/iden3/go-merkletree/db/memory"
) )
// ConfigCircuit contains the circuit configuration
type ConfigCircuit struct { type ConfigCircuit struct {
TxsMax uint64 TxsMax uint64
L1TxsMax uint64 L1TxsMax uint64
SMTLevelsMax uint64 SMTLevelsMax uint64
} }
// BatchBuilder implements the batch builder type, which contains the functionallities
type BatchBuilder struct { type BatchBuilder struct {
StateDB db.Storage // where the MTs will be stored by the Synchronizer StateDB db.Storage // where the MTs will be stored by the Synchronizer
idx uint64 idx uint64
@ -21,6 +24,7 @@ type BatchBuilder struct {
configCircuits []ConfigCircuit configCircuits []ConfigCircuit
} }
// ConfigBatch contains the batch configuration
type ConfigBatch struct { type ConfigBatch struct {
CoordinatorAddress ethCommon.Address CoordinatorAddress ethCommon.Address
} }
@ -48,10 +52,11 @@ func NewBatchBuilder(stateDB db.Storage, configCircuits []ConfigCircuit, batchNu
// copy of the rollup state from the Synchronizer at that `batchNum`, otherwise // copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
// it can just roll back the internal copy. // it can just roll back the internal copy.
func (bb *BatchBuilder) Reset(batchNum int, idx uint64, fromSynchronizer bool) error { func (bb *BatchBuilder) Reset(batchNum int, idx uint64, fromSynchronizer bool) error {
// TODO
return nil return nil
} }
// BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
func (bb *BatchBuilder) BuildBatch(configBatch ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.L2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) { func (bb *BatchBuilder) BuildBatch(configBatch ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.L2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
for _, tx := range l1usertxs { for _, tx := range l1usertxs {
err := bb.processL1Tx(tx) err := bb.processL1Tx(tx)
@ -137,7 +142,7 @@ func (bb *BatchBuilder) applyCreateLeaf(tx common.L1Tx) error {
TokenID: tx.TokenID, TokenID: tx.TokenID,
Nonce: 0, // TODO check w spec: always that a new leaf is created nonce is at 0 Nonce: 0, // TODO check w spec: always that a new leaf is created nonce is at 0
Balance: tx.LoadAmount, Balance: tx.LoadAmount,
Ax: tx.FromBJJ.X,
Sign: babyjub.PointCoordSign(tx.FromBJJ.X),
Ay: tx.FromBJJ.Y, Ay: tx.FromBJJ.Y,
EthAddr: tx.FromEthAddr, EthAddr: tx.FromEthAddr,
} }

+ 2
- 2
batchbuilder/state.go

@ -7,7 +7,7 @@ import (
"github.com/iden3/go-merkletree/db" "github.com/iden3/go-merkletree/db"
) )
// TODO move this file into StateDB, which Synchronizer will use in the disk DB, and BatchBuilder will use with the MemoryDB
// TODO next iteration move the methods of this file into StateDB, which Synchronizer will use in the disk DB, and BatchBuilder will use with the MemoryDB
// GetBalance returns the balance for a given Idx from the DB // GetBalance returns the balance for a given Idx from the DB
func (bb *BatchBuilder) GetBalance(tx db.Tx, idx common.Idx) (*common.Leaf, error) { func (bb *BatchBuilder) GetBalance(tx db.Tx, idx common.Idx) (*common.Leaf, error) {
@ -16,7 +16,7 @@ func (bb *BatchBuilder) GetBalance(tx db.Tx, idx common.Idx) (*common.Leaf, erro
if err != nil { if err != nil {
return nil, err return nil, err
} }
var b [32 * 5]byte
var b [32 * common.NLEAFELEMS]byte
copy(b[:], vBytes) copy(b[:], vBytes)
leaf, err := common.LeafFromBytes(b) leaf, err := common.LeafFromBytes(b)
if err != nil { if err != nil {

+ 23
- 22
common/leaf.go

@ -12,19 +12,21 @@ import (
cryptoUtils "github.com/iden3/go-iden3-crypto/utils" cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
) )
const NLEAFELEMS = 4
// Leaf is the data structure stored in the Leaf of the MerkleTree // Leaf is the data structure stored in the Leaf of the MerkleTree
type Leaf struct { type Leaf struct {
TokenID TokenID TokenID TokenID
Nonce uint64 // max of 40 bits used Nonce uint64 // max of 40 bits used
Balance *big.Int // max of 192 bits used Balance *big.Int // max of 192 bits used
Ax *big.Int
Sign bool
Ay *big.Int Ay *big.Int
EthAddr eth.Address EthAddr eth.Address
} }
// Bytes returns the bytes representing the Leaf, in a way that each BigInt is represented by 32 bytes, in spite of the BigInt could be represented in less bytes (due a small big.Int), so in this way each BigInt is always 32 bytes and can be automatically parsed from a byte array. // Bytes returns the bytes representing the Leaf, in a way that each BigInt is represented by 32 bytes, in spite of the BigInt could be represented in less bytes (due a small big.Int), so in this way each BigInt is always 32 bytes and can be automatically parsed from a byte array.
func (l *Leaf) Bytes() ([32 * 5]byte, error) {
var b [32 * 5]byte
func (l *Leaf) Bytes() ([32 * NLEAFELEMS]byte, error) {
var b [32 * NLEAFELEMS]byte
if l.Nonce >= uint64(math.Pow(2, 40)) { if l.Nonce >= uint64(math.Pow(2, 40)) {
return b, fmt.Errorf("%s Nonce", ErrNumOverflow) return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
@ -40,17 +42,19 @@ func (l *Leaf) Bytes() ([32 * 5]byte, error) {
copy(b[0:4], tokenIDBytes[:]) copy(b[0:4], tokenIDBytes[:])
copy(b[4:9], nonceBytes[:]) copy(b[4:9], nonceBytes[:])
if l.Sign {
b[10] = 1
}
copy(b[32:64], SwapEndianness(l.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian copy(b[32:64], SwapEndianness(l.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
copy(b[64:96], SwapEndianness(l.Ax.Bytes()))
copy(b[96:128], SwapEndianness(l.Ay.Bytes()))
copy(b[128:148], l.EthAddr.Bytes())
copy(b[64:96], SwapEndianness(l.Ay.Bytes()))
copy(b[96:116], l.EthAddr.Bytes())
return b, nil return b, nil
} }
// BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field // BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
func (l *Leaf) BigInts() ([5]*big.Int, error) {
e := [5]*big.Int{}
func (l *Leaf) BigInts() ([NLEAFELEMS]*big.Int, error) {
e := [NLEAFELEMS]*big.Int{}
b, err := l.Bytes() b, err := l.Bytes()
if err != nil { if err != nil {
@ -61,7 +65,6 @@ func (l *Leaf) BigInts() ([5]*big.Int, error) {
e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64])) e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96])) e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128])) e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
e[4] = new(big.Int).SetBytes(SwapEndianness(b[128:160]))
return e, nil return e, nil
} }
@ -80,41 +83,39 @@ func (l *Leaf) HashValue() (*big.Int, error) {
} }
// LeafFromBigInts returns a Leaf from a [5]*big.Int // LeafFromBigInts returns a Leaf from a [5]*big.Int
func LeafFromBigInts(e [5]*big.Int) (*Leaf, error) {
func LeafFromBigInts(e [NLEAFELEMS]*big.Int) (*Leaf, error) {
if !cryptoUtils.CheckBigIntArrayInField(e[:]) { if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
return nil, ErrNotInFF return nil, ErrNotInFF
} }
var b [32 * 5]byte
var b [32 * NLEAFELEMS]byte
copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
copy(b[32:64], SwapEndianness(e[1].Bytes())) copy(b[32:64], SwapEndianness(e[1].Bytes()))
copy(b[64:96], SwapEndianness(e[2].Bytes())) copy(b[64:96], SwapEndianness(e[2].Bytes()))
copy(b[96:128], SwapEndianness(e[3].Bytes())) copy(b[96:128], SwapEndianness(e[3].Bytes()))
copy(b[128:160], SwapEndianness(e[4].Bytes()))
return LeafFromBytes(b) return LeafFromBytes(b)
} }
// LeafFromBytes returns a Leaf from a byte array // LeafFromBytes returns a Leaf from a byte array
func LeafFromBytes(b [32 * 5]byte) (*Leaf, error) {
func LeafFromBytes(b [32 * NLEAFELEMS]byte) (*Leaf, error) {
tokenID := binary.LittleEndian.Uint32(b[0:4]) tokenID := binary.LittleEndian.Uint32(b[0:4])
nonce := binary.LittleEndian.Uint64(b[4:12])
if !bytes.Equal(b[9:12], []byte{0, 0, 0}) { // alternatively: if nonce >= uint64(math.Pow(2, 40)) {
var nonceBytes [8]byte
copy(nonceBytes[:], b[4:9])
nonce := binary.LittleEndian.Uint64(nonceBytes[:])
if nonce >= uint64(math.Pow(2, 40)) {
return nil, fmt.Errorf("%s Nonce", ErrNumOverflow) return nil, fmt.Errorf("%s Nonce", ErrNumOverflow)
} }
sign := b[10] == 1
balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes) balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) { if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
return nil, fmt.Errorf("%s Balance", ErrNumOverflow) return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
} }
ax := new(big.Int).SetBytes(SwapEndianness(b[64:96])) // SwapEndianness, as big.Int uses BigEndian
ay := new(big.Int).SetBytes(SwapEndianness(b[96:128]))
ethAddr := eth.BytesToAddress(b[128:148])
ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
ethAddr := eth.BytesToAddress(b[96:116])
if !cryptoUtils.CheckBigIntInField(balance) { if !cryptoUtils.CheckBigIntInField(balance) {
return nil, ErrNotInFF return nil, ErrNotInFF
} }
if !cryptoUtils.CheckBigIntInField(ax) {
return nil, ErrNotInFF
}
if !cryptoUtils.CheckBigIntInField(ay) { if !cryptoUtils.CheckBigIntInField(ay) {
return nil, ErrNotInFF return nil, ErrNotInFF
} }
@ -123,7 +124,7 @@ func LeafFromBytes(b [32 * 5]byte) (*Leaf, error) {
TokenID: TokenID(tokenID), TokenID: TokenID(tokenID),
Nonce: nonce, Nonce: nonce,
Balance: balance, Balance: balance,
Ax: ax,
Sign: sign,
Ay: ay, Ay: ay,
EthAddr: ethAddr, EthAddr: ethAddr,
} }

+ 42
- 49
common/leaf_test.go

@ -7,7 +7,6 @@ import (
"testing" "testing"
ethCommon "github.com/ethereum/go-ethereum/common" ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
cryptoConstants "github.com/iden3/go-iden3-crypto/constants" cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
cryptoUtils "github.com/iden3/go-iden3-crypto/utils" cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -18,12 +17,13 @@ func TestLeaf(t *testing.T) {
TokenID: TokenID(1), TokenID: TokenID(1),
Nonce: uint64(1234), Nonce: uint64(1234),
Balance: big.NewInt(1000), Balance: big.NewInt(1000),
Ax: big.NewInt(9876),
Sign: true,
Ay: big.NewInt(6789), Ay: big.NewInt(6789),
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"), EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
} }
b, err := leaf.Bytes() b, err := leaf.Bytes()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, byte(1), b[10])
l1, err := LeafFromBytes(b) l1, err := LeafFromBytes(b)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, leaf, l1) assert.Equal(t, leaf, l1)
@ -34,11 +34,10 @@ func TestLeaf(t *testing.T) {
assert.True(t, cryptoUtils.CheckBigIntInField(e[1])) assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
assert.True(t, cryptoUtils.CheckBigIntInField(e[2])) assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
assert.True(t, cryptoUtils.CheckBigIntInField(e[3])) assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
assert.True(t, cryptoUtils.CheckBigIntInField(e[4]))
assert.Equal(t, "1000", e[1].String()) assert.Equal(t, "1000", e[1].String())
assert.Equal(t, "9876", e[2].String())
assert.Equal(t, "6789", e[3].String())
assert.Equal(t, "6789", e[2].String())
assert.Equal(t, new(big.Int).SetBytes(SwapEndianness(leaf.EthAddr.Bytes())).String(), e[3].String())
l2, err := LeafFromBigInts(e) l2, err := LeafFromBigInts(e)
assert.Nil(t, err) assert.Nil(t, err)
@ -46,60 +45,59 @@ func TestLeaf(t *testing.T) {
assert.Equal(t, l1, l2) assert.Equal(t, l1, l2)
} }
func TestLeafLoop(t *testing.T) {
// check that for different Address there is no problem
for i := 0; i < 256; i++ {
key, err := ethCrypto.GenerateKey()
assert.Nil(t, err)
address := ethCrypto.PubkeyToAddress(key.PublicKey)
leaf := &Leaf{
TokenID: TokenID(i),
Nonce: uint64(i),
Balance: big.NewInt(1000),
Ax: big.NewInt(9876),
Ay: big.NewInt(6789),
EthAddr: address,
}
b, err := leaf.Bytes()
assert.Nil(t, err)
l1, err := LeafFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, leaf, l1)
e, err := leaf.BigInts()
assert.Nil(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]))
assert.True(t, cryptoUtils.CheckBigIntInField(e[4]))
l2, err := LeafFromBigInts(e)
assert.Nil(t, err)
assert.Equal(t, leaf, l2)
}
}
// func TestLeafLoop(t *testing.T) {
// // check that for different Address there is no problem
// for i := 0; i < 256; i++ {
// key, err := ethCrypto.GenerateKey()
// assert.Nil(t, err)
// address := ethCrypto.PubkeyToAddress(key.PublicKey)
//
// leaf := &Leaf{
// TokenID: TokenID(i),
// Nonce: uint64(i),
// Balance: big.NewInt(1000),
// Sign: true,
// Ay: big.NewInt(6789),
// EthAddr: address,
// }
// b, err := leaf.Bytes()
// assert.Nil(t, err)
// l1, err := LeafFromBytes(b)
// assert.Nil(t, err)
// assert.Equal(t, leaf, l1)
//
// e, err := leaf.BigInts()
// assert.Nil(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]))
//
// l2, err := LeafFromBigInts(e)
// assert.Nil(t, err)
// assert.Equal(t, leaf, l2)
// }
// }
func TestLeafErrNotInFF(t *testing.T) { func TestLeafErrNotInFF(t *testing.T) {
z := big.NewInt(0) z := big.NewInt(0)
// Q-1 should not give error // Q-1 should not give error
r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1)) r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
e := [5]*big.Int{z, z, r, r, r}
e := [NLEAFELEMS]*big.Int{z, z, r, r}
_, err := LeafFromBigInts(e) _, err := LeafFromBigInts(e)
assert.Nil(t, err) assert.Nil(t, err)
// Q should give error // Q should give error
r = cryptoConstants.Q r = cryptoConstants.Q
e = [5]*big.Int{z, z, r, r, r}
e = [NLEAFELEMS]*big.Int{z, z, r, r}
_, err = LeafFromBigInts(e) _, err = LeafFromBigInts(e)
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, ErrNotInFF, err) assert.Equal(t, ErrNotInFF, err)
// Q+1 should give error // Q+1 should give error
r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1)) r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1))
e = [5]*big.Int{z, z, r, r, r}
e = [NLEAFELEMS]*big.Int{z, z, r, r}
_, err = LeafFromBigInts(e) _, err = LeafFromBigInts(e)
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, ErrNotInFF, err) assert.Equal(t, ErrNotInFF, err)
@ -111,7 +109,7 @@ func TestLeafErrNumOverflowNonce(t *testing.T) {
TokenID: TokenID(1), TokenID: TokenID(1),
Nonce: uint64(math.Pow(2, 40) - 1), Nonce: uint64(math.Pow(2, 40) - 1),
Balance: big.NewInt(1000), Balance: big.NewInt(1000),
Ax: big.NewInt(9876),
Sign: true,
Ay: big.NewInt(6789), Ay: big.NewInt(6789),
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"), EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
} }
@ -126,11 +124,6 @@ func TestLeafErrNumOverflowNonce(t *testing.T) {
_, err = LeafFromBytes(b) _, err = LeafFromBytes(b)
assert.Nil(t, err) assert.Nil(t, err)
b[9] = 1
_, err = LeafFromBytes(b)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
} }
func TestLeafErrNumOverflowBalance(t *testing.T) { func TestLeafErrNumOverflowBalance(t *testing.T) {
@ -139,7 +132,7 @@ func TestLeafErrNumOverflowBalance(t *testing.T) {
TokenID: TokenID(1), TokenID: TokenID(1),
Nonce: uint64(math.Pow(2, 40) - 1), Nonce: uint64(math.Pow(2, 40) - 1),
Balance: new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil), big.NewInt(1)), Balance: new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil), big.NewInt(1)),
Ax: big.NewInt(9876),
Sign: true,
Ay: big.NewInt(6789), Ay: big.NewInt(6789),
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"), EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
} }

Loading…
Cancel
Save