Skip to content

Commit

Permalink
Merge remote-tracking branch 'hl/main' into erc20-anchoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jimthematrix committed Jul 31, 2024
2 parents b9d10af + 9a6026e commit d9de496
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 63 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Golang SDK

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

jobs:
build:
env:
TEST_ARGS: -v
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.22

- name: Build and Test
run: cd zkp/golang && make

- uses: codecov/codecov-action@v4
with:
codecov_yml_path: ./codecov.yml
token: ${{ secrets.CODECOV_TOKEN }}
32 changes: 32 additions & 0 deletions zkp/golang/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
VGO=go
GOFILES := $(shell find ./internal ./pkg -name '*.go' -print)
GOBIN := $(shell $(VGO) env GOPATH)/bin
LINT := $(GOBIN)/golangci-lint

# Expect that Zeto compiles with CGO disabled
CGO_ENABLED=0
GOGC=30

.DELETE_ON_ERROR:

all: test go-mod-tidy
test: deps lint
$(VGO) test ./internal/... ./pkg/... -cover -coverprofile=coverage.txt -covermode=atomic -timeout=30s ${TEST_ARGS}
coverage.html:
$(VGO) tool cover -html=coverage.txt
coverage: test coverage.html
lint: ${LINT}
GOGC=20 $(LINT) run -v --timeout 5m
${LINT}:
$(VGO) install github.com/golangci/golangci-lint/cmd/[email protected]

go-mod-tidy: .ALWAYS
$(VGO) mod tidy
e2e: test
$(VGO) test ./integration-test
.ALWAYS: ;
clean:
$(VGO) clean
rm -f *.so
deps:
$(VGO) get -u ./...
8 changes: 4 additions & 4 deletions zkp/golang/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
)

