Skip to content

Commit

Permalink
arbo implementation improved to remove hint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Nov 5, 2024
1 parent 10cc1f9 commit fd2233a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 391 deletions.
25 changes: 0 additions & 25 deletions arbo/hints.go

This file was deleted.

29 changes: 0 additions & 29 deletions arbo/hints_test.go

This file was deleted.

20 changes: 14 additions & 6 deletions arbo/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package arbo

import (
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/math/cmp"
"github.com/vocdoni/gnark-crypto-primitives/poseidon"
)

Expand Down Expand Up @@ -50,18 +51,25 @@ func calcRoot(api frontend.API, current frontend.Variable, path, valid, siblings
return calcRoot(api, newCurrent, path[:i], valid[:i], siblings[:i])
}

// validSiblings function creates a binary map with the slots where a valid
// sibling is located in the siblings list. This function helps to skip
// unnecessary iterations when walking through the merkle tree.
func validSiblings(api frontend.API, siblings []frontend.Variable, nsibling frontend.Variable) []frontend.Variable {
valid := make([]frontend.Variable, len(siblings))
for i := 0; i < len(siblings); i++ {
valid[i] = cmp.IsLess(api, frontend.Variable(i), nsibling)
}
return valid
}

// CheckProof receives the parameters of a proof of Arbo to recalculate the
// root with them and compare it with the provided one, verifiying the proof.
func CheckProof(api frontend.API, key, value, root, nsiblings frontend.Variable, siblings []frontend.Variable) error {
// ensure that the number of valid siblings are less or equal to the number
// of provided siblings
api.AssertIsLessOrEqual(nsiblings, len(siblings))
// call to ValidSiblings that creates a map of valid siblings
valid, err := api.NewHint(ValidSiblings, len(siblings), nsiblings)
if err != nil {
return err
}

// get a map with the valid siblings
valid := validSiblings(api, siblings, nsiblings)
// calculta the path from the provided key to decide which leaf is the
// correct one in every level of the tree
path := api.ToBinary(key, api.Compiler().FieldBitLen())
Expand Down
15 changes: 6 additions & 9 deletions arbo/verifier_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package arbo

import (
"encoding/hex"
"math/big"
"testing"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test"
qt "github.com/frankban/quicktest"
Expand Down Expand Up @@ -41,15 +39,14 @@ func successInputs(t *testing.T, n int) testVerifierCircuit {
})
c.Assert(err, qt.IsNil)

key, err := hex.DecodeString("2a4636A5a1138e35F7f93e81FA56d3c970BC6777")
c.Assert(err, qt.IsNil)
key := util.BigToFF(new(big.Int).SetBytes(util.RandomBytes(20))).Bytes()
value := big.NewInt(10)

err = tree.Add(key, value.Bytes())
c.Assert(err, qt.IsNil)

for i := 1; i < n; i++ {
err = tree.Add(util.RandomBytes(20), value.Bytes())
err = tree.Add(util.BigToFF(new(big.Int).SetBytes(util.RandomBytes(20))).Bytes(), value.Bytes())
c.Assert(err, qt.IsNil)
}

Expand All @@ -65,7 +62,7 @@ func successInputs(t *testing.T, n int) testVerifierCircuit {
siblings := [160]frontend.Variable{}
for i := 0; i < 160; i++ {
if i < len(uSiblings) {
siblings[i] = arbo.BytesToBigInt(uSiblings[i])
siblings[i] = arbo.BytesLEToBigInt(uSiblings[i])
} else {
siblings[i] = big.NewInt(0)
}
Expand All @@ -74,8 +71,8 @@ func successInputs(t *testing.T, n int) testVerifierCircuit {
root, err := tree.Root()
c.Assert(err, qt.IsNil)
return testVerifierCircuit{
Root: arbo.BytesToBigInt(root),
Key: arbo.BytesToBigInt(key),
Root: arbo.BytesLEToBigInt(root),
Key: arbo.BytesLEToBigInt(key),
Value: value,
Siblings: siblings,
NSiblings: new(big.Int).SetInt64(int64(len(uSiblings))),
Expand All @@ -86,5 +83,5 @@ func TestVerifier(t *testing.T) {
assert := test.NewAssert(t)

inputs := successInputs(t, 10)
assert.SolvingSucceeded(&testVerifierCircuit{}, &inputs, test.WithCurves(ecc.BN254), test.WithBackends(backend.GROTH16), test.WithSolverOpts(solver.WithHints(ValidSiblings)))
assert.SolvingSucceeded(&testVerifierCircuit{}, &inputs, test.WithCurves(ecc.BN254), test.WithBackends(backend.GROTH16))
}
34 changes: 18 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
module github.com/vocdoni/gnark-crypto-primitives

go 1.22
go 1.23.0

toolchain go1.22.4
toolchain go1.23.2

require (
github.com/consensys/gnark v0.11.0
github.com/consensys/gnark-crypto v0.14.0
github.com/frankban/quicktest v1.14.6
github.com/iden3/go-iden3-crypto v0.0.17
go.vocdoni.io/dvote v1.10.1
go.vocdoni.io/dvote v1.10.2-0.20241024102542-c1ce6d744bc5
)

require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.14.3 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593 // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/cockroachdb/pebble v1.1.2 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.22 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/glendc/go-external-ip v0.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 // indirect
github.com/ingonyama-zk/icicle v1.1.0 // indirect
github.com/ingonyama-zk/iciclegnark v0.1.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/ronanh/intcomp v1.1.0 // indirect
github.com/rs/zerolog v1.33.0 // indirect
Expand All @@ -58,7 +59,8 @@ require (
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit fd2233a

Please sign in to comment.