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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Operations in the secp256k1 group.
//!
//! The purpose of this module is to hold all the operations that can be done
//! with secp256k1 [`Points`] and [`Scalars`].  Usually, you shouldn't call
//! these directly but instead use the group [`g!`] and scalar [`s!`] expression
//! macros which compile your expressions into (potentially more efficient)
//! calls to these functions.
//!
//! Most of the functions here are [`specialized`] so the compiler may be able to
//! choose a faster algorithm depending on the arguments. For example scalar
//! multiplications are faster points marked `BasePoint` like [`G`], so in the
//! following snippet computing `X1` will be computed faster than `X2` even
//! though the same function is being called.
//! ```
//! use secp256kfun::{marker::*, op, Scalar, G};
//! let x = Scalar::random(&mut rand::thread_rng());
//! let X1 = op::scalar_mul_point(&x, G); // fast
//! let H = &G.clone().mark::<Normal>(); // scrub `BasePoint` marker
//! let X2 = op::scalar_mul_point(&x, &H); // slow
//! assert_eq!(X1, X2);
//! ```
//! [`Points`]: crate::Point
//! [`Scalars`]: crate::Scalar
//! [`specialized`]: https://github.com/rust-lang/rust/issues/31844
//! [`G`]: crate::G
use crate::{
    backend::{self, ConstantTime, TimeSensitive, VariableTime},
    marker::*,
    Point, Scalar, XOnly,
};

/// Computes `x * A + y * B` more efficiently than calling [`scalar_mul_point`] twice.
pub fn double_mul<ZA, SA, TA, ZX, SX, ZB, SB, TB, ZY, SY>(
    x: &Scalar<SX, ZX>,
    A: &Point<TA, SA, ZA>,
    y: &Scalar<SY, ZY>,
    B: &Point<TB, SB, ZB>,
) -> Point<Jacobian, Public, Zero> {
    Point::from_inner(DoubleMul::double_mul((x, A, y, B)), Jacobian)
}

/// Computes multiplies the point `P` by the scalar `x`.
pub fn scalar_mul_point<Z1, S1, T2, S2, Z2>(
    x: &Scalar<S1, Z1>,
    P: &Point<T2, S2, Z2>,
) -> Point<Jacobian, Public, Z1::Out>
where
    Z1: DecideZero<Z2>,
{
    Point::from_inner(MulPoint::mul_point(x, P), Jacobian)
}

/// Multiplies two scalars together (modulo the curve order)
pub fn scalar_mul<Z1, Z2, S1, S2>(x: &Scalar<S1, Z1>, y: &Scalar<S2, Z2>) -> Scalar<Secret, Z1::Out>
where
    Z1: DecideZero<Z2>,
{
    Scalar::from_inner(ScalarBinary::mul((x, y)))
}

/// Adds two scalars together (modulo the curve order)
pub fn scalar_add<Z1, Z2, S1, S2>(x: &Scalar<S1, Z1>, y: &Scalar<S2, Z2>) -> Scalar<Secret, Zero> {
    Scalar::from_inner(ScalarBinary::add((x, y)))
}

/// Subtracts one scalar from another
pub fn scalar_sub<Z1, Z2, S1, S2>(x: &Scalar<S1, Z1>, y: &Scalar<S2, Z2>) -> Scalar<Secret, Zero> {
    Scalar::from_inner(ScalarBinary::sub((x, y)))
}

/// Subtracts one point from another
pub fn point_sub<Z1, S1, T1, Z2, S2, T2>(
    A: &Point<T1, S1, Z1>,
    B: &Point<T2, S2, Z2>,
) -> Point<Jacobian, Public, Zero> {
    Point::from_inner(PointBinary::sub((A, B)), Jacobian)
}

/// Adds two points together
pub fn point_add<Z1, Z2, S1, S2, T1, T2>(
    A: &Point<T1, S1, Z1>,
    B: &Point<T2, S2, Z2>,
) -> Point<Jacobian, Public, Zero> {
    Point::from_inner(PointBinary::add((A, B)), Jacobian)
}

pub(crate) trait PointBinary {
    fn add(self) -> backend::Point;
    fn sub(self) -> backend::Point;
    fn eq(self) -> bool;
}

impl<T1, S1, Z1, T2, S2, Z2> PointBinary for (&Point<S1, T1, Z1>, &Point<S2, T2, Z2>) {
    default fn add(self) -> backend::Point {
        let (lhs, rhs) = self;
        ConstantTime::point_add_point(&lhs.0, &rhs.0)
    }

    default fn sub(self) -> backend::Point {
        let (lhs, rhs) = self;
        ConstantTime::point_sub_point(&lhs.0, &rhs.0)
    }

