o1-labs / o1js

TypeScript framework for zk-SNARKs and zkApps
https://docs.minaprotocol.com/en/zkapps/how-to-write-a-zkapp
Apache License 2.0
520 stars 118 forks source link

Signature verification in SmartContract sometimes throw "Commit failed Error: Option.value_exn None" in o1js 0.18.0 #1561

Closed dfstio closed 3 weeks ago

dfstio commented 7 months ago

Proving the following code with o1js 0.18.0

 @method async approveEscrow(
    signature: Signature,
    owner: PublicKey
  ) {
    Provable.log("owner", owner);
    Provable.log("signature", signature);
    signature.verify(owner, [Field(2)]).assertEquals(Bool(true));
  }

throws the error while proving. This occurs only when there is another unused method in the SmartContract that also verifies the signature. If comment the line with the verification of the signature in the other method, the code works as expected. The code of the other method is

@method async update(
    update: Update,
    signature: Signature,
    owner: PublicKey,
    proof: MinaNFTMetadataUpdateProof
  ) {
    signature.verify(owner, [Field(30)]).assertEquals(Bool(true));  // Commenting this line will remove the error
    this.metadata.set(update.newRoot);
    this.version.set(update.version);
    this.storage.set(update.storage);
  }

Unfortunately, I was unable to create a minimum reproducible example, so simply sharing the log

[3:49:46 PM] Approving escrow, iteration 1...
[3:49:46 PM] owner public key B62qqLYpzdW8fkLA4kNU9dprFBQeJgvpvP3SfPJ8ziHZY76UfL9Nkcd
[3:49:46 PM] owner private key EKEQXCQCEeojgQbFxkFpAT1vbX47hdpCbq1aRDVBmoASMruFq9SJ
[3:49:46 PM] signature ok: true
[3:49:46 PM] owner B62qqLYpzdW8fkLA4kNU9dprFBQeJgvpvP3SfPJ8ziHZY76UfL9Nkcd
[3:49:46 PM] signature {
  r: '21240500687585751706999857330516879949583978647956536702933126943455767922173',
  s: '8803133926502501954732386011602944229390735489634165193246783893857416091040'
}
[3:49:46 PM] owner B62qqLYpzdW8fkLA4kNU9dprFBQeJgvpvP3SfPJ8ziHZY76UfL9Nkcd
[3:49:46 PM] signature {
  r: '21240500687585751706999857330516879949583978647956536702933126943455767922173',
  s: '8803133926502501954732386011602944229390735489634165193246783893857416091040'
}
[3:49:46 PM] owner B62qqLYpzdW8fkLA4kNU9dprFBQeJgvpvP3SfPJ8ziHZY76UfL9Nkcd
[3:49:46 PM] signature {
  r: '21240500687585751706999857330516879949583978647956536702933126943455767922173',
  s: '8803133926502501954732386011602944229390735489634165193246783893857416091040'
}
[3:49:55 PM] owner B62qqLYpzdW8fkLA4kNU9dprFBQeJgvpvP3SfPJ8ziHZY76UfL9Nkcd
[3:49:55 PM] signature _Signature {
  r: _Field { value: [ 1, 125 ] },
  s: _Scalar {
    value: [
      0,       [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array], [Array], [Array], [Array], [Array], [Array],
      [Array], [Array],
      ... 156 more items
    ]
  }
}
Error when proving MinaNFTContract.approveEscrow()
[3:50:01 PM] Commit failed Error: Option.value_exn None
    at raise (ocaml/base/error.ml:8:21)
    at value_exn (ocaml/base/option.ml:123:68)
    at /workspace_root/src/mina/src/lib/pickles_types/opt.ml:80:36
    at /workspace_root/src/mina/src/lib/snarky/src/base/typ.ml:181:55
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:354:39)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at /workspace_root/src/mina/src/lib/snarky/src/base/typ.ml:181:55
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:354:39)
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:355:42)
    at /workspace_root/src/mina/src/lib/snarky/src/base/typ.ml:181:55
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:354:39)
    at /workspace_root/src/mina/src/lib/snarky/src/base/typ.ml:181:55
    at _pA__ (src/mina/src/lib/snarky/src/base/typ.ml:354:39)
    at /workspace_root/src/mina/src/lib/snarky/src/base/typ.ml:181:55
    at /workspace_root/src/mina/src/lib/snarky/src/base/typ.ml:181:55
    at /workspace_root/src/mina/src/lib/snarky/src/base/checked_runner.ml:279:31
    at /workspace_root/src/mina/src/lib/snarky/src/base/checked_runner.ml:54:30
    at exists (src/mina/src/lib/snarky/src/base/snark0.ml:1213:11)
    at with_label (src/mina/src/lib/snarky/src/base/snark0.ml:1253:15)
    at with_label (src/mina/src/lib/snarky/src/base/snark0.ml:1253:15)
    at /workspace_root/src/mina/src/lib/promise/js/promise.js:27:16
    at /workspace_root/src/mina/src/lib/pickles/wrap.ml:891:59
    at handle (src/mina/src/lib/snarky/src/base/snark0.ml:1237:15)
    at _iBv_ (src/mina/src/lib/pickles/wrap.ml:891:13)
    at mark_active (src/mina/src/lib/snarky/src/base/snark0.ml:1167:19)
    at _pnt_ (src/mina/src/lib/snarky/src/base/snark0.ml:1349:52)
    at as_stateful (src/mina/src/lib/snarky/src/base/snark0.ml:755:15)
    at _pB5_ (src/mina/src/lib/snarky/src/base/runners.ml:434:17)
    at run_computation (src/mina/src/lib/snarky/src/base/runners.ml:354:34)
    at /workspace_root/src/mina/src/lib/snarky/src/base/snark0.ml:1349:19
    at finalize_is_running (src/mina/src/lib/snarky/src/base/snark0.ml:1272:15)
    at generate_witness_conv (src/mina/src/lib/snarky/src/base/snark0.ml:1348:7)
    at /workspace_root/src/mina/src/lib/promise/js/promise.js:38:29
