From b75a88458a61947fbb28e621346ac004a98eb633 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Sun, 12 Mar 2017 23:18:50 -0700 Subject: [PATCH] Add monte carlo test for FieldElements --- src/curve.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/curve.rs b/src/curve.rs index 6bb964a..b2c0caa 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -1399,6 +1399,26 @@ mod test { assert!( ExtendedPoint::identity().is_identity() == true); assert!(constants::ED25519_BASEPOINT.is_identity() == false); } + + /// Rust's debug builds have overflow and underflow trapping, + /// and enable `debug_assert!()`. This performs many scalar + /// multiplications to attempt to trigger possible overflows etc. + /// + /// For instance, the `radix_51` `Mul` implementation for + /// `FieldElements` requires the input `Limb`s to be bounded by + /// 2^54, but we cannot enforce this dynamically at runtime, or + /// statically at compile time (until Rust gets type-level + /// integers, at which point we can encode "bits of headroom" into + /// the type system and prove correctness). + #[test] + fn monte_carlo_overflow_underflow_debug_assert_test() { + let mut P = ExtendedPoint::basepoint(); + // N.B. each scalar_mult does 1407 field mults, 1024 field squarings, + // so this does ~ 1M of each operation. + for _ in 0..1_000 { + P = P.scalar_mult(&A_SCALAR); + } + } } // ------------------------------------------------------------------------