Skip to content

Commit 3e14c4f

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

File tree

9 files changed

+4304
-28
lines changed

9 files changed

+4304
-28
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

build.zig

+20-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

@@ -59,6 +59,8 @@ pub fn build(b: *Builder) !void {
5959
.target = target,
6060
.optimize = optimize,
6161
});
62+
zeam_state_proving_manager.addImport("@zeam/types", zeam_types);
63+
zeam_state_proving_manager.addImport("ssz", ssz);
6264

6365
const st_lib = b.addStaticLibrary(.{
6466
.name = "zeam-state-transition",
@@ -93,6 +95,23 @@ pub fn build(b: *Builder) !void {
9395
cli_exe.root_module.addImport("@zeam/state-proving-manager", zeam_state_proving_manager);
9496
cli_exe.root_module.addImport("@zeam/node", zeam_beam_node);
9597
cli_exe.root_module.addImport("@zeam/params", zeam_params);
98+
for (zkvm_targets) |zkvm_target| {
99+
if (zkvm_target.build_glue) {
100+
const zkvm_host_cmd = b.addSystemCommand(&.{
101+
"cargo",
102+
"+nightly",
103+
"-C",
104+
b.fmt("pkgs/state-transition-runtime/src/{s}/host", .{zkvm_target.name}),
105+
"-Z",
106+
"unstable-options",
107+
"build",
108+
"--release",
109+
});
110+
cli_exe.step.dependOn(&zkvm_host_cmd.step);
111+
cli_exe.addLibraryPath(b.path(b.fmt("pkgs/state-transition-runtime/src/{s}/host/target/release/", .{zkvm_target.name})));
112+
cli_exe.linkSystemLibrary("zeam_prover_host_powdr");
113+
}
114+
}
96115
b.installArtifact(cli_exe);
97116

98117
const tests = b.addTest(.{
@@ -169,15 +188,5 @@ fn build_zkvm_targets(b: *Builder) !void {
169188
}
170189
exe.setLinkerScript(b.path(b.fmt("pkgs/state-transition-runtime/src/{s}/{s}.ld", .{ zkvm_target.name, zkvm_target.name })));
171190
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-
}
182191
}
183192
}

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 "C" fn powdr_prove(serialized: [*]const u8, len: usize, output: [*]u8, output_len: usize) void;
13+
14+
pub const zkvm_configs: []const ZKVMContext = &.{
15+
.{ .program_path = "dist/zeam-stf-powdr.asm", .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);
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)