-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
arbo: add CheckProofBatch and CalculateProofNodes
- Loading branch information
Showing
3 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package arbo | ||
|
||
import ( | ||
"math/big" | ||
"slices" | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
"go.vocdoni.io/dvote/db/metadb" | ||
) | ||
|
||
func TestCheckProofBatch(t *testing.T) { | ||
database := metadb.NewTest(t) | ||
c := qt.New(t) | ||
|
||
keyLen := 1 | ||
maxLevels := keyLen * 8 | ||
tree, err := NewTree(Config{ | ||
Database: database, MaxLevels: maxLevels, | ||
HashFunction: HashFunctionBlake3, | ||
}) | ||
c.Assert(err, qt.IsNil) | ||
|
||
processID := []byte("01234567890123456789012345678900") | ||
censusRoot := []byte("01234567890123456789012345678901") | ||
ballotMode := []byte("1234") | ||
|
||
err = tree.Add(BigIntToBytesLE(keyLen, big.NewInt(0x00)), processID) | ||
c.Assert(err, qt.IsNil) | ||
|
||
err = tree.Add(BigIntToBytesLE(keyLen, big.NewInt(0x01)), censusRoot) | ||
c.Assert(err, qt.IsNil) | ||
|
||
err = tree.Add(BigIntToBytesLE(keyLen, big.NewInt(0x02)), ballotMode) | ||
c.Assert(err, qt.IsNil) | ||
|
||
var oldProofs, newProofs []*CircomVerifierProof | ||
|
||
for i := int64(0x00); i <= int64(0x02); i++ { | ||
proof, err := tree.GenerateCircomVerifierProof(BigIntToBytesLE(keyLen, big.NewInt(i))) | ||
c.Assert(err, qt.IsNil) | ||
oldProofs = append(oldProofs, proof) | ||
} | ||
|
||
censusRoot[0] = byte(0x02) | ||
ballotMode[0] = byte(0x02) | ||
|
||
err = tree.Update(BigIntToBytesLE(keyLen, big.NewInt(0x01)), censusRoot) | ||
c.Assert(err, qt.IsNil) | ||
|
||
err = tree.Update(BigIntToBytesLE(keyLen, big.NewInt(0x02)), ballotMode) | ||
c.Assert(err, qt.IsNil) | ||
|
||
for i := int64(0x00); i <= int64(0x02); i++ { | ||
proof, err := tree.GenerateCircomVerifierProof(BigIntToBytesLE(keyLen, big.NewInt(i))) | ||
c.Assert(err, qt.IsNil) | ||
newProofs = append(newProofs, proof) | ||
} | ||
|
||
// this mix should pass: proof 0 is unchanged, proof 1 + 2 verify together | ||
err = CheckProofBatch(HashFunctionBlake3, oldProofs, newProofs) | ||
c.Assert(err, qt.IsNil) | ||
|
||
// omitting proof 0 (unchanged) should also pass | ||
err = CheckProofBatch(HashFunctionBlake3, oldProofs[1:], newProofs[1:]) | ||
c.Assert(err, qt.IsNil) | ||
|
||
// providing just proof 0 (unchanged) should also pass | ||
err = CheckProofBatch(HashFunctionBlake3, oldProofs[:0], newProofs[:0]) | ||
c.Assert(err, qt.IsNil) | ||
|
||
// length mismatch | ||
err = CheckProofBatch(HashFunctionBlake3, oldProofs, newProofs[:1]) | ||
c.Assert(err, qt.ErrorMatches, "batch of proofs incomplete") | ||
|
||
// omitting proof 2 should fail (since changed siblings in proof 1 can't be explained) | ||
err = CheckProofBatch(HashFunctionBlake3, oldProofs[:1], newProofs[:1]) | ||
c.Assert(err, qt.ErrorMatches, ".*changed but there's no proof why.*") | ||
|
||
// the rest is mangling proofs to simulate other unexplained changes in the tree, all of these should fail | ||
badProofs := deepClone(oldProofs) | ||
badProofs[0].Root = []byte("01234567890123456789012345678900") | ||
err = CheckProofBatch(HashFunctionBlake3, badProofs, newProofs) | ||
c.Assert(err, qt.ErrorMatches, "old proof invalid: calculated root doesn't match expected root") | ||
|
||
badProofs = deepClone(oldProofs) | ||
badProofs[0].Siblings[0] = []byte("01234567890123456789012345678900") | ||
err = CheckProofBatch(HashFunctionBlake3, badProofs, newProofs) | ||
c.Assert(err, qt.ErrorMatches, "old proof invalid: calculated root doesn't match expected root") | ||
|
||
badProofs = deepClone(newProofs) | ||
badProofs[0].Root = []byte("01234567890123456789012345678900") | ||
err = CheckProofBatch(HashFunctionBlake3, oldProofs, badProofs) | ||
c.Assert(err, qt.ErrorMatches, "new proof invalid: root doesn't match") | ||
|
||
badProofs = deepClone(newProofs) | ||
badProofs[0].Siblings[0] = []byte("01234567890123456789012345678900") | ||
err = CheckProofBatch(HashFunctionBlake3, oldProofs, badProofs) | ||
c.Assert(err, qt.ErrorMatches, "new proof invalid: root doesn't match") | ||
} | ||
|
||
func deepClone(src []*CircomVerifierProof) []*CircomVerifierProof { | ||
dst := slices.Clone(src) | ||
for i := range src { | ||
proof := *src[i] | ||
dst[i] = &proof | ||
|
||
dst[i].Siblings = slices.Clone(src[i].Siblings) | ||
} | ||
return dst | ||
} |