Skip to content

Commit 8d18d24

Browse files
committed
add max shape
1 parent e70043c commit 8d18d24

11 files changed

Lines changed: 250 additions & 19 deletions

File tree

crates/core/machine/src/shape/maximal_shapes.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27052,6 +27052,26 @@
2705227052
"CloClz": 6,
2705327053
"SyscallCore": 3
2705427054
}
27055+
},
27056+
{
27057+
"inner": {
27058+
"Cpu": 21,
27059+
"MemoryLocal": 20,
27060+
"Lt": 20,
27061+
"MemoryInstrs": 20,
27062+
"SyscallInstrs": 20,
27063+
"ShiftLeft": 20,
27064+
"Bitwise": 20,
27065+
"ShiftRight": 20,
27066+
"Mul": 20,
27067+
"Global": 20,
27068+
"MiscInstrs": 20,
27069+
"Branch": 20,
27070+
"Jump": 20,
27071+
"AddSub": 20,
27072+
"CloClz": 20,
27073+
"SyscallCore": 20
27074+
}
2705527075
}
2705627076
]
2705727077
}

crates/core/machine/src/shape/small_shapes.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152683,5 +152683,28 @@
152683152683
"CloClz": 6,
152684152684
"SyscallCore": 18
152685152685
}
152686+
},
152687+
{
152688+
"inner": {
152689+
"Cpu": 21,
152690+
"MemoryLocal": 20,
152691+
"Lt": 20,
152692+
"MemoryInstrs": 20,
152693+
"DivRem": 20,
152694+
"SyscallInstrs": 20,
152695+
"ShiftLeft": 20,
152696+
"Bitwise": 20,
152697+
"ShiftRight": 20,
152698+
"Mul": 20,
152699+
"Global": 20,
152700+
"MemoryGlobalInit": 20,
152701+
"MiscInstrs": 20,
152702+
"Branch": 20,
152703+
"MemoryGlobalFinalize": 20,
152704+
"Jump": 20,
152705+
"AddSub": 20,
152706+
"CloClz": 20,
152707+
"SyscallCore": 20
152708+
}
152686152709
}
152687152710
]

examples/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/fibonacci/guest/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ publish = false
66

77
[dependencies]
88
zkm-zkvm = { path = "../../../crates/zkvm/entrypoint", features= ["embedded"] }
9+
sha3 = { version = "0.10.8", default-features = false }
10+
sha2 = { version = "0.10.8", default-features = false }

examples/fibonacci/guest/src/main.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
// inside the zkVM.
88
#![no_std]
99
#![no_main]
10+
11+
use sha2::Sha256;
12+
use sha3::{Digest, Keccak256};
13+
extern crate alloc;
14+
use alloc::vec::Vec;
15+
1016
zkm_zkvm::entrypoint!(main);
1117

