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.
7 // For details of the algorithms used, see "Multiplication and Squaring on
8 // Pairing-Friendly Fields, Devegili et al.
9 // http://eprint.iacr.org/2006/471.pdf.
15 // gfP12 implements the field of size p¹² as a quadratic extension of gfP6
18 x, y *gfP6 // value is xω + y
21 func newGFp12(pool *bnPool) *gfP12 {
22 return &gfP12{newGFp6(pool), newGFp6(pool)}
25 func (e *gfP12) String() string {
26 return "(" + e.x.String() + "," + e.y.String() + ")"
29 func (e *gfP12) Put(pool *bnPool) {
34 func (e *gfP12) Set(a *gfP12) *gfP12 {
40 func (e *gfP12) SetZero() *gfP12 {
46 func (e *gfP12) SetOne() *gfP12 {
52 func (e *gfP12) Minimal() {
57 func (e *gfP12) IsZero() bool {
59 return e.x.IsZero() && e.y.IsZero()
62 func (e *gfP12) IsOne() bool {
64 return e.x.IsZero() && e.y.IsOne()
67 func (e *gfP12) Conjugate(a *gfP12) *gfP12 {
73 func (e *gfP12) Negative(a *gfP12) *gfP12 {
79 // Frobenius computes (xω+y)^p = x^p ω·ξ^((p-1)/6) + y^p
80 func (e *gfP12) Frobenius(a *gfP12, pool *bnPool) *gfP12 {
81 e.x.Frobenius(a.x, pool)
82 e.y.Frobenius(a.y, pool)
83 e.x.MulScalar(e.x, xiToPMinus1Over6, pool)
87 // FrobeniusP2 computes (xω+y)^p² = x^p² ω·ξ^((p²-1)/6) + y^p²
88 func (e *gfP12) FrobeniusP2(a *gfP12, pool *bnPool) *gfP12 {
90 e.x.MulGFP(e.x, xiToPSquaredMinus1Over6)
95 func (e *gfP12) Add(a, b *gfP12) *gfP12 {
101 func (e *gfP12) Sub(a, b *gfP12) *gfP12 {
107 func (e *gfP12) Mul(a, b *gfP12, pool *bnPool) *gfP12 {
109 tx.Mul(a.x, b.y, pool)
111 t.Mul(b.x, a.y, pool)
115 ty.Mul(a.y, b.y, pool)
116 t.Mul(a.x, b.x, pool)
127 func (e *gfP12) MulScalar(a *gfP12, b *gfP6, pool *bnPool) *gfP12 {
128 e.x.Mul(e.x, b, pool)
129 e.y.Mul(e.y, b, pool)
133 func (c *gfP12) Exp(a *gfP12, power *big.Int, pool *bnPool) *gfP12 {
134 sum := newGFp12(pool)
138 for i := power.BitLen() - 1; i >= 0; i-- {
140 if power.Bit(i) != 0 {
155 func (e *gfP12) Square(a *gfP12, pool *bnPool) *gfP12 {
156 // Complex squaring algorithm
158 v0.Mul(a.x, a.y, pool)
180 func (e *gfP12) Invert(a *gfP12, pool *bnPool) *gfP12 {
181 // See "Implementing cryptographic pairings", M. Scott, section 3.2.
182 // ftp://136.206.11.249/pub/crypto/pairings.pdf
194 e.MulScalar(e, t2, pool)