This repository was archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbls12.go
145 lines (132 loc) · 2.89 KB
/
bls12.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
// Package bls12 implements bilinear pairing curve BLS12-381
package bls12 // import "github.com/dis2/bls12"
import "math/big"
import "encoding/hex"
// G1/G2 interface, but not for GT.
type G interface {
Copy() G
Add(q G) G
Double() G
ScalarMult(s *Scalar) G
ScalarBaseMult(s *Scalar) G
SetXY(x, y Field) G
SetNormalized() G
ScaleByCofactor() G
HashToPoint(msg []byte) G
HashToPointFast(msg []byte) G
MapIntToPoint(Field) bool
GetSize() int
GetXYZ() (x, y, z Field)
Check() bool
SetZero() G
SetOne() G
Normalize() G
IsZero() bool
String() string
Marshal() []byte
MarshalUncompressed() []byte
Unmarshal(in []byte) []byte
}
func hexConst(s string) (ret *big.Int) {
ret, _ = new(big.Int).SetString(s, 16)
return
}
func hexBytes(s string) []byte {
var buf [48]byte
_, err := hex.Decode(buf[:], []byte(s))
if err != nil {
panic("bad hex bytes constant " + s)
}
return buf[:]
}
func QConst(s string) (f Fq) {
if len(s)%2 != 0 {
panic("bad const padding for " + s)
}
initPending()
var buf [48]byte
pad := 48 - len(s)/2
_, err := hex.Decode(buf[pad:], []byte(s))
if err != nil || f.Unmarshal(buf[:]) == nil {
panic("invalid const " + s)
}
return
}
const (
// https://github.com/ebfull/pairing/tree/master/src/bls12_381#serialization
serializationMask = (1 << 5) - 1
serializationCompressed = 1 << 7 // 0x80
serializationInfinity = 1 << 6 // 0x40
serializationBigY = 1 << 5 // 0x20
)
// Unmarshal G1 or G2 point.
func GUnmarshal(p G, in []byte) (res []byte) {
size := p.GetSize()
if len(in) < size {
return nil
}
var bin = make([]byte, size)
copy(bin[:], in)
flags := bin[0]
bin[0] &= serializationMask
compressed := flags&serializationCompressed != 0
inlen := size * 2
if compressed {
inlen = size
} else if len(in) < inlen {
return nil
}
res = in[inlen:]
// Big Y, but we're not compressed, or infinity is serialized
if (flags&serializationBigY != 0) && (!compressed || (flags&serializationInfinity != 0)) {
return nil
}
if flags&serializationInfinity != 0 {
// Check that rest is zero
for _, v := range in[1:inlen] {
if v != 0 {
return nil
}
}
p.SetZero()
return res
}
X, Y, _ := p.GetXYZ()
X.Unmarshal(bin[:])
if compressed {
if !Y.Y2FromX(X).Sqrt(nil) {
return nil
}
Y.EnsureParity(flags&serializationBigY != 0)
} else {
Y.Unmarshal(in[size : size*2])
}
p.SetNormalized()
if !p.Check() {
return nil
}
return res
}
// Marshal either G1 or G2, comp=1 compressed, comp=2 uncompressed.
func GMarshal(p G, comp int) (res []byte) {
p.Normalize()
X, Y, _ := p.GetXYZ()
if p.IsZero() {
res = make([]byte, p.GetSize()*comp)
res[0] = serializationInfinity
if comp == 1 {
res[0] |= serializationCompressed
}
return
}
res = X.Marshal()
if comp == 1 {
if Y.Copy().EnsureParity(false) {
res[0] |= serializationBigY
}
res[0] |= serializationCompressed
} else {
res = append(res, Y.Marshal()...)
}
return
}