Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pass stdin as a ref #183

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -133,7 +133,7 @@ fn generate_fixture_inclusion_ethereum_lc() {
test_assets.finality_update().clone().into(),
test_assets.eip1186_proof().clone(),
);
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 @@ -225,7 +225,10 @@ fn generate_fixture_epoch_change_ethereum_lc() {
let new_period_inputs =
CommitteeChangeIn::new(test_assets.store, 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
Loading