-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbjj.go
208 lines (168 loc) · 4.89 KB
/
bjj.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package c_polygonid
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/iden3/go-iden3-crypto/babyjub"
)
type BabyJubJubSignPoseidonResponse struct {
Signature babyjub.SignatureComp `json:"signature"`
}
func BabyJubJubSignPoseidon(ctx context.Context, cfg EnvConfig,
in []byte) (BabyJubJubSignPoseidonResponse, error) {
var req struct {
PrivKey *JsonBJJPrivateKey `json:"private_key"`
Msg *JsonFieldIntStr `json:"msg_int"`
}
if in == nil {
return BabyJubJubSignPoseidonResponse{}, errors.New("request is empty")
}
err := json.Unmarshal(in, &req)
if err != nil {
return BabyJubJubSignPoseidonResponse{},
fmt.Errorf("failed to unmarshal request: %w", err)
}
if req.PrivKey == nil {
return BabyJubJubSignPoseidonResponse{},
errors.New("private key is not set")
}
if req.Msg == nil {
return BabyJubJubSignPoseidonResponse{},
errors.New("message is not set")
}
sig := req.PrivKey.PrivateKey().SignPoseidon(req.Msg.Int())
return BabyJubJubSignPoseidonResponse{
Signature: sig.Compress(),
}, nil
}
type BabyJubJubVerifyPoseidonResponse struct {
Valid bool `json:"valid"`
}
func BabyJubJubVerifyPoseidon(_ context.Context, _ EnvConfig,
in []byte) (BabyJubJubVerifyPoseidonResponse, error) {
var req struct {
Pub *JsonBJJPublicKey `json:"public_key"`
Sig *JsonBJJSignature `json:"signature"`
Msg *JsonFieldIntStr `json:"msg_int"`
}
if in == nil {
return BabyJubJubVerifyPoseidonResponse{},
errors.New("request is empty")
}
err := json.Unmarshal(in, &req)
if err != nil {
return BabyJubJubVerifyPoseidonResponse{},
fmt.Errorf("failed to unmarshal request: %w", err)
}
if req.Pub == nil {
return BabyJubJubVerifyPoseidonResponse{},
errors.New("public key is not set")
}
if req.Msg == nil {
return BabyJubJubVerifyPoseidonResponse{},
errors.New("message is not set")
}
if req.Sig == nil {
return BabyJubJubVerifyPoseidonResponse{},
errors.New("signature is not set")
}
return BabyJubJubVerifyPoseidonResponse{
Valid: req.Pub.PublicKey().
VerifyPoseidon(req.Msg.Int(), req.Sig.Signature()),
}, nil
}
type BabyJubJubPrivate2PublicResponse struct {
PublicKey string `json:"public_key"`
PublicKeyX string `json:"public_key_x_int"`
PublicKeyY string `json:"public_key_y_int"`
}
func BabyJubJubPrivate2Public(_ context.Context, _ EnvConfig,
in []byte) (BabyJubJubPrivate2PublicResponse, error) {
var req struct {
PrivKey *JsonBJJPrivateKey `json:"private_key"`
}
if in == nil {
return BabyJubJubPrivate2PublicResponse{},
errors.New("request is empty")
}
err := json.Unmarshal(in, &req)
if err != nil {
return BabyJubJubPrivate2PublicResponse{},
fmt.Errorf("failed to unmarshal request: %w", err)
}
if req.PrivKey == nil {
return BabyJubJubPrivate2PublicResponse{},
errors.New("private key is not set")
}
pubKey := req.PrivKey.PrivateKey().Public()
compPubKey := pubKey.Compress()
return BabyJubJubPrivate2PublicResponse{
PublicKey: hex.EncodeToString(compPubKey[:]),
PublicKeyX: pubKey.X.Text(10),
PublicKeyY: pubKey.Y.Text(10),
}, nil
}
type BabyJubJubPublicUncompressResponse struct {
PublicKeyX string `json:"public_key_x_int"`
PublicKeyY string `json:"public_key_y_int"`
}
func BabyJubJubPublicUncompress(_ context.Context, _ EnvConfig,
in []byte) (BabyJubJubPublicUncompressResponse, error) {
var req struct {
Pub *JsonBJJPublicKey `json:"public_key"`
}
if in == nil {
return BabyJubJubPublicUncompressResponse{},
errors.New("request is empty")
}
err := json.Unmarshal(in, &req)
if err != nil {
return BabyJubJubPublicUncompressResponse{},
fmt.Errorf("failed to unmarshal request: %w", err)
}
if req.Pub == nil {
return BabyJubJubPublicUncompressResponse{},
errors.New("public key is not set")
}
return BabyJubJubPublicUncompressResponse{
PublicKeyX: req.Pub.PublicKey().X.Text(10),
PublicKeyY: req.Pub.PublicKey().Y.Text(10),
}, nil
}
type BabyJubJubPublicCompressResponse struct {
PublicKey string `json:"public_key"`
}
func BabyJubJubPublicCompress(_ context.Context, _ EnvConfig,
in []byte) (BabyJubJubPublicCompressResponse, error) {
var req struct {
PublicKeyX *JsonFieldIntStr `json:"public_key_x_int"`
PublicKeyY *JsonFieldIntStr `json:"public_key_y_int"`
}
if in == nil {
return BabyJubJubPublicCompressResponse{},
errors.New("request is empty")
}
err := json.Unmarshal(in, &req)
if err != nil {
return BabyJubJubPublicCompressResponse{},
fmt.Errorf("failed to unmarshal request: %w", err)
}
if req.PublicKeyX == nil {
return BabyJubJubPublicCompressResponse{},
errors.New("public key X is not set")
}
if req.PublicKeyY == nil {
return BabyJubJubPublicCompressResponse{},
errors.New("public key Y is not set")
}
pubKey := babyjub.PublicKey{
X: req.PublicKeyX.Int(),
Y: req.PublicKeyY.Int(),
}
compPubKey := pubKey.Compress()
return BabyJubJubPublicCompressResponse{
PublicKey: hex.EncodeToString(compPubKey[:]),
}, nil
}