mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add common/Leaf struct & parsers (bigint & byte arrays)
This commit is contained in:
9
common/errors.go
Normal file
9
common/errors.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package common
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrNotInFF is used when the *big.Int does not fit inside the Finite Field
|
||||
var ErrNotInFF = errors.New("BigInt not inside the Finite Field")
|
||||
|
||||
// ErrNumOverflow is used when a given value overflows the maximum capacity of the parameter
|
||||
var ErrNumOverflow = errors.New("Value overflows the type")
|
||||
117
common/leaf.go
Normal file
117
common/leaf.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
|
||||
eth "github.com/ethereum/go-ethereum/common"
|
||||
cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
|
||||
)
|
||||
|
||||
// Leaf is the data structure stored in the Leaf of the MerkleTree
|
||||
type Leaf struct {
|
||||
TokenID TokenID
|
||||
Nonce uint64 // max of 40 bits used
|
||||
Balance *big.Int // max of 192 bits used
|
||||
Ax *big.Int
|
||||
Ay *big.Int
|
||||
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.
|
||||
func (l *Leaf) Bytes() ([32 * 5]byte, error) {
|
||||
var b [32 * 5]byte
|
||||
|
||||
if l.Nonce >= uint64(math.Pow(2, 40)) {
|
||||
return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
|
||||
}
|
||||
if len(l.Balance.Bytes()) > 24 {
|
||||
return b, fmt.Errorf("%s Balance", ErrNumOverflow)
|
||||
}
|
||||
|
||||
var tokenIDBytes [4]byte
|
||||
binary.LittleEndian.PutUint32(tokenIDBytes[:], uint32(l.TokenID))
|
||||
var nonceBytes [8]byte
|
||||
binary.LittleEndian.PutUint64(nonceBytes[:], l.Nonce)
|
||||
|
||||
copy(b[0:4], tokenIDBytes[:])
|
||||
copy(b[4:9], nonceBytes[:])
|
||||
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())
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// 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{}
|
||||
|
||||
b, err := l.Bytes()
|
||||
if err != nil {
|
||||
return e, err
|
||||
}
|
||||
|
||||
e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
|
||||
e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
|
||||
e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
|
||||
e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
|
||||
e[4] = new(big.Int).SetBytes(SwapEndianness(b[128:160]))
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// LeafFromBigInts returns a Leaf from a [5]*big.Int
|
||||
func LeafFromBigInts(e [5]*big.Int) (*Leaf, error) {
|
||||
if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
|
||||
return nil, ErrNotInFF
|
||||
}
|
||||
var b [32 * 5]byte
|
||||
copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
|
||||
copy(b[32:64], SwapEndianness(e[1].Bytes()))
|
||||
copy(b[64:96], SwapEndianness(e[2].Bytes()))
|
||||
copy(b[96:128], SwapEndianness(e[3].Bytes()))
|
||||
copy(b[128:160], SwapEndianness(e[4].Bytes()))
|
||||
|
||||
return LeafFromBytes(b)
|
||||
}
|
||||
|
||||
// LeafFromBytes returns a Leaf from a byte array
|
||||
func LeafFromBytes(b [32 * 5]byte) (*Leaf, error) {
|
||||
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)) {
|
||||
return nil, fmt.Errorf("%s Nonce", ErrNumOverflow)
|
||||
}
|
||||
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}) {
|
||||
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])
|
||||
|
||||
if !cryptoUtils.CheckBigIntInField(balance) {
|
||||
return nil, ErrNotInFF
|
||||
}
|
||||
if !cryptoUtils.CheckBigIntInField(ax) {
|
||||
return nil, ErrNotInFF
|
||||
}
|
||||
if !cryptoUtils.CheckBigIntInField(ay) {
|
||||
return nil, ErrNotInFF
|
||||
}
|
||||
|
||||
l := Leaf{
|
||||
TokenID: TokenID(tokenID),
|
||||
Nonce: nonce,
|
||||
Balance: balance,
|
||||
Ax: ax,
|
||||
Ay: ay,
|
||||
EthAddr: ethAddr,
|
||||
}
|
||||
return &l, nil
|
||||
}
|
||||
164
common/leaf_test.go
Normal file
164
common/leaf_test.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
|
||||
cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLeaf(t *testing.T) {
|
||||
leaf := &Leaf{
|
||||
TokenID: TokenID(1),
|
||||
Nonce: uint64(1234),
|
||||
Balance: big.NewInt(1000),
|
||||
Ax: big.NewInt(9876),
|
||||
Ay: big.NewInt(6789),
|
||||
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
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]))
|
||||
|
||||
assert.Equal(t, "1000", e[1].String())
|
||||
assert.Equal(t, "9876", e[2].String())
|
||||
assert.Equal(t, "6789", e[3].String())
|
||||
|
||||
l2, err := LeafFromBigInts(e)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, leaf, 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 TestLeafErrNotInFF(t *testing.T) {
|
||||
z := big.NewInt(0)
|
||||
|
||||
// Q-1 should not give error
|
||||
r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
|
||||
e := [5]*big.Int{z, z, r, r, r}
|
||||
_, err := LeafFromBigInts(e)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// Q should give error
|
||||
r = cryptoConstants.Q
|
||||
e = [5]*big.Int{z, z, r, r, r}
|
||||
_, err = LeafFromBigInts(e)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrNotInFF, err)
|
||||
|
||||
// Q+1 should give error
|
||||
r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1))
|
||||
e = [5]*big.Int{z, z, r, r, r}
|
||||
_, err = LeafFromBigInts(e)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrNotInFF, err)
|
||||
}
|
||||
|
||||
func TestLeafErrNumOverflowNonce(t *testing.T) {
|
||||
// check limit
|
||||
leaf := &Leaf{
|
||||
TokenID: TokenID(1),
|
||||
Nonce: uint64(math.Pow(2, 40) - 1),
|
||||
Balance: big.NewInt(1000),
|
||||
Ax: big.NewInt(9876),
|
||||
Ay: big.NewInt(6789),
|
||||
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
_, err := leaf.Bytes()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// force value overflow
|
||||
leaf.Nonce = uint64(math.Pow(2, 40))
|
||||
b, err := leaf.Bytes()
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
|
||||
|
||||
_, err = LeafFromBytes(b)
|
||||
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) {
|
||||
// check limit
|
||||
leaf := &Leaf{
|
||||
TokenID: TokenID(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)),
|
||||
Ax: big.NewInt(9876),
|
||||
Ay: big.NewInt(6789),
|
||||
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", leaf.Balance.String())
|
||||
|
||||
_, err := leaf.Bytes()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// force value overflow
|
||||
leaf.Balance = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
|
||||
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512896", leaf.Balance.String())
|
||||
b, err := leaf.Bytes()
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
|
||||
|
||||
_, err = LeafFromBytes(b)
|
||||
assert.Nil(t, err)
|
||||
|
||||
b[56] = 1
|
||||
_, err = LeafFromBytes(b)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
|
||||
}
|
||||
10
common/utils.go
Normal file
10
common/utils.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package common
|
||||
|
||||
// SwapEndianness swaps the order of the bytes in the slice.
|
||||
func SwapEndianness(b []byte) []byte {
|
||||
o := make([]byte, len(b))
|
||||
for i := range b {
|
||||
o[len(b)-1-i] = b[i]
|
||||
}
|
||||
return o
|
||||
}
|
||||
Reference in New Issue
Block a user