Skip to content

Commit

Permalink
arbo: CheckProof now returns nil or error instead of (bool, error)
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Nov 4, 2024
1 parent 963fc4d commit 665b4f9
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 38 deletions.
9 changes: 3 additions & 6 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,12 @@ func (a *API) censusVerifyHandler(msg *apirest.APIdata, ctx *httprouter.HTTPCont
}
}

valid, err := ref.Tree().VerifyProof(leafKey, cdata.Value, cdata.CensusProof, cdata.CensusRoot)
if err != nil {
if err := ref.Tree().VerifyProof(leafKey, cdata.Value, cdata.CensusProof, cdata.CensusRoot); err != nil {
return ErrCensusProofVerificationFailed.WithErr(err)
}
if !valid {
return ctx.Send(nil, apirest.HTTPstatusBadRequest)
}

response := Census{
Valid: valid,
Valid: true,
}
var data []byte
if data, err = json.Marshal(&response); err != nil {
Expand Down
9 changes: 6 additions & 3 deletions censustree/censustree.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func (t *Tree) Get(key []byte) ([]byte, error) {
// VerifyProof verifies a census proof.
// If the census is indexed key can be nil (value provides the key already).
// If root is nil the last merkle root is used for verify.
func (t *Tree) VerifyProof(key, value, proof, root []byte) (bool, error) {
func (t *Tree) VerifyProof(key, value, proof, root []byte) error {
var err error
if root == nil {
root, err = t.Root()
if err != nil {
return false, fmt.Errorf("cannot get tree root: %w", err)
return fmt.Errorf("cannot get tree root: %w", err)
}
}
// If the provided key is longer than the defined maximum length truncate it
Expand All @@ -176,7 +176,10 @@ func (t *Tree) VerifyProof(key, value, proof, root []byte) (bool, error) {
if len(leafKey) > DefaultMaxKeyLen {
leafKey = leafKey[:DefaultMaxKeyLen]
}
return t.tree.VerifyProof(leafKey, value, proof, root)
if err := t.tree.VerifyProof(leafKey, value, proof, root); err != nil {
return err
}
return nil
}

// GenProof generates a census proof for the provided key.
Expand Down
3 changes: 1 addition & 2 deletions censustree/censustree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ func TestWeightedProof(t *testing.T) {
root, err := censusTree.Root()
qt.Assert(t, err, qt.IsNil)

verified, err := censusTree.VerifyProof(userKey, value, siblings, root)
err = censusTree.VerifyProof(userKey, value, siblings, root)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, verified, qt.IsTrue)
}

func TestGetCensusWeight(t *testing.T) {
Expand Down
14 changes: 5 additions & 9 deletions tree/arbo/addbatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,27 +998,23 @@ func TestAddKeysWithEmptyValues(t *testing.T) {
// check with empty array
root, err := tree.Root()
c.Assert(err, qt.IsNil)
verif, err := CheckProof(tree.hashFunction, keys[9], []byte{}, root, siblings)
err = CheckProof(tree.hashFunction, keys[9], []byte{}, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)

// check with array with only 1 zero
verif, err = CheckProof(tree.hashFunction, keys[9], []byte{0}, root, siblings)
err = CheckProof(tree.hashFunction, keys[9], []byte{0}, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)

// check with array with 32 zeroes
e32 := []byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
c.Assert(len(e32), qt.Equals, 32)
verif, err = CheckProof(tree.hashFunction, keys[9], e32, root, siblings)
err = CheckProof(tree.hashFunction, keys[9], e32, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)

// check with array with value!=0 returns false at verification
verif, err = CheckProof(tree.hashFunction, keys[9], []byte{0, 1}, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsFalse)
err = CheckProof(tree.hashFunction, keys[9], []byte{0, 1}, root, siblings)
c.Assert(err, qt.ErrorMatches, "calculated root doesn't match expected root")
}
10 changes: 7 additions & 3 deletions tree/arbo/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,16 @@ func bytesToBitmap(b []byte) []bool {

// CheckProof verifies the given proof. The proof verification depends on the
// HashFunction passed as parameter.
func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool, error) {
// Returns nil if the proof is valid, or an error otherwise.
func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) error {
hashes, err := CalculateProofNodes(hashFunc, k, v, packedSiblings)
if err != nil {
return false, err
return err
}
return bytes.Equal(hashes[0], root), nil
if !bytes.Equal(hashes[0], root) {
return fmt.Errorf("calculated root doesn't match expected root")
}
return nil
}

// CalculateProofNodes calculates the chain of hashes in the path of the given proof.
Expand Down
11 changes: 4 additions & 7 deletions tree/arbo/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,8 @@ func TestGenProofAndVerify(t *testing.T) {

root, err := tree.Root()
c.Assert(err, qt.IsNil)
verif, err := CheckProof(tree.hashFunction, k, v, root, siblings)
err = CheckProof(tree.hashFunction, k, v, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)
}

func TestDumpAndImportDump(t *testing.T) {
Expand Down Expand Up @@ -933,16 +932,14 @@ func TestKeyLen(t *testing.T) {

root, err := tree.Root()
c.Assert(err, qt.IsNil)
verif, err := CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
err = CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
c.Assert(err, qt.IsNil)
c.Assert(verif, qt.IsTrue)

// use a similar key but with one zero, expect that CheckProof fails on
// the verification
kAux = append(kAux, 0)
verif, err = CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
c.Assert(err, qt.IsNil)
c.Assert(verif, qt.IsFalse)
err = CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
c.Assert(err, qt.ErrorMatches, "calculated root doesn't match expected root")
}

func TestKeyLenBiggerThan32(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,13 @@ func (t *Tree) GenProof(rTx db.Reader, key []byte) ([]byte, []byte, error) {

// VerifyProof checks the proof for the given key, value and root, using the
// passed hash function
func VerifyProof(hashFunc arbo.HashFunction, key, value, proof, root []byte) (bool, error) {
func VerifyProof(hashFunc arbo.HashFunction, key, value, proof, root []byte) error {
return arbo.CheckProof(hashFunc, key, value, root, proof)
}

// VerifyProof checks the proof for the given key, value and root, using the
// hash function of the Tree
func (t *Tree) VerifyProof(key, value, proof, root []byte) (bool, error) {
func (t *Tree) VerifyProof(key, value, proof, root []byte) error {
return VerifyProof(t.tree.HashFunction(), key, value, proof, root)
}

Expand Down
3 changes: 1 addition & 2 deletions tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ func TestGenProof(t *testing.T) {
root, err := tree.Root(wTx)
qt.Assert(t, err, qt.IsNil)

verif, err := tree.VerifyProof(k, v, proof, root)
err = tree.VerifyProof(k, v, proof, root)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, verif, qt.IsTrue)

err = wTx.Commit()
qt.Assert(t, err, qt.IsNil)
Expand Down
4 changes: 2 additions & 2 deletions vochain/transaction/proofs/arboproof/arboproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (*ProofVerifierArbo) Verify(process *models.Process, envelope *models.VoteE
key = key[:censustree.DefaultMaxKeyLen]
}
}
valid, err := tree.VerifyProof(hashFunc, key, p.AvailableWeight, p.Siblings, process.CensusRoot)
if !valid || err != nil {

if err := tree.VerifyProof(hashFunc, key, p.AvailableWeight, p.Siblings, process.CensusRoot); err != nil {
return false, nil, err
}
// Legacy: support p.LeafWeight == nil, assume then value=1
Expand Down
3 changes: 1 addition & 2 deletions vochain/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func testCreateKeysAndBuildWeightedZkCensus(t *testing.T, size int, weight *big.
_, proof, err := tr.GenProof(k.Address().Bytes())
qt.Check(t, err, qt.IsNil)
proofs = append(proofs, proof)
valid, err := tr.VerifyProof(k.Address().Bytes(), encWeight, proof, root)
err = tr.VerifyProof(k.Address().Bytes(), encWeight, proof, root)
qt.Check(t, err, qt.IsNil)
qt.Check(t, valid, qt.IsTrue)
}
return keys, root, proofs
}
Expand Down

0 comments on commit 665b4f9

Please sign in to comment.