Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions templates/sha_hasher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ members = ["host", "guest"]
resolver = "2"

[workspace.dependencies]
byteorder = "1.5.0"
sha2 = "0.10.8"

zisk-sdk = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.16.0" }
ziskos = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.16.0" }
zisk-sdk = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.16.1" }
ziskos = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.16.1" }
80 changes: 73 additions & 7 deletions templates/sha_hasher/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,80 @@
# sha_hasher

See https://0xpolygonhermez.github.io/zisk/getting_started/quickstart.html for
full instructions.
Computes SHA-256 iteratively inside the ZisK zkVM and generates a proof of correct execution.

## Usage
See https://0xpolygonhermez.github.io/zisk/getting_started/quickstart.html for full instructions.

Build with `cargo build --release`
## Prerequisites

Test execution with `cargo run --release --bin ziskemu`
Enter the dev shell:

Verify constraints with `cargo run --release --bin verify-constraints`
```
direnv allow
# or: nix develop
```

Generate proof with `cargo run --release --bin prove`
## Build

```
cargo build --release
```

To use the ZisK SHA-256 precompile instead of the Rust `sha2` crate:

```
cargo build --release --features precompile
```

## Run

**Execute** (run the guest program without generating a proof):

```
cargo run --release --bin execute
```

**Emulator** (run via the ZisK emulator with debug output):

```
cargo run --release --bin ziskemu
```

**Verify constraints** (check constraint satisfaction without proof generation):

```
cargo run --release --bin verify-constraints
```

**Generate and verify a proof**:

```
cargo run --release --bin prove
```

**Generate a compressed proof**:

```
cargo run --release --bin compressed
```

**Generate a PLONK SNARK proof** (for on-chain verification):

```
cargo run --release --bin plonk
```

## Structure

```
sha_hasher/
├── guest/src/main.rs # zkVM program: reads n, computes SHA-256 n times, commits result
└── host/
├── src/main.rs # Default binary: setup, execute, prove, verify
└── bin/
├── execute.rs # Execute only (no proof)
├── ziskemu.rs # Run via emulator
├── verify-constraints.rs # Constraint verification
├── prove.rs # Full proof generation + save/load
├── compressed.rs # Compressed proof
└── plonk.rs # PLONK SNARK proof
```
62 changes: 41 additions & 21 deletions templates/sha_hasher/flake.lock

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

7 changes: 5 additions & 2 deletions templates/sha_hasher/guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version = "0.1.0"
edition = "2024"

[dependencies]
byteorder = "1.5.0"
sha2 = "0.10.8"
sha2 = { version = "0.10.8", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"] }
ziskos = { workspace = true }

[features]
default = ["sha2"]
precompile = []
17 changes: 13 additions & 4 deletions templates/sha_hasher/guest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
ziskos::entrypoint!(main);

use serde::{Deserialize, Serialize};

#[cfg(not(feature = "precompile"))]
use sha2::{Digest, Sha256};

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -22,10 +24,17 @@ fn main() {

// Compute SHA-256 hashing 'n' times
for _ in 0..n {
let mut hasher = Sha256::new();
hasher.update(hash);
let digest = &hasher.finalize();
hash = Into::<[u8; 32]>::into(*digest);
#[cfg(feature = "precompile")]
{
hash = ziskos::zisklib::sha256(&hash);
}
#[cfg(not(feature = "precompile"))]
{
let mut hasher = Sha256::new();
hasher.update(hash);
let digest = &hasher.finalize();
hash = Into::<[u8; 32]>::into(*digest);
}
}

let output = Output { hash, iterations: n, magic_number: 0xDEADBEEF };
Expand Down
2 changes: 2 additions & 0 deletions templates/sha_hasher/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ sha2 = "0.10.8"

[build-dependencies]
zisk-sdk = { workspace = true }
clap = { version = "4", features = ["derive"] }

[features]
default = []
precompile = []
packed = []
gpu = ["zisk-sdk/gpu"]

Expand Down
6 changes: 3 additions & 3 deletions templates/sha_hasher/host/bin/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ fn main() -> Result<()> {
println!("PLONK proof verification successful!");

println!("Saving PLONK proof to disk...");
snark_proof.save_proof_with_publics("/tmp/sha_hasher_proof_snark_with_publics.bin")?;
println!("Proof saved to /tmp/sha_hasher_proof_snark_with_publics.bin");
snark_proof.save_proof_with_publics("tmp/sha_hasher_proof_snark_with_publics.bin")?;
println!("Proof saved to tmp/sha_hasher_proof_snark_with_publics.bin");

println!("Loading and verifying saved PLONK proof...");
let proof = ZiskProofWithPublicValues::load("/tmp/sha_hasher_proof_snark_with_publics.bin")?;
let proof = ZiskProofWithPublicValues::load("tmp/sha_hasher_proof_snark_with_publics.bin")?;
let vk = client.vk(&ELF)?;
client.verify(proof.get_proof(), proof.get_publics(), &vk)?;
println!("Saved PLONK proof verification successful!");
Expand Down
12 changes: 9 additions & 3 deletions templates/sha_hasher/host/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::path::PathBuf;
use zisk_sdk::{ZiskStdin, build_program};
use clap::Parser;
use zisk_sdk::{BuildArgs, ZiskStdin, build_program, build_program_with_args};

fn main() {
build_program("../guest");
if std::env::var("CARGO_FEATURE_PRECOMPILE").is_ok() {
let args = BuildArgs::parse_from(["build", "--no-default-features", "-F", "precompile"]);
build_program_with_args("../guest", args);
} else {
build_program("../guest");
}

let n = 1000u32;
let stdin_save = ZiskStdin::new();
stdin_save.write(&n);
// Check if path exists, if not write
let path = PathBuf::from("tmp/input.bin");
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).unwrap();
Expand Down
1 change: 1 addition & 0 deletions templates/sha_hasher_aggregation/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
4 changes: 4 additions & 0 deletions templates/sha_hasher_aggregation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
target
Cargo.lock
**/tmp
7 changes: 7 additions & 0 deletions templates/sha_hasher_aggregation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[workspace]
members = ["host", "guest", "guest_agg"]
resolver = "2"

[workspace.dependencies]
zisk-sdk = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.16.1" }
ziskos = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.16.1" }
Loading
Loading