Skip to main content

p256/arithmetic/
tables.rs

1//! Precomputed tables (optional).
2
3use super::NistP256;
4use crate::ProjectivePoint;
5use primeorder::PrimeCurveWithBasepointTable;
6
7/// Window size for the basepoint table (1 + 32-byte modulus)
8pub(super) const WINDOW_SIZE: usize = 33;
9
10/// Basepoint table for multiples of NIST P-256's generator.
11pub(super) type BasepointTable = primeorder::BasepointTable<ProjectivePoint, WINDOW_SIZE>;
12
13/// Lazily computed basepoint table.
14pub(super) static BASEPOINT_TABLE: BasepointTable = BasepointTable::new();
15
16impl PrimeCurveWithBasepointTable<WINDOW_SIZE> for NistP256 {
17    const BASEPOINT_TABLE: &'static BasepointTable = &BASEPOINT_TABLE;
18}
19
20/// Workaround for rust-lang/rust#140653 to support MSRV 1.85: we can't use the generic
21/// implementation in `primeorder::mul_backend::PrecomputedTables` until MSRV 1.90 due to restrictions
22/// on referencing a type with interior mutability from a `const`.
23// TODO(tarcieri): remove this and switch to `primeorder::mul_backend::PrecomputedTables` when MSRV 1.90
24pub(crate) mod backend {
25    use super::BASEPOINT_TABLE;
26    use crate::{NistP256, ProjectivePoint, Scalar};
27    use primeorder::MulBackend;
28
29    /// Backend based on precomputed tables.
30    pub struct PrecomputedTables;
31
32    impl MulBackend<NistP256> for PrecomputedTables {
33        #[inline]
34        fn mul_by_generator(k: &Scalar) -> ProjectivePoint {
35            BASEPOINT_TABLE.mul(k)
36        }
37
38        #[inline]
39        fn mul_by_generator_vartime(k: &Scalar) -> ProjectivePoint {
40            BASEPOINT_TABLE.mul_vartime(k)
41        }
42    }
43}
44
45/// These are the main tests for `primeorder::BasepointTable` as we need a concrete curve to test
46/// against.
47#[cfg(test)]
48mod tests {
49    use super::BASEPOINT_TABLE;
50    use crate::{ProjectivePoint, Scalar};
51    use elliptic_curve::{
52        array::{Array, sizes::U32},
53        ops::Reduce,
54    };
55    use proptest::prelude::*;
56
57    prop_compose! {
58        fn scalar()(bytes in any::<[u8; 32]>()) -> Scalar {
59            Scalar::reduce(&Array::<u8, U32>::from(bytes))
60        }
61    }
62
63    proptest! {
64        #[test]
65        fn basepoint_table_mul(x in scalar()) {
66            let expected = ProjectivePoint::GENERATOR * &x;
67            let actual = BASEPOINT_TABLE.mul(&x);
68            prop_assert_eq!(expected, actual);
69        }
70    }
71
72    proptest! {
73        #[test]
74        fn basepoint_table_mul_vartime(x in scalar()) {
75            let expected = ProjectivePoint::GENERATOR * &x;
76            let actual = BASEPOINT_TABLE.mul_vartime(&x);
77            prop_assert_eq!(expected, actual);
78        }
79    }
80}