Skip to content

Commit

Permalink
Use 16-element compression function.
Browse files Browse the repository at this point in the history
  • Loading branch information
nhukc committed May 17, 2024
1 parent 873fe22 commit 02ec186
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
21 changes: 15 additions & 6 deletions core/src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use p3_symmetric::Hash;
use p3_symmetric::{PaddingFreeSponge, TruncatedPermutation};
use serde::Deserialize;
use serde::Serialize;
use sp1_primitives::poseidon2_init;
use sp1_primitives::{poseidon2_16_init, poseidon2_24_init};

pub const DIGEST_SIZE: usize = 8;

Expand All @@ -24,10 +24,12 @@ pub type InnerVal = BabyBear;
pub type InnerChallenge = BinomialExtensionField<InnerVal, 4>;
pub type InnerPerm =
Poseidon2<InnerVal, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 24, 7>;
pub type InnerPerm16 =
Poseidon2<InnerVal, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 16, 7>;
pub type InnerHash = PaddingFreeSponge<InnerPerm, 24, 16, 8>;
pub type InnerDigestHash = Hash<InnerVal, InnerVal, DIGEST_SIZE>;
pub type InnerDigest = [InnerVal; DIGEST_SIZE];
pub type InnerCompress = TruncatedPermutation<InnerPerm, 2, 8, 24>;
pub type InnerCompress = TruncatedPermutation<InnerPerm16, 2, 8, 16>;
pub type InnerValMmcs = FieldMerkleTreeMmcs<
<InnerVal as Field>::Packing,
<InnerVal as Field>::Packing,
Expand All @@ -48,14 +50,19 @@ pub type InnerPcsProof =

/// The permutation for inner recursion.
pub fn inner_perm() -> InnerPerm {
poseidon2_init()
poseidon2_24_init()
}

pub fn inner_perm16() -> InnerPerm16 {
poseidon2_16_init()
}

