Skip to content

Commit 6908703

Browse files
committed
fix risc0 input decoding and reactivate powdr
Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com>
1 parent 50d50f4 commit 6908703

File tree

7 files changed

+55
-20
lines changed

7 files changed

+55
-20
lines changed

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const zkvmTarget = struct {
1010

1111
const zkvm_targets: []const zkvmTarget = &.{
1212
.{ .name = "powdr", .set_pie = true, .build_glue = true },
13-
.{ .name = "ceno", .set_pie = false },
13+
// .{ .name = "ceno", .set_pie = false },
1414
.{ .name = "risc0", .build_glue = true },
1515
};
1616

pkgs/state-proving-manager/src/manager.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub fn prove_transition(state: types.BeamState, block: types.SignedBeamBlock, op
3939
const output_len = switch (opts) {
4040
.powdr => |powdrcfg| powdr_prove(serialized.items.ptr, serialized.items.len, @ptrCast(&output), 256, powdrcfg.program_path.ptr, powdrcfg.program_path.len, powdrcfg.output_dir.ptr, powdrcfg.output_dir.len),
4141
.risc0 => |risc0cfg| risc0_prove(serialized.items.ptr, serialized.items.len, risc0cfg.program_path.ptr, risc0cfg.program_path.len, output.ptr, output.len),
42+
// else => @panic("prover isn't enabled"),
4243
};
4344
std.debug.print("proof len={}\n", .{output_len});
4445
const proof = types.BeamSTFProof{

pkgs/state-transition-runtime/src/main.zig

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ const ssz = @import("ssz");
55
const types = @import("@zeam/types");
66
const state_transition = @import("@zeam/state-transition");
77

8+
// TODO(gballet) move to zkvm module as each zkvm has a
9+
// different allocated space.
810
var fixed_mem = [_]u8{0} ** (128 * 1024 * 1024);
911

10-
const __powdr_prover_data_start: [*]const u8 = @ptrFromInt(0x10000000);
11-
// this is hardcoded for now, because the compiler seems unable to see
12-
// linker script symbols.
13-
// extern const _powdr_prover_data_start: [*]const u8;
14-
// extern const __powdr_prover_data_end: [*]const u8;
15-
1612
// implements risv5 runtime that runs in zkvm on provided inputs and witnesses to execute
1713
// and prove the state transition as imported from `pkgs/state-transition`
1814
export fn main() noreturn {
@@ -24,16 +20,11 @@ export fn main() noreturn {
2420
const allocator = fixed_allocator.allocator();
2521

2622
// Get input from memory and deserialize it
27-
// TODO(gballet) move that to powdr-specific code when
28-
// another zkvm is being used that uses a different way
29-
// of passing data to guests.
30-
const total_input_len = std.mem.bytesToValue(u32, __powdr_prover_data_start[2048..2052]);
31-
const total_input: []const u8 = __powdr_prover_data_start[2052 .. 2052 + total_input_len];
32-
const input_len = std.mem.bytesAsValue(u32, total_input[0..4]);
33-
const input = total_input[4 .. 4 + input_len.*];
3423
// TODO(gballet) figure out why printing this string is necessary.
3524
// It might be worth commenting it once the powdr rebase has been
3625
// completed.
26+
const input = zkvm.get_input(allocator);
27+
defer zkvm.free_input(allocator);
3728
var input_dump: [2048]u8 = undefined;
3829
_ = std.fmt.bufPrint(input_dump[0..], "serialized input={any} len={}\n", .{ input[0..], input_len.* }) catch @panic("error allocating string to dump serialized input");
3930
// Uncomment when debugging

pkgs/state-transition-runtime/src/powdr/lib.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,18 @@ pub fn halt(_: u32) noreturn {
9292
);
9393
while (true) {}
9494
}
95+
96+
const __powdr_prover_data_start: [*]const u8 = @ptrFromInt(0x10000000);
97+
// this is hardcoded for now, because the compiler seems unable to see
98+
// linker script symbols.
99+
// extern const _powdr_prover_data_start: [*]const u8;
100+
// extern const __powdr_prover_data_end: [*]const u8;
101+
102+
pub fn get_input(_: std.mem.Allocator) []const u8 {
103+
const total_input_len = std.mem.bytesToValue(u32, __powdr_prover_data_start[2048..2052]);
104+
const total_input: []const u8 = __powdr_prover_data_start[2052 .. 2052 + total_input_len];
105+
const input_len = std.mem.bytesAsValue(u32, total_input[0..4]);
106+
return total_input[4 .. 4 + input_len.*];
107+
}
108+
109+
pub fn free_input(_: std.mem.Allocator, _: []const u8) void {}

pkgs/state-transition-runtime/src/risc0/io.zig

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,43 @@ fn sys_write(fd: u32, data: []const u8) void {
2424
);
2525
}
2626

27-
fn sys_read(fd: u32, comptime nrequested: usize, buffer: []u8) void {
27+
fn sys_read(fd: u32, nrequested: usize, buffer: []u8, nread: *u32, last: *u32) void {
2828
const main_words = nrequested / 4;
2929

3030
const syscall_name: [:0]const u8 = "risc0_zkvm_platform::syscall::nr::SYS_READ";
31+
var a0: u32 = undefined;
32+
var a1: u32 = undefined;
3133
asm volatile (
3234
\\ ecall
33-
:
35+
: [a0] "={a0}" (a0),
36+
[a1] "={a1}" (a1),
3437
: [syscallNumber] "{t0}" (syscalls.software),
35-
[from_host] "{a0}" (buffer),
38+
[from_host] "{a0}" (buffer.ptr),
3639
[from_host_words] "{a1}" (main_words),
3740
[syscall_name] "{a2}" (syscall_name.ptr),
3841
[file_descriptor] "{a3}" (fd),
3942
[main_requested] "{a4}" (nrequested),
4043
: "memory"
4144
);
45+
46+
nread.* = a0;
47+
last.* = a1;
4248
}
4349

44-
pub fn read_slice(_: u32, data: []u32) void {
45-
var as_u8: []u8 = @ptrCast(data);
46-
read_slice(.stdin, as_u8[0..]);
50+
pub fn read_slice(fd: u32, data: []u8) void {
51+
var count: u32 = 0;
52+
// TODO unaligned read if needed for starting point
53+
while (count < data.len) {
54+
const chunk_len = @min(data.len, 1024 * 4);
55+
var nread: u32 = undefined;
56+
var last: u32 = 0;
57+
sys_read(fd, chunk_len, data[count..], &nread, &last);
58+
if (nread == 0) print_str("read 0\n");
59+
if (last == 0) print_str("last 0\n");
60+
if (nread < chunk_len) break;
61+
count += nread;
62+
}
63+
// TODO unaligned read at the end if need be
4764
}
4865

4966
pub fn write_slice(fd: u32, data: []const u8) void {

pkgs/state-transition-runtime/src/risc0/lib.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ fn sys_halt(out_state: *const [8]u32, status: u32) noreturn {
1818
);
1919
unreachable;
2020
}
21+
22+
pub fn get_input(allocator: std.mem.Allocator) []const u8 {
23+
var input: []u8 = allocator.alloc(u8, 256) catch @panic("could not allocate space for the input slice");
24+
io.read_slice(0, input[0..]);
25+
return input;
26+
}
27+
28+
pub fn free_input(allocator: std.mem.Allocator, input: []const u8) void {
29+
allocator.free(input);
30+
}

pkgs/state-transition/src/transition.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub fn verify_signatures(signedBlock: types.SignedBeamBlock) !void {
8888
pub fn apply_transition(allocator: Allocator, state: *types.BeamState, signedBlock: types.SignedBeamBlock) !void {
8989
const block = signedBlock.message;
9090
if (block.slot <= state.slot) {
91+
log("slots are invalid for block {any}: {} >= {}\n", .{ block, block.slot, state.slot }) catch @panic("error printing block and state slots");
9192
return StateTransitionError.InvalidPreState;
9293
}
9394

0 commit comments

Comments
 (0)