Skip to content

Commit

Permalink
clean up commitment module exports
Browse files Browse the repository at this point in the history
  • Loading branch information
martyall committed Feb 7, 2025
1 parent 3940789 commit 927ff5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
6 changes: 3 additions & 3 deletions saffron/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<G: KimchiCurve> FieldBlob<G> {
#[instrument(skip_all, level = "debug")]
pub fn encode<
D: EvaluationDomain<G::ScalarField>,
EFqSponge: Clone + FqSponge<G::BaseField, G, G::ScalarField>,
EFqSponge: FqSponge<G::BaseField, G, G::ScalarField>,
>(
srs: &SRS<G>,
domain: D,
Expand Down Expand Up @@ -151,9 +151,9 @@ mod tests {
#[test]
fn test_user_and_storage_provider_commitments_equal(UserData(xs) in UserData::arbitrary())
{ let elems = encode_for_domain(&*DOMAIN, &xs);
let user_commitments = commit_to_field_elems(&*SRS, *DOMAIN, elems);
let user_commitments = commit_to_field_elems::<_, VestaFqSponge>(&*SRS, *DOMAIN, elems);
let blob = FieldBlob::<Vesta>::encode::<_, VestaFqSponge>(&*SRS, *DOMAIN, &xs);
prop_assert_eq!(user_commitments, blob.commitment.chunks);
prop_assert_eq!(user_commitments, blob.commitment);
}
}
}
28 changes: 10 additions & 18 deletions saffron/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,27 @@ impl<G: KimchiCurve> Commitment<G> {
}

#[instrument(skip_all, level = "debug")]
pub fn commit_to_field_elems<G: CommitmentCurve>(
pub fn commit_to_field_elems<G: KimchiCurve, EFqSponge>(
srs: &SRS<G>,
domain: D<G::ScalarField>,
field_elems: Vec<Vec<G::ScalarField>>,
) -> Vec<PolyComm<G>> {
field_elems
) -> Commitment<G>
where
EFqSponge: Clone + FqSponge<G::BaseField, G, G::ScalarField>,
{
let commitments = field_elems
.par_iter()
.map(|chunk| {
let evals = Evaluations::from_vec_and_domain(chunk.to_vec(), domain);
srs.commit_evaluations_non_hiding(domain, &evals)
})
.collect()
.collect();
let mut sponge = EFqSponge::new(G::other_curve_sponge_params());
Commitment::from_chunks(commitments, &mut sponge)
}

#[instrument(skip_all, level = "debug")]
pub fn fold_commitments<G: AffineRepr, EFqSponge: FqSponge<G::BaseField, G, G::ScalarField>>(
fn fold_commitments<G: AffineRepr, EFqSponge: FqSponge<G::BaseField, G, G::ScalarField>>(
sponge: &mut EFqSponge,
commitments: &[PolyComm<G>],
) -> (PolyComm<G>, G::ScalarField) {
Expand All @@ -75,16 +80,3 @@ pub fn fold_commitments<G: AffineRepr, EFqSponge: FqSponge<G::BaseField, G, G::S
alpha,
)
}

pub fn user_commitment<G: KimchiCurve, EFqSponge: FqSponge<G::BaseField, G, G::ScalarField>>(
srs: &SRS<G>,
domain: D<G::ScalarField>,
field_elems: Vec<Vec<G::ScalarField>>,
) -> PolyComm<G> {
let commitments = commit_to_field_elems(srs, domain, field_elems);
let (commitment, _) = {
let mut sponge = EFqSponge::new(G::other_curve_sponge_params());
fold_commitments(&mut sponge, &commitments)
};
commitment
}
4 changes: 2 additions & 2 deletions saffron/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rand::rngs::OsRng;
use saffron::{
blob::FieldBlob,
cli::{self, HexString},
commitment::user_commitment,
commitment::commit_to_field_elems,
env,
proof::{self, StorageProof},
utils,
Expand Down Expand Up @@ -102,7 +102,7 @@ pub fn compute_commitment(args: cli::ComputeCommitmentArgs) -> Result<HexString>
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
let field_elems = utils::encode_for_domain(&domain_fp, &buf);
let commitment = user_commitment::<_, VestaFqSponge>(&srs, domain_fp, field_elems);
let commitment = commit_to_field_elems::<_, VestaFqSponge>(&srs, domain_fp, field_elems);
let res = rmp_serde::to_vec(&commitment)?;
Ok(HexString(res))
}
Expand Down
12 changes: 4 additions & 8 deletions saffron/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ where
mod tests {
use super::*;
use crate::{
commitment::{commit_to_field_elems, fold_commitments},
commitment::commit_to_field_elems,
env,
utils::{encode_for_domain, test_utils::UserData},
};
Expand Down Expand Up @@ -153,13 +153,9 @@ mod tests {
#[test]
fn test_storage_prove_verify(UserData(data) in UserData::arbitrary()) {
let mut rng = OsRng;
let (commitment,_) = {
let commitment = {
let field_elems = encode_for_domain(&*DOMAIN, &data);
let user_commitments = commit_to_field_elems(&*SRS, *DOMAIN, field_elems);
let mut fq_sponge = VestaFqSponge::new(
mina_poseidon::pasta::fq_kimchi::static_params(),
);
fold_commitments(&mut fq_sponge, &user_commitments)
commit_to_field_elems::<_, VestaFqSponge>(&*SRS, *DOMAIN, field_elems)
};
let blob = FieldBlob::<Vesta>::encode::<_, VestaFqSponge>(&*SRS, *DOMAIN, &data);
let evaluation_point = Fp::rand(&mut rng);
Expand All @@ -170,7 +166,7 @@ mod tests {
let res = verify_storage_proof::<Vesta, VestaFqSponge>(
&*SRS,
&*GROUP_MAP,
commitment,
commitment.folded,
evaluation_point,
&proof,
&mut rng,
Expand Down

0 comments on commit 927ff5b

Please sign in to comment.