-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpairing.go
147 lines (128 loc) · 2.93 KB
/
pairing.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
package bls
// MillerLoopItem are the inputs to the miller loop.
type MillerLoopItem struct {
P *G1Affine
Q *G2Prepared
}
type pairingItem struct {
p *G1Affine
q [][3]FQ2
qIndex int
}
// MillerLoop runs the miller loop algorithm.
func MillerLoop(items []MillerLoopItem) *FQ12 {
pairs := make([]pairingItem, len(items))
for i, item := range items {
if !item.P.IsZero() && !item.Q.IsZero() {
pairs[i] = pairingItem{
p: item.P.Copy(),
q: item.Q.coeffs,
qIndex: 0,
}
}
}
ell := func(f *FQ12, coeffs [3]FQ2, p *G1Affine) {
c0 := coeffs[0]
c1 := coeffs[1]
c0.c0.MulAssign(p.y)
c0.c1.MulAssign(p.y)
c1.c0.MulAssign(p.x)
c1.c1.MulAssign(p.x)
f.MulBy014Assign(coeffs[2], c1, c0)
}
f := FQ12One.Copy()
foundOne := false
blsXRsh1 := blsX.Copy()
blsXRsh1.Rsh(1)
for q := uint(0); q <= blsXRsh1.BitLen(); q++ {
set := blsXRsh1.Bit(blsXRsh1.BitLen() - q)
if !foundOne {
foundOne = set
continue
}
for i, pair := range pairs {
ell(f, pair.q[pair.qIndex], pair.p.Copy())
pairs[i].qIndex++
}
if set {
for i, pair := range pairs {
ell(f, pair.q[pair.qIndex], pair.p.Copy())
pairs[i].qIndex++
}
}
f.SquareAssign()
}
for i, pair := range pairs {
ell(f, pair.q[pair.qIndex], pair.p.Copy())
pairs[i].qIndex++
}
if blsIsNegative {
f.ConjugateAssign()
}
return f
}
// FinalExponentiation performs the final exponentiation on the
// FQ12 element.
func FinalExponentiation(r *FQ12) *FQ12 {
f1 := r.Copy()
f1.ConjugateAssign()
f2 := r.Copy()
if !f2.InverseAssign() {
return nil
}
r = f1.Copy()
r.MulAssign(f2)
f2 = r.Copy()
r.FrobeniusMapAssign(2)
r.MulAssign(f2)
ExpByX := func(f *FQ12, x FQRepr) *FQ12 {
newf := f.Exp(x)
if blsIsNegative {
newf.ConjugateAssign()
}
return newf
}
x := blsX.Copy()
y0 := r.Copy()
y0.SquareAssign()
y1 := ExpByX(y0, x)
x.Rsh(1)
y2 := ExpByX(y1, x)
x.Lsh(1)
y3 := r.Copy()
y3.ConjugateAssign()
y1 = y1.Copy()
y1.MulAssign(y3)
y1.ConjugateAssign()
y1.MulAssign(y2)
y2 = ExpByX(y1, x)
y3 = ExpByX(y2, x)
y1.ConjugateAssign()
y3.MulAssign(y1)
y1.ConjugateAssign()
y1.FrobeniusMapAssign(3)
y2.FrobeniusMapAssign(2)
y1.MulAssign(y2)
y2 = ExpByX(y3, x)
y2.MulAssign(y0)
y2.MulAssign(r)
y1.MulAssign(y2)
y3.FrobeniusMapAssign(1)
y1.MulAssign(y3)
return y1
}
// Pairing performs a pairing given the G1 and G2 elements.
func Pairing(p *G1Projective, q *G2Projective) *FQ12 {
return FinalExponentiation(MillerLoop([]MillerLoopItem{
{p.ToAffine(), G2AffineToPrepared(q.ToAffine())},
}))
}
// CompareTwoPairings checks e(P1, Q1) == e(P2, Q2)
// <=> FE(ML(P1, Q1)ML(-P2, Q2)) == 1
func CompareTwoPairings(P1 *G1Projective, Q1 *G2Projective, P2 *G1Projective, Q2 *G2Projective) bool {
negP2 := P2.Copy()
negP2.NegAssign()
return FinalExponentiation(
MillerLoop(
[]MillerLoopItem{{P1.ToAffine(), G2AffineToPrepared(Q1.ToAffine())}, {negP2.ToAffine(), G2AffineToPrepared(Q2.ToAffine())}})).Equals(FQ12One)
}