Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade zktrie to v0.6.0 #403

Merged
merged 19 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions crypto/poseidon/poseidon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/iden3/go-iden3-crypto/ff"
"github.com/iden3/go-iden3-crypto/utils"

"github.com/scroll-tech/go-ethereum/log"
)

const NROUNDSF = 8 //nolint:golint
Expand Down Expand Up @@ -153,8 +155,8 @@ func HashWithCap(inpBI []*big.Int, width int, nBytes int64) (*big.Int, error) {

}

// Hash computes the Poseidon hash for the given fixed-size inputs, select specs automatically from the size, no capacity flag is applied
func HashFixed(inpBI []*big.Int) (*big.Int, error) {
// Hash computes the Poseidon hash for the given fixed-size inputs, with specified domain field
func HashFixedWithDomain(inpBI []*big.Int, domain *big.Int) (*big.Int, error) {
t := len(inpBI) + 1
if len(inpBI) == 0 || len(inpBI) > len(NROUNDSP) {
return nil, fmt.Errorf("invalid inputs length %d, max %d", len(inpBI), len(NROUNDSP)) //nolint:gomnd,lll
Expand All @@ -165,7 +167,7 @@ func HashFixed(inpBI []*big.Int) (*big.Int, error) {
inp := utils.BigIntArrayToElementArray(inpBI[:])

state := make([]*ff.Element, t)
state[0] = zero()
state[0] = ff.NewElement().SetBigInt(domain)
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
copy(state[1:], inp[:])

state = permute(state, t)
Expand All @@ -175,3 +177,9 @@ func HashFixed(inpBI []*big.Int) (*big.Int, error) {
rE.ToBigIntRegular(r)
return r, nil
}

// Deprecated HashFixed entry, with domain field is 0
func HashFixed(inpBI []*big.Int) (*big.Int, error) {
log.Warn("called a deprecated method for poseidon fixed hash", "inputs", inpBI)
return HashFixedWithDomain(inpBI, big.NewInt(0))
}
8 changes: 8 additions & 0 deletions crypto/poseidon/poseidon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ func TestPoseidonHashFixed(t *testing.T) {
assert.Equal(t,
"9989051620750914585850546081941653841776809718687451684622678807385399211877",
h.String())

h, err = HashFixedWithDomain([]*big.Int{b1, b2}, big.NewInt(256))
assert.Nil(t, err)
assert.Equal(t,
"2362370911616048355006851495576377379220050231129891536935411970097789775493",
h.String())
h_ref, _ := HashFixed([]*big.Int{b1, b2})
assert.NotEqual(t, h_ref, h)
}

func TestErrorInputs(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
github.com/prometheus/tsdb v0.7.1
github.com/rjeczalik/notify v0.9.1
github.com/rs/cors v1.7.0
github.com/scroll-tech/zktrie v0.5.3
github.com/scroll-tech/zktrie v0.6.0-candidate.2
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
github.com/stretchr/testify v1.8.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scroll-tech/zktrie v0.5.3 h1:jjzQchGU6XPL5s1C5bwwivSadefSRuYASE9OL7UKAdE=
github.com/scroll-tech/zktrie v0.5.3/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/scroll-tech/zktrie v0.6.0-candidate.2 h1:6xlulf6x8XjqkYEnYwk06NtBNNYWcKW8i5Ja5+8vGlo=
github.com/scroll-tech/zktrie v0.6.0-candidate.2/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand Down
4 changes: 2 additions & 2 deletions trie/zk_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ZkTrie struct {
}

func init() {
zkt.InitHashScheme(poseidon.HashFixed)
zkt.InitHashScheme(poseidon.HashFixedWithDomain)
}

func sanityCheckByte32Key(b []byte) {
Expand Down Expand Up @@ -180,7 +180,7 @@ func (t *ZkTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter)
return err
}

if n.Type == zktrie.NodeTypeLeaf {
if n.Type == zktrie.NodeTypeLeaf_New {
noel2004 marked this conversation as resolved.
Show resolved Hide resolved
preImage := t.GetKey(n.NodeKey.Bytes())
if len(preImage) > 0 {
n.KeyPreimage = &zkt.Byte32{}
Expand Down
4 changes: 2 additions & 2 deletions trie/zk_trie_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func randomZktrie(t *testing.T, n int) (*ZkTrie, map[string]*kv) {
func TestProofWithDeletion(t *testing.T) {
tr, _ := NewZkTrie(common.Hash{}, NewZktrieDatabase((memorydb.New())))
mt := &zkTrieImplTestWrapper{tr.Tree()}
key1 := bytes.Repeat([]byte("k"), 32)
key1 := bytes.Repeat([]byte("l"), 32)
key2 := bytes.Repeat([]byte("m"), 32)
err := mt.UpdateWord(
zkt.NewByte32FromBytesPaddingZero(key1),
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestProofWithDeletion(t *testing.T) {

err = proofTracer.Prove(s_key2.Bytes(), 0, proof)
assert.NoError(t, err)
// assert.Equal(t, len(sibling1), len(delTracer.GetProofs()))
//assert.Equal(t, len(sibling1), len(delTracer.GetProofs()))

siblings, err := proofTracer.GetDeletionProofs()
assert.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions trie/zkproof/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func resumeProofs(proof []hexutil.Bytes, db *memorydb.Database) *zktrie.Node {
//notice: must consistent with trie/merkletree.go
bt := hash[:]
db.Put(bt, buf)
if n.Type == zktrie.NodeTypeLeaf || n.Type == zktrie.NodeTypeEmpty {
if n.Type == zktrie.NodeTypeLeaf_New || n.Type == zktrie.NodeTypeEmpty_New {
return n
}
}
Expand Down Expand Up @@ -107,9 +107,9 @@ func decodeProofForMPTPath(proof proofList, path *SMTPath) {
keyCounter.Mul(keyCounter, big.NewInt(2))
}
switch n.Type {
case zktrie.NodeTypeParent:
case zktrie.NodeTypeBranch_0, zktrie.NodeTypeBranch_1, zktrie.NodeTypeBranch_2, zktrie.NodeTypeBranch_3:
lastNode = n
case zktrie.NodeTypeLeaf:
case zktrie.NodeTypeLeaf_New:
vhash, _ := n.ValueHash()
path.Leaf = &SMTPathNode{
//here we just return the inner represent of hash (little endian, reversed byte order to common hash)
Expand All @@ -127,7 +127,7 @@ func decodeProofForMPTPath(proof proofList, path *SMTPath) {
}

return
case zktrie.NodeTypeEmpty:
case zktrie.NodeTypeEmpty_New:
return
default:
panic(fmt.Errorf("unknown node type %d", n.Type))
Expand Down Expand Up @@ -160,7 +160,7 @@ func NewZkTrieProofWriter(storage *types.StorageTrace) (*zktrieProofWriter, erro
for addrs, proof := range storage.Proofs {
if n := resumeProofs(proof, underlayerDb); n != nil {
addr := common.HexToAddress(addrs)
if n.Type == zktrie.NodeTypeEmpty {
if n.Type == zktrie.NodeTypeEmpty_New {
accounts[addr] = nil
} else if acc, err := types.UnmarshalStateAccount(n.Data()); err == nil {
if bytes.Equal(n.NodeKey[:], addressToKey(addr)[:]) {
Expand Down Expand Up @@ -328,7 +328,7 @@ func verifyAccount(addr common.Address, data *types.StateAccount, leaf *SMTPathN
}
} else if data != nil {
arr, flag := data.MarshalFields()
h, err := zkt.PreHandlingElems(flag, arr)
h, err := zkt.HandlingElemsAndByte32(flag, arr)
//log.Info("sanity check acc before", "addr", addr.String(), "key", leaf.Sibling.Text(16), "hash", h.Text(16))

if err != nil {
Expand Down
15 changes: 10 additions & 5 deletions trie/zktrie_deletionproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package trie

import (
"bytes"
"fmt"

zktrie "github.com/scroll-tech/zktrie/trie"
zkt "github.com/scroll-tech/zktrie/types"
Expand Down Expand Up @@ -98,7 +99,7 @@ func (t *ProofTracer) GetDeletionProofs() ([][]byte, error) {
if err != nil {
return nil, err
}
if sibling.Type != zktrie.NodeTypeEmpty {
if sibling.Type != zktrie.NodeTypeEmpty_New {
retMap[*siblingHash] = sibling.Value()
}
}
Expand All @@ -125,7 +126,7 @@ func (t *ProofTracer) MarkDeletion(key []byte) {
// sanity check
leafNode := path[len(path)-1]

if leafNode.Type != zktrie.NodeTypeLeaf {
if leafNode.Type != zktrie.NodeTypeLeaf_New {
panic("all path recorded in proofTrace should be ended with leafNode")
}

Expand All @@ -145,19 +146,23 @@ func (t *ProofTracer) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWr
return err
}

if n.Type == zktrie.NodeTypeLeaf {
switch n.Type {
case zktrie.NodeTypeLeaf_New:
preImage := t.GetKey(n.NodeKey.Bytes())
if len(preImage) > 0 {
n.KeyPreimage = &zkt.Byte32{}
copy(n.KeyPreimage[:], preImage)
}
} else if n.Type == zktrie.NodeTypeParent {
case zktrie.NodeTypeBranch_0, zktrie.NodeTypeBranch_1,
zktrie.NodeTypeBranch_2, zktrie.NodeTypeBranch_3:
mptPath = append(mptPath, n)
} else if n.Type == zktrie.NodeTypeEmpty {
case zktrie.NodeTypeEmpty_New:
// empty node is considered as "unhit" but it should be also being added
// into a temporary slot for possibly being marked as deletion later
mptPath = append(mptPath, n)
t.emptyTermPaths[string(key)] = mptPath
default:
panic(fmt.Errorf("unexpected node type %d", n.Type))
}

return proofDb.Put(nodeHash[:], n.Value())
Expand Down