12-
pub fn main() {
18+
pub fn fib() {
1319
// Read an input to the program.
1420
//
1521
// Behind the scenes, this compiles down to a system call which handles reading inputs
@@ -36,3 +42,27 @@ pub fn main() {
3642
zkm_zkvm::io::commit(&a);
3743
zkm_zkvm::io::commit(&b);
3844
}
45+
46+
pub fn sha2() {
47+
let input: Vec<u8> = zkm_zkvm::io::read();
48+
49+
let mut hasher = Sha256::new();
50+
hasher.update(input);
51+
let result: [u8; 32] = hasher.finalize().into();
52+
53+
zkm_zkvm::io::commit::<[u8; 32]>(&result.into());
54+
}
55+
56+
pub fn sha3() {
57+
let input: Vec<u8> = zkm_zkvm::io::read();
58+
59+
let mut hasher = Keccak256::new();
60+
hasher.update(input);
61+
let result = hasher.finalize();
62+
63+
zkm_zkvm::io::commit::<[u8; 32]>(&result.into());
64+
}
65+
66+
pub fn main() {
67+
sha3();
68+
}

examples/fibonacci/host/src/main.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
use std::time::{Duration, Instant};
12
use zkm_sdk::{include_elf, utils, ProverClient, ZKMProofWithPublicValues, ZKMStdin};
23

34
/// The ELF we want to execute inside the zkVM.
45
const ELF: &[u8] = include_elf!("fibonacci");
56

6-
fn main() {
7+
fn fib() {
78
// Setup logging.
89
utils::setup_logger();
910

1011
// Create an input stream and write '1000' to it.
11-
let n = 1000u32;
12+
let n = 100000u32;
1213

1314
// The input stream that the guest will read from using `zkm_zkvm::io::read`. Note that the
1415
// types of the elements in the input stream must match the types being read in the guest.
@@ -24,8 +25,11 @@ fn main() {
2425

2526
// Generate the proof for the given guest and input.
2627
let (pk, vk) = client.setup(ELF);
28+
let start = Instant::now();
2729
let mut proof = client.prove(&pk, stdin).run().unwrap();
28-
30+
let end = Instant::now();
31+
let duration = end.duration_since(start);
32+
println!("benchmark_sha2 end, duration: {:?}", duration.as_secs_f64());
2933
println!("generated proof");
3034

3135
// Read and verify the output.
@@ -52,3 +56,51 @@ fn main() {
5256

5357
println!("successfully generated and verified proof for the program!")
5458
}
59+
60+
fn sha2(num_bytes: usize) {
61+
// Setup logging.
62+
utils::setup_logger();
63+
64+
// The input stream that the guest will read from using `zkm_zkvm::io::read`. Note that the
65+
// types of the elements in the input stream must match the types being read in the guest.
66+
let mut stdin = ZKMStdin::new();
67+
let input = vec![5u8; num_bytes];
68+
stdin.write(&input);
69+
70+
// Create a `ProverClient` method.
71+
let client = ProverClient::new();
72+
73+
// Execute the guest using the `ProverClient.execute` method, without generating a proof.
74+
let (_, report) = client.execute(ELF, stdin.clone()).run().unwrap();
75+
println!("executed program with {} cycles", report.total_instruction_count());
76+
77+
/*
78+
// Generate the proof for the given guest and input.
79+
let (pk, vk) = client.setup(ELF);
80+
let start = Instant::now();
81+
let mut proof = client.prove(&pk, stdin).run().unwrap();
82+
let end = Instant::now();
83+
let duration = end.duration_since(start);
84+
println!("benchmark_sha2 end, duration: {:?}", duration.as_secs_f64());
85+
86+
println!("generated proof");
87+
88+
// Verify proof and public values
89+
client.verify(&proof, &vk).expect("verification failed");
90+
91+
// Test a round trip of proof serialization and deserialization.
92+
proof.save("proof-with-pis.bin").expect("saving proof failed");
93+
let deserialized_proof =
94+
ZKMProofWithPublicValues::load("proof-with-pis.bin").expect("loading proof failed");
95+
96+
// Verify the deserialized proof.
97+
client.verify(&deserialized_proof, &vk).expect("verification failed");
98+
*/
99+
println!("successfully generated and verified proof for the program!")
100+
}
101+
102+
fn main() {
103+
sha2(256);
104+
sha2(512);
105+
sha2(1048);
106+
}

examples/fibonacci_c_lib/host/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ use zkm_build::{build_program_with_args, BuildArgs};
33

44
fn main() {
55
zkm_build::build_program("../guest");
6-
}
6+
}

examples/simple-go/guest/go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
module simple-go
22

3-
go 1.22.5
3+
go 1.24.0
4+
5+
toolchain go1.24.7
46

57
replace github.com/ProjectZKM/Ziren/crates/go-runtime/zkm_runtime => ../../../crates/go-runtime/zkm_runtime
68

79
require github.com/ProjectZKM/Ziren/crates/go-runtime/zkm_runtime v0.0.0-20240817102429-2faba0888c02
10+
11+
require (
12+
golang.org/x/crypto v0.42.0 // indirect
13+
golang.org/x/sys v0.36.0 // indirect
14+
)

examples/simple-go/guest/main.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
package main
22

33
import (
4-
"log"
4+
"crypto/sha256"
5+
"fmt"
56

67
"github.com/ProjectZKM/Ziren/crates/go-runtime/zkm_runtime"
8+
"golang.org/x/crypto/sha3"
79
)
810

9-
func main() {
10-
a := zkm_runtime.Read[uint32]()
11+
func fib() {
12+
n := zkm_runtime.Read[uint32]()
13+
zkm_runtime.Commit[uint32](n)
1114

12-
if a != 10 {
13-
log.Fatal("%x != 10", a)
15+
a := uint32(0)
16+
b := uint32(1)
17+
for i := uint32(0); i < n; i++ {
18+
c := a + b
19+
c %= 7919
20+
a = b
21+
b = c
1422
}
1523

1624
zkm_runtime.Commit[uint32](a)
25+
zkm_runtime.Commit[uint32](b)
26+
}
27+
28+
func sha2() {
29+
data := zkm_runtime.Read[[]byte]()
30+
31+
hash := sha256.Sum256(data)
32+
33+
zkm_runtime.Commit[[32]byte](hash)
34+
}
35+
36+
func keccak() {
37+
data := zkm_runtime.Read[[]byte]()
38+
39+
hasher := sha3.NewLegacyKeccak256()
40+
hasher.Write(data)
41+
hash := hasher.Sum(nil)
42+
43+
var sumArray [32]byte
44+
copy(sumArray[:], hash)
45+
fmt.Println(sumArray)
46+
zkm_runtime.Commit[[32]byte](sumArray)
47+
}
48+
49+
func main() {
50+
// fib()
51+
keccak()
1752
}

examples/simple-go/host/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::process::Command;
21
use std::path::Path;
2+
use std::process::Command;
33

44
fn main() {
55
let go_src = Path::new("../guest");

0 commit comments

Comments
 (0)