Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poseidon: replaced unimplemented with error and added solana feature flag #9

Merged
merged 5 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions light-poseidon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ keywords = ["cryptography", "hash", "poseidon", "zero-knowledge", "zkSNARK"]
license = "Apache-2.0"
edition = "2021"

[features]
# limits the width of poseidon paramters to 13 (12 inputs)
solana = []
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I would rename it to something line width_limit_13.

  1. That way, we could introduce other limits if there is need at any time.
  2. The first thing I'd think about when seeing this feature, without knowing the code, would be "oh, this enables some solana-program/Anchor stuff in the crate".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, I will change it


[dependencies]
ark-bn254 = "0.4.0"

Expand Down
4 changes: 2 additions & 2 deletions light-poseidon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub mod parameters;
pub const HASH_LEN: usize = 32;
pub const MAX_X5_LEN: usize = 16;

#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum PoseidonError {
#[error("Invalid number of inputs: {inputs}, the maximum limit is {max_limit} ({width} - 1)")]
InvalidNumberOfInputs {
Expand Down Expand Up @@ -428,7 +428,7 @@ impl<F: PrimeField> Poseidon<F> {

let params = crate::parameters::bn254_x5::get_poseidon_parameters::<Fr>(
(width).try_into().map_err(|_| PoseidonError::U64Tou8)?,
);
)?;
Ok(Poseidon::<Fr>::new(params))
}
}
109 changes: 75 additions & 34 deletions light-poseidon/src/parameters/bn254_x5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const PARTIAL_ROUNDS: [usize; 15] =
[56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64];
pub const ALPHA: u64 = 5;

use crate::PoseidonParameters;
use crate::{PoseidonError, PoseidonParameters};
/// Returns Poseidon parameters for the BN254 curve with the following
/// properties:
///
Expand All @@ -31,11 +31,22 @@ use crate::PoseidonParameters;
/// The argument of this macro is a type which implements
/// [`ark_ff::PrimeField`](ark_ff::PrimeField).
use ark_ff::PrimeField;
// to avoid warnings when solana feature is used
#[allow(unused_variables)]
pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInteger256>>(
t: u8,
) -> PoseidonParameters<F> {
) -> Result<PoseidonParameters<F>, PoseidonError> {
if t == 0_u8 {
unimplemented!()
#[cfg(not(feature = "solana"))]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 16usize,
});
#[cfg(feature = "solana")]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 13usize,
});
} else if 2 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -837,14 +848,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[0],
t.into(),
ALPHA,
);
));
} else if 3 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -2080,14 +2091,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[1],
t.into(),
ALPHA,
);
));
} else if 4 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -3733,14 +3744,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[2],
t.into(),
ALPHA,
);
));
} else if 5 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -5946,14 +5957,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[3],
t.into(),
ALPHA,
);
));
} else if 6 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -8635,14 +8646,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[4],
t.into(),
ALPHA,
);
));
} else if 7 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -11938,14 +11949,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[5],
t.into(),
ALPHA,
);
));
} else if 8 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -15807,14 +15818,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[6],
t.into(),
ALPHA,
);
));
} else if 9 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -20158,14 +20169,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[7],
t.into(),
ALPHA,
);
));
} else if 10 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -24871,14 +24882,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[8],
t.into(),
ALPHA,
);
));
} else if 11 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -30516,14 +30527,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[9],
t.into(),
ALPHA,
);
));
} else if 12 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -36313,14 +36324,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[10],
t.into(),
ALPHA,
);
));
} else if 13 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -43060,14 +43071,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[11],
t.into(),
ALPHA,
);
));
} else if 14 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -50829,14 +50840,21 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
#[cfg(feature = "solana")]
return Err(PoseidonError::InvalidWidthCircom {
width: 14 as usize,
max_limit: 13usize,
});

#[cfg(not(feature = "solana"))]
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[12],
t.into(),
ALPHA,
);
));
} else if 15 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -58342,14 +58360,21 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
#[cfg(feature = "solana")]
return Err(PoseidonError::InvalidWidthCircom {
width: 15 as usize,
max_limit: 13usize,
});

#[cfg(not(feature = "solana"))]
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[13],
t.into(),
ALPHA,
);
));
} else if 16 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -66835,15 +66860,31 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
#[cfg(feature = "solana")]
return Err(PoseidonError::InvalidWidthCircom {
width: 16 as usize,
max_limit: 13usize,
});

#[cfg(not(feature = "solana"))]
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[14],
t.into(),
ALPHA,
);
));
} else {
unimplemented!();
#[cfg(not(feature = "solana"))]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 16usize,
});
#[cfg(feature = "solana")]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 13usize,
});
}
}
Loading