p256/arithmetic/
tables.rs1use super::NistP256;
4use crate::ProjectivePoint;
5use primeorder::PrimeCurveWithBasepointTable;
6
7pub(super) const WINDOW_SIZE: usize = 33;
9
10pub(super) type BasepointTable = primeorder::BasepointTable<ProjectivePoint, WINDOW_SIZE>;
12
13pub(super) static BASEPOINT_TABLE: BasepointTable = BasepointTable::new();
15
16impl PrimeCurveWithBasepointTable<WINDOW_SIZE> for NistP256 {
17 const BASEPOINT_TABLE: &'static BasepointTable = &BASEPOINT_TABLE;
18}
19
20pub(crate) mod backend {
25 use super::BASEPOINT_TABLE;
26 use crate::{NistP256, ProjectivePoint, Scalar};
27 use primeorder::MulBackend;
28
29 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#[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}