Skip to content

Commit

Permalink
Upgrade wazero to v1.8.0 and add Close() method to the Calculator int…
Browse files Browse the repository at this point in the history
…erface (#23)

* Upgrade wazero module to v1.8.0, that fixes #22 memory leak. Also add a method Close to Calculator interface. It calls the Close method on the underlying CalculatorImpl if one is supports optional io.Closer interface.

* Call the Close method on witness calculator

* Upgrade golangci-lint version

* Upgrade Go version we test witnesscalc from 1.21 to 1.23
  • Loading branch information
olomix authored Sep 13, 2024
1 parent 9323fbe commit ed1af0a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 25 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/lint-witness.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ jobs:
lint:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20.4
go-version: 1.23.1
- name: lint witness
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.52.2
version: v1.61.0
working-directory: witness
- name: lint witness/wazero
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.52.2
version: v1.61.0
working-directory: witness/wazero
- name: lint witness/wasmer
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.52.2
version: v1.61.0
working-directory: witness/wasmer
- name: lint witness/test_wasm_impls
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.52.2
version: v1.61.0
working-directory: witness/test_wasm_impls
10 changes: 5 additions & 5 deletions .github/workflows/test-witness.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
strategy:
matrix:
containers:
- 1.18.10-bullseye
- 1.19.9-bullseye
- 1.20.4-bullseye
- 1.21.13
- 1.22.7
- 1.23.1
runs-on: ubuntu-20.04
container: golang:${{matrix.containers}}
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/go-build
Expand Down
6 changes: 4 additions & 2 deletions witness/test_wasm_impls/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/iden3/go-rapidsnark/witness/test-wasm-impls

go 1.18
go 1.21

toolchain go1.23.1

require (
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0
Expand All @@ -15,7 +17,7 @@ require (
github.com/iden3/go-iden3-crypto v0.0.15 // indirect
github.com/iden3/wasmer-go v0.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tetratelabs/wazero v1.1.0 // indirect
github.com/tetratelabs/wazero v1.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
5 changes: 3 additions & 2 deletions witness/test_wasm_impls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/iden3/go-iden3-crypto v0.0.15/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBe
github.com/iden3/wasmer-go v0.0.1 h1:TZKh8Se8B/73PvWrcu+FTU9L1k5XYAmtFbioj7l0Uog=
github.com/iden3/wasmer-go v0.0.1/go.mod h1:ZnZBAO012M7o+Q1INXLRIxKQgEcH2FuwL0Iga8A4ufg=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -15,8 +16,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tetratelabs/wazero v1.1.0 h1:EByoAhC+QcYpwSZJSs/aV0uokxPwBgKxfiokSUwAknQ=
github.com/tetratelabs/wazero v1.1.0/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
github.com/tetratelabs/wazero v1.8.0 h1:iEKu0d4c2Pd+QSRieYbnQC9yiFlMS9D+Jr0LsRmcF4g=
github.com/tetratelabs/wazero v1.8.0/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
4 changes: 4 additions & 0 deletions witness/test_wasm_impls/witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func TestEngines(t *testing.T) {

require.NoError(t, err)

defer func() {
require.NoError(t, calc.Close())
}()

inputs, err := witness.ParseInputs(inputBytes)
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion witness/wazero/circom2witnesscalc_wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ func fromArray32(arr []uint32) *big.Int {
// fnvHash returns the 64 bit FNV-1a hash split into two 32 bit values: (MSB, LSB)
func fnvHash(s string) (int32, int32) {
hash := fnv.New64a()
hash.Write([]byte(s))
// sum64a.Write always returns nil as an error
_, _ = hash.Write([]byte(s))
h := hash.Sum64()
return int32(h >> 32), int32(h & 0xffffffff)
}
Expand Down
6 changes: 4 additions & 2 deletions witness/wazero/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/iden3/go-rapidsnark/witness/wazero

go 1.18
go 1.21

toolchain go1.23.1

require (
github.com/iden3/go-iden3-crypto v0.0.15
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0
github.com/tetratelabs/wazero v1.1.0
github.com/tetratelabs/wazero v1.8.0
)

require golang.org/x/sys v0.6.0 // indirect
9 changes: 7 additions & 2 deletions witness/wazero/go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iden3/go-iden3-crypto v0.0.15 h1:4MJYlrot1l31Fzlo2sF56u7EVFeHHJkxGXXZCtESgK4=
github.com/iden3/go-iden3-crypto v0.0.15/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0 h1:mkY6VDfwKVJc83QGKmwVXY2LYepidPrFAxskrjr8UCs=
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0/go.mod h1:3JRjqUfW1hgI9hzLDO0v8z/DUkR0ZUehhYLlnIfRxnA=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/tetratelabs/wazero v1.1.0 h1:EByoAhC+QcYpwSZJSs/aV0uokxPwBgKxfiokSUwAknQ=
github.com/tetratelabs/wazero v1.1.0/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tetratelabs/wazero v1.8.0 h1:iEKu0d4c2Pd+QSRieYbnQC9yiFlMS9D+Jr0LsRmcF4g=
github.com/tetratelabs/wazero v1.8.0/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 9 additions & 0 deletions witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Calculator interface {
sanityCheck bool) ([]byte, error)
CalculateWTNSBin(inputs map[string]interface{},
sanityCheck bool) ([]byte, error)
Close() error
}

type calcConfig struct {
Expand Down Expand Up @@ -132,6 +133,14 @@ func (c *calc) CalculateWTNSBin(inputs map[string]interface{},
return buff.Bytes(), nil
}

func (c *calc) Close() error {
closer, ok := c.wc.(io.Closer)
if ok {
return closer.Close()
}
return nil
}

func writeInt(out io.Writer, i *big.Int, bytesLn int) error {
bs := utils.SwapEndianness(i.Bytes())
_, err := out.Write(bs)
Expand Down

0 comments on commit ed1af0a

Please sign in to comment.