Skip to content

Commit

Permalink
fix: Pass stdin as a ref (#183)
Browse files Browse the repository at this point in the history
Co-authored-by: wwared <[email protected]>
  • Loading branch information
2 people authored and tchataigner committed Sep 27, 2024
1 parent c8535cb commit edfe344
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ethereum/light-client/benches/committee_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn main() {
let start_proving = Instant::now();
let proof = benchmark_assets
.prover
.prove(inputs, mode)
.prove(&inputs, mode)
.expect("Failed to prove committee change");
let proving_time = start_proving.elapsed();

Expand Down
2 changes: 1 addition & 1 deletion ethereum/light-client/benches/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn main() {
let start_proving = Instant::now();
let proof = benchmark_assets
.prover
.prove(inputs, mode)
.prove(&inputs, mode)
.expect("Failed to prove storage inclusion");
let proving_time = start_proving.elapsed();

Expand Down
2 changes: 1 addition & 1 deletion ethereum/light-client/src/bin/server_primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn main() -> Result<()> {
info!("Start proving");
let proof_handle = spawn_blocking(move || {
let (proving_mode, inputs) = *boxed;
committee_prover.prove(inputs, proving_mode)
committee_prover.prove(&inputs, proving_mode)
});
let proof = proof_handle.await??;
info!("Proof generated. Serializing");
Expand Down
2 changes: 1 addition & 1 deletion ethereum/light-client/src/bin/server_secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn main() -> Result<()> {
info!("Start proving");
let proof_handle = spawn_blocking(move || {
let (proving_mode, inputs) = *boxed;
inclusion_prover.prove(inputs, proving_mode)
inclusion_prover.prove(&inputs, proving_mode)
});
let proof = proof_handle.await??;
info!("Proof generated. Serializing");
Expand Down
16 changes: 10 additions & 6 deletions ethereum/light-client/src/proofs/committee_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Prover for CommitteeChangeProver {
type StdIn = CommitteeChangeIn;
type StdOut = CommitteeChangeOut;

fn generate_sphinx_stdin(&self, inputs: Self::StdIn) -> Result<SphinxStdin, Self::Error> {
fn generate_sphinx_stdin(&self, inputs: &Self::StdIn) -> Result<SphinxStdin, Self::Error> {
let mut stdin = SphinxStdin::new();
stdin.write(
&inputs
Expand All @@ -179,7 +179,7 @@ impl Prover for CommitteeChangeProver {
Ok(stdin)
}

fn execute(&self, inputs: Self::StdIn) -> Result<Self::StdOut, Self::Error> {
fn execute(&self, inputs: &Self::StdIn) -> Result<Self::StdOut, Self::Error> {
sphinx_sdk::utils::setup_logger();

let stdin = self.generate_sphinx_stdin(inputs)?;
Expand All @@ -193,7 +193,7 @@ impl Prover for CommitteeChangeProver {
Ok(CommitteeChangeOut::from(&mut public_values))
}

fn prove(&self, inputs: Self::StdIn, mode: ProvingMode) -> Result<ProofType, Self::Error> {
fn prove(&self, inputs: &Self::StdIn, mode: ProvingMode) -> Result<ProofType, Self::Error> {
let stdin = self.generate_sphinx_stdin(inputs)?;

match mode {
Expand Down Expand Up @@ -257,7 +257,7 @@ mod test {
update: test_assets.update_new_period.clone(),
};

let new_period_output = prover.execute(new_period_inputs).unwrap();
let new_period_output = prover.execute(&new_period_inputs).unwrap();

assert_eq!(
&new_period_output.finalized_block_height,
Expand Down Expand Up @@ -317,7 +317,9 @@ mod test {
println!("Starting STARK proving for sync committee change...");
let start = Instant::now();

let _ = prover.prove(new_period_inputs, ProvingMode::STARK).unwrap();
let _ = prover
.prove(&new_period_inputs, ProvingMode::STARK)
.unwrap();
println!("Proving took {:?}", start.elapsed());
}

Expand All @@ -343,7 +345,9 @@ mod test {
println!("Starting SNARK proving for sync committee change...");
let start = Instant::now();

let _ = prover.prove(new_period_inputs, ProvingMode::SNARK).unwrap();
let _ = prover
.prove(&new_period_inputs, ProvingMode::SNARK)
.unwrap();
println!("Proving took {:?}", start.elapsed());
}
}
39 changes: 20 additions & 19 deletions ethereum/light-client/src/proofs/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,37 +211,38 @@ impl Prover for StorageInclusionProver {
type StdIn = StorageInclusionIn;
type StdOut = StorageInclusionOut;

fn generate_sphinx_stdin(&self, inputs: Self::StdIn) -> Result<SphinxStdin, Self::Error> {
fn generate_sphinx_stdin(&self, inputs: &Self::StdIn) -> Result<SphinxStdin, Self::Error> {
let mut stdin = SphinxStdin::new();

let update_sig_period = calc_sync_period(inputs.update.signature_slot());
let store_period = calc_sync_period(inputs.store.finalized_header().beacon().slot());

let finalized_beacon_slot = *inputs.store.finalized_header().beacon().slot();
let correct_sync_committee = if update_sig_period == store_period {
inputs.store.into_current_sync_committee()
} else {
inputs
.store
.into_next_sync_committee()
.ok_or_else(|| ProverError::SphinxInput {
source: "Expected next sync committee".into(),
let correct_sync_committee =
if update_sig_period == store_period {
inputs.store.current_sync_committee()
} else {
inputs.store.next_sync_committee().as_ref().ok_or_else(|| {
ProverError::SphinxInput {
source: "Expected next sync committee".into(),
}
})?
};
};

stdin.write(
&CompactStore::new(finalized_beacon_slot, correct_sync_committee).to_ssz_bytes(),
&CompactStore::new(finalized_beacon_slot, correct_sync_committee.clone())
.to_ssz_bytes(),
);
stdin.write(
&CompactUpdate::from(inputs.update)
&CompactUpdate::from(inputs.update.clone())
.to_ssz_bytes()
.map_err(|err| ProverError::SphinxInput { source: err.into() })?,
);
stdin.write(&inputs.eip1186_proof.to_ssz_bytes());
Ok(stdin)
}

fn execute(&self, inputs: Self::StdIn) -> Result<Self::StdOut, Self::Error> {
fn execute(&self, inputs: &Self::StdIn) -> Result<Self::StdOut, Self::Error> {
sphinx_sdk::utils::setup_logger();

let stdin = self.generate_sphinx_stdin(inputs)?;
Expand All @@ -255,7 +256,7 @@ impl Prover for StorageInclusionProver {
Ok(StorageInclusionOut::from(&mut public_values))
}

fn prove(&self, inputs: Self::StdIn, mode: ProvingMode) -> Result<ProofType, Self::Error> {
fn prove(&self, inputs: &Self::StdIn, mode: ProvingMode) -> Result<ProofType, Self::Error> {
let stdin = self.generate_sphinx_stdin(inputs)?;

match mode {
Expand Down Expand Up @@ -315,7 +316,7 @@ mod test {
eip1186_proof: test_assets.eip1186_proof().clone(),
};

let inclusion_output = prover.execute(inclusion_input).unwrap();
let inclusion_output = prover.execute(&inclusion_input).unwrap();

assert_eq!(
inclusion_output.sync_committee_hash,
Expand Down Expand Up @@ -366,10 +367,10 @@ mod test {
eip1186_proof: test_assets.eip1186_proof().clone(),
};

println!("Starting STARK proving for sync committee change...");
println!("Starting STARK proving for storage inclusion...");
let start = Instant::now();

let _ = prover.prove(inclusion_inputs, ProvingMode::STARK).unwrap();
let _ = prover.prove(&inclusion_inputs, ProvingMode::STARK).unwrap();
println!("Proving took {:?}", start.elapsed());
}

Expand All @@ -388,10 +389,10 @@ mod test {
eip1186_proof: test_assets.eip1186_proof().clone(),
};

println!("Starting SNARK proving for sync committee change...");
println!("Starting SNARK proving for storage inclusion...");
let start = Instant::now();

let _ = prover.prove(inclusion_inputs, ProvingMode::SNARK).unwrap();
let _ = prover.prove(&inclusion_inputs, ProvingMode::SNARK).unwrap();
println!("Proving took {:?}", start.elapsed());
}
}
6 changes: 3 additions & 3 deletions ethereum/light-client/src/proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub trait Prover {
/// # Returns
///
/// The Sphinx stdin.
fn generate_sphinx_stdin(&self, inputs: Self::StdIn) -> Result<SphinxStdin, Self::Error>;
fn generate_sphinx_stdin(&self, inputs: &Self::StdIn) -> Result<SphinxStdin, Self::Error>;

/// Execute the program, useful to get the cycles that the program will take.
///
Expand All @@ -219,7 +219,7 @@ pub trait Prover {
/// # Returns
///
/// The output of the prover.
fn execute(&self, inputs: Self::StdIn) -> Result<Self::StdOut, Self::Error>;
fn execute(&self, inputs: &Self::StdIn) -> Result<Self::StdOut, Self::Error>;

/// Generate a proof for the program. The proof can either be a STARK or a SNARK proof.
///
Expand All @@ -231,7 +231,7 @@ pub trait Prover {
/// # Returns
///
/// The proof.
fn prove(&self, inputs: Self::StdIn, mode: ProvingMode) -> Result<ProofType, Self::Error>;
fn prove(&self, inputs: &Self::StdIn, mode: ProvingMode) -> Result<ProofType, Self::Error>;

/// Verify a proof for the program.
///
Expand Down
7 changes: 5 additions & 2 deletions fixture-generator/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn generate_fixture_inclusion_ethereum_lc() {
test_assets.finality_update.into(),
test_assets.eip1186_proof,
);
let proof = match prover.prove(input, ProvingMode::SNARK).unwrap() {
let proof = match prover.prove(&input, ProvingMode::SNARK).unwrap() {
ProofType::SNARK(inner_proof) => inner_proof,
_ => {
panic!("Unexpected proof")
Expand Down Expand Up @@ -231,7 +231,10 @@ fn generate_fixture_epoch_change_ethereum_lc() {
test_assets.update_new_period,
);
let prover = CommitteeChangeProver::new();
let proof = match prover.prove(new_period_inputs, ProvingMode::SNARK).unwrap() {
let proof = match prover
.prove(&new_period_inputs, ProvingMode::SNARK)
.unwrap()
{
ProofType::SNARK(inner_proof) => inner_proof,
_ => {
panic!("Unexpected proof")
Expand Down

0 comments on commit edfe344

Please sign in to comment.