From e9a6b9f6829e471d736235fdf479652b30c2a555 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 2 Dec 2023 08:44:49 -0700 Subject: [PATCH] Convert i128s to SignedWideWord --- src/limb.rs | 8 ++++++++ src/modular/bernstein_yang.rs | 10 +++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/limb.rs b/src/limb.rs index 22e10569..408babb4 100644 --- a/src/limb.rs +++ b/src/limb.rs @@ -45,6 +45,10 @@ pub type WideWord = u64; #[cfg(target_pointer_width = "32")] pub(crate) type SignedWord = i32; +/// Signed equivalent of a wide word. +#[cfg(target_pointer_width = "32")] +pub(crate) type SignedWideWord = i32; + // // 64-bit definitions // @@ -61,6 +65,10 @@ pub type WideWord = u128; #[cfg(target_pointer_width = "64")] pub(crate) type SignedWord = i64; +/// Signed equivalent of a wide word. +#[cfg(target_pointer_width = "64")] +pub(crate) type SignedWideWord = i64; + /// Highest bit in a [`Limb`]. pub(crate) const HI_BIT: usize = Limb::BITS - 1; diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index 8c9cc43e..9258470e 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -6,9 +6,9 @@ //! //! Copyright (c) 2023 Privacy Scaling Explorations Team -#![allow(clippy::needless_range_loop)] +#![allow(clippy::needless_range_loop, trivial_numeric_casts)] -use crate::limb::{Limb, SignedWord, WideWord, Word}; +use crate::limb::{Limb, SignedWideWord, SignedWord, WideWord, Word}; use core::{ cmp::PartialEq, ops::{Add, Mul, Neg, Sub}, @@ -95,7 +95,7 @@ impl BernsteinYangInverter { let (mut steps, mut f, mut g) = ( BITS as SignedWord, f.lowest() as SignedWord, - g.lowest() as i128, + g.lowest() as SignedWideWord, ); let mut t: Matrix = [[1, 0], [0, 1]]; @@ -108,7 +108,7 @@ impl BernsteinYangInverter { break; } if delta > 0 { - (delta, f, g) = (-delta, g as SignedWord, -f as i128); + (delta, f, g) = (-delta, g as SignedWord, -f as SignedWideWord); (t[0], t[1]) = (t[1], [-t[0][0], -t[0][1]]); } @@ -119,7 +119,7 @@ impl BernsteinYangInverter { let w = (g as SignedWord).wrapping_mul(f.wrapping_mul(3) ^ 28) & mask; t[1] = [t[0][0] * w + t[1][0], t[0][1] * w + t[1][1]]; - g += w as i128 * f as i128; + g += w as SignedWideWord * f as SignedWideWord; } (delta, t)