Add StateDB & LocalStateDB

This commit is contained in:
arnaucube
2020-08-06 19:52:21 +02:00
parent 4bff96859c
commit d8cb7298e0
10 changed files with 715 additions and 324 deletions

View File

@@ -1,18 +1,133 @@
package common
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
eth "github.com/ethereum/go-ethereum/common"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-iden3-crypto/poseidon"
cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
)
// Account is a struct that gives information of the holdings of an address for a specific token
const NLEAFELEMS = 4
// Account is a struct that gives information of the holdings of an address for a specific token. Is the data structure that generates the Value stored in the leaf of the MerkleTree
type Account struct {
TokenID TokenID
Nonce uint64 // max of 40 bits used
Balance *big.Int // max of 192 bits used
PublicKey *babyjub.PublicKey
EthAddr eth.Address
TokenID TokenID // effective 32 bits
Idx uint32 // bits = SMT levels (SMT levels needs to be decided)
Nonce uint64 // effective 48 bits
Balance *big.Int // Up to 192 bits
PublicKey babyjub.PublicKey
}
// Bytes returns the bytes representing the Account, 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 *Account) Bytes() ([32 * NLEAFELEMS]byte, error) {
var b [32 * NLEAFELEMS]byte
if l.Nonce > 0xffffffffff {
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[:])
if babyjub.PointCoordSign(l.PublicKey.X) {
b[10] = 1
}
copy(b[32:64], SwapEndianness(l.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
copy(b[64:96], SwapEndianness(l.PublicKey.Y.Bytes()))
copy(b[96:116], l.EthAddr.Bytes())
return b, nil
}
// BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
func (l *Account) BigInts() ([NLEAFELEMS]*big.Int, error) {
e := [NLEAFELEMS]*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]))
return e, nil
}
// HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
func (l *Account) HashValue() (*big.Int, error) {
b0 := big.NewInt(0)
toHash := [poseidon.T]*big.Int{b0, b0, b0, b0, b0, b0}
lBI, err := l.BigInts()
if err != nil {
return nil, err
}
copy(toHash[:], lBI[:])
v, err := poseidon.Hash(toHash)
return v, err
}
// AccountFromBigInts returns a Account from a [5]*big.Int
func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) {
if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
return nil, ErrNotInFF
}
var b [32 * NLEAFELEMS]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()))
return AccountFromBytes(b)
}
// AccountFromBytes returns a Account from a byte array
func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, error) {
tokenID := binary.LittleEndian.Uint32(b[0:4])
var nonceBytes [8]byte
copy(nonceBytes[:], b[4:9])
nonce := binary.LittleEndian.Uint64(nonceBytes[:])
sign := b[10] == 1
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)
}
ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
if err != nil {
return nil, err
}
publicKey := babyjub.PublicKey(*pkPoint)
ethAddr := eth.BytesToAddress(b[96:116])
if !cryptoUtils.CheckBigIntInField(balance) {
return nil, ErrNotInFF
}
if !cryptoUtils.CheckBigIntInField(ay) {
return nil, ErrNotInFF
}
l := Account{
TokenID: TokenID(tokenID),
Nonce: nonce,
Balance: balance,
PublicKey: &publicKey,
EthAddr: ethAddr,
}
return &l, nil
}

196
common/account_test.go Normal file
View File

