Debian Codegraph @

edwards25519/table.go

raw symbols SCIP
1 package edwards25519
2
3 type NielsPoint struct {
4 YPlusX, YMinusX, XY2D FieldElement
5 }
6
7 // Precomputed scalar multiplication table
8 type ScalarMultTable [32][8]NielsPoint
9
10 // Set p to zero, the neutral element. Return p.
11 func (p *NielsPoint) SetZero() *NielsPoint {
12 p.YMinusX.SetOne()
13 p.YPlusX.SetOne()
14 p.XY2D.SetZero()
15 return p
16 }
17
18 // Set p to q. Returns p.
19 func (p *NielsPoint) Set(q *NielsPoint) *NielsPoint {
20 p.YPlusX.Set(&q.YPlusX)
21 p.YMinusX.Set(&q.YMinusX)
22 p.XY2D.Set(&q.XY2D)
23 return p
24 }
25
26 // Set p to q if b == 1. Assumes b is 0 or 1. Returns p.
27 func (p *NielsPoint) ConditionalSet(q *NielsPoint, b int32) *NielsPoint {
28 p.YPlusX.ConditionalSet(&q.YPlusX, b)
29 p.YMinusX.ConditionalSet(&q.YMinusX, b)
30 p.XY2D.ConditionalSet(&q.XY2D, b)
31 return p
32 }
33
34 // Set p to -q. Returns p.
35 func (p *NielsPoint) Neg(q *NielsPoint) *NielsPoint {
36 p.YMinusX.Set(&q.YPlusX)
37 p.YPlusX.Set(&q.YMinusX)
38 p.XY2D.Neg(&q.XY2D)
39 return p
40 }
41
42 // Sets p to q+r. Returns p.
43 func (p *CompletedPoint) AddExtendedNiels(q *ExtendedPoint, r *NielsPoint) *CompletedPoint {
44 var t0 FieldElement
45
46 p.X.add(&q.Y, &q.X)
47 p.Y.sub(&q.Y, &q.X)
48 p.Z.Mul(&p.X, &r.YPlusX)
49 p.Y.Mul(&p.Y, &r.YMinusX)
50 p.T.Mul(&r.XY2D, &q.T)
51 t0.add(&q.Z, &q.Z)
52 p.X.sub(&p.Z, &p.Y)
53 p.Y.add(&p.Z, &p.Y)
54 p.Z.add(&t0, &p.T)
55 p.T.sub(&t0, &p.T)
56 return p
57 }
58
59 // Set p to q-r. Returns p.
60 func (p *CompletedPoint) SubExtendedNiels(q *ExtendedPoint, r *NielsPoint) *CompletedPoint {
61 var t0 FieldElement
62
63 p.X.add(&q.Y, &q.X)
64 p.Y.sub(&q.Y, &q.X)
65 p.Z.Mul(&p.X, &r.YMinusX)
66 p.Y.Mul(&p.Y, &r.YPlusX)
67 p.T.Mul(&r.XY2D, &q.T)
68 t0.add(&q.Z, &q.Z)
69 p.X.sub(&p.Z, &p.Y)
70 p.Y.add(&p.Z, &p.Y)
71 p.Z.sub(&t0, &p.T)
72 p.T.add(&t0, &p.T)
73 return p
74 }
75
76 // Sets p to q. Returns p.
77 func (p *NielsPoint) SetExtended(q *ExtendedPoint) *NielsPoint {
78 var x, y, zInv FieldElement
79 zInv.Inverse(&q.Z)
80 x.Mul(&q.X, &zInv)
81 y.Mul(&q.Y, &zInv)
82 p.YPlusX.Add(&y, &x)
83 p.YMinusX.Sub(&y, &x)
84 p.XY2D.Mul(&x, &y)
85 p.XY2D.Add(&p.XY2D, &p.XY2D)
86 p.XY2D.Mul(&p.XY2D, &feD)
87 return p
88 }
89
90 // Fill the table t with data for the point p.
91 func (t *ScalarMultTable) Compute(p *ExtendedPoint) {
92 var c, cp ExtendedPoint
93 var c_pp ProjectivePoint
94 var c_cp CompletedPoint
95 cp.Set(p)
96 for i := 0; i < 32; i++ {
97 c.SetZero()
98 for v := 0; v < 8; v++ {
99 c.Add(&c, &cp)
100 t[i][v].SetExtended(&c)
101 }
102 c_cp.DoubleExtended(&c)
103 c_pp.SetCompleted(&c_cp)
104 c_cp.DoubleProjective(&c_pp)
105 c_pp.SetCompleted(&c_cp)
106 c_cp.DoubleProjective(&c_pp)
107 c_pp.SetCompleted(&c_cp)
108 c_cp.DoubleProjective(&c_pp)
109 c_pp.SetCompleted(&c_cp)
110 c_cp.DoubleProjective(&c_pp)
111 cp.SetCompleted(&c_cp)
112 }
113 }
114
115 // Compute 4-bit signed window for the scalar s
116 func computeScalarWindow4(s *[32]byte, w *[64]int8) {
117 for i := 0; i < 32; i++ {
118 w[2*i] = int8(s[i] & 15)
119 w[2*i+1] = int8((s[i] >> 4) & 15)
120 }
121 carry := int8(0)
122 for i := 0; i < 63; i++ {
123 w[i] += carry
124 carry = (w[i] + 8) >> 4
125 w[i] -= carry << 4
126 }
127 w[63] += carry
128 }
129
130 // Set p to s * q, where t was computed for q using t.Compute(q).
131 func (t *ScalarMultTable) ScalarMult(p *ExtendedPoint, s *[32]byte) {
132 var w [64]int8
133 computeScalarWindow4(s, &w)
134
135 p.SetZero()
136 var np NielsPoint
137 var cp CompletedPoint
138 var pp ProjectivePoint
139
140 for i := int32(0); i < 32; i++ {
141 t.selectPoint(&np, i, int32(w[2*i+1]))
142 cp.AddExtendedNiels(p, &np)
143 p.SetCompleted(&cp)
144 }
145
146 cp.DoubleExtended(p)
147 pp.SetCompleted(&cp)
148 cp.DoubleProjective(&pp)
149 pp.SetCompleted(&cp)
150 cp.DoubleProjective(&pp)
151 pp.SetCompleted(&cp)
152 cp.DoubleProjective(&pp)
153 p.SetCompleted(&cp)
154
155 for i := int32(0); i < 32; i++ {
156 t.selectPoint(&np, i, int32(w[2*i]))
157 cp.AddExtendedNiels(p, &np)
158 p.SetCompleted(&cp)
159 }
160 }
161
162 func (t *ScalarMultTable) VarTimeScalarMult(p *ExtendedPoint, s *[32]byte) {
163 var w [64]int8
164 computeScalarWindow4(s, &w)
165
166 p.SetZero()
167 var np NielsPoint
168 var cp CompletedPoint
169 var pp ProjectivePoint
170
171 for i := int32(0); i < 32; i++ {
172 if t.varTimeSelectPoint(&np, i, int32(w[2*i+1])) {
173 cp.AddExtendedNiels(p, &np)
174 p.SetCompleted(&cp)
175 }
176 }
177
178 cp.DoubleExtended(p)
179 pp.SetCompleted(&cp)
180 cp.DoubleProjective(&pp)
181 pp.SetCompleted(&cp)
182 cp.DoubleProjective(&pp)
183 pp.SetCompleted(&cp)
184 cp.DoubleProjective(&pp)
185 p.SetCompleted(&cp)
186
187 for i := int32(0); i < 32; i++ {
188 if t.varTimeSelectPoint(&np, i, int32(w[2*i])) {
189 cp.AddExtendedNiels(p, &np)
190 p.SetCompleted(&cp)
191 }
192 }
193 }
194
195 func (t *ScalarMultTable) selectPoint(p *NielsPoint, pos int32, b int32) {
196 bNegative := negative(b)
197 bAbs := b - (((-bNegative) & b) << 1)
198 p.SetZero()
199 for i := int32(0); i < 8; i++ {
200 p.ConditionalSet(&t[pos][i], equal30(bAbs, i+1))
201 }
202 var negP NielsPoint
203 negP.Neg(p)
204 p.ConditionalSet(&negP, bNegative)
205 }
206
207 func (t *ScalarMultTable) varTimeSelectPoint(p *NielsPoint, pos int32, b int32) bool {
208 if b == 0 {
209 return false
210 }
211 if b < 0 {
212 p.Neg(&t[pos][-b-1])
213 } else {
214 p.Set(&t[pos][b-1])
215 }
216 return true
217 }