From 0786526e595dda5d912947b0c4e9e674a78fc4ed Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Wed, 8 Jul 2026 13:02:31 +0100 Subject: [PATCH] Add tiny curve tests --- curve/src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++- field/src/lib.rs | 6 ++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/curve/src/lib.rs b/curve/src/lib.rs index e2cc3ad..c08b078 100644 --- a/curve/src/lib.rs +++ b/curve/src/lib.rs @@ -298,7 +298,7 @@ impl> Scalar { #[cfg(test)] mod tests { use super::*; - use field::{Secp256k1FieldOrder as Fp, Secp256k1GroupOrder}; + use field::{MontgomeryParams, Secp256k1FieldOrder as Fp, Secp256k1GroupOrder}; type Fe = FieldElement; type Pt = Point; @@ -619,6 +619,60 @@ mod tests { assert!(E.lift(Fe::zero()).is_none()); } + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + struct Fp13; + + impl Field for Fp13 { + type Limbs = [u64; 1]; + const MODULUS: [u64; 1] = [13]; + const PARAMS: MontgomeryParams<[u64; 1]> = MontgomeryParams::new([9], 0xB13B13B13B13B13B); + } + + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + struct Fr7; + + impl Field for Fr7 { + type Limbs = [u64; 1]; + const MODULUS: [u64; 1] = [7]; + const PARAMS: MontgomeryParams<[u64; 1]> = MontgomeryParams::new([4], 0x9249249249249249); + } + + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + struct TinyCurve; + + impl Curve for TinyCurve { + fn a(&self) -> FieldElement { + FieldElement::zero() + } + fn b(&self) -> FieldElement { + FieldElement::from_u64(7) + } + } + + fn tiny_g() -> Point { + Point::from_affine(FieldElement::from_u64(7), FieldElement::from_u64(5)) + } + + #[test] + fn tiny_curve_generator_has_order_seven() { + let g = tiny_g(); + let mut acc = Point::infinity(); + for _ in 0..7 { + acc = TinyCurve.add(acc, g); + } + assert!(acc.is_infinity()); + } + + #[test] + fn tiny_curve_scalar_mul_matches_iterated_add() { + let g = tiny_g(); + let mut acc = Point::infinity(); + for k in 0..7u64 { + assert_eq!(TinyCurve.multiply(Scalar::::from_u64(k), g), acc); + acc = TinyCurve.add(acc, g); + } + } + #[test] fn lift_two_torsion() { #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/field/src/lib.rs b/field/src/lib.rs index 5c76b48..25a0ebb 100644 --- a/field/src/lib.rs +++ b/field/src/lib.rs @@ -15,6 +15,12 @@ pub struct MontgomeryParams { p_inv: u64, } +impl MontgomeryParams { + pub const fn new(r_squared: T, p_inv: u64) -> Self { + Self { r_squared, p_inv } + } +} + pub trait Field: fmt::Debug + Clone + Copy + PartialEq + Eq { type Limbs: Limbs; const MODULUS: Self::Limbs;