-
Notifications
You must be signed in to change notification settings - Fork 4
/
public_key.go
222 lines (189 loc) · 4.35 KB
/
public_key.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package tpke
import (
"crypto/rand"
"errors"
"fmt"
"github.com/DE-labtory/tpke/bls"
)
type PublicKey struct {
G1 *bls.G1Projective
}
func (p *PublicKey) Hash(msg [32]byte) *bls.G2Projective {
return bls.HashG2WithDomain(msg, 0)
}
func (p *PublicKey) Verify(sig *Signature, msg []byte) bool {
hashG2 := bls.HashG2(msg)
one := bls.G1ProjectiveOne
left := bls.Pairing(p.G1.Copy(), hashG2.ToProjective().Copy())
right := bls.Pairing(one, sig.G2)
return left.Equals(right)
}
func (p *PublicKey) Encrypt(msg []byte) (*CipherText, error) {
//if len(msg) > 32 {
// return nil, nil
//}
r, err := bls.RandFR(rand.Reader)
if err != nil {
return nil, err
}
u := bls.G1AffineOne.MulFR(r.ToRepr())
g := p.G1.ToAffine().MulFR(r.ToRepr())
v := xorHash(*g, msg)
if err != nil {
return nil, err
}
hashed := hashG1G2(*u, v)
affine := hashed
w := affine.MulFR(r.ToRepr())
return &CipherText{
U : *u,
V : v,
W : *w,
}, nil
}
func (p *PublicKey) FromBytes(bytes []byte) {
}
func (p *PublicKey) Serialize() [96]byte {
return p.G1.ToAffine().SerializeBytes()
}
func (p *PublicKey) Equals(other *PublicKey) bool {
return p.G1.Equal(other.G1)
}
func NewPublicKeyFromBytes(bytes [96]byte) *PublicKey {
var b [48]byte
for i := range b {
b[i] = bytes[i]
}
x := bls.FQReprFromBytes(b)
for i := range b {
b[i] = bytes[i + 48]
}
y := bls.FQReprFromBytes(b)
g1Affine := bls.NewG1Affine(bls.FQReprToFQ(x), bls.FQReprToFQ(y))
return &PublicKey {
G1: g1Affine.ToProjective(),
}
}
type PublicKeySet struct {
commitment *Commitment
}
func (pks *PublicKeySet) Clone() *PublicKeySet {
return &PublicKeySet {
commitment: pks.commitment.Clone(),
}
}
func (pks *PublicKeySet) Hash() {
// TODO
}
func (pks *PublicKeySet) Threshold() int {
return pks.commitment.degree()
}
func (pks *PublicKeySet) PublicKey() *PublicKey {
return &PublicKey {
G1: pks.commitment.coeff[0],
}
}
func (pks *PublicKeySet) KeyShare(i int) *PublicKeyShare {
fr := bls.FRReprToFR(bls.NewFRRepr(uint64(i + 1)))
eval := pks.commitment.evaluate(*fr)
return &PublicKeyShare {
pk: &PublicKey {
G1: eval,
},
}
}
func (pks *PublicKeySet) Decrypt(ds map[int]*DecryptionShare, ct *CipherText) ([]byte, error) {
samples := make([]*Sample, 0)
i := 0
for id, d := range ds {
samples = append(samples, &Sample {
fr: bls.FRReprToFR(bls.NewFRRepr(uint64(id + 1))),
g1: d.G1.Copy(),
})
i++
}
g, err := Interpolate(pks.commitment.degree(), samples)
fmt.Printf("g: %v\n", g)
if err != nil {
return nil, err
}
return xorHash(*g, ct.V), nil
}
func (pks *PublicKeySet) DecryptUsingStringMap(ds map[string]*DecryptionShare, ct *CipherText) ([]byte, error) {
samples := make([]*Sample, 0)
i := 0
for id, d := range ds {
fr, err := bls.FRReprFromString(id, 10)
if err != nil {
return nil, err
}
samples = append(samples, &Sample {
fr: bls.FRReprToFR(fr),
g1: d.G1.Copy(),
})
i++
}
g, err := Interpolate(pks.commitment.degree(), samples)
fmt.Printf("g: %v\n", g)
if err != nil {
return nil, err
}
return xorHash(*g, ct.V), nil
}
func (pks *PublicKeySet) Equals(other *PublicKeySet) bool {
if len(pks.commitment.coeff) != len(other.commitment.coeff) {
return false
}
for i := range pks.commitment.coeff {
if !pks.commitment.coeff[i].Equal(other.commitment.coeff[i]) {
return false
}
}
return true
}
func (pks *PublicKeySet) Serialize() []byte {
bytes := make([]byte, 0)
for i := range pks.commitment.coeff {
g := pks.commitment.coeff[i]
affine := g.ToAffine()
serial := affine.SerializeBytes()
for j := range serial {
bytes = append(bytes, serial[j])
}
}
return bytes
}
func NewPublicKeySetFromBytes(bytes []byte) (*PublicKeySet, error) {
l := len(bytes)
if l % 96 != 0 {
return nil, errors.New("the length of input must be a multiple of 96")
}
g1Arr := make([]*bls.G1Projective, 0)
idx := 0
for i := 0; i < l / 96; i++ {
var xArr [48]byte
var yArr [48]byte
for j := 0; j < 48; j++ {
xArr[j] = bytes[idx]
idx++
}
for j := 0; j < 48; j++ {
yArr[j] = bytes[idx]
idx++
}
fqXrepr := bls.FQReprFromBytes(xArr)
fqYrepr := bls.FQReprFromBytes(yArr)
fqX := bls.FQReprToFQ(fqXrepr)
fqY := bls.FQReprToFQ(fqYrepr)
g1 := bls.NewG1Affine(fqX, fqY).ToProjective()
g1Arr = append(g1Arr, g1)
}
return &PublicKeySet {
commitment: &Commitment {
coeff: g1Arr,
},
}, nil
}
type PublicKeyShare struct {
pk *PublicKey
}