aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/bn256/cloudflare/gfp12.go
blob: 93fb368a7bf0abfb098be066dd2c79ed67c58f31 (plain) (blame)
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
package bn256

// For details of the algorithms used, see "Multiplication and Squaring on
// Pairing-Friendly Fields, Devegili et al.
// http://eprint.iacr.org/2006/471.pdf.

import (
    "math/big"
)

// gfP12 implements the field of size p¹² as a quadratic extension of gfP6
// where ω²=τ.
type gfP12 struct {
    x, y gfP6 // value is xω + y
}

func (e *gfP12) String() string {
    return "(" + e.x.String() + "," + e.y.String() + ")"
}

func (e *gfP12) Set(a *gfP12) *gfP12 {
    e.x.Set(&a.x)
    e.y.Set(&a.y)
    return e
}

func (e *gfP12) SetZero() *gfP12 {
    e.x.SetZero()
    e.y.SetZero()
    return e
}

func (e *gfP12) SetOne() *gfP12 {
    e.x.SetZero()
    e.y.SetOne()
    return e
}

func (e *gfP12) IsZero() bool {
    return e.x.IsZero() && e.y.IsZero()
}

func (e *gfP12) IsOne() bool {
    return e.x.IsZero() && e.y.IsOne()
}

func (e *gfP12) Conjugate(a *gfP12) *gfP12 {
    e.x.Neg(&a.x)
    e.y.Set(&a.y)
    return e
}

func (e *gfP12) Neg(a *gfP12) *gfP12 {
    e.x.Neg(&a.x)
    e.y.Neg(&a.y)
    return e
}

// Frobenius computes (xω+y)^p = x^p ω·ξ^((p-1)/6) + y^p
func (e *gfP12) Frobenius(a *gfP12) *gfP12 {
    e.x.Frobenius(&a.x)
    e.y.Frobenius(&a.y)
    e.x.MulScalar(&e.x, xiToPMinus1Over6)
    return e
}

// FrobeniusP2 computes (xω+y)^p² = x^p² ω·ξ^((p²-1)/6) + y^p²
func (e *gfP12) FrobeniusP2(a *gfP12) *gfP12 {
    e.x.FrobeniusP2(&a.x)
    e.x.MulGFP(&e.x, xiToPSquaredMinus1Over6)
    e.y.FrobeniusP2(&a.y)
    return e
}

func (e *gfP12) FrobeniusP4(a *gfP12) *gfP12 {
    e.x.FrobeniusP4(&a.x)
    e.x.MulGFP(&e.x, xiToPSquaredMinus1Over3)
    e.y.FrobeniusP4(&a.y)
    return e
}

func (e *gfP12) Add(a, b *gfP12) *gfP12 {
    e.x.Add(&a.x, &b.x)
    e.y.Add(&a.y, &b.y)
    return e
}

func (e *gfP12) Sub(a, b *gfP12) *gfP12 {
    e.x.Sub(&a.x, &b.x)
    e.y.Sub(&a.y, &b.y)
    return e
}

func (e *gfP12) Mul(a, b *gfP12) *gfP12 {
    tx := (&gfP6{}).Mul(&a.x, &b.y)
    t := (&gfP6{}).Mul(&b.x, &a.y)
    tx.Add(tx, t)

    ty := (&gfP6{}).Mul(&a.y, &b.y)
    t.Mul(&a.x, &b.x).MulTau(t)

    e.x.Set(tx)
    e.y.Add(ty, t)
    return e
}

func (e *gfP12) MulScalar(a *gfP12, b *gfP6) *gfP12 {
    e.x.Mul(&e.x, b)
    e.y.Mul(&e.y, b)
    return e
}

func (c *gfP12) Exp(a *gfP12, power *big.Int) *gfP12 {
    sum := (&gfP12{}).SetOne()
    t := &gfP12{}

    for i := power.BitLen() - 1; i >= 0; i-- {
        t.Square(sum)
        if power.Bit(i) != 0 {
            sum.Mul(t, a)
        } else {
            sum.Set(t)
        }
    }

    c.Set(sum)
    return c
}

func (e *gfP12) Square(a *gfP12) *gfP12 {
    // Complex squaring algorithm
    v0 := (&gfP6{}).Mul(&a.x, &a.y)

    t := (&gfP6{}).MulTau(&a.x)
    t.Add(&a.y, t)
    ty := (&gfP6{}).Add(&a.x, &a.y)
    ty.Mul(ty, t).Sub(ty, v0)
    t.MulTau(v0)
    ty.Sub(ty, t)

    e.x.Add(v0, v0)
    e.y.Set(ty)
    return e
}

func (e *gfP12) Invert(a *gfP12) *gfP12 {
    // See "Implementing cryptographic pairings", M. Scott, section 3.2.
    // ftp://136.206.11.249/pub/crypto/pairings.pdf
    t1, t2 := &gfP6{}, &gfP6{}

    t1.Square(&a.x)
    t2.Square(&a.y)
    t1.MulTau(t1).Sub(t2, t1)
    t2.Invert(t1)

    e.x.Neg(&a.x)
    e.y.Set(&a.y)
    e.MulScalar(e, t2)
    return e
}