1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 func TestGFp2Invert(t *testing.T) {
18 a.x.SetString("23423492374", 10)
19 a.y.SetString("12934872398472394827398470", 10)
24 b := newGFp2(pool).Mul(inv, a, pool)
25 if b.x.Int64() != 0 || b.y.Int64() != 1 {
26 t.Fatalf("bad result for a^-1*a: %s %s", b.x, b.y)
33 if c := pool.Count(); c > 0 {
34 t.Errorf("Pool count non-zero: %d\n", c)
38 func isZero(n *big.Int) bool {
39 return new(big.Int).Mod(n, p).Int64() == 0
42 func isOne(n *big.Int) bool {
43 return new(big.Int).Mod(n, p).Int64() == 1
46 func TestGFp6Invert(t *testing.T) {
50 a.x.x.SetString("239487238491", 10)
51 a.x.y.SetString("2356249827341", 10)
52 a.y.x.SetString("082659782", 10)
53 a.y.y.SetString("182703523765", 10)
54 a.z.x.SetString("978236549263", 10)
55 a.z.y.SetString("64893242", 10)
60 b := newGFp6(pool).Mul(inv, a, pool)
67 t.Fatalf("bad result for a^-1*a: %s", b)
74 if c := pool.Count(); c > 0 {
75 t.Errorf("Pool count non-zero: %d\n", c)
79 func TestGFp12Invert(t *testing.T) {
83 a.x.x.x.SetString("239846234862342323958623", 10)
84 a.x.x.y.SetString("2359862352529835623", 10)
85 a.x.y.x.SetString("928836523", 10)
86 a.x.y.y.SetString("9856234", 10)
87 a.x.z.x.SetString("235635286", 10)
88 a.x.z.y.SetString("5628392833", 10)
89 a.y.x.x.SetString("252936598265329856238956532167968", 10)
90 a.y.x.y.SetString("23596239865236954178968", 10)
91 a.y.y.x.SetString("95421692834", 10)
92 a.y.y.y.SetString("236548", 10)
93 a.y.z.x.SetString("924523", 10)
94 a.y.z.y.SetString("12954623", 10)
99 b := newGFp12(pool).Mul(inv, a, pool)
100 if !isZero(b.x.x.x) ||
112 t.Fatalf("bad result for a^-1*a: %s", b)
119 if c := pool.Count(); c > 0 {
120 t.Errorf("Pool count non-zero: %d\n", c)
124 func TestCurveImpl(t *testing.T) {
128 pool.Get().SetInt64(1),
129 pool.Get().SetInt64(-2),
130 pool.Get().SetInt64(1),
131 pool.Get().SetInt64(0),
134 x := pool.Get().SetInt64(32498273234)
135 X := newCurvePoint(pool).Mul(g, x, pool)
137 y := pool.Get().SetInt64(98732423523)
138 Y := newCurvePoint(pool).Mul(g, y, pool)
140 s1 := newCurvePoint(pool).Mul(X, y, pool).MakeAffine(pool)
141 s2 := newCurvePoint(pool).Mul(Y, x, pool).MakeAffine(pool)
143 if s1.x.Cmp(s2.x) != 0 ||
144 s2.x.Cmp(s1.x) != 0 {
145 t.Errorf("DH points don't match: (%s, %s) (%s, %s)", s1.x, s1.y, s2.x, s2.y)
156 if c := pool.Count(); c > 0 {
157 t.Errorf("Pool count non-zero: %d\n", c)
161 func TestOrderG1(t *testing.T) {
162 g := new(G1).ScalarBaseMult(Order)
163 if !g.p.IsInfinity() {
164 t.Error("G1 has incorrect order")
167 one := new(G1).ScalarBaseMult(new(big.Int).SetInt64(1))
170 if g.p.x.Cmp(one.p.x) != 0 || g.p.y.Cmp(one.p.y) != 0 {
171 t.Errorf("1+0 != 1 in G1")
175 func TestOrderG2(t *testing.T) {
176 g := new(G2).ScalarBaseMult(Order)
177 if !g.p.IsInfinity() {
178 t.Error("G2 has incorrect order")
181 one := new(G2).ScalarBaseMult(new(big.Int).SetInt64(1))
184 if g.p.x.x.Cmp(one.p.x.x) != 0 ||
185 g.p.x.y.Cmp(one.p.x.y) != 0 ||
186 g.p.y.x.Cmp(one.p.y.x) != 0 ||
187 g.p.y.y.Cmp(one.p.y.y) != 0 {
188 t.Errorf("1+0 != 1 in G2")
192 func TestOrderGT(t *testing.T) {
193 gt := Pair(&G1{curveGen}, &G2{twistGen})
194 g := new(GT).ScalarMult(gt, Order)
196 t.Error("GT has incorrect order")
200 func TestBilinearity(t *testing.T) {
201 for i := 0; i < 2; i++ {
202 a, p1, _ := RandomG1(rand.Reader)
203 b, p2, _ := RandomG2(rand.Reader)
206 e2 := Pair(&G1{curveGen}, &G2{twistGen})
210 minusE2 := new(GT).Neg(e2)
214 t.Fatalf("bad pairing result: %s", e1)
219 func TestG1Marshal(t *testing.T) {
220 g := new(G1).ScalarBaseMult(new(big.Int).SetInt64(1))
222 _, ok := new(G1).Unmarshal(form)
224 t.Fatalf("failed to unmarshal")
227 g.ScalarBaseMult(Order)
229 g2, ok := new(G1).Unmarshal(form)
231 t.Fatalf("failed to unmarshal ∞")
233 if !g2.p.IsInfinity() {
234 t.Fatalf("∞ unmarshaled incorrectly")
238 func TestG2Marshal(t *testing.T) {
239 g := new(G2).ScalarBaseMult(new(big.Int).SetInt64(1))
241 _, ok := new(G2).Unmarshal(form)
243 t.Fatalf("failed to unmarshal")
246 g.ScalarBaseMult(Order)
248 g2, ok := new(G2).Unmarshal(form)
250 t.Fatalf("failed to unmarshal ∞")
252 if !g2.p.IsInfinity() {
253 t.Fatalf("∞ unmarshaled incorrectly")
257 func TestG1Identity(t *testing.T) {
258 g := new(G1).ScalarBaseMult(new(big.Int).SetInt64(0))
259 if !g.p.IsInfinity() {
264 func TestG2Identity(t *testing.T) {
265 g := new(G2).ScalarBaseMult(new(big.Int).SetInt64(0))
266 if !g.p.IsInfinity() {
271 func TestTripartiteDiffieHellman(t *testing.T) {
272 a, _ := rand.Int(rand.Reader, Order)
273 b, _ := rand.Int(rand.Reader, Order)
274 c, _ := rand.Int(rand.Reader, Order)
276 pa, _ := new(G1).Unmarshal(new(G1).ScalarBaseMult(a).Marshal())
277 qa, _ := new(G2).Unmarshal(new(G2).ScalarBaseMult(a).Marshal())
278 pb, _ := new(G1).Unmarshal(new(G1).ScalarBaseMult(b).Marshal())
279 qb, _ := new(G2).Unmarshal(new(G2).ScalarBaseMult(b).Marshal())
280 pc, _ := new(G1).Unmarshal(new(G1).ScalarBaseMult(c).Marshal())
281 qc, _ := new(G2).Unmarshal(new(G2).ScalarBaseMult(c).Marshal())
285 k1Bytes := k1.Marshal()
289 k2Bytes := k2.Marshal()
293 k3Bytes := k3.Marshal()
295 if !bytes.Equal(k1Bytes, k2Bytes) || !bytes.Equal(k2Bytes, k3Bytes) {
296 t.Errorf("keys didn't agree")
300 func BenchmarkPairing(b *testing.B) {
301 for i := 0; i < b.N; i++ {
302 Pair(&G1{curveGen}, &G2{twistGen})