Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ uninlined_format_args = "allow"
upper_case_acronyms = "allow"

[dependencies]
rand = "0.9"
rand = "0.10"
sha3 = "0.10.8"
num-bigint = "0.4.6"
rayon = "1.10.0"
Expand All @@ -41,10 +41,10 @@ thiserror = "2.0"

ssz = { package = "ethereum_ssz", version = "0.10.0" }

p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "d421e32" }
p3-baby-bear = { git = "https://github.com/Plonky3/Plonky3.git", rev = "d421e32" }
p3-koala-bear = { git = "https://github.com/Plonky3/Plonky3.git", rev = "d421e32" }
p3-symmetric = { git = "https://github.com/Plonky3/Plonky3.git", rev = "d421e32" }
p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "b4dcde46" }
p3-baby-bear = { git = "https://github.com/Plonky3/Plonky3.git", rev = "b4dcde46" }
p3-koala-bear = { git = "https://github.com/Plonky3/Plonky3.git", rev = "b4dcde46" }
p3-symmetric = { git = "https://github.com/Plonky3/Plonky3.git", rev = "b4dcde46" }

[dev-dependencies]
criterion = "0.7"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ See also function `test_signature_scheme_correctness` in [this file](https://git

## Schemes
The code implements a generic framework from [this paper](https://eprint.iacr.org/2025/055.pdf), which builds XMSS-like hash-based signatures from a primitive called incomparable encodings.
Hardcoded instantiations of this generic framework (using Poseidon2) are defined in `leansig::signature::generalized_xmss`.
Hardcoded instantiations of this generic framework (using Poseidon1) are defined in `leansig::signature::generalized_xmss`.
The parameters have been chosen based on the analysis in the paper using Python scripts. Details are as follows:

| Submodule | Paper / Documentation | Parameters Set With |
Expand Down
2 changes: 1 addition & 1 deletion benches/benchmark_poseidon.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::hint::black_box;

use criterion::{Criterion, SamplingMode};
use rand::Rng;
use rand::RngExt;

use leansig::{
MESSAGE_LENGTH,
Expand Down
2 changes: 1 addition & 1 deletion benches/benchmark_poseidon_top_level.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cmp::min, hint::black_box};

use criterion::{Criterion, SamplingMode};
use rand::Rng;
use rand::RngExt;

use leansig::{
MESSAGE_LENGTH,
Expand Down
1 change: 1 addition & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl<'de, const N: usize> Deserialize<'de> for FieldArray<N> {
mod tests {
use super::*;
use proptest::prelude::*;
use rand::RngExt;

/// Small parameter arrays
const SMALL_SIZE: usize = 5;
Expand Down
4 changes: 2 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use leansig::signature::generalized_xmss::instantiations_poseidon::lifetime_2_to
use leansig::signature::generalized_xmss::instantiations_poseidon::lifetime_2_to_the_20::target_sum::SIGTargetSumLifetime20W8NoOff;
use leansig::signature::SignatureScheme;
use rand::rngs::ThreadRng;
use rand::Rng;
use rand::RngExt;
use std::time::Instant;

// Function to measure execution time
fn measure_time<T: SignatureScheme, R: Rng>(description: &str, rng: &mut R) {
fn measure_time<T: SignatureScheme, R: RngExt>(description: &str, rng: &mut R) {
// key gen

let start = Instant::now();
Expand Down
4 changes: 2 additions & 2 deletions src/inc_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rand::Rng;
use rand::RngExt;
use std::fmt::Debug;

use crate::MESSAGE_LENGTH;
Expand Down Expand Up @@ -33,7 +33,7 @@ pub trait IncomparableEncoding {
const BASE: usize;

/// Samples a randomness to be used for the encoding.
fn rand<R: Rng>(rng: &mut R) -> Self::Randomness;
fn rand<R: RngExt>(rng: &mut R) -> Self::Randomness;

/// Apply the incomparable encoding to a message.
/// It could happen that this fails. Otherwise,
Expand Down
3 changes: 2 additions & 1 deletion src/inc_encoding/target_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<MH: MessageHash, const TARGET_SUM: usize> IncomparableEncoding

const BASE: usize = MH::BASE;

fn rand<R: rand::Rng>(rng: &mut R) -> Self::Randomness {
fn rand<R: rand::RngExt>(rng: &mut R) -> Self::Randomness {
MH::rand(rng)
}

Expand Down Expand Up @@ -97,6 +97,7 @@ mod tests {
use crate::symmetric::message_hash::poseidon::PoseidonMessageHash445;
use p3_field::PrimeField32;
use proptest::prelude::*;
use rand::RngExt;

const TEST_TARGET_SUM: usize = 115;
type TestTargetSumEncoding = TargetSumEncoding<PoseidonMessageHash445, TEST_TARGET_SUM>;
Expand Down
30 changes: 15 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use p3_field::Field;
use p3_koala_bear::{
KoalaBear, Poseidon2KoalaBear, default_koalabear_poseidon2_16, default_koalabear_poseidon2_24,
KoalaBear, Poseidon1KoalaBear, default_koalabear_poseidon1_16, default_koalabear_poseidon1_24,
};
use std::sync::OnceLock;

Expand All @@ -22,27 +22,27 @@ pub mod signature;
pub(crate) mod simd_utils;
pub mod symmetric;

// Cached Poseidon2 permutations.
// Cached Poseidon1 permutations.
//
// We cache the default Plonky3 Poseidon2 instances once and return a clone.
// We cache the default Plonky3 Poseidon1 instances once and return a clone.
// Returning by value preserves existing call sites that take `&perm`.

/// A lazily-initialized, thread-safe cache for the Poseidon2 permutation with a width of 24.
static POSEIDON2_24: OnceLock<Poseidon2KoalaBear<24>> = OnceLock::new();
/// A lazily-initialized, thread-safe cache for the Poseidon1 permutation with a width of 24.
static POSEIDON1_24: OnceLock<Poseidon1KoalaBear<24>> = OnceLock::new();

/// A lazily-initialized, thread-safe cache for the Poseidon2 permutation with a width of 16.
static POSEIDON2_16: OnceLock<Poseidon2KoalaBear<16>> = OnceLock::new();
/// A lazily-initialized, thread-safe cache for the Poseidon1 permutation with a width of 16.
static POSEIDON1_16: OnceLock<Poseidon1KoalaBear<16>> = OnceLock::new();

/// Poseidon2 permutation (width 24)
pub(crate) fn poseidon2_24() -> Poseidon2KoalaBear<24> {
POSEIDON2_24
.get_or_init(default_koalabear_poseidon2_24)
/// Poseidon1 permutation (width 24)
pub(crate) fn poseidon1_24() -> Poseidon1KoalaBear<24> {
POSEIDON1_24
.get_or_init(default_koalabear_poseidon1_24)
.clone()
}

/// Poseidon2 permutation (width 16)
pub(crate) fn poseidon2_16() -> Poseidon2KoalaBear<16> {
POSEIDON2_16
.get_or_init(default_koalabear_poseidon2_16)
/// Poseidon1 permutation (width 16)
pub(crate) fn poseidon1_16() -> Poseidon1KoalaBear<16> {
POSEIDON1_16
.get_or_init(default_koalabear_poseidon1_16)
.clone()
}
5 changes: 3 additions & 2 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops::Range;

use crate::MESSAGE_LENGTH;
use crate::serialization::Serializable;
use rand::Rng;
use rand::RngExt;
use thiserror::Error;

/// Error enum for the signing process.
Expand Down Expand Up @@ -146,7 +146,7 @@ pub trait SignatureScheme {
///
/// ### Returns
/// A tuple containing the new `(PublicKey, SecretKey)`.
fn key_gen<R: Rng>(
fn key_gen<R: RngExt>(
rng: &mut R,
activation_epoch: usize,
num_active_epochs: usize,
Expand Down Expand Up @@ -209,6 +209,7 @@ pub mod generalized_xmss;

#[cfg(test)]
mod test_templates {
use rand::RngExt;
use serde::{Serialize, de::DeserializeOwned};

use super::*;
Expand Down
10 changes: 5 additions & 5 deletions src/signature/generalized_xmss.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use rand::Rng;
use rand::RngExt;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -653,7 +653,7 @@ where

const LIFETIME: u64 = 1 << LOG_LIFETIME;

fn key_gen<R: Rng>(
fn key_gen<R: RngExt>(
rng: &mut R,
activation_epoch: usize,
num_active_epochs: usize,
Expand Down Expand Up @@ -1004,10 +1004,10 @@ impl<PRF: Pseudorandom, IE: IncomparableEncoding, TH: TweakableHash, const LOG_L
/// Instantiations of the generalized XMSS signature scheme based on the
/// aborting hypercube message hash (rejection sampling)
pub mod instantiations_aborting;
/// Instantiations of the generalized XMSS signature scheme based on Poseidon2
/// Instantiations of the generalized XMSS signature scheme based on Poseidon1
pub mod instantiations_poseidon;
/// Instantiations of the generalized XMSS signature scheme based on the
/// top level target sum encoding using Poseidon2
/// top level target sum encoding using Poseidon1
pub mod instantiations_poseidon_top_level;

#[cfg(test)]
Expand All @@ -1034,7 +1034,7 @@ mod tests {

use crate::{F, symmetric::tweak_hash::poseidon::PoseidonTweakHash};
use p3_field::RawDataSerializable;
use rand::rng;
use rand::{RngExt, rng};
use ssz::{Decode, Encode};

type TestTH = PoseidonTweakHash<5, 7, 2, 9, 155>;
Expand Down
47 changes: 18 additions & 29 deletions src/signature/generalized_xmss/instantiations_poseidon_top_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ pub mod lifetime_2_to_the_18 {
pub type SIGTopLevelTargetSumLifetime18Dim64Base8 =
GeneralizedXMSSSignatureScheme<PRF, IE, TH, LOG_LIFETIME>;

#[cfg(test)]
#[cfg(all(test, feature = "slow-tests"))]
mod test {

use crate::signature::{
SignatureScheme,
generalized_xmss::instantiations_poseidon_top_level::lifetime_2_to_the_18::SIGTopLevelTargetSumLifetime18Dim64Base8,
test_templates::test_signature_scheme_correctness,
};

#[cfg(feature = "slow-tests")]
use crate::signature::test_templates::test_signature_scheme_correctness;

#[test]
#[cfg(feature = "slow-tests")]
pub fn test_correctness() {
test_signature_scheme_correctness::<SIGTopLevelTargetSumLifetime18Dim64Base8>(
213,
Expand Down Expand Up @@ -130,17 +127,15 @@ pub mod lifetime_2_to_the_32 {
pub type PubKeyTopLevelTargetSumLifetime32Dim64Base8 = GeneralizedXMSSPublicKey<TH>;
pub type SigTopLevelTargetSumLifetime32Dim64Base8 = GeneralizedXMSSSignature<IE, TH>;

#[cfg(test)]
#[cfg(all(test, feature = "slow-tests"))]
mod test {

use super::*;
use crate::signature::SignatureScheme;

#[cfg(feature = "slow-tests")]
use crate::signature::test_templates::test_signature_scheme_correctness;
use crate::signature::{
SignatureScheme, test_templates::test_signature_scheme_correctness,
};

#[test]
#[cfg(feature = "slow-tests")]
pub fn test_correctness() {
test_signature_scheme_correctness::<SIGTopLevelTargetSumLifetime32Dim64Base8>(
213,
Expand Down Expand Up @@ -206,17 +201,15 @@ pub mod lifetime_2_to_the_32 {
pub type SIGTopLevelTargetSumLifetime32Dim48Base10 =
GeneralizedXMSSSignatureScheme<PRF, IE, TH, LOG_LIFETIME>;

#[cfg(test)]
#[cfg(all(test, feature = "slow-tests"))]
mod test {

use super::*;
use crate::signature::SignatureScheme;

#[cfg(feature = "slow-tests")]
use crate::signature::test_templates::test_signature_scheme_correctness;
use crate::signature::{
SignatureScheme, test_templates::test_signature_scheme_correctness,
};

#[test]
#[cfg(feature = "slow-tests")]
pub fn test_correctness() {
test_signature_scheme_correctness::<SIGTopLevelTargetSumLifetime32Dim48Base10>(
213,
Expand Down Expand Up @@ -281,17 +274,15 @@ pub mod lifetime_2_to_the_32 {
pub type SIGTopLevelTargetSumLifetime32Dim32Base26 =
GeneralizedXMSSSignatureScheme<PRF, IE, TH, LOG_LIFETIME>;

#[cfg(test)]
#[cfg(all(test, feature = "slow-tests"))]
mod test {

use super::*;
use crate::signature::SignatureScheme;

#[cfg(feature = "slow-tests")]
use crate::signature::test_templates::test_signature_scheme_correctness;
use crate::signature::{
SignatureScheme, test_templates::test_signature_scheme_correctness,
};

#[test]
#[cfg(feature = "slow-tests")]
pub fn test_correctness() {
test_signature_scheme_correctness::<SIGTopLevelTargetSumLifetime32Dim32Base26>(
213,
Expand Down Expand Up @@ -361,17 +352,15 @@ pub mod lifetime_2_to_the_8 {
pub type SIGTopLevelTargetSumLifetime8Dim64Base8 =
GeneralizedXMSSSignatureScheme<PRF, IE, TH, LOG_LIFETIME>;

#[cfg(test)]
#[cfg(all(test, feature = "slow-tests"))]
mod test {

use crate::signature::SignatureScheme;

#[cfg(feature = "slow-tests")]
use crate::signature::test_templates::test_signature_scheme_correctness;
use crate::signature::{
SignatureScheme, test_templates::test_signature_scheme_correctness,
};

use super::SIGTopLevelTargetSumLifetime8Dim64Base8;

#[cfg(feature = "slow-tests")]
#[test]
pub fn test_correctness() {
test_signature_scheme_correctness::<SIGTopLevelTargetSumLifetime8Dim64Base8>(
Expand Down
2 changes: 1 addition & 1 deletion src/simd_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod tests {
use super::*;
use p3_field::PrimeCharacteristicRing;
use proptest::prelude::*;
use rand::Rng;
use rand::RngExt;

#[test]
fn test_pack_array_simple() {
Expand Down
4 changes: 2 additions & 2 deletions src/symmetric/message_hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Debug;

use rand::Rng;
use rand::RngExt;

use crate::MESSAGE_LENGTH;
use crate::serialization::Serializable;
Expand All @@ -25,7 +25,7 @@ pub trait MessageHash {
const BASE: usize;

/// Generates a random domain element.
fn rand<R: Rng>(rng: &mut R) -> Self::Randomness;
fn rand<R: RngExt>(rng: &mut R) -> Self::Randomness;

/// Applies the message hash to a parameter, an epoch,
/// a randomness, and a message. It outputs a list of chunks.
Expand Down
4 changes: 2 additions & 2 deletions src/symmetric/message_hash/aborting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
const DIMENSION: usize = DIMENSION; // v
const BASE: usize = BASE; // w

fn rand<R: rand::Rng>(rng: &mut R) -> Self::Randomness {
fn rand<R: rand::RngExt>(rng: &mut R) -> Self::Randomness {
FieldArray(rng.random())
}

Expand Down Expand Up @@ -166,7 +166,7 @@ mod tests {
use super::*;
use p3_field::PrimeField32;
use proptest::prelude::*;
use rand::{SeedableRng, rngs::StdRng};
use rand::{RngExt, SeedableRng, rngs::StdRng};

#[test]
fn test_apply() {
Expand Down
Loading
Loading