mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add StateTree leafs & ExtTree data ZKI calculation
This commit is contained in:
@@ -27,6 +27,11 @@ func (n Nonce) Bytes() ([5]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// BigInt returns the *big.Int representation of the Nonce value
|
||||
func (n Nonce) BigInt() *big.Int {
|
||||
return big.NewInt(int64(n))
|
||||
}
|
||||
|
||||
// NonceFromBytes returns Nonce from a [5]byte
|
||||
func NonceFromBytes(b [5]byte) Nonce {
|
||||
var nonceBytes [8]byte
|
||||
@@ -76,7 +81,7 @@ type PoolL2Tx struct {
|
||||
// [ 32 bits ] tokenID // 4 bytes: [20:24]
|
||||
// [ 40 bits ] nonce // 5 bytes: [24:29]
|
||||
// [ 8 bits ] userFee // 1 byte: [29:30]
|
||||
// [ 1 bits ] toBjjSign // 1 byte: [30:31]
|
||||
// [ 1 bits ] toBJJSign // 1 byte: [30:31]
|
||||
// Total bits compressed data: 241 bits // 31 bytes in *big.Int representation
|
||||
func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
|
||||
// sigconstant
|
||||
@@ -102,11 +107,11 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
|
||||
}
|
||||
copy(b[24:29], nonceBytes[:])
|
||||
b[29] = byte(tx.Fee)
|
||||
toBjjSign := byte(0)
|
||||
toBJJSign := byte(0)
|
||||
if babyjub.PointCoordSign(tx.ToBJJ.X) {
|
||||
toBjjSign = byte(1)
|
||||
toBJJSign = byte(1)
|
||||
}
|
||||
b[30] = toBjjSign
|
||||
b[30] = toBJJSign
|
||||
bi := new(big.Int).SetBytes(SwapEndianness(b[:]))
|
||||
|
||||
return bi, nil
|
||||
@@ -119,7 +124,7 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
|
||||
// [ 32 bits ] tokenID // 4 bytes: [14:18]
|
||||
// [ 40 bits ] nonce // 5 bytes: [18:23]
|
||||
// [ 8 bits ] userFee // 1 byte: [23:24]
|
||||
// [ 1 bits ] toBjjSign // 1 byte: [24:25]
|
||||
// [ 1 bits ] toBJJSign // 1 byte: [24:25]
|
||||
// Total bits compressed data: 193 bits // 25 bytes in *big.Int representation
|
||||
func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
|
||||
amountFloat16, err := utils.NewFloat16(tx.Amount)
|
||||
@@ -137,11 +142,11 @@ func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
|
||||
}
|
||||
copy(b[18:23], nonceBytes[:])
|
||||
b[23] = byte(tx.Fee)
|
||||
toBjjSign := byte(0)
|
||||
toBJJSign := byte(0)
|
||||
if babyjub.PointCoordSign(tx.ToBJJ.X) {
|
||||
toBjjSign = byte(1)
|
||||
toBJJSign = byte(1)
|
||||
}
|
||||
b[24] = toBjjSign
|
||||
b[24] = toBJJSign
|
||||
|
||||
bi := new(big.Int).SetBytes(SwapEndianness(b[:]))
|
||||
return bi, nil
|
||||
@@ -154,13 +159,13 @@ func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
|
||||
return nil, err
|
||||
}
|
||||
toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
|
||||
toBjjAy := tx.ToBJJ.Y
|
||||
toBJJAy := tx.ToBJJ.Y
|
||||
rqTxCompressedDataV2, err := tx.TxCompressedDataV2()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBjjAy, rqTxCompressedDataV2, EthAddrToBigInt(tx.RqToEthAddr), tx.RqToBJJ.Y})
|
||||
return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJAy, rqTxCompressedDataV2, EthAddrToBigInt(tx.RqToEthAddr), tx.RqToBJJ.Y})
|
||||
}
|
||||
|
||||
// VerifySignature returns true if the signature verification is correct for the given PublicKey
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"math/big"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
)
|
||||
|
||||
// SwapEndianness swaps the order of the bytes in the slice.
|
||||
@@ -20,20 +19,3 @@ func SwapEndianness(b []byte) []byte {
|
||||
func EthAddrToBigInt(a ethCommon.Address) *big.Int {
|
||||
return new(big.Int).SetBytes(a.Bytes())
|
||||
}
|
||||
|
||||
// BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
|
||||
// representation of the babyjub.PublicKeyComp
|
||||
func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
|
||||
var r [256]*big.Int
|
||||
b := pkComp[:]
|
||||
|
||||
for i := 0; i < 256; i++ {
|
||||
if b[i/8]&(1<<(i%8)) == 0 {
|
||||
r[i] = big.NewInt(0)
|
||||
} else {
|
||||
r[i] = big.NewInt(1)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBJJCompressedTo256BigInt(t *testing.T) {
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
r := BJJCompressedTo256BigInts(pkComp)
|
||||
zero := big.NewInt(0)
|
||||
for i := 0; i < 256; i++ {
|
||||
assert.Equal(t, zero, r[i])
|
||||
}
|
||||
|
||||
pkComp[0] = 3
|
||||
r = BJJCompressedTo256BigInts(pkComp)
|
||||
one := big.NewInt(1)
|
||||
for i := 0; i < 256; i++ {
|
||||
if i != 0 && i != 1 {
|
||||
assert.Equal(t, zero, r[i])
|
||||
} else {
|
||||
assert.Equal(t, one, r[i])
|
||||
}
|
||||
}
|
||||
|
||||
pkComp[31] = 4
|
||||
r = BJJCompressedTo256BigInts(pkComp)
|
||||
for i := 0; i < 256; i++ {
|
||||
if i != 0 && i != 1 && i != 250 {
|
||||
assert.Equal(t, zero, r[i])
|
||||
} else {
|
||||
assert.Equal(t, one, r[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
17
common/zk.go
17
common/zk.go
@@ -64,13 +64,12 @@ type ZKInputs struct {
|
||||
|
||||
// OnChain determines if is L1 (1/true) or L2 (0/false)
|
||||
OnChain []*big.Int // bool, len: [nTx]
|
||||
// NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
|
||||
NewAccount []*big.Int // bool, len: [nTx]
|
||||
|
||||
//
|
||||
// Txs/L1Txs
|
||||
//
|
||||
// transaction L1
|
||||
// NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
|
||||
NewAccount []*big.Int // bool, len: [nTx]
|
||||
// LoadAmountF encoded as float16
|
||||
LoadAmountF []*big.Int // uint16, len: [nTx]
|
||||
// FromEthAddr
|
||||
@@ -109,8 +108,8 @@ type ZKInputs struct {
|
||||
TokenID1 []*big.Int // uint32, len: [nTx]
|
||||
Nonce1 []*big.Int // uint64 (max 40 bits), len: [nTx]
|
||||
Sign1 []*big.Int // bool, len: [nTx]
|
||||
Balance1 []*big.Int // big.Int (max 192 bits), len: [nTx]
|
||||
Ay1 []*big.Int // big.Int, len: [nTx]
|
||||
Balance1 []*big.Int // big.Int (max 192 bits), len: [nTx]
|
||||
EthAddr1 []*big.Int // ethCommon.Address, len: [nTx]
|
||||
Siblings1 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
|
||||
// Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
|
||||
@@ -123,8 +122,8 @@ type ZKInputs struct {
|
||||
TokenID2 []*big.Int // uint32, len: [nTx]
|
||||
Nonce2 []*big.Int // uint64 (max 40 bits), len: [nTx]
|
||||
Sign2 []*big.Int // bool, len: [nTx]
|
||||
Balance2 []*big.Int // big.Int (max 192 bits), len: [nTx]
|
||||
Ay2 []*big.Int // big.Int, len: [nTx]
|
||||
Balance2 []*big.Int // big.Int (max 192 bits), len: [nTx]
|
||||
EthAddr2 []*big.Int // ethCommon.Address, len: [nTx]
|
||||
Siblings2 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
|
||||
// newExit determines if an exit transaction has to create a new leaf in the exit tree
|
||||
@@ -140,8 +139,8 @@ type ZKInputs struct {
|
||||
TokenID3 []*big.Int // uint32, len: [maxFeeTx]
|
||||
Nonce3 []*big.Int // uint64 (max 40 bits), len: [maxFeeTx]
|
||||
Sign3 []*big.Int // bool, len: [maxFeeTx]
|
||||
Balance3 []*big.Int // big.Int (max 192 bits), len: [maxFeeTx]
|
||||
Ay3 []*big.Int // big.Int, len: [maxFeeTx]
|
||||
Balance3 []*big.Int // big.Int (max 192 bits), len: [maxFeeTx]
|
||||
EthAddr3 []*big.Int // ethCommon.Address, len: [maxFeeTx]
|
||||
Siblings3 [][]*big.Int // Hash, len: [maxFeeTx][nLevels + 1]
|
||||
|
||||
@@ -218,8 +217,8 @@ func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
|
||||
zki.TokenID1 = newSlice(nTx)
|
||||
zki.Nonce1 = newSlice(nTx)
|
||||
zki.Sign1 = newSlice(nTx)
|
||||
zki.Balance1 = newSlice(nTx)
|
||||
zki.Ay1 = newSlice(nTx)
|
||||
zki.Balance1 = newSlice(nTx)
|
||||
zki.EthAddr1 = newSlice(nTx)
|
||||
zki.Siblings1 = make([][]*big.Int, nTx)
|
||||
for i := 0; i < len(zki.Siblings1); i++ {
|
||||
@@ -232,8 +231,8 @@ func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
|
||||
zki.TokenID2 = newSlice(nTx)
|
||||
zki.Nonce2 = newSlice(nTx)
|
||||
zki.Sign2 = newSlice(nTx)
|
||||
zki.Balance2 = newSlice(nTx)
|
||||
zki.Ay2 = newSlice(nTx)
|
||||
zki.Balance2 = newSlice(nTx)
|
||||
zki.EthAddr2 = newSlice(nTx)
|
||||
zki.Siblings2 = make([][]*big.Int, nTx)
|
||||
for i := 0; i < len(zki.Siblings2); i++ {
|
||||
@@ -247,8 +246,8 @@ func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
|
||||
zki.TokenID3 = newSlice(maxFeeTx)
|
||||
zki.Nonce3 = newSlice(maxFeeTx)
|
||||
zki.Sign3 = newSlice(maxFeeTx)
|
||||
zki.Balance3 = newSlice(maxFeeTx)
|
||||
zki.Ay3 = newSlice(maxFeeTx)
|
||||
zki.Balance3 = newSlice(maxFeeTx)
|
||||
zki.EthAddr3 = newSlice(maxFeeTx)
|
||||
zki.Siblings3 = make([][]*big.Int, maxFeeTx)
|
||||
for i := 0; i < len(zki.Siblings3); i++ {
|
||||
|
||||
Reference in New Issue
Block a user