/
util.go
183 lines (165 loc) · 4.66 KB
/
util.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2018 ING Bank N.V.
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package zkproofs
import (
"crypto/sha256"
"encoding/json"
"math/big"
"../byteconversion"
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/google"
// "../crypto/bn256"
)
//Constants that are going to be used frequently, then we just need to compute them once.
var (
G1 = new(bn256.G1).ScalarBaseMult(new(big.Int).SetInt64(1))
G2 = new(bn256.G2).ScalarBaseMult(new(big.Int).SetInt64(1))
E = bn256.Pair(G1, G2)
)
/*
Decompose receives as input a bigint x and outputs an array of integers such that
x = sum(xi.u^i), i.e. it returns the decomposition of x into base u.
*/
func Decompose(x *big.Int, u int64, l int64) ([]int64, error) {
var (
result []int64
i int64
)
result = make([]int64, l, l)
i = 0
for i < l {
result[i] = Mod(x, new(big.Int).SetInt64(u)).Int64()
x = new(big.Int).Div(x, new(big.Int).SetInt64(u))
i = i + 1
}
return result, nil
}
/*
Commit method corresponds to the Pedersen commitment scheme. Namely, given input
message x, and randomness r, it outputs g^x.h^r.
*/
func Commit(x, r *big.Int, h *bn256.G2) (*bn256.G2, error) {
var (
C *bn256.G2
)
C = new(bn256.G2).ScalarBaseMult(x)
C.Add(C, new(bn256.G2).ScalarMult(h, r))
return C, nil
}
/*
CommitG1 method corresponds to the Pedersen commitment scheme. Namely, given input
message x, and randomness r, it outputs g^x.h^r.
*/
func CommitG1(x, r *big.Int, h *p256) (*p256, error) {
var (
C *p256
)
C = new(p256).ScalarBaseMult(x)
Hr := new(p256).ScalarMult(h, r)
C.Add(C, Hr)
return C, nil
}
func Mult(a *p256, n *big.Int) *p256 {
return new(p256).ScalarMult(a, n)
}
/*
HashSet is responsible for the computing a Zp element given elements from GT and G2.
*/
func HashSet(a *bn256.GT, D *bn256.G2) (*big.Int, error) {
digest := sha256.New()
digest.Write([]byte(a.String()))
digest.Write([]byte(D.String()))
output := digest.Sum(nil)
tmp := output[0:len(output)]
return byteconversion.FromByteArray(tmp)
}
/*
Hash is responsible for the computing a Zp element given elements from GT and G2.
*/
func Hash(a []*bn256.GT, D *bn256.G2) (*big.Int, error) {
digest := sha256.New()
for i := range a {
digest.Write([]byte(a[i].String()))
}
digest.Write([]byte(D.String()))
output := digest.Sum(nil)
tmp := output[0:len(output)]
return byteconversion.FromByteArray(tmp)
}
/*
Read big integer in base 10 from string.
*/
func GetBigInt(value string) *big.Int {
i := new(big.Int)
i.SetString(value, 10)
return i
}
/*
Get common base
*/
func GetZkrp() *Bp {
var zkrp Bp
zkrp.Setup(0, 4294967296)
return &zkrp
}
/*
Get zkrp verifier
*/
func GetVerifier(t *big.Int, h []*p256, p *p256) *Bp {
zkrp := GetZkrp()
zkrp.Zkip.Cc = t
zkrp.Zkip.Hh = h
zkrp.Zkip.P = p
return zkrp
}
/*
Pedersen Commitment verification, check input_sum == output_sum ?
*/
func VerifyPedersenCommitment(input, output []*PedersenCommitment, blindDiff *big.Int) bool {
inputCommitment := new(PedersenCommitment)
outputCommitment := new(PedersenCommitment)
for _, p := range input {
inputCommitment = inputCommitment.Add(inputCommitment, p)
}
for _, p := range output {
outputCommitment = outputCommitment.Add(outputCommitment, p)
}
// 计算佩德森承诺输入输出之差
diffCommitment := inputCommitment.Add(inputCommitment, outputCommitment.Neg(outputCommitment))
// 根据盲因子计算理论值
H, _ := MapToGroup(SEEDH)
checkCommitment := Mult(H, blindDiff)
// 比较是否相等
return checkCommitment.X.Cmp(diffCommitment.X) == 0 && checkCommitment.Y.Cmp(diffCommitment.Y) == 0
}
type ProofData struct {
Proof *proofBP
T *big.Int
Hh []*p256
P *p256
}
func DumpProof(t *big.Int, h []*p256, p *p256, proof *proofBP) ([]byte, error) {
zkrproof := &ProofData{proof, t, h, p}
data, err := json.Marshal(zkrproof)
return data, err
}
func LoadProof(data []byte) (*Bp, *proofBP, error) {
var p ProofData
err := json.Unmarshal(data, &p)
if err != nil {
return nil, nil, err
}
return GetVerifier(p.T, p.Hh, p.P), p.Proof, nil
}