Skip to content

Commit de25aac

Browse files
committed
powdr: create prover bindings lib
1 parent 468c6ac commit de25aac

File tree

12 files changed

+4352
-32
lines changed

12 files changed

+4352
-32
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ main*
33
zig-cache/
44
.zig-cache/
55
zig-out/
6+
target

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,28 @@ out for this space for annoucements on the same or reach out to us via our [comm
5959

6060
### Build instructions
6161

62-
Zeam requires zig version 0.14.0 to build.
62+
63+
#### Prerequisites
64+
65+
- Zeam requires zig version 0.14.0 to build.
66+
- Zeam requires Rust 1.85+ to be able to build the rust connections to various zkvms
67+
68+
#### Build
6369

6470
In order to build the transition functions (one for each supported zkvm), as well as the whole client, type:
6571

6672
```
67-
> zig build
73+
> zig build -Doptimize=ReleaseFast
6874
```
6975

7076
at the root of the repository.
7177

78+
#### Running the prover demo
79+
80+
```
81+
> zig build -Doptimize=ReleaseFast run -- prove
82+
```
83+
7284
### Reporting Issues
7385

7486
Open an [issue or a bug](https://github.com/blockblaz/zeam/issues/new) or else talk to us via our [community telegram group](https://t.me/zeamETH).

build.zig

+29-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const zkvmTarget = struct {
88
};
99

1010
const zkvm_targets: []const zkvmTarget = &.{
11-
.{ .name = "powdr", .set_pie = true },
11+
.{ .name = "powdr", .set_pie = true, .build_glue = true },
1212
.{ .name = "ceno", .set_pie = false },
1313
};
1414

@@ -52,13 +52,16 @@ pub fn build(b: *Builder) !void {
5252
.target = target,
5353
.optimize = optimize,
5454
});
55+
zeam_state_transition.addImport("@zeam/types", zeam_types);
5556

5657
// add state proving manager
5758
const zeam_state_proving_manager = b.addModule("@zeam/state-proving-manager", .{
5859
.root_source_file = b.path("pkgs/state-proving-manager/src/manager.zig"),
5960
.target = target,
6061
.optimize = optimize,
6162
});
63+
zeam_state_proving_manager.addImport("@zeam/types", zeam_types);
64+
zeam_state_proving_manager.addImport("ssz", ssz);
6265

6366
const st_lib = b.addStaticLibrary(.{
6467
.name = "zeam-state-transition",
@@ -93,8 +96,33 @@ pub fn build(b: *Builder) !void {
9396
cli_exe.root_module.addImport("@zeam/state-proving-manager", zeam_state_proving_manager);
9497
cli_exe.root_module.addImport("@zeam/node", zeam_beam_node);
9598
cli_exe.root_module.addImport("@zeam/params", zeam_params);
99+
for (zkvm_targets) |zkvm_target| {
100+
if (zkvm_target.build_glue) {
101+
const zkvm_host_cmd = b.addSystemCommand(&.{
102+
"cargo",
103+
"+nightly",
104+
"-C",
105+
b.fmt("pkgs/state-transition-runtime/src/{s}/host", .{zkvm_target.name}),
106+
"-Z",
107+
"unstable-options",
108+
"build",
109+
"--release",
110+
});
111+
cli_exe.step.dependOn(&zkvm_host_cmd.step);
112+
cli_exe.addObjectFile(b.path(b.fmt("pkgs/state-transition-runtime/src/{s}/host/target/release/libzeam_prover_host_{s}.a", .{ zkvm_target.name, zkvm_target.name })));
113+
}
114+
}
115+
cli_exe.linkLibC(); // for rust static libs to link
116+
cli_exe.linkSystemLibrary("unwind"); // to be able to display rust backtraces
96117
b.installArtifact(cli_exe);
97118

119+
const run_prover = b.addRunArtifact(cli_exe);
120+
const prover_step = b.step("run", "Run cli executable");
121+
prover_step.dependOn(&run_prover.step);
122+
if (b.args) |args| {
123+
run_prover.addArgs(args);
124+
}
125+
98126
const tests = b.addTest(.{
99127
.root_source_file = b.path("pkgs/tests.zig"),
100128
.optimize = optimize,
@@ -169,15 +197,5 @@ fn build_zkvm_targets(b: *Builder) !void {
169197
}
170198
exe.setLinkerScript(b.path(b.fmt("pkgs/state-transition-runtime/src/{s}/{s}.ld", .{ zkvm_target.name, zkvm_target.name })));
171199
b.installArtifact(exe);
172-
173-
// build the library connecting to the zkvm
174-
if (zkvm_target.build_glue) {
175-
_ = b.addSystemCommand(&.{
176-
"cargo",
177-
"-C",
178-
b.fmt("pkgs/state-transition-runtime/src/{s}/host", .{zkvm_target.name}),
179-
"build",
180-
});
181-
}
182200
}
183201
}

pkgs/cli/src/main.zig

+30-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ const simargs = @import("simargs");
44
const types = @import("@zeam/types");
55
const nodeLib = @import("@zeam/node");
66
const Clock = nodeLib.Clock;
7+
const stateProvingManager = @import("@zeam/state-proving-manager");
78

89
const ZeamArgs = struct {
910
genesis: ?u64,
1011

11-
__commands__: union(enum) { clock: struct {} },
12+
__commands__: union(enum) { clock: struct {}, prove: struct {} },
1213

1314
pub const __messages__ = .{
1415
.genesis = "genesis time",
@@ -28,5 +29,33 @@ pub fn main() !void {
2829

2930
try clock.run();
3031
},
32+
.prove => {
33+
const state = types.BeamState{
34+
.genesis_time = 0,
35+
.slot = 0,
36+
.latest_block_header = .{
37+
.slot = 0,
38+
.proposer_index = 0,
39+
.parent_root = [_]u8{0} ** 32,
40+
.state_root = [_]u8{0} ** 32,
41+
.body_root = [_]u8{0} ** 32,
42+
},
43+
};
44+
const block = types.SignedBeamBlock{
45+
.message = .{
46+
.slot = 0,
47+
.proposer_index = 0,
48+
.parent_root = [_]u8{0} ** 32,
49+
.state_root = [_]u8{0} ** 32,
50+
.body = .{},
51+
},
52+
.signature = [_]u8{0} ** 48,
53+
};
54+
const options = stateProvingManager.StateTransitionOpts{
55+
.zk_vm = stateProvingManager.zkvm_configs[0],
56+
};
57+
58+
_ = try stateProvingManager.execute_transition(state, block, options);
59+
},
3160
}
3261
}
+36-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
const types = @import("types")
2-
const state_transition = @import("state-transition")
1+
const std = @import("std");
2+
const ssz = @import("ssz");
3+
const types = @import("@zeam/types");
4+
const allocator = std.heap.page_allocator;
35

6+
pub const ZKVMContext = struct {
7+
program_path: []const u8,
8+
output_dir: []const u8,
9+
backend: ?[]const u8 = null,
10+
};
411

5-
const StateTransitionOpts = struct{
6-
zk_vm: ZKVM_TYPES,
7-
}
12+
extern fn powdr_prove(serialized: [*]const u8, len: usize, output: [*]u8, output_len: usize, binary_path: [*]const u8, binary_path_length: usize) void;
13+
14+
pub const zkvm_configs: []const ZKVMContext = &.{
15+
.{ .program_path = "zig-out/bin/zeam-stf-powdr", .output_dir = "out", .backend = "plonky3" },
16+
};
17+
18+
pub const StateTransitionOpts = struct {
19+
zk_vm: ZKVMContext,
20+
};
821

9-
pub fn execute_transition(state: types.BeamState, block: types.SignedBeamBlock, opts: StateTransitionOpts) types.BeamSTFProof {
22+
pub fn execute_transition(state: types.BeamState, block: types.SignedBeamBlock, opts: StateTransitionOpts) !types.BeamSTFProof {
23+
_ = opts;
24+
25+
var serialized = std.ArrayList(u8).init(allocator);
26+
defer serialized.deinit();
27+
const prover_input = types.BeamSTFProverInput{
28+
.state = state,
29+
.block = block,
30+
};
31+
try ssz.serialize(types.BeamSTFProverInput, prover_input, &serialized);
32+
33+
var output: [256]u8 = undefined;
34+
powdr_prove(serialized.items.ptr, serialized.items.len, @ptrCast(&output), 256, zkvm_configs[0].program_path.ptr, zkvm_configs[0].program_path.len);
1035
return types.BeamSTFProof{};
1136
}
1237

13-
pub fn verify_transition(stf_proof: types.BeamSTFProof, state_root: types.Bytes32, block_root: types.Bytes32, opts: StateTransitionOpts) !void{
14-
38+
pub fn verify_transition(stf_proof: types.BeamSTFProof, state_root: types.Bytes32, block_root: types.Bytes32, opts: StateTransitionOpts) !void {
39+
_ = stf_proof;
40+
_ = state_root;
41+
_ = block_root;
42+
_ = opts;
1543
}

0 commit comments

Comments
 (0)