Skip to content

Commit

Permalink
add storage proof implementation to main, add proof generation to e2e…
Browse files Browse the repository at this point in the history
… script
  • Loading branch information
martyall committed Jan 30, 2025
1 parent ea470fe commit e99a6c9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
32 changes: 29 additions & 3 deletions saffron/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use anyhow::Result;
use ark_poly::{EvaluationDomain, Radix2EvaluationDomain};
use clap::Parser;
use mina_curves::pasta::{Fp, Vesta};
use poly_commitment::{ipa::SRS, SRS as _};
use kimchi::groupmap::GroupMap;
use mina_curves::pasta::{Fp, Vesta, VestaParameters};
use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, sponge::DefaultFqSponge};
use poly_commitment::{commitment::CommitmentCurve, ipa::SRS, SRS as _};
use rand::rngs::OsRng;
use saffron::{
blob::FieldBlob,
cli::{self, HexString},
commitment, env, utils,
commitment, env, proof, utils,
};
use sha3::{Digest, Sha3_256};
use std::{
Expand Down Expand Up @@ -102,6 +105,24 @@ pub fn compute_commitment(args: cli::ComputeCommitmentArgs) -> Result<HexString>
Ok(HexString(hash))
}

pub fn storage_proof(args: cli::StorageProofArgs) -> Result<HexString> {
let file = File::open(args.input)?;
let blob: FieldBlob<Vesta> = rmp_serde::decode::from_read(file)?;
let proof =
{
let (srs, _) = get_srs(args.srs_cache);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let mut rng = OsRng;
let evaluation_point = utils::encode(&args.challenge.0);
proof::storage_proof::<
Vesta,
DefaultFqSponge<VestaParameters, PlonkSpongeConstantsKimchi>,
>(&srs, &group_map, blob, evaluation_point, &mut rng)
};
let bytes = rmp_serde::to_vec(&proof).unwrap();
Ok(HexString(bytes))
}

pub fn main() -> Result<()> {
env::init_console_subscriber();
let args = cli::Commands::parse();
Expand All @@ -113,5 +134,10 @@ pub fn main() -> Result<()> {
println!("{}", commitment);
Ok(())
}
cli::Commands::StorageProof(args) => {
let proof = storage_proof(args)?;
println!("{}", proof);
Ok(())
}
}
}
2 changes: 1 addition & 1 deletion saffron/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ark_poly::EvaluationDomain;

// For injectivity, you can only use this on inputs of length at most
// 'F::MODULUS_BIT_SIZE / 8', e.g. for Vesta this is 31.
fn encode<Fp: PrimeField>(bytes: &[u8]) -> Fp {
pub fn encode<Fp: PrimeField>(bytes: &[u8]) -> Fp {
Fp::from_be_bytes_mod_order(bytes)
}

Expand Down
13 changes: 13 additions & 0 deletions saffron/test-encoding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ if ! cargo run --release --bin saffron encode -i "$INPUT_FILE" -o "$ENCODED_FILE
exit 1
fi

# Generate 32-byte random challenge as hex string
echo "Generating random challenge..."
CHALLENGE=$(head -c 32 /dev/urandom | xxd -p -c 32)
echo "Challenge: $CHALLENGE"

# Generate storage proof and capture proof output
echo "Generating storage proof..."
PROOF=$(cargo run --release --bin saffron storage-proof -i "$ENCODED_FILE" --challenge "$CHALLENGE" $SRS_ARG | tee /dev/stderr | tail -n 1)
if [ $? -ne 0 ]; then
echo "Storage proof generation failed"
exit 1
fi

# Run decode
echo "Decoding $ENCODED_FILE to $DECODED_FILE"
if ! cargo run --release --bin saffron decode -i "$ENCODED_FILE" -o "$DECODED_FILE" $SRS_ARG; then
Expand Down

0 comments on commit e99a6c9

Please sign in to comment.