Skip to content

Commit

Permalink
chore: prover setup
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVikasRushi committed Oct 13, 2024
1 parent 11e7b72 commit 6ea59a2
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 199 deletions.
71 changes: 36 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "fibonacci-lib"
name = "lib"
version = "0.1.0"
edition = "2021"

Expand All @@ -8,7 +8,7 @@ alloy-sol-types = { workspace = true }
sha2 = { version = "0.10", default-features = false }
p256 = "0.13.2"
hex = "0.4.3"

serde = { version = "1.0",features = ["derive"] }

[patch.crates-io]
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-v0.10.8" }
20 changes: 3 additions & 17 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod constants;
pub mod sxg;
pub mod test_cases;

use alloy_sol_types::sol;
use p256::ecdsa::{signature::Verifier, Signature, VerifyingKey};
Expand All @@ -8,24 +8,10 @@ use sha2::{Digest, Sha256};
sol! {
/// The public values encoded as a struct that can be easily deserialized inside Solidity.
struct PublicValuesStruct {
uint32 n;
uint32 a;
uint32 b;
uint32 result;
}
}

/// Compute the n'th fibonacci number (wrapping around on overflows), using normal Rust code.
pub fn fibonacci(n: u32) -> (u32, u32) {
let mut a = 0u32;
let mut b = 1u32;
for _ in 0..n {
let c = a.wrapping_add(b);
a = b;
b = c;
}
(a, b)
}