/// The FRI config for sp1 proofs.
pub fn sp1_fri_config() -> FriConfig<InnerChallengeMmcs> {
let perm = inner_perm();
let perm16 = inner_perm16();
let hash = InnerHash::new(perm.clone());
let compress = InnerCompress::new(perm.clone());
let compress = InnerCompress::new(perm16.clone());
let challenge_mmcs = InnerChallengeMmcs::new(InnerValMmcs::new(hash, compress));
let num_queries = match std::env::var("FRI_QUERIES") {
Ok(value) => value.parse().unwrap(),
Expand All @@ -72,8 +79,9 @@ pub fn sp1_fri_config() -> FriConfig<InnerChallengeMmcs> {
/// The FRI config for inner recursion.
pub fn inner_fri_config() -> FriConfig<InnerChallengeMmcs> {
let perm = inner_perm();
let perm16 = inner_perm16();
let hash = InnerHash::new(perm.clone());
let compress = InnerCompress::new(perm.clone());
let compress = InnerCompress::new(perm16.clone());
let challenge_mmcs = InnerChallengeMmcs::new(InnerValMmcs::new(hash, compress));
let num_queries = match std::env::var("FRI_QUERIES") {
Ok(value) => value.parse().unwrap(),
Expand Down Expand Up @@ -119,8 +127,9 @@ impl From<std::marker::PhantomData<BabyBearPoseidon2Inner>> for BabyBearPoseidon
impl BabyBearPoseidon2Inner {
pub fn new() -> Self {
let perm = inner_perm();
let perm16 = inner_perm16();
let hash = InnerHash::new(perm.clone());
let compress = InnerCompress::new(perm.clone());
let compress = InnerCompress::new(perm16.clone());
let val_mmcs = InnerValMmcs::new(hash, compress);
let dft = InnerDft {};
let fri_config = inner_fri_config();
Expand Down
39 changes: 33 additions & 6 deletions core/src/utils/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,17 @@ pub mod baby_bear_poseidon2 {
use p3_poseidon2::Poseidon2ExternalMatrixGeneral;
use p3_symmetric::{PaddingFreeSponge, TruncatedPermutation};
use serde::{Deserialize, Serialize};
use sp1_primitives::RC_24_29;
use sp1_primitives::{RC_24_29, RC_16_30};

use crate::stark::StarkGenericConfig;

pub type Val = BabyBear;
pub type Challenge = BinomialExtensionField<Val, 4>;

pub type Perm = Poseidon2<Val, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 24, 7>;
pub type Perm16 = Poseidon2<Val, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 16, 7>;
pub type MyHash = PaddingFreeSponge<Perm, 24, 16, 8>;
pub type MyCompress = TruncatedPermutation<Perm, 2, 8, 24>;
pub type MyCompress = TruncatedPermutation<Perm16, 2, 8, 16>;
pub type ValMmcs = FieldMerkleTreeMmcs<
<Val as Field>::Packing,
<Val as Field>::Packing,
Expand Down Expand Up @@ -445,11 +446,34 @@ pub mod baby_bear_poseidon2 {
DiffusionMatrixBabyBear,
)
}

pub fn my_perm16() -> Perm16 {
const ROUNDS_F: usize = 8;
const ROUNDS_P: usize = 13;
let mut round_constants = RC_16_30.to_vec();
let internal_start = ROUNDS_F / 2;
let internal_end = (ROUNDS_F / 2) + ROUNDS_P;
let internal_round_constants = round_constants
.drain(internal_start..internal_end)
.map(|vec| vec[0])
.collect::<Vec<_>>();
let external_round_constants = round_constants;
Perm16::new(
ROUNDS_F,
external_round_constants,
Poseidon2ExternalMatrixGeneral,
ROUNDS_P,
internal_round_constants,
DiffusionMatrixBabyBear,
)
}


pub fn default_fri_config() -> FriConfig<ChallengeMmcs> {
let perm = my_perm();
let perm16 = my_perm16();
let hash = MyHash::new(perm.clone());
let compress = MyCompress::new(perm.clone());
let compress = MyCompress::new(perm16.clone());
let challenge_mmcs = ChallengeMmcs::new(ValMmcs::new(hash, compress));
let num_queries = match std::env::var("FRI_QUERIES") {
Ok(value) => value.parse().unwrap(),
Expand All @@ -465,8 +489,9 @@ pub mod baby_bear_poseidon2 {

pub fn compressed_fri_config() -> FriConfig<ChallengeMmcs> {
let perm = my_perm();
let perm16 = my_perm16();
let hash = MyHash::new(perm.clone());
let compress = MyCompress::new(perm.clone());
let compress = MyCompress::new(perm16.clone());
let challenge_mmcs = ChallengeMmcs::new(ValMmcs::new(hash, compress));
let num_queries = match std::env::var("FRI_QUERIES") {
Ok(value) => value.parse().unwrap(),
Expand Down Expand Up @@ -496,8 +521,9 @@ pub mod baby_bear_poseidon2 {
impl BabyBearPoseidon2 {
pub fn new() -> Self {
let perm = my_perm();
let perm16 = my_perm16();
let hash = MyHash::new(perm.clone());
let compress = MyCompress::new(perm.clone());
let compress = MyCompress::new(perm16.clone());
let val_mmcs = ValMmcs::new(hash, compress);
let dft = Dft {};
let fri_config = default_fri_config();
Expand All @@ -511,8 +537,9 @@ pub mod baby_bear_poseidon2 {

pub fn compressed() -> Self {
let perm = my_perm();
let perm16 = my_perm16();
let hash = MyHash::new(perm.clone());
let compress = MyCompress::new(perm.clone());
let compress = MyCompress::new(perm16.clone());
let val_mmcs = ValMmcs::new(hash, compress);
let dft = Dft {};
let fri_config = compressed_fri_config();
Expand Down
28 changes: 25 additions & 3 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,29 @@ lazy_static! {
};
}

pub fn poseidon2_init(
pub fn poseidon2_16_init(
) -> Poseidon2<BabyBear, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 16, 7> {
const ROUNDS_F: usize = 8;
const ROUNDS_P: usize = 13;
let mut round_constants = RC_16_30.to_vec();
let internal_start = ROUNDS_F / 2;
let internal_end = (ROUNDS_F / 2) + ROUNDS_P;
let internal_round_constants = round_constants
.drain(internal_start..internal_end)
.map(|vec| vec[0])
.collect::<Vec<_>>();
let external_round_constants = round_constants;
Poseidon2::new(
ROUNDS_F,
external_round_constants,
Poseidon2ExternalMatrixGeneral,
ROUNDS_P,
internal_round_constants,
DiffusionMatrixBabyBear,
)
}

pub fn poseidon2_24_init(
) -> Poseidon2<BabyBear, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 24, 7> {
const ROUNDS_F: usize = 8;
const ROUNDS_P: usize = 21;
Expand Down Expand Up @@ -1166,7 +1188,7 @@ mod tests {

#[test]
fn test_24_permutation() {
let h1 = poseidon2_init();
let h1 = poseidon2_24_init();

type Perm = Poseidon2<BabyBear, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 24, 7>;
let h2 = Perm::new_from_rng_128(
Expand Down Expand Up @@ -1209,7 +1231,7 @@ pub fn poseidon2_hasher() -> PaddingFreeSponge<
16,
8,
> {
let hasher = poseidon2_init();
let hasher = poseidon2_24_init();
PaddingFreeSponge::<
Poseidon2<BabyBear, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 24, 7>,
24,
Expand Down
4 changes: 3 additions & 1 deletion recursion/program/src/fri/two_adic_pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ pub mod tests {
use rand::rngs::OsRng;
use sp1_core::utils::baby_bear_poseidon2::compressed_fri_config;
use sp1_core::utils::inner_perm;
use sp1_core::utils::inner_perm16;
use sp1_core::utils::InnerChallenge;
use sp1_core::utils::InnerChallenger;
use sp1_core::utils::InnerCompress;
Expand All @@ -307,9 +308,10 @@ pub mod tests {
let mut rng = &mut OsRng;
let log_degrees = &[nb_log2_rows];
let perm = inner_perm();
let perm16 = inner_perm16();
let fri_config = compressed_fri_config();
let hash = InnerHash::new(perm.clone());
let compress = InnerCompress::new(perm.clone());
let compress = InnerCompress::new(perm16.clone());
let val_mmcs = InnerValMmcs::new(hash, compress);
let dft = InnerDft {};
let pcs_val: InnerPcs = InnerPcs::new(
Expand Down
3 changes: 2 additions & 1 deletion recursion/program/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ type C = AsmConfig<F, EF>;
type Val = BabyBear;
type Challenge = BinomialExtensionField<Val, 4>;
type Perm = Poseidon2<Val, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 24, 7>;
type Perm16 = Poseidon2<Val, Poseidon2ExternalMatrixGeneral, DiffusionMatrixBabyBear, 16, 7>;
type Hash = PaddingFreeSponge<Perm, 24, 16, 8>;
type Compress = TruncatedPermutation<Perm, 2, 8, 24>;
type Compress = TruncatedPermutation<Perm16, 2, 8, 16>;
type ValMmcs =
FieldMerkleTreeMmcs<<Val as Field>::Packing, <Val as Field>::Packing, Hash, Compress, 8>;
type ChallengeMmcs = ExtensionMmcs<Val, Challenge, ValMmcs>;
Expand Down

0 comments on commit 02ec186

Please sign in to comment.