@@ -0,0 +1,196 @@
package common
import (
"encoding/hex"
"fmt"
"math"
"math/big"
"testing"
ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/iden3/go-iden3-crypto/babyjub"
cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
"github.com/stretchr/testify/assert"
)
func TestAccount(t *testing.T) {
var sk babyjub.PrivateKey
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
pk := sk.Public()
account := &Account{
TokenID: TokenID(1),
Nonce: uint64(1234),
Balance: big.NewInt(1000),
PublicKey: pk,
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
}
b, err := account.Bytes()
assert.Nil(t, err)
assert.Equal(t, byte(1), b[10])
a1, err := AccountFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, account, a1)
e, err := account.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.Equal(t, "1000", e[1].String())
assert.Equal(t, pk.Y.String(), e[2].String())
assert.Equal(t, new(big.Int).SetBytes(SwapEndianness(account.EthAddr.Bytes())).String(), e[3].String())
a2, err := AccountFromBigInts(e)
assert.Nil(t, err)
assert.Equal(t, account, a2)
assert.Equal(t, a1, a2)
}
func TestAccountLoop(t *testing.T) {
// check that for different Address there is no problem
for i := 0; i < 256; i++ {
var sk babyjub.PrivateKey
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
pk := sk.Public()
key, err := ethCrypto.GenerateKey()
assert.Nil(t, err)
address := ethCrypto.PubkeyToAddress(key.PublicKey)
account := &Account{
TokenID: TokenID(i),
Nonce: uint64(i),
Balance: big.NewInt(1000),
PublicKey: pk,
EthAddr: address,
}
b, err := account.Bytes()
assert.Nil(t, err)
a1, err := AccountFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, account, a1)
e, err := account.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]))
a2, err := AccountFromBigInts(e)
assert.Nil(t, err)
assert.Equal(t, account, a2)
}
}
func TestAccountHashValue(t *testing.T) {
var sk babyjub.PrivateKey
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
pk := sk.Public()
account := &Account{
TokenID: TokenID(1),
Nonce: uint64(1234),
Balance: big.NewInt(1000),
PublicKey: pk,
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
}
v, err := account.HashValue()
assert.Nil(t, err)
assert.Equal(t, "6335844662301214382338419199835935731871537354006112711277201708185593574314", v.String())
}
func TestAccountErrNotInFF(t *testing.T) {
z := big.NewInt(0)
// Q-1 should not give error
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)
// Q should give error
r = cryptoConstants.Q
e = [NLEAFELEMS]*big.Int{z, z, r, r}
_, err = AccountFromBigInts(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 = [NLEAFELEMS]*big.Int{z, z, r, r}
_, err = AccountFromBigInts(e)
assert.NotNil(t, err)
assert.Equal(t, ErrNotInFF, err)
}
func TestAccountErrNumOverflowNonce(t *testing.T) {
var sk babyjub.PrivateKey
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
pk := sk.Public()
// check limit
account := &Account{
TokenID: TokenID(1),
Nonce: uint64(math.Pow(2, 40) - 1),
Balance: big.NewInt(1000),
PublicKey: pk,
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
}
_, err = account.Bytes()
assert.Nil(t, err)
// force value overflow
account.Nonce = uint64(math.Pow(2, 40))
b, err := account.Bytes()
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
_, err = AccountFromBytes(b)
assert.Nil(t, err)
}
func TestAccountErrNumOverflowBalance(t *testing.T) {
var sk babyjub.PrivateKey
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
pk := sk.Public()
// check limit
account := &Account{
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)),
PublicKey: pk,
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
}
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", account.Balance.String())
_, err = account.Bytes()
assert.Nil(t, err)
// force value overflow
account.Balance = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512896", account.Balance.String())
b, err := account.Bytes()
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
_, err = AccountFromBytes(b)
assert.Nil(t, err)
b[56] = 1
_, err = AccountFromBytes(b)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
}

View File

@@ -1,128 +0,0 @@
package common
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
eth "github.com/ethereum/go-ethereum/common"
"github.com/iden3/go-iden3-crypto/poseidon"
cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
)
const NLEAFELEMS = 4
// 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
Sign bool
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 * NLEAFELEMS]byte, error) {
var b [32 * NLEAFELEMS]byte
if l.Nonce > 0xffffffffff {
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[:])
if l.Sign {
b[10] = 1
}
copy(b[32:64], SwapEndianness(l.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
copy(b[64:96], SwapEndianness(l.Ay.Bytes()))
copy(b[96:116], 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() ([NLEAFELEMS]*big.Int, error) {
e := [NLEAFELEMS]*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]))
return e, nil
}
// HashValue returns the value of the Leaf, which is the Poseidon hash of its *big.Int representation
func (l *Leaf) HashValue() (*big.Int, error) {
toHash := [poseidon.T]*big.Int{}
lBI, err := l.BigInts()
if err != nil {
return nil, err
}
copy(toHash[:], lBI[:])
v, err := poseidon.Hash(toHash)
return v, err
}
// LeafFromBigInts returns a Leaf from a [5]*big.Int
func LeafFromBigInts(e [NLEAFELEMS]*big.Int) (*Leaf, error) {
if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
return nil, ErrNotInFF
}
var b [32 * NLEAFELEMS]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()))
return LeafFromBytes(b)
}
// LeafFromBytes returns a Leaf from a byte array
func LeafFromBytes(b [32 * NLEAFELEMS]byte) (*Leaf, error) {
tokenID := binary.LittleEndian.Uint32(b[0:4])
var nonceBytes [8]byte
copy(nonceBytes[:], b[4:9])
nonce := binary.LittleEndian.Uint64(nonceBytes[:])
sign := b[10] == 1
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)
}
ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
ethAddr := eth.BytesToAddress(b[96:116])
if !cryptoUtils.CheckBigIntInField(balance) {
return nil, ErrNotInFF
}
if !cryptoUtils.CheckBigIntInField(ay) {
return nil, ErrNotInFF
}
l := Leaf{
TokenID: TokenID(tokenID),
Nonce: nonce,
Balance: balance,
Sign: sign,
Ay: ay,
EthAddr: ethAddr,
}
return &l, nil
}

View File

@@ -1,158 +0,0 @@
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),
Sign: true,
Ay: big.NewInt(6789),
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
}
b, err := leaf.Bytes()
assert.Nil(t, err)
assert.Equal(t, byte(1), b[10])
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.Equal(t, "1000", e[1].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)
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),
// 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) {
z := big.NewInt(0)
// Q-1 should not give error
r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
e := [NLEAFELEMS]*big.Int{z, z, r, r}
_, err := LeafFromBigInts(e)
assert.Nil(t, err)
// Q should give error
r = cryptoConstants.Q
e = [NLEAFELEMS]*big.Int{z, z, 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 = [NLEAFELEMS]*big.Int{z, z, 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),
Sign: true,
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)
}
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)),
Sign: true,
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)
}