pub fn sha256_hash(bytes: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(bytes);
Expand Down Expand Up @@ -69,7 +55,7 @@ pub fn verify_ecdsa_p256_r_s(

#[cfg(test)]
mod tests {
use constants::FINAL_PAYLOAD;
use test_cases::FINAL_PAYLOAD;

use super::*;

Expand Down
80 changes: 42 additions & 38 deletions lib/src/sxg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use crate::{sha256_hash, verify_ecdsa_p256_r_s};

use crate::{
sha256_hash,
test_cases::{DATA_TO_VERIFY, FINAL_PAYLOAD, PAYLOAD},
verify_ecdsa_p256_r_s,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct SXGInput {
pub final_payload: Vec<u8>,
pub data_to_verify: Vec<u8>,
Expand Down Expand Up @@ -78,43 +84,32 @@ fn base64_encode_mice(input: &[u8]) -> String {
result
}

pub fn sxg_verify(input: SXGInput) -> Result<bool, Box<dyn std::error::Error>> {
let SXGInput {
final_payload,
data_to_verify,
data_to_verify_start_index,
integrity_start_index,
payload,
r,
s,
px,
py,
} = input;

if payload[data_to_verify_start_index..data_to_verify_start_index + data_to_verify.len()]
!= data_to_verify
{
return Ok(false);
}

let mice = base64_encode_mice(&calculate_integrity(&payload, 16384));
let mice_bytes = mice.as_bytes();
impl SXGInput {
pub fn verify(&self) -> Result<bool, Box<dyn std::error::Error>> {
if self.payload[self.data_to_verify_start_index
..self.data_to_verify_start_index + self.data_to_verify.len()]
!= self.data_to_verify
{
return Ok(false);
}

if final_payload[integrity_start_index..integrity_start_index + mice_bytes.len()]
!= mice_bytes[..]
{
return Ok(false);
}
let mice = base64_encode_mice(&calculate_integrity(&self.payload, 16384));
let mice_bytes = mice.as_bytes();

Ok(verify_ecdsa_p256_r_s(&final_payload, &r, &s, &px, &py).is_ok())
}
if self.final_payload
[self.integrity_start_index..self.integrity_start_index + mice_bytes.len()]
!= mice_bytes[..]
{
return Ok(false);
}

#[cfg(test)]
mod tests {
use crate::constants::{DATA_TO_VERIFY, FINAL_PAYLOAD, PAYLOAD};
Ok(
verify_ecdsa_p256_r_s(&self.final_payload, &self.r, &self.s, &self.px, &self.py)
.is_ok(),
)
}

#[test]
fn test_sxg() {
pub fn default_testcase() -> SXGInput {
let final_payload = FINAL_PAYLOAD;
let data_to_verify = DATA_TO_VERIFY;
let payload = PAYLOAD;
Expand All @@ -134,7 +129,7 @@ mod tests {
let px = hex::decode(px).unwrap();
let py = hex::decode(py).unwrap();

let input = super::SXGInput {
SXGInput {
final_payload: final_payload.to_vec(),
data_to_verify: data_to_verify.to_vec(),
data_to_verify_start_index,
Expand All @@ -144,8 +139,17 @@ mod tests {
s: s.try_into().unwrap(),
px: px.try_into().unwrap(),
py: py.try_into().unwrap(),
};
}
}
}

assert!(super::sxg_verify(input).unwrap());
#[cfg(test)]
mod tests {
use crate::sxg::SXGInput;

#[test]
fn test_sxg() {
let default_input = SXGInput::default_testcase();
assert!(default_input.verify().unwrap());
}
}
File renamed without changes.
4 changes: 2 additions & 2 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
version = "0.1.0"
name = "fibonacci-program"
name = "sxg-program"
edition = "2021"

[dependencies]
alloy-sol-types = { workspace = true }
sp1-zkvm = "2.0.0"
fibonacci-lib = { path = "../lib" }
lib = { path = "../lib" }
hex = "0.4.3"
63 changes: 5 additions & 58 deletions program/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,13 @@
//! A simple program that takes a number `n` as input, and writes the `n-1`th and `n`th fibonacci
//! number as an output.

// These two lines are necessary for the program to properly compile.
//
// Under the hood, we wrap your main function with some extra code so that it behaves properly
// inside the zkVM.
#![no_main]
sp1_zkvm::entrypoint!(main);

use alloy_sol_types::SolType;
use fibonacci_lib::constants::{DATA_TO_VERIFY, FINAL_PAYLOAD, PAYLOAD};

use fibonacci_lib::sxg::{sxg_verify, SXGInput};
use fibonacci_lib::{fibonacci, sha256_hash, verify_ecdsa_p256_signature, PublicValuesStruct};
use hex;
use lib::sxg::SXGInput;
use lib::PublicValuesStruct;
pub fn main() {
// Read an input to the program.
//
// Behind the scenes, this compiles down to a custom system call which handles reading inputs
// from the prover.

let n = sp1_zkvm::io::read::<u32>();

let (a, b) = fibonacci(n);

let final_payload = FINAL_PAYLOAD;
let data_to_verify = DATA_TO_VERIFY;
let payload = PAYLOAD;

let data_to_verify_start_index = 0;
let integrity_start_index = 694 / 2;

let px = "45E3943B0705F9EF69B53A4EFB8C668E6A9F90124E9BCF917662CFADEA56C0C1";
let py = "F3703834F92F6FE70A004BA4098D079BFB5F927E042991EFD5A1572E8F9D39D6";

let r = "9970818CBCA38C196795EEAD295BDED48311702DF7DDB0C2BB448276894C393D";
let s = "729B2F9229D545A553F0F7CBC1792E9A6185E539DBF667FE5BC38D673D90C014";

let r = hex::decode(r).unwrap();
let s = hex::decode(s).unwrap();

let px = hex::decode(px).unwrap();
let py = hex::decode(py).unwrap();

let input = SXGInput {
final_payload: final_payload.to_vec(),
data_to_verify: data_to_verify.to_vec(),
data_to_verify_start_index,
integrity_start_index,
payload: payload.to_vec(),
r: r.try_into().unwrap(),
s: s.try_into().unwrap(),
px: px.try_into().unwrap(),
py: py.try_into().unwrap(),
};

let result = sxg_verify(input).unwrap();

// Encode the public values of the program.
let bytes = PublicValuesStruct::abi_encode(&PublicValuesStruct { n, a, b });

// Commit to the public values of the program. The final proof will have a commitment to all the
// bytes that were committed to.
let sxg_input = sp1_zkvm::io::read::<SXGInput>();
let result = sxg_input.verify().unwrap() as u32;
let bytes = PublicValuesStruct::abi_encode(&PublicValuesStruct { result });
sp1_zkvm::io::commit_slice(&bytes);
}
8 changes: 4 additions & 4 deletions script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
version = "0.1.0"
name = "fibonacci-script"
name = "sxg-script"
edition = "2021"
default-run = "fibonacci"
default-run = "sxg"

[[bin]]
name = "fibonacci"
name = "sxg"
path = "src/bin/main.rs"

[[bin]]
Expand All @@ -20,7 +20,7 @@ clap = { version = "4.0", features = ["derive", "env"] }
tracing = "0.1.40"
hex = "0.4.3"
alloy-sol-types = { workspace = true }
fibonacci-lib = { path = "../lib" }
lib = { path = "../lib" }

[build-dependencies]
sp1-helper = "2.0.0"
Loading

0 comments on commit 6ea59a2

Please sign in to comment.