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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use super::{Scalar, MODULUS};
use crate::ORDER;
use elliptic_curve::{
bigint::{Limb, U256, U512},
subtle::{Choice, ConditionallySelectable},
};
const NEG_MODULUS: [u64; 4] = [!MODULUS[0] + 1, !MODULUS[1], !MODULUS[2], !MODULUS[3]];
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct WideScalar(pub(super) U512);
impl WideScalar {
pub const fn from_bytes(bytes: &[u8; 64]) -> Self {
Self(U512::from_be_slice(bytes))
}
#[inline(always)] pub fn mul_wide(a: &Scalar, b: &Scalar) -> Self {
let a = a.0.to_uint_array();
let b = b.0.to_uint_array();
let c0 = 0;
let c1 = 0;
let c2 = 0;
let (c0, c1) = muladd_fast(a[0], b[0], c0, c1);
let (l0, c0, c1) = (c0, c1, 0);
let (c0, c1, c2) = muladd(a[0], b[1], c0, c1, c2);
let (c0, c1, c2) = muladd(a[1], b[0], c0, c1, c2);
let (l1, c0, c1, c2) = (c0, c1, c2, 0);
let (c0, c1, c2) = muladd(a[0], b[2], c0, c1, c2);
let (c0, c1, c2) = muladd(a[1], b[1], c0, c1, c2);
let (c0, c1, c2) = muladd(a[2], b[0], c0, c1, c2);
let (l2, c0, c1, c2) = (c0, c1, c2, 0);
let (c0, c1, c2) = muladd(a[0], b[3], c0, c1, c2);
let (c0, c1, c2) = muladd(a[1], b[2], c0, c1, c2);
let (c0, c1, c2) = muladd(a[2], b[1], c0, c1, c2);
let (c0, c1, c2) = muladd(a[3], b[0], c0, c1, c2);
let (l3, c0, c1, c2) = (c0, c1, c2, 0);
let (c0, c1, c2) = muladd(a[1], b[3], c0, c1, c2);
let (c0, c1, c2) = muladd(a[2], b[2], c0, c1, c2);
let (c0, c1, c2) = muladd(a[3], b[1], c0, c1, c2);
let (l4, c0, c1, c2) = (c0, c1, c2, 0);
let (c0, c1, c2) = muladd(a[2], b[3], c0, c1, c2);
let (c0, c1, c2) = muladd(a[3], b[2], c0, c1, c2);
let (l5, c0, c1, _c2) = (c0, c1, c2, 0);
let (c0, c1) = muladd_fast(a[3], b[3], c0, c1);
let (l6, c0, _c1) = (c0, c1, 0);
let l7 = c0;
Self(U512::from_uint_array([l0, l1, l2, l3, l4, l5, l6, l7]))
}
pub(crate) fn mul_shift_vartime(a: &Scalar, b: &Scalar, shift: usize) -> Scalar {
debug_assert!(shift >= 256);
fn ifelse(c: bool, x: u64, y: u64) -> u64 {
if c {
x
} else {
y
}
}
let l = Self::mul_wide(a, b).0.to_uint_array();
let shiftlimbs = shift >> 6;
let shiftlow = shift & 0x3F;
let shifthigh = 64 - shiftlow;
let r0 = ifelse(
shift < 512,
(l[shiftlimbs] >> shiftlow)
| ifelse(
shift < 448 && shiftlow != 0,
l[1 + shiftlimbs] << shifthigh,
0,
),
0,
);
let r1 = ifelse(
shift < 448,
(l[1 + shiftlimbs] >> shiftlow)
| ifelse(
shift < 448 && shiftlow != 0,
l[2 + shiftlimbs] << shifthigh,
0,
),
0,
);
let r2 = ifelse(
shift < 384,
(l[2 + shiftlimbs] >> shiftlow)
| ifelse(
shift < 320 && shiftlow != 0,
l[3 + shiftlimbs] << shifthigh,
0,
),
0,
);
let r3 = ifelse(shift < 320, l[3 + shiftlimbs] >> shiftlow, 0);
let res = Scalar(U256::from_uint_array([r0, r1, r2, r3]));
let c = (l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1;
Scalar::conditional_select(&res, &res.add(&Scalar::ONE), Choice::from(c as u8))
}
fn reduce_impl(&self, modulus_minus_one: bool) -> Scalar {
let neg_modulus0 = if modulus_minus_one {
NEG_MODULUS[0] + 1
} else {
NEG_MODULUS[0]
};
let modulus = if modulus_minus_one {
ORDER.wrapping_sub(&U256::ONE)
} else {
ORDER
};
let w = self.0.to_uint_array();
let n0 = w[4];
let n1 = w[5];
let n2 = w[6];
let n3 = w[7];
let c0 = w[0];
let c1 = 0;
let c2 = 0;
let (c0, c1) = muladd_fast(n0, neg_modulus0, c0, c1);
let (m0, c0, c1) = (c0, c1, 0);
let (c0, c1) = sumadd_fast(w[1], c0, c1);
let (c0, c1, c2) = muladd(n1, neg_modulus0, c0, c1, c2);
let (c0, c1, c2) = muladd(n0, NEG_MODULUS[1], c0, c1, c2);
let (m1, c0, c1, c2) = (c0, c1, c2, 0);
let (c0, c1, c2) = sumadd(w[2], c0, c1, c2);
let (c0, c1, c2) = muladd(n2, neg_modulus0, c0, c1, c2);
let (c0, c1, c2) = muladd(n1, NEG_MODULUS[1], c0, c1, c2);
let (c0, c1, c2) = sumadd(n0, c0, c1, c2);
let (m2, c0, c1, c2) = (c0, c1, c2, 0);
let (c0, c1, c2) = sumadd(w[3], c0, c1, c2);
let (c0, c1, c2) = muladd(n3, neg_modulus0, c0, c1, c2);
let (c0, c1, c2) = muladd(n2, NEG_MODULUS[1], c0, c1, c2);
let (c0, c1, c2) = sumadd(n1, c0, c1, c2);
let (m3, c0, c1, c2) = (c0, c1, c2, 0);
let (c0, c1, c2) = muladd(n3, NEG_MODULUS[1], c0, c1, c2);
let (c0, c1, c2) = sumadd(n2, c0, c1, c2);
let (m4, c0, c1, _c2) = (c0, c1, c2, 0);
let (c0, c1) = sumadd_fast(n3, c0, c1);
let (m5, c0, _c1) = (c0, c1, 0);
debug_assert!(c0 <= 1);
let m6 = c0;
let c0 = m0;
let c1 = 0;
let c2 = 0;
let (c0, c1) = muladd_fast(m4, neg_modulus0, c0, c1);
let (p0, c0, c1) = (c0, c1, 0);
let (c0, c1) = sumadd_fast(m1, c0, c1);
let (c0, c1, c2) = muladd(m5, neg_modulus0, c0, c1, c2);
let (c0, c1, c2) = muladd(m4, NEG_MODULUS[1], c0, c1, c2);
let (p1, c0, c1) = (c0, c1, 0);
let (c0, c1, c2) = sumadd(m2, c0, c1, c2);
let (c0, c1, c2) = muladd(m6, neg_modulus0, c0, c1, c2);
let (c0, c1, c2) = muladd(m5, NEG_MODULUS[1], c0, c1, c2);
let (c0, c1, c2) = sumadd(m4, c0, c1, c2);
let (p2, c0, c1, _c2) = (c0, c1, c2, 0);
let (c0, c1) = sumadd_fast(m3, c0, c1);
let (c0, c1) = muladd_fast(m6, NEG_MODULUS[1], c0, c1);
let (c0, c1) = sumadd_fast(m5, c0, c1);
let (p3, c0, _c1) = (c0, c1, 0);
let p4 = c0 + m6;
debug_assert!(p4 <= 2);
let mut c = (p0 as u128) + (neg_modulus0 as u128) * (p4 as u128);
let r0 = (c & 0xFFFFFFFFFFFFFFFFu128) as u64;
c >>= 64;
c += (p1 as u128) + (NEG_MODULUS[1] as u128) * (p4 as u128);
let r1 = (c & 0xFFFFFFFFFFFFFFFFu128) as u64;
c >>= 64;
c += (p2 as u128) + (p4 as u128);
let r2 = (c & 0xFFFFFFFFFFFFFFFFu128) as u64;
c >>= 64;
c += p3 as u128;
let r3 = (c & 0xFFFFFFFFFFFFFFFFu128) as u64;
c >>= 64;
let r = U256::from([r0, r1, r2, r3]);
let (r2, underflow) = r.sbb(&modulus, Limb::ZERO);
let high_bit = Choice::from(c as u8);
let underflow = Choice::from((underflow.0 >> 63) as u8);
Scalar(U256::conditional_select(&r, &r2, !underflow | high_bit))
}
#[inline(always)] pub(super) fn reduce(&self) -> Scalar {
self.reduce_impl(false)
}
pub(super) fn reduce_nonzero(&self) -> Scalar {
self.reduce_impl(true) + Scalar::ONE
}
}
#[inline(always)]
fn ct_less(a: u64, b: u64) -> u64 {
(a < b) as u64
}
fn sumadd(a: u64, c0: u64, c1: u64, c2: u64) -> (u64, u64, u64) {
let new_c0 = c0.wrapping_add(a); let over = ct_less(new_c0, a);
let new_c1 = c1.wrapping_add(over); let new_c2 = c2 + ct_less(new_c1, over); (new_c0, new_c1, new_c2)
}
fn sumadd_fast(a: u64, c0: u64, c1: u64) -> (u64, u64) {
let new_c0 = c0.wrapping_add(a); let new_c1 = c1 + ct_less(new_c0, a); debug_assert!((new_c1 != 0) | (new_c0 >= a));
(new_c0, new_c1)
}
fn muladd(a: u64, b: u64, c0: u64, c1: u64, c2: u64) -> (u64, u64, u64) {
let t = (a as u128) * (b as u128);
let th = (t >> 64) as u64; let tl = t as u64;
let new_c0 = c0.wrapping_add(tl); let new_th = th + if new_c0 < tl { 1 } else { 0 }; let new_c1 = c1.wrapping_add(new_th); let new_c2 = c2 + ct_less(new_c1, new_th); debug_assert!((new_c1 >= new_th) || (new_c2 != 0));
(new_c0, new_c1, new_c2)
}
fn muladd_fast(a: u64, b: u64, c0: u64, c1: u64) -> (u64, u64) {
let t = (a as u128) * (b as u128);
let th = (t >> 64) as u64; let tl = t as u64;
let new_c0 = c0.wrapping_add(tl); let new_th = th + ct_less(new_c0, tl); let new_c1 = c1 + new_th; debug_assert!(new_c1 >= new_th);
(new_c0, new_c1)
}