Skip to content

Commit

Permalink
Merge pull request #225 from a16z/ncitron/add-hash-chain-examples
Browse files Browse the repository at this point in the history
feat: add hash chain examples
  • Loading branch information
sragss authored Mar 28, 2024
2 parents 630fabc + 37187d6 commit 77c09d6
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ members = [
"examples/sha2-ex/guest",
"examples/sha3-ex",
"examples/sha3-ex/guest",
"examples/sha2-chain",
"examples/sha2-chain/guest",
"examples/sha3-chain",
"examples/sha3-chain/guest",
]

[profile.test]
Expand Down
11 changes: 11 additions & 0 deletions examples/sha2-chain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "sha2-chain"
version = "0.1.0"
edition = "2021"

[dependencies]
jolt-sdk = { path = "../../jolt-sdk", features = ["std"] }
guest = { package = "sha2-chain-guest", path = "./guest" }

hex = "0.4.3"
sha3 = { version = "0.10.8", default-features = false }
14 changes: 14 additions & 0 deletions examples/sha2-chain/guest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "sha2-chain-guest"
version = "0.1.0"
edition = "2021"

[lib]
path = "./src/main.rs"

[features]
guest = []

[dependencies]
sha2 = { version = "0.10.8", default-features = false }
jolt-sdk = { path = "../../../jolt-sdk" }
18 changes: 18 additions & 0 deletions examples/sha2-chain/guest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![cfg_attr(feature = "guest", no_std)]
#![cfg_attr(feature = "guest", no_main)]

use sha2::{Sha256, Digest};

#[jolt_sdk::main]
fn sha2_chain(input: [u8; 32], num_iters: u32) -> [u8; 32] {
let mut hash = input;
for _ in 0..num_iters {
let mut hasher = Sha256::new();
hasher.update(input);
let res = &hasher.finalize();
hash = Into::<[u8; 32]>::into (*res);
}

hash
}

12 changes: 12 additions & 0 deletions examples/sha2-chain/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub fn main() {
let (prove_sha2_chain, verify_sha2_chain) = guest::build_sha2_chain();

let input = [5u8; 32];
let iters = 100;
let (output, proof) = prove_sha2_chain(input, iters);
let is_valid = verify_sha2_chain(proof);


println!("output: {}", hex::encode(output));
println!("valid: {}", is_valid);
}
11 changes: 11 additions & 0 deletions examples/sha3-chain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "sha3-chain"
version = "0.1.0"
edition = "2021"

[dependencies]
jolt-sdk = { path = "../../jolt-sdk", features = ["std"] }
guest = { package = "sha3-chain-guest", path = "./guest" }

hex = "0.4.3"
sha3 = { version = "0.10.8", default-features = false }
14 changes: 14 additions & 0 deletions examples/sha3-chain/guest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "sha3-chain-guest"
version = "0.1.0"
edition = "2021"

[lib]
path = "./src/main.rs"

[features]
guest = []

[dependencies]
sha3 = { version = "0.10.8", default-features = false }
jolt-sdk = { path = "../../../jolt-sdk" }
18 changes: 18 additions & 0 deletions examples/sha3-chain/guest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![cfg_attr(feature = "guest", no_std)]
#![cfg_attr(feature = "guest", no_main)]

use sha3::{Keccak256, Digest};

#[jolt_sdk::main]
fn sha3_chain(input: [u8; 32], num_iters: u32) -> [u8; 32] {
let mut hash = input;
for _ in 0..num_iters {
let mut hasher = Keccak256::new();
hasher.update(input);
let res = &hasher.finalize();
hash = Into::<[u8; 32]>::into (*res);
}

hash
}

12 changes: 12 additions & 0 deletions examples/sha3-chain/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub fn main() {
let (prove_sha3_chain, verify_sha3_chain) = guest::build_sha3_chain();

let input = [5u8; 32];
let iters = 100;
let (output, proof) = prove_sha3_chain(input, iters);
let is_valid = verify_sha3_chain(proof);


println!("output: {}", hex::encode(output));
println!("valid: {}", is_valid);
}

0 comments on commit 77c09d6

Please sign in to comment.