Skip to main content

p384/arithmetic/
tables.rs

1//! Precomputed tables (optional).
2
3use super::NistP384;
4use crate::ProjectivePoint;
5use primeorder::PrimeCurveWithBasepointTable;
6
7/// Window size for the basepoint table (1 + 48-byte modulus)
8pub(super) const WINDOW_SIZE: usize = 49;
9
10/// Basepoint table for multiples of NIST P-384'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 NistP384 {
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::{NistP384, ProjectivePoint, Scalar};
27    use primeorder::MulBackend;
28
29    /// Backend based on precomputed tables.
30    pub struct PrecomputedTables;
31
32    impl MulBackend<NistP384> 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}