-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from a16z/ncitron/add-hash-chain-examples
feat: add hash chain examples
- Loading branch information
Showing
9 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |