Skip to content

Commit 98fff29

Browse files
committed
arbo: CheckProof now returns nil or error instead of (bool, error)
1 parent c1ce6d7 commit 98fff29

10 files changed

Lines changed: 37 additions & 40 deletions

File tree

api/censuses.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -945,15 +945,15 @@ func (a *API) censusVerifyHandler(msg *apirest.APIdata, ctx *httprouter.HTTPCont
945945
}
946946
}
947947

948-
valid, err := ref.Tree().VerifyProof(leafKey, cdata.Value, cdata.CensusProof, cdata.CensusRoot)
949-
if err != nil {
948+
if err := ref.Tree().VerifyProof(leafKey, cdata.Value, cdata.CensusProof, cdata.CensusRoot); err != nil {
949+
if strings.Contains(err.Error(), "calculated vs expected root mismatch") {
950+
return ctx.Send(nil, apirest.HTTPstatusBadRequest)
951+
}
950952
return ErrCensusProofVerificationFailed.WithErr(err)
951953
}
952-
if !valid {
953-
return ctx.Send(nil, apirest.HTTPstatusBadRequest)
954-
}
954+
955955
response := Census{
956-
Valid: valid,
956+
Valid: true,
957957
}
958958
var data []byte
959959
if data, err = json.Marshal(&response); err != nil {

censustree/censustree.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ func (t *Tree) Get(key []byte) ([]byte, error) {
162162
// VerifyProof verifies a census proof.
163163
// If the census is indexed key can be nil (value provides the key already).
164164
// If root is nil the last merkle root is used for verify.
165-
func (t *Tree) VerifyProof(key, value, proof, root []byte) (bool, error) {
165+
func (t *Tree) VerifyProof(key, value, proof, root []byte) error {
166166
var err error
167167
if root == nil {
168168
root, err = t.Root()
169169
if err != nil {
170-
return false, fmt.Errorf("cannot get tree root: %w", err)
170+
return fmt.Errorf("cannot get tree root: %w", err)
171171
}
172172
}
173173
// If the provided key is longer than the defined maximum length truncate it
@@ -176,7 +176,10 @@ func (t *Tree) VerifyProof(key, value, proof, root []byte) (bool, error) {
176176
if len(leafKey) > DefaultMaxKeyLen {
177177
leafKey = leafKey[:DefaultMaxKeyLen]
178178
}
179-
return t.tree.VerifyProof(leafKey, value, proof, root)
179+
if err := t.tree.VerifyProof(leafKey, value, proof, root); err != nil {
180+
return err
181+
}
182+
return nil
180183
}
181184

182185
// GenProof generates a census proof for the provided key.

censustree/censustree_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ func TestWeightedProof(t *testing.T) {
110110
root, err := censusTree.Root()
111111
qt.Assert(t, err, qt.IsNil)
112112

113-
verified, err := censusTree.VerifyProof(userKey, value, siblings, root)
113+
err = censusTree.VerifyProof(userKey, value, siblings, root)
114114
qt.Assert(t, err, qt.IsNil)
115-
qt.Assert(t, verified, qt.IsTrue)
116115
}
117116

118117
func TestGetCensusWeight(t *testing.T) {

tree/arbo/addbatch_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -998,27 +998,23 @@ func TestAddKeysWithEmptyValues(t *testing.T) {
998998
// check with empty array
999999
root, err := tree.Root()
10001000
c.Assert(err, qt.IsNil)
1001-
verif, err := CheckProof(tree.hashFunction, keys[9], []byte{}, root, siblings)
1001+
err = CheckProof(tree.hashFunction, keys[9], []byte{}, root, siblings)
10021002
c.Assert(err, qt.IsNil)
1003-
c.Check(verif, qt.IsTrue)
10041003

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

10101008
// check with array with 32 zeroes
10111009
e32 := []byte{
10121010
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10131011
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10141012
}
10151013
c.Assert(len(e32), qt.Equals, 32)
1016-
verif, err = CheckProof(tree.hashFunction, keys[9], e32, root, siblings)
1014+
err = CheckProof(tree.hashFunction, keys[9], e32, root, siblings)
10171015
c.Assert(err, qt.IsNil)
1018-
c.Check(verif, qt.IsTrue)
10191016

10201017
// check with array with value!=0 returns false at verification
1021-
verif, err = CheckProof(tree.hashFunction, keys[9], []byte{0, 1}, root, siblings)
1022-
c.Assert(err, qt.IsNil)
1023-
c.Check(verif, qt.IsFalse)
1018+
err = CheckProof(tree.hashFunction, keys[9], []byte{0, 1}, root, siblings)
1019+
c.Assert(err, qt.ErrorMatches, "calculated vs expected root mismatch")
10241020
}

tree/arbo/proof.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,19 @@ func bytesToBitmap(b []byte) []bool {
159159

160160
// CheckProof verifies the given proof. The proof verification depends on the
161161
// HashFunction passed as parameter.
162-
func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool, error) {
162+
// Returns nil if the proof is valid, or an error otherwise.
163+
func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) error {
163164
siblings, err := UnpackSiblings(hashFunc, packedSiblings)
164165
if err != nil {
165-
return false, err
166+
return err
166167
}
167168

168169
keyPath := make([]byte, int(math.Ceil(float64(len(siblings))/float64(8))))
169170
copy(keyPath, k)
170171

171172
key, _, err := newLeafValue(hashFunc, k, v)
172173
if err != nil {
173-
return false, err
174+
return err
174175
}
175176

176177
path := getPath(len(siblings), keyPath)
@@ -181,8 +182,11 @@ func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool,
181182
key, _, err = newIntermediate(hashFunc, key, sibling)
182183
}
183184
if err != nil {
184-
return false, err
185+
return err
185186
}
186187
}
187-
return bytes.Equal(key, root), nil
188+
if !bytes.Equal(key, root) {
189+
return fmt.Errorf("calculated vs expected root mismatch")
190+
}
191+
return nil
188192
}

tree/arbo/tree_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,8 @@ func TestGenProofAndVerify(t *testing.T) {
539539

540540
root, err := tree.Root()
541541
c.Assert(err, qt.IsNil)
542-
verif, err := CheckProof(tree.hashFunction, k, v, root, siblings)
542+
err = CheckProof(tree.hashFunction, k, v, root, siblings)
543543
c.Assert(err, qt.IsNil)
544-
c.Check(verif, qt.IsTrue)
545544
}
546545

547546
func TestDumpAndImportDump(t *testing.T) {
@@ -933,16 +932,14 @@ func TestKeyLen(t *testing.T) {
933932

934933
root, err := tree.Root()
935934
c.Assert(err, qt.IsNil)
936-
verif, err := CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
935+
err = CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
937936
c.Assert(err, qt.IsNil)
938-
c.Assert(verif, qt.IsTrue)
939937

940938
// use a similar key but with one zero, expect that CheckProof fails on
941939
// the verification
942940
kAux = append(kAux, 0)
943-
verif, err = CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
944-
c.Assert(err, qt.IsNil)
945-
c.Assert(verif, qt.IsFalse)
941+
err = CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
942+
c.Assert(err, qt.ErrorMatches, "calculated vs expected root mismatch")
946943
}
947944

948945
func TestKeyLenBiggerThan32(t *testing.T) {

tree/tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,13 @@ func (t *Tree) GenProof(rTx db.Reader, key []byte) ([]byte, []byte, error) {
229229

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

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

tree/tree_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ func TestGenProof(t *testing.T) {
8484
root, err := tree.Root(wTx)
8585
qt.Assert(t, err, qt.IsNil)
8686

87-
verif, err := tree.VerifyProof(k, v, proof, root)
87+
err = tree.VerifyProof(k, v, proof, root)
8888
qt.Assert(t, err, qt.IsNil)
89-
qt.Assert(t, verif, qt.IsTrue)
9089

9190
err = wTx.Commit()
9291
qt.Assert(t, err, qt.IsNil)

vochain/transaction/proofs/arboproof/arboproof.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func (*ProofVerifierArbo) Verify(process *models.Process, envelope *models.VoteE
5252
key = key[:censustree.DefaultMaxKeyLen]
5353
}
5454
}
55-
valid, err := tree.VerifyProof(hashFunc, key, p.AvailableWeight, p.Siblings, process.CensusRoot)
56-
if !valid || err != nil {
55+
56+
if err := tree.VerifyProof(hashFunc, key, p.AvailableWeight, p.Siblings, process.CensusRoot); err != nil {
5757
return false, nil, err
5858
}
5959
// Legacy: support p.LeafWeight == nil, assume then value=1

vochain/vote_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ func testCreateKeysAndBuildWeightedZkCensus(t *testing.T, size int, weight *big.
4646
_, proof, err := tr.GenProof(k.Address().Bytes())
4747
qt.Check(t, err, qt.IsNil)
4848
proofs = append(proofs, proof)
49-
valid, err := tr.VerifyProof(k.Address().Bytes(), encWeight, proof, root)
49+
err = tr.VerifyProof(k.Address().Bytes(), encWeight, proof, root)
5050
qt.Check(t, err, qt.IsNil)
51-
qt.Check(t, valid, qt.IsTrue)
5251
}
5352
return keys, root, proofs
5453
}

0 commit comments

Comments
 (0)