Skip to content

Commit

Permalink
feat: integrate poseidon port into mpcs + transcript (#325)
Browse files Browse the repository at this point in the history
To avoid adding +Poseidon super trait bound everywhere like #237, added
it as a requirement for BaseField in the ExtField trait
  • Loading branch information
iammadab authored Oct 11, 2024
1 parent c972e6a commit a9cd143
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 296 deletions.
14 changes: 3 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ criterion = { version = "0.5", features = ["html_reports"] }
ff = "0.13"
goldilocks = { git = "https://github.com/zhenfeizhang/Goldilocks" }
halo2curves = "0.1.0"
poseidon = { git = "https://github.com/zhenfeizhang/poseidon" }
poseidon = { path = "./poseidon" }
serde = { version = "1.0", features = ["derive"] }
subtle = "2.6.1"
rand_core = "0.6.4"
Expand Down
1 change: 1 addition & 0 deletions ff_ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ license.workspace = true
serde.workspace = true
goldilocks.workspace = true
ff.workspace = true
poseidon.workspace = true
3 changes: 2 additions & 1 deletion ff_ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ff::FromUniformBytes;
use goldilocks::SmallField;
use serde::Serialize;
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use poseidon::poseidon::Poseidon;

pub trait ExtensionField:
Serialize
Expand All @@ -23,7 +24,7 @@ pub trait ExtensionField:
{
const DEGREE: usize;

type BaseField: SmallField + FromUniformBytes<64>;
type BaseField: SmallField + FromUniformBytes<64> + Poseidon;

fn from_bases(bases: &[Self::BaseField]) -> Self;

Expand Down
29 changes: 7 additions & 22 deletions mpcs/src/basefold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
expression::{Expression, Query, Rotation},
ext_to_usize,
hash::{new_hasher, write_digest_to_transcript, Digest},
hash::{write_digest_to_transcript, Digest},
log2_strict,
merkle_tree::MerkleTree,
multiply_poly,
Expand Down Expand Up @@ -318,14 +318,13 @@ where
};

// 2. Compute and store all the layers of the Merkle tree
let hasher = new_hasher::<E::BaseField>();

// 1. Encode the polynomials. Simultaneously get:
// (1) The evaluations over the hypercube (just a clone of the input)
// (2) The encoding of the coefficient vector (need an interpolation)
let ret = match Self::get_poly_bh_evals_and_codeword(pp, poly) {
PolyEvalsCodeword::Normal((bh_evals, codeword)) => {
let codeword_tree = MerkleTree::<E>::from_leaves(codeword, &hasher);
let codeword_tree = MerkleTree::<E>::from_leaves(codeword);

// All these values are stored in the `CommitmentWithData` because
// they are useful in opening, and we don't want to recompute them.
Expand All @@ -338,7 +337,7 @@ where
})
}
PolyEvalsCodeword::TooSmall(evals) => {
let codeword_tree = MerkleTree::<E>::from_leaves(evals.clone(), &hasher);
let codeword_tree = MerkleTree::<E>::from_leaves(evals.clone());

// All these values are stored in the `CommitmentWithData` because
// they are useful in opening, and we don't want to recompute them.
Expand Down Expand Up @@ -400,8 +399,6 @@ where
end_timer!(encode_timer);

// build merkle tree from leaves
let hasher = new_hasher::<E::BaseField>();

let ret = match evals_codewords[0] {
PolyEvalsCodeword::Normal(_) => {
let (bh_evals, codewords) = evals_codewords
Expand All @@ -416,7 +413,7 @@ where
}
})
.collect::<(Vec<_>, Vec<_>)>();
let codeword_tree = MerkleTree::<E>::from_batch_leaves(codewords, &hasher);
let codeword_tree = MerkleTree::<E>::from_batch_leaves(codewords);
Self::CommitmentWithData {
codeword_tree,
polynomials_bh_evals: bh_evals,
Expand All @@ -436,7 +433,7 @@ where
}
})
.collect::<Vec<_>>();
let codeword_tree = MerkleTree::<E>::from_batch_leaves(bh_evals.clone(), &hasher);
let codeword_tree = MerkleTree::<E>::from_batch_leaves(bh_evals.clone());
Self::CommitmentWithData {
codeword_tree,
polynomials_bh_evals: bh_evals,
Expand Down Expand Up @@ -476,7 +473,6 @@ where
_eval: &E, // Opening does not need eval, except for sanity check
transcript: &mut Transcript<E>,
) -> Result<Self::Proof, Error> {
let hasher = new_hasher::<E::BaseField>();
let timer = start_timer!(|| "Basefold::open");

// The encoded polynomial should at least have the number of
Expand Down Expand Up @@ -506,7 +502,6 @@ where
transcript,
poly.num_vars,
poly.num_vars - Spec::get_basecode_msg_size_log(),
&hasher,
);

