|
| 1 | +package arbo |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "slices" |
| 6 | + "testing" |
| 7 | + |
| 8 | + qt "github.com/frankban/quicktest" |
| 9 | + "go.vocdoni.io/dvote/db/metadb" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCheckProofBatch(t *testing.T) { |
| 13 | + database := metadb.NewTest(t) |
| 14 | + c := qt.New(t) |
| 15 | + |
| 16 | + keyLen := 1 |
| 17 | + maxLevels := keyLen * 8 |
| 18 | + tree, err := NewTree(Config{ |
| 19 | + Database: database, MaxLevels: maxLevels, |
| 20 | + HashFunction: HashFunctionBlake3, |
| 21 | + }) |
| 22 | + c.Assert(err, qt.IsNil) |
| 23 | + |
| 24 | + censusRoot := []byte("01234567890123456789012345678901") |
| 25 | + ballotMode := []byte("1234") |
| 26 | + |
| 27 | + err = tree.Add(BigIntToBytesLE(keyLen, big.NewInt(0x01)), censusRoot) |
| 28 | + c.Assert(err, qt.IsNil) |
| 29 | + |
| 30 | + err = tree.Add(BigIntToBytesLE(keyLen, big.NewInt(0x02)), ballotMode) |
| 31 | + c.Assert(err, qt.IsNil) |
| 32 | + |
| 33 | + var oldProofs, newProofs []*CircomVerifierProof |
| 34 | + |
| 35 | + for i := int64(0x00); i <= int64(0x04); i++ { |
| 36 | + proof, err := tree.GenerateCircomVerifierProof(BigIntToBytesLE(keyLen, big.NewInt(i))) |
| 37 | + c.Assert(err, qt.IsNil) |
| 38 | + oldProofs = append(oldProofs, proof) |
| 39 | + } |
| 40 | + |
| 41 | + censusRoot[0] = byte(0x02) |
| 42 | + ballotMode[0] = byte(0x02) |
| 43 | + |
| 44 | + err = tree.Update(BigIntToBytesLE(keyLen, big.NewInt(0x01)), censusRoot) |
| 45 | + c.Assert(err, qt.IsNil) |
| 46 | + |
| 47 | + err = tree.Update(BigIntToBytesLE(keyLen, big.NewInt(0x02)), ballotMode) |
| 48 | + c.Assert(err, qt.IsNil) |
| 49 | + |
| 50 | + err = tree.Add(BigIntToBytesLE(keyLen, big.NewInt(0x03)), ballotMode) |
| 51 | + c.Assert(err, qt.IsNil) |
| 52 | + |
| 53 | + for i := int64(0x00); i <= int64(0x04); i++ { |
| 54 | + proof, err := tree.GenerateCircomVerifierProof(BigIntToBytesLE(keyLen, big.NewInt(i))) |
| 55 | + c.Assert(err, qt.IsNil) |
| 56 | + newProofs = append(newProofs, proof) |
| 57 | + } |
| 58 | + |
| 59 | + // passing all proofs should be OK: |
| 60 | + // proof 1 + 2 + 3 are required |
| 61 | + // proof 0 and 4 are of unchanged keys, but the new siblings are explained by the other proofs |
| 62 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs, newProofs) |
| 63 | + c.Assert(err, qt.IsNil) |
| 64 | + |
| 65 | + // omitting proof 0 and 4 (unchanged keys) should also be OK |
| 66 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[1:4], newProofs[1:4]) |
| 67 | + c.Assert(err, qt.IsNil) |
| 68 | + |
| 69 | + // providing an empty batch should not pass |
| 70 | + err = CheckProofBatch(HashFunctionBlake3, []*CircomVerifierProof{}, []*CircomVerifierProof{}) |
| 71 | + c.Assert(err, qt.ErrorMatches, "empty batch") |
| 72 | + |
| 73 | + // length mismatch |
| 74 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs, newProofs[:1]) |
| 75 | + c.Assert(err, qt.ErrorMatches, "batch of proofs incomplete") |
| 76 | + |
| 77 | + // providing just proof 0 (unchanged key) should not pass since siblings can't be explained |
| 78 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[:1], newProofs[:1]) |
| 79 | + c.Assert(err, qt.ErrorMatches, ".*changed but there's no proof why.*") |
| 80 | + |
| 81 | + // providing just proof 0 (unchanged key) and an add, should fail |
| 82 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[:1], newProofs[3:4]) |
| 83 | + c.Assert(err, qt.ErrorMatches, ".*changed but there's no proof why.*") |
| 84 | + |
| 85 | + // omitting proof 3 should fail (since changed siblings in other proofs can't be explained) |
| 86 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[:3], newProofs[:3]) |
| 87 | + c.Assert(err, qt.ErrorMatches, ".*changed but there's no proof why.*") |
| 88 | + |
| 89 | + // the next 4 are mangling proofs to simulate other unexplained changes in the tree, all of these should fail |
| 90 | + badProofs := deepClone(oldProofs) |
| 91 | + badProofs[0].Root = []byte("01234567890123456789012345678900") |
| 92 | + err = CheckProofBatch(HashFunctionBlake3, badProofs, newProofs) |
| 93 | + c.Assert(err, qt.ErrorMatches, "old proof invalid: root doesn't match") |
| 94 | + |
| 95 | + badProofs = deepClone(oldProofs) |
| 96 | + badProofs[0].Siblings[0] = []byte("01234567890123456789012345678900") |
| 97 | + err = CheckProofBatch(HashFunctionBlake3, badProofs, newProofs) |
| 98 | + c.Assert(err, qt.ErrorMatches, "old proof invalid: root doesn't match") |
| 99 | + |
| 100 | + badProofs = deepClone(newProofs) |
| 101 | + badProofs[0].Root = []byte("01234567890123456789012345678900") |
| 102 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs, badProofs) |
| 103 | + c.Assert(err, qt.ErrorMatches, "new proof invalid: root doesn't match") |
| 104 | + |
| 105 | + badProofs = deepClone(newProofs) |
| 106 | + badProofs[0].Siblings[0] = []byte("01234567890123456789012345678900") |
| 107 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs, badProofs) |
| 108 | + c.Assert(err, qt.ErrorMatches, "new proof invalid: root doesn't match") |
| 109 | + |
| 110 | + // also test exclusion proofs: |
| 111 | + // exclusion proof of key 0x04 can't be used to prove exclusion of 0x01, 0x03 or 0x05 obviously |
| 112 | + badProofs = deepClone(oldProofs) |
| 113 | + badProofs[4].Key = []byte{0x01} |
| 114 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 115 | + c.Assert(err, qt.ErrorMatches, "new proof invalid: root doesn't match") |
| 116 | + badProofs[4].Key = []byte{0x03} |
| 117 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 118 | + c.Assert(err, qt.ErrorMatches, "new proof invalid: root doesn't match") |
| 119 | + badProofs[4].Key = []byte{0x05} |
| 120 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 121 | + c.Assert(err, qt.ErrorMatches, "new proof invalid: root doesn't match") |
| 122 | + // also can't prove key 0x02 exclusion (since that leaf exists and is indeed the starting point of the proof) |
| 123 | + badProofs[4].Key = []byte{0x02} |
| 124 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 125 | + c.Assert(err, qt.ErrorMatches, "new proof invalid: exclusion proof invalid, key and oldKey are equal") |
| 126 | + // but exclusion proof of key 0x04 can also prove exclusion of the whole prefix (0x00, 0x08, 0x0c, 0x10, etc) |
| 127 | + badProofs[4].Key = []byte{0x00} |
| 128 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 129 | + c.Assert(err, qt.IsNil) |
| 130 | + badProofs[4].Key = []byte{0x08} |
| 131 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 132 | + c.Assert(err, qt.IsNil) |
| 133 | + badProofs[4].Key = []byte{0x0c} |
| 134 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 135 | + c.Assert(err, qt.IsNil) |
| 136 | + badProofs[4].Key = []byte{0x10} |
| 137 | + err = CheckProofBatch(HashFunctionBlake3, oldProofs[4:], badProofs[4:]) |
| 138 | + c.Assert(err, qt.IsNil) |
| 139 | +} |
| 140 | + |
| 141 | +func deepClone(src []*CircomVerifierProof) []*CircomVerifierProof { |
| 142 | + dst := slices.Clone(src) |
| 143 | + for i := range src { |
| 144 | + proof := *src[i] |
| 145 | + dst[i] = &proof |
| 146 | + |
| 147 | + dst[i].Siblings = slices.Clone(src[i].Siblings) |
| 148 | + } |
| 149 | + return dst |
| 150 | +} |
0 commit comments