mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Parametrize ChainID
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
@@ -128,7 +129,7 @@ func (tx *PoolL2Tx) SetID() error {
|
||||
// [ 16 bits ] chainId // 2 bytes
|
||||
// [ 32 bits ] signatureConstant // 4 bytes
|
||||
// Total bits compressed data: 241 bits // 31 bytes in *big.Int representation
|
||||
func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
|
||||
func (tx *PoolL2Tx) TxCompressedData(chainID uint16) (*big.Int, error) {
|
||||
amountFloat16, err := NewFloat16(tx.Amount)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
@@ -160,7 +161,7 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
copy(b[19:25], fromIdxBytes[:])
|
||||
copy(b[25:27], []byte{0, 0}) // TODO this will be generated by the ChainID config parameter
|
||||
binary.BigEndian.PutUint16(b[25:27], chainID)
|
||||
copy(b[27:31], SignatureConstantBytes[:])
|
||||
|
||||
bi := new(big.Int).SetBytes(b[:])
|
||||
@@ -271,8 +272,8 @@ func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
|
||||
}
|
||||
|
||||
// HashToSign returns the computed Poseidon hash from the *PoolL2Tx that will be signed by the sender.
|
||||
func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
|
||||
toCompressedData, err := tx.TxCompressedData()
|
||||
func (tx *PoolL2Tx) HashToSign(chainID uint16) (*big.Int, error) {
|
||||
toCompressedData, err := tx.TxCompressedData(chainID)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
@@ -292,8 +293,8 @@ func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
|
||||
}
|
||||
|
||||
// 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()
|
||||
func (tx *PoolL2Tx) VerifySignature(chainID uint16, pkComp babyjub.PublicKeyComp) bool {
|
||||
h, err := tx.HashToSign(chainID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ func TestNewPoolL2Tx(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTxCompressedData(t *testing.T) {
|
||||
chainID := uint16(0)
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.NoError(t, err)
|
||||
@@ -36,12 +37,24 @@ func TestTxCompressedData(t *testing.T) {
|
||||
Nonce: 6,
|
||||
ToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
txCompressedData, err := tx.TxCompressedData()
|
||||
txCompressedData, err := tx.TxCompressedData(chainID)
|
||||
assert.NoError(t, err)
|
||||
// test vector value generated from javascript implementation
|
||||
expectedStr := "1766847064778421992193717128424891165872736891548909569553540445094274575"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
assert.Equal(t, "010000000000060000000500040000000000030000000000020000c60be60f", hex.EncodeToString(txCompressedData.Bytes()))
|
||||
// using a different chainID
|
||||
txCompressedData, err = tx.TxCompressedData(uint16(100))
|
||||
assert.NoError(t, err)
|
||||
expectedStr = "1766847064778421992193717128424891165872736891548909569553540874591004175"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
assert.Equal(t, "010000000000060000000500040000000000030000000000020064c60be60f", hex.EncodeToString(txCompressedData.Bytes()))
|
||||
txCompressedData, err = tx.TxCompressedData(uint16(65535))
|
||||
assert.NoError(t, err)
|
||||
expectedStr = "1766847064778421992193717128424891165872736891548909569553821915776017935"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
assert.Equal(t, "01000000000006000000050004000000000003000000000002ffffc60be60f", hex.EncodeToString(txCompressedData.Bytes()))
|
||||
|
||||
tx = PoolL2Tx{
|
||||
RqFromIdx: 7,
|
||||
RqToIdx: 8,
|
||||
@@ -51,12 +64,12 @@ func TestTxCompressedData(t *testing.T) {
|
||||
RqFee: 12,
|
||||
RqToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
txCompressedData, err = tx.RqTxCompressedDataV2()
|
||||
rqTxCompressedData, err := tx.RqTxCompressedDataV2()
|
||||
assert.NoError(t, err)
|
||||
// test vector value generated from javascript implementation
|
||||
expectedStr = "6571340879233176732837827812956721483162819083004853354503"
|
||||
assert.Equal(t, expectedStr, txCompressedData.String())
|
||||
assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes()))
|
||||
assert.Equal(t, expectedStr, rqTxCompressedData.String())
|
||||
assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(rqTxCompressedData.Bytes()))
|
||||
}
|
||||
|
||||
func TestTxCompressedDataV2(t *testing.T) {
|
||||
@@ -109,6 +122,7 @@ func TestRqTxCompressedDataV2(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHashToSign(t *testing.T) {
|
||||
chainID := uint16(0)
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.NoError(t, err)
|
||||
@@ -120,12 +134,13 @@ func TestHashToSign(t *testing.T) {
|
||||
Nonce: 6,
|
||||
ToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
toSign, err := tx.HashToSign()
|
||||
toSign, err := tx.HashToSign(chainID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1469900657138253851938022936440971384682713995864967090251961124784132925291", toSign.String())
|
||||
}
|
||||
|
||||
func TestVerifyTxSignature(t *testing.T) {
|
||||
chainID := uint16(0)
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.NoError(t, err)
|
||||
@@ -139,13 +154,13 @@ func TestVerifyTxSignature(t *testing.T) {
|
||||
RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
RqToBJJ: sk.Public().Compress(),
|
||||
}
|
||||
toSign, err := tx.HashToSign()
|
||||
toSign, err := tx.HashToSign(chainID)
|
||||
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().Compress()))
|
||||
assert.True(t, tx.VerifySignature(chainID, sk.Public().Compress()))
|
||||
}
|
||||
|
||||
func TestDecompressEmptyBJJComp(t *testing.T) {
|
||||
|
||||
@@ -282,7 +282,7 @@ func (z ZKInputs) MarshalJSON() ([]byte, error) {
|
||||
}
|
||||
|
||||
// NewZKInputs returns a pointer to an initialized struct of ZKInputs
|
||||
func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatch *big.Int) *ZKInputs {
|
||||
func NewZKInputs(chainID uint16, nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatch *big.Int) *ZKInputs {
|
||||
zki := &ZKInputs{}
|
||||
zki.Metadata.NTx = nTx
|
||||
zki.Metadata.MaxFeeIdxs = maxFeeIdxs
|
||||
@@ -290,12 +290,13 @@ func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatc
|
||||
zki.Metadata.NLevels = nLevels
|
||||
zki.Metadata.MaxL1Tx = maxL1Tx
|
||||
zki.Metadata.MaxTx = maxTx
|
||||
zki.Metadata.ChainID = chainID
|
||||
|
||||
// General
|
||||
zki.CurrentNumBatch = currentNumBatch
|
||||
zki.OldLastIdx = big.NewInt(0)
|
||||
zki.OldStateRoot = big.NewInt(0)
|
||||
zki.GlobalChainID = big.NewInt(0) // TODO pass by parameter
|
||||
zki.GlobalChainID = big.NewInt(int64(chainID))
|
||||
zki.FeeIdxs = newSlice(maxFeeIdxs)
|
||||
zki.FeePlanTokens = newSlice(maxFeeIdxs)
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@ import (
|
||||
)
|
||||
|
||||
func TestZKInputs(t *testing.T) {
|
||||
zki := NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
|
||||
chainID := uint16(0)
|
||||
zki := NewZKInputs(chainID, 100, 16, 512, 24, 32, big.NewInt(1))
|
||||
_, err := json.Marshal(zki)
|
||||
require.NoError(t, err)
|
||||
// fmt.Println(string(s))
|
||||
|
||||
Reference in New Issue
Block a user