mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
Update ExitProofs-Siblings padding
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"math/big"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/iden3/go-merkletree"
|
||||
)
|
||||
|
||||
// SwapEndianness swaps the order of the bytes in the slice.
|
||||
@@ -42,3 +44,21 @@ func BJJFromStringWithChecksum(s string) (babyjub.PublicKeyComp, error) {
|
||||
func CopyBigInt(a *big.Int) *big.Int {
|
||||
return new(big.Int).SetBytes(a.Bytes())
|
||||
}
|
||||
|
||||
// RmEndingZeroes is used to convert the Siblings from a CircomProof into
|
||||
// Siblings of a merkletree Proof compatible with the js version. This method
|
||||
// should be used only if it exist an already generated CircomProof compatible
|
||||
// with circom circuits and a CircomProof compatible with SmartContracts is
|
||||
// needed. If the proof is not generated yet, this method should not be needed
|
||||
// and should be used mt.GenerateSCVerifierProof to directly generate the
|
||||
// CircomProof for the SmartContracts.
|
||||
func RmEndingZeroes(siblings []*merkletree.Hash) []*merkletree.Hash {
|
||||
pos := 0
|
||||
for i := len(siblings) - 1; i >= 0; i-- {
|
||||
if !bytes.Equal(siblings[i].Bytes(), merkletree.HashZero.Bytes()) {
|
||||
pos = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
return siblings[:pos]
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/iden3/go-merkletree"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBJJFromStringWithChecksum(t *testing.T) {
|
||||
@@ -22,3 +24,52 @@ func TestBJJFromStringWithChecksum(t *testing.T) {
|
||||
assert.Equal(t, "2492816973395423007340226948038371729989170225696553239457870892535792679622", pk.X.String())
|
||||
assert.Equal(t, "15238403086306505038849621710779816852318505119327426213168494964113886299863", pk.Y.String())
|
||||
}
|
||||
|
||||
func TestRmEndingZeroes(t *testing.T) {
|
||||
s0, err := merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000000")
|
||||
require.NoError(t, err)
|
||||
s1, err := merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000001")
|
||||
require.NoError(t, err)
|
||||
s2, err := merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000002")
|
||||
require.NoError(t, err)
|
||||
|
||||
// expect cropped last zeroes
|
||||
circomSiblings := []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2, s0, s0, s0, s0}
|
||||
siblings := RmEndingZeroes(circomSiblings)
|
||||
expected := []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2}
|
||||
assert.Equal(t, expected, siblings)
|
||||
|
||||
// expect empty array when input is an empty array
|
||||
siblings = RmEndingZeroes([]*merkletree.Hash{})
|
||||
assert.Equal(t, []*merkletree.Hash{}, siblings)
|
||||
// expect nil when input is nil
|
||||
siblings = RmEndingZeroes(nil)
|
||||
assert.Nil(t, siblings)
|
||||
|
||||
// cases when inputs are [x], [x,0], [0,x]
|
||||
circomSiblings = []*merkletree.Hash{s1}
|
||||
siblings = RmEndingZeroes(circomSiblings)
|
||||
assert.Equal(t, []*merkletree.Hash{s1}, siblings)
|
||||
circomSiblings = []*merkletree.Hash{s1, s0}
|
||||
siblings = RmEndingZeroes(circomSiblings)
|
||||
assert.Equal(t, []*merkletree.Hash{s1}, siblings)
|
||||
circomSiblings = []*merkletree.Hash{s0, s1}
|
||||
siblings = RmEndingZeroes(circomSiblings)
|
||||
assert.Equal(t, []*merkletree.Hash{s0, s1}, siblings)
|
||||
|
||||
// expect empty array when input is all zeroes
|
||||
circomSiblings = []*merkletree.Hash{s0}
|
||||
siblings = RmEndingZeroes(circomSiblings)
|
||||
assert.Equal(t, []*merkletree.Hash{}, siblings)
|
||||
circomSiblings = []*merkletree.Hash{s0, s0}
|
||||
siblings = RmEndingZeroes(circomSiblings)
|
||||
assert.Equal(t, []*merkletree.Hash{}, siblings)
|
||||
circomSiblings = []*merkletree.Hash{s0, s0, s0, s0, s0}
|
||||
siblings = RmEndingZeroes(circomSiblings)
|
||||
assert.Equal(t, []*merkletree.Hash{}, siblings)
|
||||
|
||||
// expect input equal to output when last element!=0
|
||||
circomSiblings = []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2, s0, s0, s0, s0, s2}
|
||||
siblings = RmEndingZeroes(circomSiblings)
|
||||
assert.Equal(t, circomSiblings, siblings)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user