Skip to content

Commit

Permalink
fix sp1 aggregation
Browse files Browse the repository at this point in the history
Signed-off-by: smtmfft <[email protected]>
  • Loading branch information
smtmfft committed Oct 3, 2024
1 parent e34567f commit d14774d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct AggregationGuestOutput {
pub hash: B256,
}

#[derive(Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZkAggregationGuestInput {
pub image_id: [u32; 8],
pub block_inputs: Vec<B256>,
Expand Down
9 changes: 9 additions & 0 deletions lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ pub fn words_to_bytes_le(words: &[u32; 8]) -> [u8; 32] {
bytes
}

pub fn words_to_bytes_be(words: &[u32; 8]) -> [u8; 32] {
let mut bytes = [0u8; 32];
for i in 0..8 {
let word_bytes = words[i].to_be_bytes();
bytes[i * 4..(i + 1) * 4].copy_from_slice(&word_bytes);
}
bytes
}

pub fn aggregation_output_combine(public_inputs: Vec<B256>) -> Vec<u8> {
let mut output = Vec::with_capacity(public_inputs.len() * 32);
for public_input in public_inputs.iter() {
Expand Down
3 changes: 2 additions & 1 deletion pipeline/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ impl Executor {
let elf = std::fs::read(&dest.join(&name.replace('_', "-")))?;
let prover = CpuProver::new();
let key_pair = prover.setup(&elf);
println!("sp1 elf vk is: {}", key_pair.1.bytes32());
println!("sp1 elf vk bn256 is: {}", key_pair.1.bytes32());
println!("sp1 elf vk hash_bytes is: {}", hex::encode(&key_pair.1.hash_bytes()));
}

Ok(())
Expand Down
26 changes: 17 additions & 9 deletions provers/sp1/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl Prover for Sp1Prover {
time.stop_with("==> Verification complete");
}

let proof_string = proof_bytes.is_empty().then_some(
let proof_string = (!proof_bytes.is_empty()).then_some(
// 0x + 64 bytes of the vkey + the proof
// vkey itself contains 0x prefix
format!(
Expand Down Expand Up @@ -269,6 +269,11 @@ impl Prover for Sp1Prover {
image_id: image_id,
block_inputs,
};
info!(
"Aggregating {:?} proofs with input: {:?}",
input.proofs.len(),
aggregation_input
);

let mut stdin = SP1Stdin::new();
stdin.write(&aggregation_input);
Expand Down Expand Up @@ -296,6 +301,12 @@ impl Prover for Sp1Prover {
.unwrap_or_else(ProverClient::new);

let (pk, vk) = client.setup(AGGREGATION_ELF);
info!(
"sp1 aggregate: {:?} based {:?} blocks with vk {:?}",
reth_primitives::hex::encode_prefixed(stark_vk.hash_bytes()),
input.proofs.len(),
vk.bytes32()
);

let prove_result = client
.prove(&pk, stdin)
Expand All @@ -306,27 +317,24 @@ impl Prover for Sp1Prover {
let proof_bytes = prove_result.bytes();
if param.verify {
let time = Measurement::start("verify", false);
let pi_hash = prove_result
.clone()
.borrow_mut()
.public_values
.read::<[u8; 32]>();
let aggregation_pi = prove_result.clone().borrow_mut().public_values.raw();
let fixture = RaikoProofFixture {
vkey: vk.bytes32().to_string(),
public_values: B256::from_slice(&pi_hash).to_string(),
public_values: reth_primitives::hex::encode_prefixed(&aggregation_pi),
proof: reth_primitives::hex::encode_prefixed(&proof_bytes),
};

verify_sol(&fixture)?;
time.stop_with("==> Verification complete");
}

let proof = proof_bytes.is_empty().then_some(
let proof = (!proof_bytes.is_empty()).then_some(
// 0x + 64 bytes of the vkey + the proof
// vkey itself contains 0x prefix
format!(
"{}{}",
"{}{}{}",
vk.bytes32(),
reth_primitives::hex::encode(stark_vk.hash_bytes()),
reth_primitives::hex::encode(proof_bytes)
),
);
Expand Down
Binary file modified provers/sp1/guest/elf/sp1-aggregation
Binary file not shown.
Binary file modified provers/sp1/guest/elf/sp1-guest
Binary file not shown.
4 changes: 2 additions & 2 deletions provers/sp1/guest/src/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sha2::{Digest, Sha256};
use raiko_lib::{
input::ZkAggregationGuestInput,
primitives::B256,
protocol_instance::{aggregation_output, words_to_bytes_le},
protocol_instance::{aggregation_output, words_to_bytes_be},
};

pub fn main() {
Expand All @@ -24,7 +24,7 @@ pub fn main() {

// The aggregation output
sp1_zkvm::io::commit_slice(&aggregation_output(
B256::from(words_to_bytes_le(&input.image_id)),
B256::from(words_to_bytes_be(&input.image_id)),
input.block_inputs,
));
}
Expand Down

0 comments on commit d14774d

Please sign in to comment.