// 2. Query phase. ---------------------------------------
Expand Down Expand Up @@ -558,7 +553,6 @@ where
evals: &[Evaluation<E>],
transcript: &mut Transcript<E>,
) -> Result<Self::Proof, Error> {
let hasher = new_hasher::<E::BaseField>();
let timer = start_timer!(|| "Basefold::batch_open");
let num_vars = polys.iter().map(|poly| poly.num_vars).max().unwrap();
let min_num_vars = polys.iter().map(|p| p.num_vars).min().unwrap();
Expand Down Expand Up @@ -734,7 +728,6 @@ where
num_vars,
num_vars - Spec::get_basecode_msg_size_log(),
coeffs.as_slice(),
&hasher,
);

let query_timer = start_timer!(|| "Basefold::batch_open query phase");
Expand Down Expand Up @@ -782,7 +775,6 @@ where
evals: &[E],
transcript: &mut Transcript<E>,
) -> Result<Self::Proof, Error> {
let hasher = new_hasher::<E::BaseField>();
let timer = start_timer!(|| "Basefold::batch_open");
let num_vars = polys[0].num_vars();

Expand Down Expand Up @@ -832,7 +824,6 @@ where
transcript,
num_vars,
num_vars - Spec::get_basecode_msg_size_log(),
&hasher,
);

let query_timer = start_timer!(|| "Basefold::open::query_phase");
Expand Down Expand Up @@ -871,11 +862,10 @@ where
transcript: &mut Transcript<E>,
) -> Result<(), Error> {
let timer = start_timer!(|| "Basefold::verify");
let hasher = new_hasher::<E::BaseField>();

if proof.is_trivial() {
let trivial_proof = &proof.trivial_proof;
let merkle_tree = MerkleTree::from_batch_leaves(trivial_proof.clone(), &hasher);
let merkle_tree = MerkleTree::from_batch_leaves(trivial_proof.clone());
if comm.root() == merkle_tree.root() {
return Ok(());
} else {
Expand Down Expand Up @@ -943,7 +933,6 @@ where
comm,
eq.as_slice(),
eval,
&hasher,
);
end_timer!(timer);

Expand All @@ -961,7 +950,6 @@ where
let timer = start_timer!(|| "Basefold::batch_verify");
// let key = "RAYON_NUM_THREADS";
// env::set_var(key, "32");
let hasher = new_hasher::<E::BaseField>();
let comms = comms.iter().collect_vec();
let num_vars = points.iter().map(|point| point.len()).max().unwrap();
let num_rounds = num_vars - Spec::get_basecode_msg_size_log();
Expand Down Expand Up @@ -1073,7 +1061,6 @@ where
&coeffs,
eq.as_slice(),
&new_target_sum,
&hasher,
);
end_timer!(timer);
Ok(())
Expand All @@ -1092,11 +1079,10 @@ where
if let Some(num_polys) = comm.num_polys {
assert_eq!(num_polys, batch_size);
}
let hasher = new_hasher::<E::BaseField>();

if proof.is_trivial() {
let trivial_proof = &proof.trivial_proof;
let merkle_tree = MerkleTree::from_batch_leaves(trivial_proof.clone(), &hasher);
let merkle_tree = MerkleTree::from_batch_leaves(trivial_proof.clone());
if comm.root() == merkle_tree.root() {
return Ok(());
} else {
Expand Down Expand Up @@ -1175,7 +1161,6 @@ where
comm,
eq.as_slice(),
evals,
&hasher,
);
end_timer!(timer);

Expand Down
11 changes: 4 additions & 7 deletions mpcs/src/basefold/commit_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
use crate::util::{
arithmetic::{interpolate2_weights, interpolate_over_boolean_hypercube},
field_type_index_ext, field_type_iter_ext,
hash::{write_digest_to_transcript, Hasher},
hash::write_digest_to_transcript,
log2_strict,
merkle_tree::MerkleTree,
};
Expand Down Expand Up @@ -37,7 +37,6 @@ pub fn commit_phase<E: ExtensionField, Spec: BasefoldSpec<E>>(
transcript: &mut Transcript<E>,
num_vars: usize,
num_rounds: usize,
hasher: &Hasher<E::BaseField>,
) -> (Vec<MerkleTree<E>>, BasefoldCommitPhaseProof<E>)
where
E::BaseField: Serialize + DeserializeOwned,
Expand Down Expand Up @@ -117,7 +116,7 @@ where
// Then the oracle will be used to fold to the next oracle in the next
// round. After that, this oracle is free to be moved to build the
// complete Merkle tree.
running_tree_inner = MerkleTree::<E>::compute_inner_ext(&new_running_oracle, hasher);
running_tree_inner = MerkleTree::<E>::compute_inner_ext(&new_running_oracle);
let running_root = MerkleTree::<E>::root_from_inner(&running_tree_inner);
write_digest_to_transcript(&running_root, transcript);
roots.push(running_root.clone());
Expand Down Expand Up @@ -188,7 +187,6 @@ pub fn batch_commit_phase<E: ExtensionField, Spec: BasefoldSpec<E>>(
num_vars: usize,
num_rounds: usize,
coeffs: &[E],
hasher: &Hasher<E::BaseField>,
) -> (Vec<MerkleTree<E>>, BasefoldCommitPhaseProof<E>)
where
E::BaseField: Serialize + DeserializeOwned,
Expand Down Expand Up @@ -282,7 +280,7 @@ where
last_sumcheck_message =
sum_check_challenge_round(&mut eq, &mut sum_of_all_evals_for_sumcheck, challenge);
sumcheck_messages.push(last_sumcheck_message.clone());
running_tree_inner = MerkleTree::<E>::compute_inner_ext(&new_running_oracle, hasher);
running_tree_inner = MerkleTree::<E>::compute_inner_ext(&new_running_oracle);
let running_root = MerkleTree::<E>::root_from_inner(&running_tree_inner);
write_digest_to_transcript(&running_root, transcript);
roots.push(running_root);
Expand Down Expand Up @@ -362,7 +360,6 @@ pub fn simple_batch_commit_phase<E: ExtensionField, Spec: BasefoldSpec<E>>(
transcript: &mut Transcript<E>,
num_vars: usize,
num_rounds: usize,
hasher: &Hasher<E::BaseField>,
) -> (Vec<MerkleTree<E>>, BasefoldCommitPhaseProof<E>)
where
E::BaseField: Serialize + DeserializeOwned,
Expand Down Expand Up @@ -435,7 +432,7 @@ where
if i < num_rounds - 1 {
last_sumcheck_message =
sum_check_challenge_round(&mut eq, &mut running_evals, challenge);
running_tree_inner = MerkleTree::<E>::compute_inner_ext(&new_running_oracle, hasher);
running_tree_inner = MerkleTree::<E>::compute_inner_ext(&new_running_oracle);
let running_root = MerkleTree::<E>::root_from_inner(&running_tree_inner);
write_digest_to_transcript(&running_root, transcript);
roots.push(running_root);
Expand Down
Loading

0 comments on commit a9cd143

Please sign in to comment.