-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (167 loc) · 3.84 KB
/
main.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
184
185
186
187
package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/big"
"os"
"strings"
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
)
var (
help bool
input string
output string
)
func init() {
flag.BoolVar(&help, "h", false, "this help")
flag.StringVar(&input, "i", "input.json", "input json file of delta, a, b and c")
flag.StringVar(&output, "o", "output.json", "output json file of b and c")
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, `gen-g16-proof version: gen-g16-proof/1.0.0
Usage: gen-g16-proof -i input.json -o output.json
Options:
`)
flag.PrintDefaults()
}
func parseBigInt(s string) *big.Int {
err := fmt.Errorf("failed to parse %s", s)
var base int
if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") {
s = s[2:]
base = 16
} else if strings.HasPrefix(s, "0") {
s = s[1:]
base = 8
} else if strings.HasPrefix(s, "b") || strings.HasPrefix(s, "B") {
s = s[1:]
base = 2
} else {
base = 10
}
if ret, ok := new(big.Int).SetString(s, base); !ok {
panic(err)
} else {
return ret
}
}
func parseG2Point(s [2][2]string) *bn256.G2 {
xIm := parseBigInt(s[0][0])
xRe := parseBigInt(s[0][1])
yIm := parseBigInt(s[1][0])
yRe := parseBigInt(s[1][1])
p := new(bn256.G2)
b := make([]byte, 32*4)
xbIm := xIm.Bytes()
xbRe := xRe.Bytes()
ybIm := yIm.Bytes()
ybRe := yRe.Bytes()
copy(b[1*32-len(xbIm):1*32], xbIm)
copy(b[2*32-len(xbRe):2*32], xbRe)
copy(b[3*32-len(ybIm):3*32], ybIm)
copy(b[4*32-len(ybRe):4*32], ybRe)
_, err := p.Unmarshal(b)
if err != nil {
panic(err)
}
return p
}
func hexG2Point(p *bn256.G2) [2][2]string {
b := p.Marshal()
xIm := b[0*32 : 1*32]
xRe := b[1*32 : 2*32]
yIm := b[2*32 : 3*32]
yRe := b[3*32 : 4*32]
return [2][2]string{
[2]string{
"0x" + hex.EncodeToString(new(big.Int).SetBytes(xIm).Bytes()),
"0x" + hex.EncodeToString(new(big.Int).SetBytes(xRe).Bytes()),
},
[2]string{
"0x" + hex.EncodeToString(new(big.Int).SetBytes(yIm).Bytes()),
"0x" + hex.EncodeToString(new(big.Int).SetBytes(yRe).Bytes()),
},
}
}
func parseG1Point(s [2]string) *bn256.G1 {
x := parseBigInt(s[0])
y := parseBigInt(s[1])
p := new(bn256.G1)
b := make([]byte, 32*2)
xb := x.Bytes()
yb := y.Bytes()
copy(b[1*32-len(xb):1*32], xb)
copy(b[2*32-len(yb):2*32], yb)
_, err := p.Unmarshal(b)
if err != nil {
panic(err)
}
return p
}
func hexG1Point(p *bn256.G1) [2]string {
b := p.Marshal()
x := b[0*32 : 1*32]
y := b[1*32 : 2*32]
return [2]string{
"0x" + hex.EncodeToString(new(big.Int).SetBytes(x).Bytes()),
"0x" + hex.EncodeToString(new(big.Int).SetBytes(y).Bytes()),
}
}
type InputData struct {
Delta [2][2]string `json:"delta"`
A [2]string `json:"a"`
B [2][2]string `json:"b"`
C [2]string `json:"c"`
Eta string `json:"eta"`
}
type OutputData struct {
B [2][2]string `json:"b"`
C [2]string `json:"c"`
Eta string `json:"eta"`
}
func main() {
flag.Parse()
if help {
flag.Usage()
return
}
data, err := ioutil.ReadFile(input)
if err != nil {
panic(err)
}
inputData := new(InputData)
err = json.Unmarshal(data, inputData)
if err != nil {
panic(err)
}
Delta := parseG2Point(inputData.Delta)
B := parseG2Point(inputData.B)
A := parseG1Point(inputData.A)
C := parseG1Point(inputData.C)
Eta := new(big.Int)
if inputData.Eta == "" {
Eta, _, err = bn256.RandomG1(rand.Reader)
if err != nil {
panic(err)
}
} else {
Eta = parseBigInt(inputData.Eta)
}
outputData := new(OutputData)
outputData.B = hexG2Point(new(bn256.G2).Add(Delta, new(bn256.G2).ScalarMult(B, Eta)))
outputData.C = hexG1Point(new(bn256.G1).Add(C, new(bn256.G1).ScalarMult(A, Eta)))
outputData.Eta = "0x" + hex.EncodeToString(Eta.Bytes())
data, err = json.Marshal(outputData)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(output, data, 0644)
if err != nil {
panic(err)
}
}