Skip to content

Commit

Permalink
Convert i128s to SignedWideWord
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Dec 2, 2023
1 parent 0dbc01a commit e9a6b9f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions src/modular/bernstein_yang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<const L: usize> BernsteinYangInverter<L> {
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]];

Expand All @@ -108,7 +108,7 @@ impl<const L: usize> BernsteinYangInverter<L> {
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]]);
}

Expand All @@ -119,7 +119,7 @@ impl<const L: usize> BernsteinYangInverter<L> {
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)
Expand Down

0 comments on commit e9a6b9f

Please sign in to comment.