DelphinusLab / zkWasm

Apache License 2.0
479 stars 95 forks source link

Failure to run basic rlp decoding program #91

Closed naps62 closed 1 year ago

naps62 commented 1 year ago

Tried to run the following code:

// src/lib.rs
#[wasm_bindgen]
pub fn add() -> i64 {
    let data = vec![0x83, b'c', b'a', b't'];
    let animal: String = rlp::decode(&data).expect("could not decode");
    assert_eq!(animal, "cat".to_owned());

    0
}
# justfile
wasm := "pkg/rlp_bg.wasm"
cli := "../../DelphinusLab/zkWasm/target/release/cli"
args := "--function add --output ./output --wasm " + wasm
proof := "output/zkwasm.0.transcript.data"
public_input := "5:i64"

build:
  wasm-pack build --release

# skipped if zkwasm.0.vkey.data is up-to-date
setup: build
  {{cli}} {{args}} setup

# skipped if zkwasm.0.vkey.data is up-to-date
run: setup
  {{cli}} {{args}} single-prove --public {{public_input}}

verify: run
  {{cli}} {{args}} single-verify --public {{public_input}} --proof {{proof}}

calling just run yieled the following: image

junyu0312 commented 1 year ago

We appreciate your feedback. Please

  1. update zkwasm, recompile it, and clear up your output directory,
  2. since your rust code doesn't call wasm_input method, remove --public 5:i64 argument,
  3. increase the circuit size to K=19 via a new command argument -k 19

For example,

./target/release/cli -k 19 --function add --output ./output --wasm rlp_bg.wasm setup
./target/release/cli -k 19 --function add --output ./output --wasm rlp_bg.wasm single-prove
./target/release/cli -k 19 --function add --output ./output --wasm rlp_bg.wasm single-verify --proof output/zkwasm.0.transcript.data
naps62 commented 1 year ago

@junyu0312 seems to work now. thank you!

One follow-up question though: I couldn't find any documentation or example on how to access the output of the execution. Is there such an example?

naps62 commented 1 year ago

@junyu0312 I'm not sure if I incorrectly tested 2 weeks ago, or if there's some regression, but this is now failing again.

I tried with a few different commits of zkwasm. also commented the assert_eq! part of the program. it still crashes. the problem seems to be on the rlp::decode call

I extracted the code into a separate repo for debugging: github.com/naps62/zkwasm-hello-world

The error I get:

zkwasm-cli-x86 -k 19 --function rlp --output ./output --wasm pkg/zkwasm_rlp_bg.wasm single-prove
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Err([ConstraintNotSatisfied { constraint: Constraint { gate: Gate { index: 105, name: "compare op res" }, index: 3,
 name: "" }, location: InRegion { region: Region { index: 13, name: "jtable mtable etable" }, offset: 14680 }, cell_values:
[(VirtualCell { name: "", column: Column { column_type: Advice, index: 7 }, rotation: 1 }, "0"), (VirtualCell { name: "", co
lumn: Column { column_type: Advice, index: 7 }, rotation: 2 }, "1"), (VirtualCell { name: "", column: Column { column_type:
Advice, index: 7 }, rotation: 3 }, "0"), (VirtualCell { name: "", column: Column { column_type: Advice, index: 7 }, rotation
: 4 }, "1"), (VirtualCell { name: "", column: Column { column_type: Advice, index: 7 }, rotation: 5 }, "1"), (VirtualCell {
name: "", column: Column { column_type: Advice, index: 7 }, rotation: 10 }, "1"), (VirtualCell { name: "", column: Column {
column_type: Advice, index: 9 }, rotation: 1 }, "1"), (VirtualCell { name: "", column: Column { column_type: Advice, index:
9 }, rotation: 14 }, "1"), (VirtualCell { name: "", column: Column { column_type: Advice, index: 12 }, rotation: 1 }, "1"),
(VirtualCell { name: "", column: Column { column_type: Fixed, index: 22 }, rotation: 0 }, "1")] }])`,
 right: `Ok(())`', src/cli/exec.rs:173:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
lanbones commented 1 year ago

Thank you very much to report this issue. It is related to a bug on negative numbers' comparison. The PR #100 is waiting for @junyu0312 review. You can try zkWasm on commit d375af87fced482c0f99c08ca14b7434a2ce0a09 before merge.