-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_commit.go
164 lines (137 loc) · 3.93 KB
/
value_commit.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package fastcommit
import (
"errors"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr/kzg"
"math/big"
//crateKzg "github.com/crate-crypto/go-kzg-4844/internal/kzg"
crateKzg "github/yyjia/fastcommit/crateKzg/kzg"
)
const POLY_SIZE = 4096
var (
ErrFullSize = errors.New("array size is full")
ErrMissKey = errors.New("miss key")
)
type Account struct {
//key fr.Element
state fr.Element
}
type ValueCommit struct {
commit bls12381.G1Affine
//keys map[fr.Element]int
values []fr.Element
//size int
}
var branchs []ValueCommit
func Updates(indexs int, stat fr.Element) error {
blob := indexs / POLY_SIZE
idx := indexs % POLY_SIZE
if blob >= len(branchs) {
vals := make([]fr.Element, POLY_SIZE)
c, err := kzg.Commit(vals, srs.Pk, 0)
if nil != err {
return err
}
branchs = append(branchs, ValueCommit{
values: vals,
commit: c,
})
}
branchs[blob].Update(idx, stat)
return nil
}
func NewContext(data []Account) *ValueCommit {
//keyInd := make(map[fr.Element]int, POLY_SIZE)
vals := make([]fr.Element, POLY_SIZE)
for i := 0; i < len(data); i++ {
//keyInd[data[i].key] = i
vals[i] = data[i].state
}
c, err := kzg.Commit(vals, srs.Pk, 0)
if nil != err {
panic(err)
}
return &ValueCommit{
//keys: keyInd,
values: vals,
//size: len(data),
commit: c,
}
}
func (s *ValueCommit) Update(index int, v fr.Element) error {
//if _, ok := s.keys[k]; !ok {
// return ErrMissKey
//}
//index := s.keys[k]
//sub := new(fr.Element).Sub(&v, &s.values[index])
//bInt := new(big.Int)
//sub.BigInt(bInt)
bInt := new(fr.Element).Sub(&v, &s.values[index]).BigInt(new(big.Int))
addC := new(bls12381.G1Affine).ScalarMultiplication(&srs.Pk.G1[index], bInt)
s.commit = *new(bls12381.G1Affine).Add(&s.commit, addC)
s.values[index] = v
return nil
}
func (s *ValueCommit) BatchUpdate(indexs []int, vals []fr.Element) error {
//if len(keys) != len(vals) {
// return fmt.Errorf("the length keys should equal vals")
//}
for i := 0; i < len(indexs); i++ {
if err := s.Update(indexs[i], vals[i]); nil != err {
return err
}
}
return nil
}
//func (s *ValueCommit) Insert(k, v fr.Element) error {
// if s.size >= 4096 {
// return ErrFullSize
// }
// s.values[s.size] = v
// s.keys[k] = s.size
//
// bInt := new(big.Int)
// v.BigInt(bInt)
// addC := new(bls12381.G1Affine).ScalarMultiplication(&srs.Pk.G1[s.size], bInt)
// s.commit = *new(bls12381.G1Affine).Add(&s.commit, addC)
// s.size++
// return nil
//}
func (s *ValueCommit) Proof() (bls12381.G1Affine, error) {
evaluationChallenge := computeChallenge(s.values, s.commit)
openingProof, err := crateKzg.Open(domains, s.values, evaluationChallenge, &crateKzg.CommitKey{G1: srs.Pk.G1}, 0)
if err != nil {
return bls12381.G1Affine{}, err
}
return openingProof.QuotientCommitment, nil
}
func (s *ValueCommit) ProofForVal(evaluation fr.Element) (bls12381.G1Affine, error) {
openingProof, err := crateKzg.Open(domains, s.values, evaluation, &crateKzg.CommitKey{G1: srs.Pk.G1}, 0)
if nil != err {
return bls12381.G1Affine{}, err
}
return openingProof.QuotientCommitment, nil
}
func (s *ValueCommit) ProofForVals(keys []fr.Element) (bls12381.G1Affine, error) {
return bls12381.G1Affine{}, nil
}
func (s *ValueCommit) Verify(proof bls12381.G1Affine) error {
evaluationChallenge := computeChallenge(s.values, s.commit)
outputPoint, err := domains.EvaluateLagrangePolynomial(s.values, evaluationChallenge)
if nil != err {
return err
}
return crateKzg.Verify(&s.commit, &crateKzg.OpeningProof{proof, evaluationChallenge, *outputPoint}, &crateKzg.OpeningKey{
srs.Vk.G1,
srs.Vk.G2[0],
srs.Vk.G2[1],
})
}
func (s *ValueCommit) VerifyForVal(evaluation, output fr.Element, proof bls12381.G1Affine) error {
return crateKzg.Verify(&s.commit, &crateKzg.OpeningProof{proof, evaluation, output}, &crateKzg.OpeningKey{
srs.Vk.G1,
srs.Vk.G2[0],
srs.Vk.G2[1],
})
}