-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_commit_test.go
120 lines (99 loc) · 2.92 KB
/
value_commit_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package fastcommit
import (
"crypto/rand"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/stretchr/testify/assert"
"testing"
)
var dataCase = rand4096data()
func rand4096data() []Account {
ret := make([]Account, 4096)
for i := uint64(0); i < 4096; i++ {
seed := make([]byte, 32)
rand.Read(seed)
ret[i] = Account{
//key: fr.NewElement(i),
state: *new(fr.Element).SetBytes(seed),
}
}
return ret
}
func TestValueCommit_Update(t *testing.T) {
fc := NewContext(dataCase)
seed := make([]byte, 32)
rand.Read(seed)
newData := Account{
// key: fr.NewElement(3),
state: *new(fr.Element).SetBytes(seed),
}
err := fc.Update(3, newData.state)
assert.Equal(t, nil, err)
dataCase[3] = newData
fc2 := NewContext(dataCase)
if !fc.commit.Equal(&fc2.commit) {
t.Fatalf("commit expect: %v, get: %v", fc2.commit, fc.commit)
}
}
//func TestValueCommit_Insert(t *testing.T) {
// dataCase1 := dataCase[:4095]
// fc := NewContext(dataCase1)
//
// err := fc.Insert(dataCase[4095].key, dataCase[4095].state)
// assert.Equal(t, nil, err)
//
// fc2 := NewContext(dataCase)
// if !fc.commit.Equal(&fc2.commit) {
// t.Fatalf("commit expect: %v, get: %v", fc2.commit, fc.commit)
// }
//}
func TestValueCommit_BatchUpdate(t *testing.T) {
fc := NewContext(dataCase)
seed1 := make([]byte, 32)
rand.Read(seed1)
seed2 := make([]byte, 32)
rand.Read(seed2)
seed3 := make([]byte, 32)
rand.Read(seed3)
//keys := []fr.Element{fr.NewElement(1), fr.NewElement(2), fr.NewElement(3)}
values := []fr.Element{*new(fr.Element).SetBytes(seed1), *new(fr.Element).SetBytes(seed2), *new(fr.Element).SetBytes(seed3)}
err := fc.BatchUpdate([]int{1, 2, 3}, values)
assert.Equal(t, nil, err)
dataCase[1] = Account{*new(fr.Element).SetBytes(seed1)}
dataCase[2] = Account{*new(fr.Element).SetBytes(seed2)}
dataCase[3] = Account{*new(fr.Element).SetBytes(seed3)}
fc2 := NewContext(dataCase)
if !fc.commit.Equal(&fc2.commit) {
t.Fatalf("commit expect: %v, get: %v", fc2.commit, fc.commit)
}
}
func TestValueCommit_Proof_Verify(t *testing.T) {
fc := NewContext(dataCase)
proof, err := fc.Proof()
assert.Equal(t, nil, err)
err = fc.Verify(proof)
assert.Equal(t, nil, err)
}
func TestValueCommit_ProofForKey_VerifyForKey(t *testing.T) {
fc := NewContext(dataCase)
// random a key
p := fr.NewElement(9)
proof, err := fc.ProofForVal(p)
assert.Equal(t, nil, err)
output, err := domains.EvaluateLagrangePolynomial(fc.values, p)
assert.Equal(t, nil, err)
err = fc.VerifyForVal(p, *output, proof)
assert.Equal(t, nil, err)
k2 := domains.Roots[4000]
proof, err = fc.ProofForVal(k2)
assert.Equal(t, nil, err)
output, err = domains.EvaluateLagrangePolynomial(fc.values, k2)
assert.Equal(t, nil, err)
assert.Equal(t, fc.values[4000], *output)
err = fc.VerifyForVal(k2, *output, proof)
assert.Equal(t, nil, err)
}
func TestBranchs(t *testing.T) {
for i := uint64(0); i < 100000; i++ {
Updates(int(i), *new(fr.Element).SetUint64(i))
}
}