-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproof_test.go
75 lines (61 loc) · 1.59 KB
/
proof_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package merkle
import (
"math/rand"
"testing"
"github.com/si-co/vpir-code/lib/utils"
"github.com/stretchr/testify/require"
)
func TestEncodeDecodeProof(t *testing.T) {
rng := utils.RandomPRG()
data := make([][]byte, rand.Intn(501))
for i := range data {
d := make([]byte, 32)
rng.Read(d)
data[i] = d
}
// create the tree
tree, err := New(data)
require.NoError(t, err)
// generate a proof for random element
proof, err := tree.GenerateProof(data[rand.Intn(len(data))])
require.NoError(t, err)
// encode the proof
b := EncodeProof(proof)
// decode proof
p := DecodeProof(b)
require.Equal(t, *proof, *p)
}
// Test Proof encoding, decoding and verification of each data item
// Thanks to Laura Hetz for this additional test
// and for pointing out collisions in the Merkle proofs generation
// Issue: https://github.com/dedis/apir-code/issues/17
func TestProofVerification(t *testing.T) {
rng := utils.RandomPRG()
numRecords := 1000000
data := make([][]byte, numRecords)
for i := range data {
d := make([]byte, 32)
rng.Read(d)
data[i] = d
}
// create the tree
tree, err := New(data)
require.NoError(t, err)
// generate a proof for EVERY element
for i := range data {
proof, err := tree.GenerateProof(data[i])
require.NoError(t, err)
// Check Encoding for each element
// encode the proof
b := EncodeProof(proof)
// decode proof
p := DecodeProof(b)
require.Equal(t, *proof, *p)
// check if proof verifies
vrf, err := VerifyProof(data[i], proof, tree.Root())
require.NoError(t, err)
if !vrf {
t.Fatal("Proof with index ", i, " did not verify")
}
}
}