Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/ff1/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@ use core::iter;
use alloc::{vec, vec::Vec};

use num_bigint::{BigInt, BigUint, Sign};
use num_traits::{
ToPrimitive,
identities::{One, Zero},
};
use num_traits::{Pow, ToPrimitive, identities::Zero};

use super::{NumeralString, Operations};

fn pow(x: u32, e: usize) -> BigUint {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back in time to figure out why I had this separate function. I added this in 8f91862 when num-bigint 0.2.0 was the latest version at the time, and BigUint::pow did not exist (only BigUint::modpow).

let mut res = BigUint::one();
for _ in 0..e {
res *= x;
}
res
}

/// Extension trait adding FF1-relevant methods to `BigUint`.
trait Numeral {
/// Type used for byte representations.
Expand Down Expand Up @@ -62,11 +51,11 @@ impl Numeral for BigUint {
}

fn add_mod_exp(self, other: Self, radix: u32, m: usize) -> Self {
(self + other) % pow(radix, m)
(self + other) % BigUint::from(radix).pow(m)
}

fn sub_mod_exp(self, other: Self, radix: u32, m: usize) -> Self {
let modulus = BigInt::from(pow(radix, m));
let modulus = BigInt::from(radix).pow(m);
let mut c = (BigInt::from(self) - BigInt::from(other)) % &modulus;
if c.sign() == Sign::Minus {
// use ((x % m) + m) % m to ensure it is in range
Expand Down
Loading