Skip to content

Commit

Permalink
nice
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierdmello committed Jun 8, 2024
1 parent 37e33b9 commit bd2a900
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 18 deletions.
5 changes: 4 additions & 1 deletion primitives/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ pub const PUBKEY_LENGTH: usize = 32;
pub const WEIGHT_LENGTH: usize = 8;

// Blake2b hash size.
pub const HASH_SIZE: usize = 32;
pub const HASH_SIZE: usize = 32;

// ABI-encoded length of the header range outputs.
pub const HEADER_OUTPUTS_LENGTH: usize = 32 * 7;
3 changes: 2 additions & 1 deletion primitives/src/header_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloy_primitives::B256;
use blake2::digest::{Update, VariableOutput};
use blake2::Blake2bVar;

use crate::consts::HEADER_OUTPUTS_LENGTH;
use crate::merkle::get_merkle_root_commitments;
use crate::types::{
CircuitJustification, DecodedHeaderData, HeaderRangeInputs, HeaderRangeOutputs,
Expand All @@ -14,7 +15,7 @@ use alloy_sol_types::SolType;
pub fn verify_header_range(
header_range_inputs: HeaderRangeInputs,
target_justification: CircuitJustification,
) -> [u8; 32 * 7] {
) -> [u8; HEADER_OUTPUTS_LENGTH] {
let encoded_headers = header_range_inputs.encoded_headers;

// 1. Decode the headers using: https://github.com/succinctlabs/vectorx/blob/fb83641259aef1f5df33efa73c23d90973d64e24/circuits/builder/decoder.rs#L104-L157
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{compute_authority_set_commitment, decode_scale_compact_int, types::R

/// Verify the justification from the current authority set on the epoch end header and return the new
/// authority set commitment.
pub fn verify_rotation(rotate_inputs: RotateInputs) -> B256 {
pub fn verify_rotate(rotate_inputs: RotateInputs) -> B256 {

// Compute new authority set hash & convert it from binary to bytes32 for the blockchain
let new_authority_set_hash =
Expand Down
10 changes: 10 additions & 0 deletions primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ pub enum ProofType {
RotateProof,
}

impl ProofType {
pub fn from_uint(value: u8) -> Option<ProofType> {
match value {
0 => Some(ProofType::HeaderRangeProof),
1 => Some(ProofType::RotateProof),
_ => None,
}
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct RotateInputs {
pub current_authority_set_id: u64,
Expand Down
Binary file modified program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
19 changes: 9 additions & 10 deletions program/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

use alloy_sol_types::SolType;
use sp1_vectorx_primitives::{
compute_authority_set_commitment, decode_scale_compact_int,
types::{
CircuitJustification, HeaderRangeInputs,
ProofOutput, RotateInputs, ProofType
},
consts::HEADER_OUTPUTS_LENGTH,
header_range::verify_header_range,
rotate::verify_rotation,
rotate::verify_rotate,
types::{CircuitJustification, HeaderRangeInputs, ProofOutput, ProofType, RotateInputs},
};
use alloy_sol_types::{sol, SolType, SolStruct};

pub fn main() {
let proof_type: ProofType = sp1_zkvm::io::read::<ProofType>();
Expand All @@ -22,13 +19,15 @@ pub fn main() {
ProofType::HeaderRangeProof => {
let header_range_inputs = sp1_zkvm::io::read::<HeaderRangeInputs>();
let target_justification = sp1_zkvm::io::read::<CircuitJustification>();
let header_range_outputs = verify_header_range(header_range_inputs, target_justification);
let header_range_outputs =
verify_header_range(header_range_inputs, target_justification);
output = ProofOutput::abi_encode(&(0, header_range_outputs, [0u8; 32]));
}
ProofType::RotateProof => {
let rotate_inputs = sp1_zkvm::io::read::<RotateInputs>();
let new_authority_set_hash = verify_rotation(rotate_inputs);
output = ProofOutput::abi_encode(&(1, [0u8; 32 * 7], new_authority_set_hash));
let new_authority_set_hash = verify_rotate(rotate_inputs);
output =
ProofOutput::abi_encode(&(1, [0u8; HEADER_OUTPUTS_LENGTH], new_authority_set_hash));
}
}

Expand Down
29 changes: 24 additions & 5 deletions script/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A simple script to generate and verify the proof of a given program.
use sp1_sdk::{utils::setup_logger, ProverClient, SP1Stdin};
use sp1_vectorx_primitives::types::{ProofOutput, ProofType};
use sp1_vectorx_primitives::types::{HeaderRangeOutputs, ProofOutput, ProofType};
use sp1_vectorx_script::input::RpcDataFetcher;
const ELF: &[u8] = include_bytes!("../../program/elf/riscv32im-succinct-zkvm-elf");
use alloy_sol_types::SolType;
Expand All @@ -10,13 +10,13 @@ use alloy_sol_types::SolType;
async fn main() -> anyhow::Result<()> {
setup_logger();

// Supply an initial authority set id.
// TODO: Read from args/contract in the future. Set to 1 for testing.
// Supply an initial authority set id, trusted block, and target block.
// TODO: Read from args/contract in the future.
let authority_set_id = 74u64;
let trusted_block = 272355;
let target_block = 272534;

let proof_type = ProofType::RotateProof;
let proof_type = ProofType::HeaderRangeProof;

let fetcher = RpcDataFetcher::new().await;
let client = ProverClient::new();
Expand Down Expand Up @@ -52,7 +52,10 @@ async fn main() -> anyhow::Result<()> {
// Read outputs.
let mut output_bytes = [0u8; 384];
proof.public_values.read_slice(&mut output_bytes);
let _outputs = ProofOutput::abi_decode(&output_bytes, true)?;
let outputs = ProofOutput::abi_decode(&output_bytes, true)?;

// Log proof outputs.
log_proof_outputs(outputs);

// Verify proof.
client.verify(&proof, &vk)?;
Expand All @@ -62,6 +65,22 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}

fn log_proof_outputs(outputs: (u8, alloy_primitives::Bytes, alloy_primitives::FixedBytes<32>)) {
let proof_type = ProofType::from_uint(outputs.0).unwrap();
match proof_type {
ProofType::HeaderRangeProof => {
let header_range_outputs = HeaderRangeOutputs::abi_decode(&outputs.1, true).unwrap();
println!("Proof Type: Header Range Proof");
println!("Header Range Outputs: {:?}", header_range_outputs);
}
ProofType::RotateProof => {
let new_authority_set_hash = outputs.2;
println!("Proof Type: Rotate Proof");
println!("New authority set hash: {:?}", new_authority_set_hash);
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit bd2a900

Please sign in to comment.