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
531 stars 121 forks source link

[feature request] exposing/(de)serializing proof and verification key #54

Closed Trivo25 closed 2 years ago

Trivo25 commented 2 years ago

Thins idea was also mentioned in Discord.

Idea: Potentially allowing to "export" and "import" proofs and their coressponding verification key to allow verification and proofing of code outside of the Mina blockchain, inside of web2.0.

Reason: It would open up SnarkyJS to more developers, allowing many web 2.0 devs and other developers to utilize the power of zero knowledge proofs to some extend in normal web applications.

Possible usage: What it could look like

// example from ex00_preimage.ts

class Main extends Circuit {
  @circuitMain
  static main(preimage: Field, @public_ hash: Field) {
    Poseidon.hash([preimage]).assertEquals(hash);
  }
}

// ... 

let proof = Main.prove(/* .. */); // returns the proof
let encodedProof = proof.toBase58()) // potentially encoded in base58 for easy use eg 'RJcTKtswS9xgY4FDfxCq4ZaLRfQwGME8GYPFGpxieWZXJMGUuqN1zdKYEM6dLZER'  

let verificationKey = Main.getVerificationKey(); // returns the verification key; potentially also encoded for easy use

// usage in a different application

class MyVerifier extends Verifier {
  constructor(key: VerificationKey) {
     // ...
  }
}

let verificationKey = 'someVerificationKeyEncoded';
let myVerifier = new Verifier(VerificationKey.fromString(verificationKey))

let proof = 'someProofEncoded';
let isValid = myVerifier.verify(Proof.fromString(proof)); // returns true if proof matches verification key

Is something like that feasible and makes sense? Or is something like that potentially even planned already?

Thanks!

Scratch-net commented 1 year ago

@Trivo25 is it implemented? It would be nice to have both compiled circuits and keys to be exportable for off-chain applications

Trivo25 commented 1 year ago

@Trivo25 is it implemented? It would be nice to have both compiled circuits and keys to be exportable for off-chain applications

Yes, proofs can be serialized as well as verification keys (of smart contract as well as the ZkProgram API). The prover key can't be currently serialized but there's a separate issue for that https://github.com/o1-labs/snarkyjs/issues/87

Scratch-net commented 1 year ago

Ok, I was just playing with a plain circuit and could not find it