1
2
3
4
5 package edwards25519
6
7 import (
8 "crypto/internal/fips140/subtle"
9 )
10
11
12 type projLookupTable struct {
13 points [8]projCached
14 }
15
16
17 type affineLookupTable struct {
18 points [8]affineCached
19 }
20
21
22 type nafLookupTable5 struct {
23 points [8]projCached
24 }
25
26
27 type nafLookupTable8 struct {
28 points [64]affineCached
29 }
30
31
32
33
34 func (v *projLookupTable) FromP3(q *Point) {
35
36
37 v.points[0].FromP3(q)
38 tmpP3 := Point{}
39 tmpP1xP1 := projP1xP1{}
40 for i := 0; i < 7; i++ {
41
42
43
44 v.points[i+1].FromP3(tmpP3.fromP1xP1(tmpP1xP1.Add(q, &v.points[i])))
45 }
46 }
47
48
49 func (v *affineLookupTable) FromP3(q *Point) {
50
51
52 v.points[0].FromP3(q)
53 tmpP3 := Point{}
54 tmpP1xP1 := projP1xP1{}
55 for i := 0; i < 7; i++ {
56
57 v.points[i+1].FromP3(tmpP3.fromP1xP1(tmpP1xP1.AddAffine(q, &v.points[i])))
58 }
59 }
60
61
62 func (v *nafLookupTable5) FromP3(q *Point) {
63
64
65 v.points[0].FromP3(q)
66 q2 := Point{}
67 q2.Add(q, q)
68 tmpP3 := Point{}
69 tmpP1xP1 := projP1xP1{}
70 for i := 0; i < 7; i++ {
71 v.points[i+1].FromP3(tmpP3.fromP1xP1(tmpP1xP1.Add(&q2, &v.points[i])))
72 }
73 }
74
75
76 func (v *nafLookupTable8) FromP3(q *Point) {
77 v.points[0].FromP3(q)
78 q2 := Point{}
79 q2.Add(q, q)
80 tmpP3 := Point{}
81 tmpP1xP1 := projP1xP1{}
82 for i := 0; i < 63; i++ {
83 v.points[i+1].FromP3(tmpP3.fromP1xP1(tmpP1xP1.AddAffine(&q2, &v.points[i])))
84 }
85 }
86
87
88
89
90 func (v *projLookupTable) SelectInto(dest *projCached, x int8) {
91
92 xmask := x >> 7
93 xabs := uint8((x + xmask) ^ xmask)
94
95 dest.Zero()
96 for j := 1; j <= 8; j++ {
97
98 cond := subtle.ConstantTimeByteEq(xabs, uint8(j))
99 dest.Select(&v.points[j-1], dest, cond)
100 }
101
102 dest.CondNeg(int(xmask & 1))
103 }
104
105
106 func (v *affineLookupTable) SelectInto(dest *affineCached, x int8) {
107
108 xmask := x >> 7
109 xabs := uint8((x + xmask) ^ xmask)
110
111 dest.Zero()
112 for j := 1; j <= 8; j++ {
113
114 cond := subtle.ConstantTimeByteEq(xabs, uint8(j))
115 dest.Select(&v.points[j-1], dest, cond)
116 }
117
118 dest.CondNeg(int(xmask & 1))
119 }
120
121
122 func (v *nafLookupTable5) SelectInto(dest *projCached, x int8) {
123 *dest = v.points[x/2]
124 }
125
126
127 func (v *nafLookupTable8) SelectInto(dest *affineCached, x int8) {
128 *dest = v.points[x/2]
129 }
130
View as plain text