    default fn eq(self) -> bool {
        let (lhs, rhs) = self;
        ConstantTime::point_eq_point(&lhs.0, &rhs.0)
    }
}

impl<Z1, Z2, T1: Normalized, S1, S2, T2> PointBinary for (&Point<S1, T1, Z1>, &Point<T2, S2, Z2>) {
    default fn add(self) -> backend::Point {
        let (lhs, rhs) = self;
        ConstantTime::point_add_norm_point(&rhs.0, &lhs.0)
    }

    default fn sub(self) -> backend::Point {
        let (lhs, rhs) = self;
        ConstantTime::norm_point_sub_point(&lhs.0, &rhs.0)
    }

    default fn eq(self) -> bool {
        let (lhs, rhs) = self;
        ConstantTime::point_eq_norm_point(&rhs.0, &lhs.0)
    }
}

impl<Z1, Z2, S1, S2, T2: Normalized> PointBinary
    for (&Point<S1, Jacobian, Z1>, &Point<T2, S2, Z2>)
{
    default fn add(self) -> backend::Point {
        let (lhs, rhs) = self;
        ConstantTime::point_add_norm_point(&lhs.0, &rhs.0)
    }

    default fn sub(self) -> backend::Point {
        let (lhs, rhs) = self;
        ConstantTime::point_sub_norm_point(&lhs.0, &rhs.0)
    }

    default fn eq(self) -> bool {
        let (lhs, rhs) = self;
        ConstantTime::point_eq_norm_point(&lhs.0, &rhs.0)
    }
}

impl<Z1, Z2> PointBinary for (&Point<Public, Jacobian, Z1>, &Point<Jacobian, Public, Z2>) {
    fn add(self) -> backend::Point {
        let (lhs, rhs) = self;
        VariableTime::point_add_point(&lhs.0, &rhs.0)
    }

    fn sub(self) -> backend::Point {
        let (lhs, rhs) = self;
        VariableTime::point_sub_point(&lhs.0, &rhs.0)
    }

    fn eq(self) -> bool {
        let (lhs, rhs) = self;
        VariableTime::point_eq_point(&lhs.0, &rhs.0)
    }
}

impl<Z1, Z2, T1: Normalized, T2> PointBinary for (&Point<Public, T1, Z1>, &Point<T2, Public, Z2>) {
    fn add(self) -> backend::Point {
        let (lhs, rhs) = self;
        VariableTime::point_add_norm_point(&rhs.0, &lhs.0)
    }

    fn sub(self) -> backend::Point {
        let (lhs, rhs) = self;
        VariableTime::norm_point_sub_point(&lhs.0, &rhs.0)
    }

    fn eq(self) -> bool {
        let (lhs, rhs) = self;
        VariableTime::point_eq_norm_point(&rhs.0, &lhs.0)
    }
}

impl<Z1, Z2, T2: Normalized> PointBinary
    for (&Point<Public, Jacobian, Z1>, &Point<T2, Public, Z2>)
{
    fn add(self) -> backend::Point {
        let (lhs, rhs) = self;
        VariableTime::point_add_norm_point(&lhs.0, &rhs.0)
    }

    fn sub(self) -> backend::Point {
        let (lhs, rhs) = self;
        VariableTime::point_sub_norm_point(&lhs.0, &rhs.0)
    }

    fn eq(self) -> bool {
        let (lhs, rhs) = self;
        VariableTime::point_eq_norm_point(&lhs.0, &rhs.0)
    }
}

pub(crate) trait EqXOnlySquareY {
    fn eq_xonly_square_y(&self, xonly: &XOnly<SquareY>) -> bool;
}

impl<T, S, Z> EqXOnlySquareY for Point<T, S, Z> {
    default fn eq_xonly_square_y(&self, xonly: &XOnly<SquareY>) -> bool {
        ConstantTime::point_eq_xonly_square_y(&self.0, &xonly.0)
    }
}

impl<T, Z> EqXOnlySquareY for Point<T, Public, Z> {
    fn eq_xonly_square_y(&self, xonly: &XOnly<SquareY>) -> bool {
        VariableTime::point_eq_xonly_square_y(&self.0, &xonly.0)
    }
}

pub(crate) trait MulPoint<S2, T2> {
    fn mul_point<Z2>(&self, rhs: &Point<T2, S2, Z2>) -> backend::Point;
}

impl<Z1, S1, S2, T2> MulPoint<S2, T2> for Scalar<S1, Z1> {
    default fn mul_point<Z2>(&self, rhs: &Point<T2, S2, Z2>) -> backend::Point {
        ConstantTime::scalar_mul_point(&self.0, &rhs.0)
    }
}

impl<Z1, S1, S2, T2: NotBasePoint + Normalized> MulPoint<S2, T2> for Scalar<S1, Z1> {
    default fn mul_point<Z2>(&self, rhs: &Point<T2, S2, Z2>) -> backend::Point {
        ConstantTime::scalar_mul_norm_point(&self.0, &rhs.0)
    }
}

impl<Z1, S1, S2> MulPoint<S2, BasePoint> for Scalar<S1, Z1> {
    default fn mul_point<Z2>(&self, rhs: &Point<BasePoint, S2, Z2>) -> backend::Point {
        ConstantTime::scalar_mul_basepoint(&self.0, &(rhs.1).0)
    }
}

impl<Z1> MulPoint<Public, Jacobian> for Scalar<Public, Z1> {
    fn mul_point<Z2>(&self, rhs: &Point<Jacobian, Public, Z2>) -> backend::Point {
        VariableTime::scalar_mul_point(&self.0, &rhs.0)
    }
}

impl<Z1, T2: Normalized + NotBasePoint> MulPoint<Public, T2> for Scalar<Public, Z1> {
    fn mul_point<Z2>(&self, rhs: &Point<T2, Public, Z2>) -> backend::Point {
        VariableTime::scalar_mul_norm_point(&self.0, &rhs.0)
    }
}

impl<Z1> MulPoint<Public, BasePoint> for Scalar<Public, Z1> {
    fn mul_point<Z2>(&self, rhs: &Point<BasePoint, Public, Z2>) -> backend::Point {
        VariableTime::scalar_mul_basepoint(&self.0, &(rhs.1).0)
    }
}

pub(crate) trait DoubleMul {
    fn double_mul(self) -> backend::Point;
}

impl<XZ, XS, AZ, AS, AT, YZ, YS, BZ, BS, BT> DoubleMul
    for (
        &Scalar<XS, XZ>,
        &Point<AT, AS, AZ>,
        &Scalar<YS, YZ>,
        &Point<BT, BS, BZ>,
    )
{
    default fn double_mul(self) -> backend::Point {
        let (x, A, y, B) = self;
        let xA = x.mul_point(A);
        let yB = y.mul_point(B);
        VariableTime::point_add_point(&xA, &yB)
    }
}

impl<XZ, AZ, YZ, BZ, BT> DoubleMul
    for (
        &Scalar<Public, XZ>,
        &Point<BasePoint, Public, AZ>,
        &Scalar<Public, YZ>,
        &Point<BT, Public, BZ>,
    )
{
    default fn double_mul(self) -> backend::Point {
        let (x, A, y, B) = self;
        VariableTime::basepoint_double_mul(&x.0, &(A.1).0, &y.0, &B.0)
    }
}

impl<XZ, AZ, YZ, BZ, AT: Normalized + NotBasePoint> DoubleMul
    for (
        &Scalar<Public, XZ>,
        &Point<AT, Public, AZ>,
        &Scalar<Public, YZ>,
        &Point<BasePoint, Public, BZ>,
    )
{
    default fn double_mul(self) -> backend::Point {
        let (x, A, y, B) = self;
        VariableTime::basepoint_double_mul(&y.0, &(B.1).0, &x.0, &A.0)
    }
}

pub(crate) trait ScalarBinary {
    fn mul(self) -> backend::Scalar;
    fn add(self) -> backend::Scalar;
    fn sub(self) -> backend::Scalar;
    fn eq(self) -> bool;
}

impl<Z1, S1, Z2, S2> ScalarBinary for (&Scalar<S1, Z1>, &Scalar<S2, Z2>) {
    default fn mul(self) -> backend::Scalar {
        let (lhs, rhs) = self;
        ConstantTime::scalar_mul(&lhs.0, &rhs.0)
    }

    default fn add(self) -> backend::Scalar {
        let (lhs, rhs) = self;
        ConstantTime::scalar_add(&lhs.0, &rhs.0)
    }

    default fn sub(self) -> backend::Scalar {
        let (lhs, rhs) = self;
        ConstantTime::scalar_sub(&lhs.0, &rhs.0)
    }

    default fn eq(self) -> bool {
        let (lhs, rhs) = self;
        ConstantTime::scalar_eq(&lhs.0, &rhs.0)
    }
}

impl<Z1, Z2> ScalarBinary for (&Scalar<Public, Z1>, &Scalar<Public, Z2>) {
    fn mul(self) -> backend::Scalar {
        let (lhs, rhs) = self;
        VariableTime::scalar_mul(&lhs.0, &rhs.0)
    }

    fn add(self) -> backend::Scalar {
        let (lhs, rhs) = self;
        VariableTime::scalar_add(&lhs.0, &rhs.0)
    }

    fn sub(self) -> backend::Scalar {
        let (lhs, rhs) = self;
        VariableTime::scalar_sub(&lhs.0, &rhs.0)
    }

