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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::arithmetic::{scalar::Scalar, ProjectivePoint};
use core::ops::{Mul, MulAssign};
use elliptic_curve::subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
struct LookupTable([ProjectivePoint; 8]);
impl From<&ProjectivePoint> for LookupTable {
fn from(p: &ProjectivePoint) -> Self {
let mut points = [*p; 8];
for j in 0..7 {
points[j + 1] = p + &points[j];
}
LookupTable(points)
}
}
impl LookupTable {
pub fn select(&self, x: i8) -> ProjectivePoint {
debug_assert!(x >= -8);
debug_assert!(x <= 8);
let xmask = x >> 7;
let xabs = (x + xmask) ^ xmask;
let mut t = ProjectivePoint::identity();
for j in 1..9 {
let c = (xabs as u8).ct_eq(&(j as u8));
t.conditional_assign(&self.0[j - 1], c);
}
let neg_mask = Choice::from((xmask & 1) as u8);
t.conditional_assign(&-t, neg_mask);
t
}
}
const MINUS_LAMBDA: Scalar = Scalar::from_bytes_unchecked(&[
0xac, 0x9c, 0x52, 0xb3, 0x3f, 0xa3, 0xcf, 0x1f, 0x5a, 0xd9, 0xe3, 0xfd, 0x77, 0xed, 0x9b, 0xa4,
0xa8, 0x80, 0xb9, 0xfc, 0x8e, 0xc7, 0x39, 0xc2, 0xe0, 0xcf, 0xc8, 0x10, 0xb5, 0x12, 0x83, 0xcf,
]);
const MINUS_B1: Scalar = Scalar::from_bytes_unchecked(&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe4, 0x43, 0x7e, 0xd6, 0x01, 0x0e, 0x88, 0x28, 0x6f, 0x54, 0x7f, 0xa9, 0x0a, 0xbf, 0xe4, 0xc3,
]);
const MINUS_B2: Scalar = Scalar::from_bytes_unchecked(&[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x8a, 0x28, 0x0a, 0xc5, 0x07, 0x74, 0x34, 0x6d, 0xd7, 0x65, 0xcd, 0xa8, 0x3d, 0xb1, 0x56, 0x2c,
]);
const G1: Scalar = Scalar::from_bytes_unchecked(&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x86,
0xd2, 0x21, 0xa7, 0xd4, 0x6b, 0xcd, 0xe8, 0x6c, 0x90, 0xe4, 0x92, 0x84, 0xeb, 0x15, 0x3d, 0xab,
]);
const G2: Scalar = Scalar::from_bytes_unchecked(&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe4, 0x43,
0x7e, 0xd6, 0x01, 0x0e, 0x88, 0x28, 0x6f, 0x54, 0x7f, 0xa9, 0x0a, 0xbf, 0xe4, 0xc4, 0x22, 0x12,
]);
fn decompose_scalar(k: &Scalar) -> (Scalar, Scalar) {
let c1 = k.mul_shift_var(&G1, 272);
let c2 = k.mul_shift_var(&G2, 272);
let c1 = c1 * MINUS_B1;
let c2 = c2 * MINUS_B2;
let r2 = c1 + c2;
let r1 = k + r2 * MINUS_LAMBDA;
(r1, r2)
}
fn to_radix_16_half(x: &Scalar) -> [i8; 33] {
let mut output = [0i8; 33];
let bytes = x.to_bytes();
for i in 0..16 {
output[2 * i] = (bytes[31 - i] & 0xf) as i8;
output[2 * i + 1] = ((bytes[31 - i] >> 4) & 0xf) as i8;
}
debug_assert!((x >> 128).is_zero().unwrap_u8() == 1);
for i in 0..32 {
let carry = (output[i] + 8) >> 4;
output[i] -= carry << 4;
output[i + 1] += carry;
}
output
}
fn mul_windowed(x: &ProjectivePoint, k: &Scalar) -> ProjectivePoint {
let (r1, r2) = decompose_scalar(k);
let x_beta = x.endomorphism();
let r1_sign = r1.is_high();
let r1_c = Scalar::conditional_select(&r1, &-r1, r1_sign);
let r2_sign = r2.is_high();
let r2_c = Scalar::conditional_select(&r2, &-r2, r2_sign);
let table1 = LookupTable::from(&ProjectivePoint::conditional_select(x, &-x, r1_sign));
let table2 = LookupTable::from(&ProjectivePoint::conditional_select(
&x_beta, &-x_beta, r2_sign,
));
let digits1 = to_radix_16_half(&r1_c);
let digits2 = to_radix_16_half(&r2_c);
let mut acc = table1.select(digits1[32]) + table2.select(digits2[32]);
for i in (0..32).rev() {
for _j in 0..4 {
acc = acc.double();
}
acc += &table1.select(digits1[i]);
acc += &table2.select(digits2[i]);
}
acc
}
impl Mul<Scalar> for ProjectivePoint {
type Output = ProjectivePoint;
fn mul(self, other: Scalar) -> ProjectivePoint {
mul_windowed(&self, &other)
}
}
impl Mul<&Scalar> for &ProjectivePoint {
type Output = ProjectivePoint;
fn mul(self, other: &Scalar) -> ProjectivePoint {
mul_windowed(self, other)
}
}
impl Mul<&Scalar> for ProjectivePoint {
type Output = ProjectivePoint;
fn mul(self, other: &Scalar) -> ProjectivePoint {
mul_windowed(&self, other)
}
}
impl MulAssign<Scalar> for ProjectivePoint {
fn mul_assign(&mut self, rhs: Scalar) {
*self = mul_windowed(self, &rhs);
}
}
impl MulAssign<&Scalar> for ProjectivePoint {
fn mul_assign(&mut self, rhs: &Scalar) {
*self = mul_windowed(self, rhs);
}
}