-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
173 lines (140 loc) · 4.57 KB
/
utils.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
package hibe
import (
"crypto/sha256"
"math/big"
"vuvuzela.io/crypto/bn256"
)
// geSize is the base size in bytes of a marshalled group element. The size of
// a marshalled element of G2 is geSize. The size of a marshalled element of G1
// is 2 * geSize. The size of a marshalled element of G2 is 6 * geSize.
const geSize = 64
// geShift is the base shift for a marshalled group element
const geShift = 6
func geIndex(encoded []byte, index int, len int) []byte {
return encoded[index<<geShift : (index+len)<<geShift]
}
// Marshal encodes the parameters as a byte slice.
func (params *Params) Marshal() []byte {
marshalled := make([]byte, (6+len(params.H))<<geShift)
copy(geIndex(marshalled, 0, 2), params.G.Marshal())
copy(geIndex(marshalled, 2, 2), params.G1.Marshal())
copy(geIndex(marshalled, 4, 1), params.G2.Marshal())
copy(geIndex(marshalled, 5, 2), params.G3.Marshal())
for i, hi := range params.H {
copy(geIndex(marshalled, 6+i, 1), hi.Marshal())
}
return marshalled
}
// Unmarshal recovers the parameters from an encoded byte slice.
func (params *Params) Unmarshal(marshalled []byte) (*Params, bool) {
if len(marshalled)&((1<<geShift)-1) != 0 {
return nil, false
}
params.G = new(bn256.G2)
if _, ok := params.G.Unmarshal(geIndex(marshalled, 0, 2)); !ok {
return nil, false
}
params.G1 = new(bn256.G2)
if _, ok := params.G1.Unmarshal(geIndex(marshalled, 2, 2)); !ok {
return nil, false
}
params.G2 = new(bn256.G1)
if _, ok := params.G2.Unmarshal(geIndex(marshalled, 4, 1)); !ok {
return nil, false
}
params.G3 = new(bn256.G1)
if _, ok := params.G3.Unmarshal(geIndex(marshalled, 5, 1)); !ok {
return nil, false
}
hlen := (len(marshalled) >> geShift) - 6
params.H = make([]*bn256.G1, hlen, hlen)
for i := range params.H {
hi := new(bn256.G1)
params.H[i] = hi
if _, ok := hi.Unmarshal(geIndex(marshalled, 6+i, 1)); !ok {
return params, false
}
}
// Clear any cached values
params.Pairing = nil
return params, true
}
// Marshal encodes the private key as a byte slice.
func (key *PrivateKey) Marshal() []byte {
marshalled := make([]byte, (3+len(key.B))<<geShift)
copy(geIndex(marshalled, 0, 1), key.A0.Marshal())
copy(geIndex(marshalled, 1, 2), key.A1.Marshal())
for i, bi := range key.B {
copy(geIndex(marshalled, 3+i, 1), bi.Marshal())
}
return marshalled
}
// Unmarshal recovers the private key from an encoded byte slice.
func (key *PrivateKey) Unmarshal(marshalled []byte) (*PrivateKey, bool) {
if len(marshalled)&((1<<geShift)-1) != 0 {
return nil, false
}
key.A0 = new(bn256.G1)
if _, ok := key.A0.Unmarshal(geIndex(marshalled, 0, 1)); !ok {
return nil, false
}
key.A1 = new(bn256.G2)
if _, ok := key.A1.Unmarshal(geIndex(marshalled, 1, 2)); !ok {
return nil, false
}
blen := (len(marshalled) >> geShift) - 3
key.B = make([]*bn256.G1, blen, blen)
for i := range key.B {
bi := new(bn256.G1)
key.B[i] = bi
if _, ok := bi.Unmarshal(geIndex(marshalled, 3+i, 1)); !ok {
return key, false
}
}
return key, true
}
// Marshal encodes the ciphertext as a byte slice.
func (ciphertext *Ciphertext) Marshal() []byte {
marshalled := make([]byte, 9<<geShift)
copy(geIndex(marshalled, 0, 6), ciphertext.A.Marshal())
copy(geIndex(marshalled, 6, 2), ciphertext.B.Marshal())
copy(geIndex(marshalled, 8, 1), ciphertext.C.Marshal())
return marshalled
}
// Unmarshal recovers the ciphertext from an encoded byte slice.
func (ciphertext *Ciphertext) Unmarshal(marshalled []byte) (*Ciphertext, bool) {
if len(marshalled) != 9<<geShift {
return nil, false
}
ciphertext.A = new(bn256.GT)
if _, ok := ciphertext.A.Unmarshal(geIndex(marshalled, 0, 6)); !ok {
return nil, false
}
ciphertext.B = new(bn256.G2)
if _, ok := ciphertext.B.Unmarshal(geIndex(marshalled, 6, 2)); !ok {
return nil, false
}
ciphertext.C = new(bn256.G1)
if _, ok := ciphertext.C.Unmarshal(geIndex(marshalled, 8, 1)); !ok {
return nil, false
}
return ciphertext, true
}
// HashToZp hashes a byte slice to an integer in Zp*.
func HashToZp(bytestring []byte) *big.Int {
digest := sha256.Sum256(bytestring)
bigint := new(big.Int).SetBytes(digest[:])
bigint.Mod(bigint, new(big.Int).Add(bn256.Order, big.NewInt(-1)))
bigint.Add(bigint, big.NewInt(1))
return bigint
}
// gtBase is e(g1, g2) where g1 and g2 are the base generators of G2 and G1
var gtBase *bn256.GT
// HashToGT hashes a byte slice to a group element in GT.
func HashToGT(bytestring []byte) *bn256.GT {
if gtBase == nil {
gtBase = bn256.Pair(new(bn256.G1).ScalarBaseMult(big.NewInt(1)),
new(bn256.G2).ScalarBaseMult(big.NewInt(1)))
}
return new(bn256.GT).ScalarMult(gtBase, HashToZp(bytestring))
}