Skip to content

Commit

Permalink
fix some clippy warnings, ignore others
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Oct 1, 2024
1 parent b0f8f5e commit 8b972dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
15 changes: 11 additions & 4 deletions curves/src/pasta/wasm_friendly/backend9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::wasm_fp::{Fp, FpBackend};
type B = [u32; 9];
type B64 = [u64; 9];

const SHIFT: u64 = 29;
const SHIFT: u32 = 29;
const MASK: u32 = (1 << SHIFT) - 1;

const SHIFT64: u64 = SHIFT as u64;
Expand Down Expand Up @@ -66,6 +66,8 @@ pub trait FpConstants: Send + Sync + 'static + Sized {
#[inline]
fn gte_modulus<FpC: FpConstants>(x: &B) -> bool {
for i in (0..9).rev() {
// don't fix warning -- that makes it 15% slower!
#[allow(clippy::comparison_chain)]
if x[i] > FpC::MODULUS[i] {
return true;
} else if x[i] < FpC::MODULUS[i] {
Expand All @@ -92,6 +94,7 @@ pub fn add_assign<FpC: FpConstants>(x: &mut B, y: &B) {

if gte_modulus::<FpC>(x) {
carry = 0;
#[allow(clippy::needless_range_loop)]
for i in 0..9 {
tmp = x[i].wrapping_sub(FpC::MODULUS[i]) + (carry as u32);
carry = (tmp as i32) >> SHIFT;
Expand All @@ -103,12 +106,15 @@ pub fn add_assign<FpC: FpConstants>(x: &mut B, y: &B) {
#[inline]
fn conditional_reduce<FpC: FpConstants>(x: &mut B) {
if gte_modulus::<FpC>(x) {
#[allow(clippy::needless_range_loop)]
for i in 0..9 {
x[i] = x[i].wrapping_sub(FpC::MODULUS[i]);
}
#[allow(clippy::needless_range_loop)]
for i in 1..9 {
x[i] = x[i] + (((x[i - 1] as i32) >> SHIFT) as u32);
x[i] += ((x[i - 1] as i32) >> SHIFT) as u32;
}
#[allow(clippy::needless_range_loop)]
for i in 0..8 {
x[i] &= MASK;
}
Expand All @@ -129,6 +135,7 @@ pub fn mul_assign<FpC: FpConstants>(x: &mut B, y: &B) {
let mut tmp: u64;

// main loop, without intermediate carries except for z0
#[allow(clippy::needless_range_loop)]
for i in 0..9 {
let xi = x[i] as u64;

Expand Down Expand Up @@ -160,7 +167,7 @@ pub fn mul_assign<FpC: FpConstants>(x: &mut B, y: &B) {
// implement FpBackend given FpConstants

pub fn from_bigint_unsafe<FpC: FpConstants>(x: BigInt<9>) -> Fp<FpC, 9> {
let mut r = x.0.clone();
let mut r = x.0;
// convert to montgomery form
mul_assign::<FpC>(&mut r, &FpC::R2);
Fp(BigInt(r), Default::default())
Expand Down Expand Up @@ -188,7 +195,7 @@ impl<FpC: FpConstants> FpBackend<9> for FpC {
}
fn to_bigint(x: Fp<Self, 9>) -> BigInt<9> {
let one = [1, 0, 0, 0, 0, 0, 0, 0, 0];
let mut r = x.0 .0.clone();
let mut r = x.0 .0;
// convert back from montgomery form
mul_assign::<Self>(&mut r, &one);
BigInt(r)
Expand Down
7 changes: 6 additions & 1 deletion curves/src/pasta/wasm_friendly/wasm_fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub trait FpBackend<const N: usize>: Send + Sync + 'static + Sized {
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Copy(bound = ""),
PartialEq(bound = ""),
Eq(bound = ""),
Expand All @@ -54,6 +53,12 @@ pub struct Fp<P: FpBackend<N>, const N: usize>(
pub PhantomData<P>,
);

impl<P: FpBackend<N>, const N: usize> Clone for Fp<P, N> {
fn clone(&self) -> Self {
*self
}
}

impl<P: FpBackend<N>, const N: usize> Fp<P, N> {
pub fn new(bigint: BigInt<N>) -> Self {
Fp(bigint, Default::default())
Expand Down
8 changes: 4 additions & 4 deletions poseidon/benches/poseidon_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub fn bench_poseidon_kimchi(c: &mut Criterion) {
SpongeParametersKimchi::static_params(),
);

poseidon.absorb(&[Fp::zero()]);
println!("{}", poseidon.squeeze().to_string());
// poseidon.absorb(&[Fp::zero()]);
// println!("{}", poseidon.squeeze());

b.iter(|| {
poseidon.absorb(&[hash]);
Expand All @@ -33,8 +33,8 @@ pub fn bench_poseidon_kimchi(c: &mut Criterion) {
let mut hash: Fp9 = Fp9::zero();
let mut poseidon = Poseidon::<Fp9, PlonkSpongeConstantsKimchi>::new(fp9_static_params());

poseidon.absorb(&[Fp9::zero()]);
println!("{}", poseidon.squeeze().to_string());
// poseidon.absorb(&[Fp9::zero()]);
// println!("{}", poseidon.squeeze());

b.iter(|| {
poseidon.absorb(&[hash]);
Expand Down

0 comments on commit 8b972dc

Please sign in to comment.