Skip to content

Commit

Permalink
Use Poseidon Sponge Hash for SC code. (0xPolygonHermez#379)
Browse files Browse the repository at this point in the history
* Use Poseidon Sponge Hash for SC code.

* make sure that the sc code hash has the right size

* use bytes buffer of the right size

* only run automatic rebase on PRs
  • Loading branch information
fgimenez authored Feb 10, 2022
1 parent 982b34e commit 6b3114c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
uses: cirrus-actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: github.event_name == 'pull_request'
- name: Lint
run: |
make install-linter
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
uses: cirrus-actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: github.event_name == 'pull_request'
- name: Install Go
uses: actions/setup-go@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ethereum/go-ethereum v1.10.12
github.com/gobuffalo/packr/v2 v2.8.3
github.com/hermeznetwork/tracerr v0.3.2
github.com/iden3/go-iden3-crypto v0.0.12
github.com/iden3/go-iden3-crypto v0.0.13
github.com/jackc/pgtype v1.8.1
github.com/jackc/pgx/v4 v4.13.0
github.com/mitchellh/mapstructure v1.4.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ github.com/huin/goupnp v1.0.2/go.mod h1:0dxJBVBHqTMjIUMkESDTNgOOx/Mw5wYIfyFmdzSa
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/iden3/go-iden3-crypto v0.0.12 h1:dXZF+R9iI07DK49LHX/EKC3jTa0O2z+TUyvxjGK7V38=
github.com/iden3/go-iden3-crypto v0.0.12/go.mod h1:swXIv0HFbJKobbQBtsB50G7IHr6PbTowutSew/iBEoo=
github.com/iden3/go-iden3-crypto v0.0.13 h1:ixWRiaqDULNyIDdOWz2QQJG5t4PpNHkQk2P6GV94cok=
github.com/iden3/go-iden3-crypto v0.0.13/go.mod h1:swXIv0HFbJKobbQBtsB50G7IHr6PbTowutSew/iBEoo=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/flux v0.65.1/go.mod h1:J754/zds0vvpfwuq7Gc2wRdVwEodfpCFM7mYlOw2LqY=
Expand Down
12 changes: 9 additions & 3 deletions state/tree/merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ const (

// MerkleTree implements merkle tree
type MerkleTree struct {
store Store
hashFunction HashFunction
arity uint8
store Store
hashFunction HashFunction
scHashFunction SCHashFunction
arity uint8
}

// UpdateProof is a proof generated on Set operation
Expand Down Expand Up @@ -59,6 +60,9 @@ type Proof struct {
// HashFunction is a function interface type to specify hash function that MT should use
type HashFunction func(inputs []*big.Int) (*big.Int, error)

// SCHashFunction is a function interface type to specify hash function that MT should use
type SCHashFunction func(inputs []byte) (*big.Int, error)

// NewMerkleTree creates new MerkleTree instance
func NewMerkleTree(store Store, arity uint8, hashFunction HashFunction) *MerkleTree {
if hashFunction == nil {
Expand All @@ -68,6 +72,8 @@ func NewMerkleTree(store Store, arity uint8, hashFunction HashFunction) *MerkleT
store: store,
arity: arity,
hashFunction: hashFunction,
// for now scHashFunction is fixed
scHashFunction: poseidon.HashBytes,
}
}

Expand Down
15 changes: 11 additions & 4 deletions state/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/iden3/go-iden3-crypto/keccak256"
)

// DefaultMerkleTreeArity specifies Merkle Tree arity used by default
Expand Down Expand Up @@ -222,10 +221,18 @@ func (tree *StateTree) SetCode(address common.Address, code []byte) (newRoot []b
}

// calculating smart contract code hash
scCodeHash := keccak256.Hash(code)
scCodeHashBI, err := tree.mt.scHashFunction(code)
if err != nil {
return nil, nil, err
}
// we need to have exactly maxBigIntLen bytes for a key in db for
// interoperability with prover/executor code, but big.Int Bytes()
// can return less, so we make sure it has the right size with FillBytes.
var scCodeHash [maxBigIntLen]byte
scCodeHashBI.FillBytes(scCodeHash[:])

// store smart contract code by its hash
err = tree.scCodeStore.Set(context.TODO(), scCodeHash, code)
err = tree.scCodeStore.Set(context.TODO(), scCodeHash[:], code)
if err != nil {
return nil, nil, err
}
Expand All @@ -238,7 +245,7 @@ func (tree *StateTree) SetCode(address common.Address, code []byte) (newRoot []b
}

k := new(big.Int).SetBytes(key[:])
updateProof, err := tree.mt.Set(context.TODO(), r, k, new(big.Int).SetBytes(scCodeHash))
updateProof, err := tree.mt.Set(context.TODO(), r, k, new(big.Int).SetBytes(scCodeHash[:]))
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 6b3114c

Please sign in to comment.