require (
Expand All @@ -30,8 +30,8 @@ require (
github.com/iden3/go-rapidsnark/types v0.0.2 // indirect
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.5.9
gorm.io/driver/sqlite v1.5.6
Expand Down
16 changes: 8 additions & 8 deletions zkp/golang/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
24 changes: 14 additions & 10 deletions zkp/golang/integration-test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,10 @@ func TestSqliteStorage(t *testing.T) {
}()
db, err := gorm.Open(sqlite.Open(dbfile.Name()), &gorm.Config{})
assert.NoError(t, err)
db.Table("smtRoots").AutoMigrate(&core.SMTRoot{})
db.Table("smtNodes_test_1").AutoMigrate(&core.SMTNode{})
err = db.Table(core.TreeRootsTable).AutoMigrate(&core.SMTRoot{})
assert.NoError(t, err)
err = db.Table(core.NodesTablePrefix + "test_1").AutoMigrate(&core.SMTNode{})
assert.NoError(t, err)

provider := &testSqlProvider{db: db}
s, err := storage.NewSqlStorage(provider, "test_1")
Expand All @@ -595,12 +597,12 @@ func TestSqliteStorage(t *testing.T) {

root := mt.Root()
dbRoot := core.SMTRoot{Name: "test_1"}
err = db.Table("smtRoots").First(&dbRoot).Error
err = db.Table(core.TreeRootsTable).First(&dbRoot).Error
assert.NoError(t, err)
assert.Equal(t, root.Hex(), dbRoot.RootIndex)

dbNode := core.SMTNode{RefKey: n1.Ref().Hex()}
err = db.Table("smtNodes_test_1").First(&dbNode).Error
err = db.Table(core.NodesTablePrefix + "test_1").First(&dbNode).Error
assert.NoError(t, err)
assert.Equal(t, n1.Ref().Hex(), dbNode.RefKey)
}
Expand All @@ -609,12 +611,14 @@ func TestPostgresStorage(t *testing.T) {
dsn := "host=localhost user=postgres password=my-secret dbname=postgres port=5432 sslmode=disable"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
assert.NoError(t, err)
db.Table("smtRoots").AutoMigrate(&core.SMTRoot{})
db.Table("smtNodes_test_1").AutoMigrate(&core.SMTNode{})
err = db.Table(core.TreeRootsTable).AutoMigrate(&core.SMTRoot{})
assert.NoError(t, err)
err = db.Table(core.NodesTablePrefix + "test_1").AutoMigrate(&core.SMTNode{})
assert.NoError(t, err)

defer func() {
db.Exec("DROP TABLE smtRoots")
db.Exec("DROP TABLE smtNodes_test_1")
db.Exec("DROP TABLE " + core.TreeRootsTable)
db.Exec("DROP TABLE " + core.NodesTablePrefix + "test_1")
}()

provider := &testSqlProvider{db: db}
Expand All @@ -638,12 +642,12 @@ func TestPostgresStorage(t *testing.T) {

root := mt.Root()
dbRoot := core.SMTRoot{Name: "test_1"}
err = db.Table("smtRoots").First(&dbRoot).Error
err = db.Table(core.TreeRootsTable).First(&dbRoot).Error
assert.NoError(t, err)
assert.Equal(t, root.Hex(), dbRoot.RootIndex)

dbNode := core.SMTNode{RefKey: n1.Ref().Hex()}
err = db.Table("smtNodes_test_1").First(&dbNode).Error
err = db.Table(core.NodesTablePrefix + "test_1").First(&dbNode).Error
assert.NoError(t, err)
assert.Equal(t, n1.Ref().Hex(), dbNode.RefKey)
}
4 changes: 2 additions & 2 deletions zkp/golang/internal/smt/merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/hyperledger-labs/zeto/internal/node"
"github.com/hyperledger-labs/zeto/internal/storage"
"github.com/hyperledger-labs/zeto/internal/utxo"
"github.com/hyperledger-labs/zeto/internal/utils"
"github.com/hyperledger-labs/zeto/pkg/core"
)

Expand Down Expand Up @@ -137,7 +137,7 @@ func (mt *sparseMerkleTree) GenerateProof(k *big.Int, rootKey core.NodeIndex) (c
return p, value.BigInt(), nil
}
// We found a leaf whose entry didn't match the node index
p.existingNode, err = node.NewLeafNode(utxo.NewIndexOnly(idx))
p.existingNode, err = node.NewLeafNode(utils.NewIndexOnly(idx))
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions zkp/golang/internal/smt/smt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestVerifyProof(t *testing.T) {
done := make(chan bool, len(values))
startProving := make(chan core.Node, len(values))
for idx, value := range values {
go func(v int) {
go func(v int, idx int) {
salt := rand.Intn(100000)
utxo := utxo.NewFungible(big.NewInt(int64(v)), alice.PublicKey, big.NewInt(int64(salt)))
node, err := node.NewLeafNode(utxo)
Expand All @@ -168,7 +168,7 @@ func TestVerifyProof(t *testing.T) {
startProving <- node
done <- true
fmt.Printf("Added node %d\n", idx)
}(value)
}(value, idx)
}

go func() {
Expand Down
50 changes: 20 additions & 30 deletions zkp/golang/internal/storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@ package storage

import (
"github.com/hyperledger-labs/zeto/internal/node"
"github.com/hyperledger-labs/zeto/internal/utxo"
"github.com/hyperledger-labs/zeto/internal/utils"
"github.com/hyperledger-labs/zeto/pkg/core"
"gorm.io/gorm"
)

const (
// we use a table to store the root node indexes for
// all the merkle trees in the database
TreeRootsTable = "smtRoots"
// we use a separate table to store the nodes of each merkle tree
// by using the following name as the prefix, followed by the
// name of the tree
NodesTable_Prefix = "smtNodes_"
)

type sqlStorage struct {
p core.SqlDBProvider
smtName string
Expand All @@ -46,15 +36,15 @@ func NewSqlStorage(p core.SqlDBProvider, smtName string) *sqlStorage {
return &sqlStorage{
p: p,
smtName: smtName,
nodesTableName: NodesTable_Prefix + smtName,
nodesTableName: core.NodesTablePrefix + smtName,
}
}

func (s *sqlStorage) GetRootNodeIndex() (core.NodeIndex, error) {
root := core.SMTRoot{
Name: s.smtName,
}
err := s.p.DB().Table(TreeRootsTable).First(&root).Error
err := s.p.DB().Table(core.TreeRootsTable).First(&root).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
} else if err != nil {
Expand All @@ -64,21 +54,21 @@ func (s *sqlStorage) GetRootNodeIndex() (core.NodeIndex, error) {
return idx, err
}

func (m *sqlStorage) UpsertRootNodeIndex(root core.NodeIndex) error {
err := m.p.DB().Table(TreeRootsTable).Save(&core.SMTRoot{
func (s *sqlStorage) UpsertRootNodeIndex(root core.NodeIndex) error {
err := s.p.DB().Table(core.TreeRootsTable).Save(&core.SMTRoot{
RootIndex: root.Hex(),
Name: m.smtName,
Name: s.smtName,
}).Error
return err
}

func (m *sqlStorage) GetNode(ref core.NodeIndex) (core.Node, error) {
func (s *sqlStorage) GetNode(ref core.NodeIndex) (core.Node, error) {
// the node's reference key (not the index) is used as the key to
// store the node in the DB
n := core.SMTNode{
RefKey: ref.Hex(),
}
err := m.p.DB().Table(m.nodesTableName).First(&n).Error
err := s.p.DB().Table(s.nodesTableName).First(&n).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
} else if err != nil {
Expand All @@ -88,27 +78,27 @@ func (m *sqlStorage) GetNode(ref core.NodeIndex) (core.Node, error) {
nodeType := core.NodeTypeFromByte(n.Type)
switch nodeType {
case core.NodeTypeLeaf:
idx, err := node.NewNodeIndexFromHex(*n.Index)
if err != nil {
return nil, err
idx, err1 := node.NewNodeIndexFromHex(*n.Index)
if err1 != nil {
return nil, err1
}
v := utxo.NewIndexOnly(idx)
v := utils.NewIndexOnly(idx)
newNode, err = node.NewLeafNode(v)
case core.NodeTypeBranch:
leftChild, err := node.NewNodeIndexFromHex(*n.LeftChild)
if err != nil {
return nil, err
leftChild, err1 := node.NewNodeIndexFromHex(*n.LeftChild)
if err1 != nil {
return nil, err1
}
rightChild, err := node.NewNodeIndexFromHex(*n.RightChild)
if err != nil {
return nil, err
rightChild, err2 := node.NewNodeIndexFromHex(*n.RightChild)
if err2 != nil {
return nil, err2
}
newNode, err = node.NewBranchNode(leftChild, rightChild)
}
return newNode, err
}

func (m *sqlStorage) InsertNode(n core.Node) error {
func (s *sqlStorage) InsertNode(n core.Node) error {
// we clone the node so that the value properties are not saved
dbNode := &core.SMTNode{
RefKey: n.Ref().Hex(),
Expand All @@ -124,7 +114,7 @@ func (m *sqlStorage) InsertNode(n core.Node) error {
dbNode.Index = &idx
}

err := m.p.DB().Table(m.nodesTableName).Create(dbNode).Error
err := s.p.DB().Table(s.nodesTableName).Create(dbNode).Error
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package utxo
package utils

import (
"github.com/hyperledger-labs/zeto/pkg/core"
Expand Down
20 changes: 15 additions & 5 deletions zkp/golang/pkg/core/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ type Storage interface {
Close()
}

const (
// we use a table to store the root node indexes for
// all the merkle trees in the database
TreeRootsTable = "merkelTreeRoots"
// we use a separate table to store the nodes of each
// sparse merkle tree by using the following name as
// the prefix, followed by the name of the tree
NodesTablePrefix = "smtNodes_"
)

// SqlDBProvider is the interface for providing access to a SQL database to
// the storage layer implementations that are backed by a SQL database.
type SqlDBProvider interface {
Expand All @@ -47,17 +57,17 @@ type SMTRoot struct {
Name string `gorm:"primaryKey"`
// this must be the hex bytes of the root index
// following the big-endian encoding
RootIndex string `gorm:"type:text"`
RootIndex string `gorm:"size:64"`
}

// SMTNode is the structure of a node in the merkle tree.
// It only captures the reference key and the index of the node.
// The value properties of a node are local states that are
// handled outside of the merkle tree library.
type SMTNode struct {
RefKey string `gorm:"primaryKey"`
RefKey string `gorm:"primaryKey;size:64"`
Type byte
Index *string // only leaf nodes have an index
LeftChild *string // only branch nodes have children
RightChild *string
Index *string `gorm:"size:64"` // only leaf nodes have an index
LeftChild *string `gorm:"size:64"` // only branch nodes have children
RightChild *string `gorm:"size:64"`
}
3 changes: 2 additions & 1 deletion zkp/golang/pkg/utxo/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package utxo
import (
"math/big"

"github.com/hyperledger-labs/zeto/internal/utils"
"github.com/hyperledger-labs/zeto/internal/utxo"
"github.com/hyperledger-labs/zeto/pkg/core"
"github.com/iden3/go-iden3-crypto/babyjub"
Expand All @@ -41,7 +42,7 @@ func NewNonFungibleNullifier(tokenId *big.Int, tokenUri *big.Int, owner *big.Int
}

func NewIndexOnly(index core.NodeIndex) core.Indexable {
return utxo.NewIndexOnly(index)
return utils.NewIndexOnly(index)
}

func HashTokenUri(tokenUri string) (*big.Int, error) {
Expand Down

0 comments on commit d9de496

Please sign in to comment.