dfstio commented 7 months ago

I solved it by creating a tiny ZkProgram that verifies the signature and by providing the proof instead of the signature as an argument to the SmartContract method.

mitschabaude commented 7 months ago

that's a confusing error for sure

Pfed-prog commented 6 months ago

I can not even deploy contract anymore, could you please link the code @dfstio

dfstio commented 6 months ago

I can not even deploy contract anymore, could you please link the code @dfstio

The code I'm referring to is at the signature-error branch. Unfortunately, I was not able to create a minimum reproducible example; all examples were working without this error; I've seen it only on a real big contract: https://github.com/dfstio/minanft-lib/blob/signature-error/src/contract/nft.ts#L142

If you're unable to deploy the contract, check first that you are using o1js 0.18.0 and deploying to devnet.

dfstio commented 6 months ago

It is also reported in Discord: https://discord.com/channels/484437221055922177/1234393036264443967

mitschabaude commented 6 months ago

The error looks vaguely reminiscent of https://github.com/o1-labs/o1js/pull/1335 but in fact the stack trace differs if you look closely

dfstio commented 6 months ago

This error can be reproduced with this repo: https://github.com/dfstio/minanft-lib/tree/signature-error

The branch is signature-error, to reproduce the error you need to

  1. Add env.json

    {
    "DEPLOYER": "",
    "DEPLOYER_API": "",
    "PINATA_JWT": "",
    "DEPLOYERS": [],
    "DEPLOYERS_API": []
    }
  2. Run yarn to install dependencies

  3. Run yarn test tests/transfer, it will give the error Commit failed Error: Option.value_exn None and call stack

The line that causes the error is https://github.com/dfstio/minanft-lib/blob/signature-error/src/contract/nft.ts#L142

Pfed-prog commented 6 months ago

I can not even deploy contract anymore, could you please link the code @dfstio

The code I'm referring to is at the signature-error branch. Unfortunately, I was not able to create a minimum reproducible example; all examples were working without this error; I've seen it only on a real big contract: https://github.com/dfstio/minanft-lib/blob/signature-error/src/contract/nft.ts#L142

If you're unable to deploy the contract, check first that you are using o1js 0.18.0 and deploying to devnet.

Absolutely, but does it work with localBlockchain?

It did not work for me even for localBlockchain, i am still on downgraded o1js, unfortunately

dfstio commented 6 months ago

Absolutely, but does it work with localBlockchain?

It did not work for me even for localBlockchain, i am still on downgraded o1js, unfortunately

My code works now with local blockchain, lightnet and devnet. Berkeley testnet is not supported anymore by the last version of o1js. This specific example with a signature throws an error on the local blockchain.

Make sure you use await everywhere with the last o1js; the deployment can fail if you forget to do it in some line.