    fn eq(self) -> bool {
        let (lhs, rhs) = self;
        ConstantTime::scalar_eq(&lhs.0, &rhs.0)
    }
}

pub(crate) trait ScalarUnary {
    fn negate(&self) -> backend::Scalar;
    fn invert(&self) -> backend::Scalar;
    fn conditional_negate(&mut self, cond: bool);
    fn is_high(&self) -> bool;
    fn is_zero(&self) -> bool;
}

impl<Z, S> ScalarUnary for Scalar<S, Z> {
    default fn negate(&self) -> backend::Scalar {
        let mut negated = self.0.clone();
        ConstantTime::scalar_cond_negate(&mut negated, true);
        negated
    }

    default fn invert(&self) -> backend::Scalar {
        ConstantTime::scalar_invert(&self.0)
    }

    default fn conditional_negate(&mut self, cond: bool) {
        ConstantTime::scalar_cond_negate(&mut self.0, cond)
    }

    default fn is_high(&self) -> bool {
        ConstantTime::scalar_is_high(&self.0)
    }

    default fn is_zero(&self) -> bool {
        ConstantTime::scalar_is_zero(&self.0)
    }
}

impl<Z> ScalarUnary for Scalar<Public, Z> {
    fn negate(&self) -> backend::Scalar {
        let mut negated = self.0.clone();
        VariableTime::scalar_cond_negate(&mut negated, true);
        negated
    }

    fn invert(&self) -> backend::Scalar {
        VariableTime::scalar_invert(&self.0)
    }

    fn conditional_negate(&mut self, cond: bool) {
        VariableTime::scalar_cond_negate(&mut self.0, cond)
    }

    fn is_high(&self) -> bool {
        VariableTime::scalar_is_high(&self.0)
    }

    fn is_zero(&self) -> bool {
        VariableTime::scalar_is_zero(&self.0)
    }
}

pub(crate) trait PointUnary {
    fn negate(self) -> backend::Point;
    fn conditional_negate(self, cond: bool) -> backend::Point;
    fn normalize(self) -> backend::Point;
}

pub(crate) trait NormPointUnary {
    fn is_y_even(&self) -> bool;
    fn is_y_square(&self) -> bool;
}

impl<T, S, Z> PointUnary for Point<T, Z, S> {
    default fn negate(mut self) -> backend::Point {
        ConstantTime::point_neg(&mut self.0);
        self.0
    }

    default fn conditional_negate(mut self, cond: bool) -> backend::Point {
        ConstantTime::point_conditional_negate(&mut self.0, cond);
        self.0
    }

    default fn normalize(mut self) -> backend::Point {
        ConstantTime::point_normalize(&mut self.0);
        self.0
    }
}

impl<T: Normalized, S, Z> PointUnary for Point<T, Z, S> {
    default fn negate(mut self) -> backend::Point {
        ConstantTime::norm_point_neg(&mut self.0);
        self.0
    }

    default fn conditional_negate(mut self, cond: bool) -> backend::Point {
        ConstantTime::norm_point_conditional_negate(&mut self.0, cond);
        self.0
    }

    default fn normalize(self) -> backend::Point {
        self.0
    }
}

impl<Z> PointUnary for Point<Jacobian, Z, Public> {
    default fn negate(mut self) -> backend::Point {
        VariableTime::point_neg(&mut self.0);
        self.0
    }

    default fn conditional_negate(mut self, cond: bool) -> backend::Point {
        VariableTime::point_conditional_negate(&mut self.0, cond);
        self.0
    }

    default fn normalize(mut self) -> backend::Point {
        VariableTime::point_normalize(&mut self.0);
        self.0
    }
}

impl<T: Normalized, Z> PointUnary for Point<T, Z, Public> {
    fn negate(mut self) -> backend::Point {
        VariableTime::norm_point_neg(&mut self.0);
        self.0
    }

    fn conditional_negate(mut self, cond: bool) -> backend::Point {
        VariableTime::norm_point_conditional_negate(&mut self.0, cond);
        self.0
    }
}

impl<T: Normalized, Z, S> NormPointUnary for Point<T, Z, S> {
    default fn is_y_square(&self) -> bool {
        ConstantTime::norm_point_is_y_square(&self.0)
    }

    default fn is_y_even(&self) -> bool {
        ConstantTime::norm_point_is_y_even(&self.0)
    }
}

impl<T: Normalized, Z> NormPointUnary for Point<T, Z, Public> {
    fn is_y_square(&self) -> bool {
        VariableTime::norm_point_is_y_square(&self.0)
    }

    fn is_y_even(&self) -> bool {
        VariableTime::norm_point_is_y_even(&self.0)
    }
}