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
500 stars 111 forks source link

Considering `ZkProgram` Interface #1701

Open harrysolovay opened 3 months ago

harrysolovay commented 3 months ago

Looking at the recursion docs, I think some subtle changes would result in greater legibility. Let's consider the following:

const Rollup = ZkProgram({
  name: "rollup-example",
  publicInput: Field,

  methods: {
    oneStep: {
    privateInputs: [ Field, Field, Field, Field, Field, MerkleMapWitness ],

    method(
      state: RollupState,
      initialRoot: Field,
      latestRoot: Field,
      key: Field,
      currentValue: Field,
      incrementAmount: Field,
      merkleMapWitness: MerkleMapWitness
    ) {
      // ...
  1. It's difficult to identify which element of privateInputs corresponds to which of the rest-spread parameters of method.
  2. We should be able to infer the types of the rest-spread tuple from privateInputs.
  3. We're unable to attach tsdocs to parameters. This would potentially be valuable for consumers of the program.

Ideally we could do the following.

const Rollup = ZkProgram({
  name: "rollup-example",
  publicInput: Field,

  methods: {
    oneStep: Method({
      /** some docs that are now attached to the `initialRoot` key */
      initialRoot: Field,
      latestRoot: Field,
      key: Field,
      currentValue: Field,
      incrementAmount: Field,
      merkleMapWitness: MerkleMapWitness,
    }, (state, { initialRoot, latestRoot, key, currentValue, incrementAmount, merkleMapWitness }) => {
      // ...

This also spares the developer of needing to––in their minds––shift the private inputs over by 1 (accommodating the public input).

mitschabaude commented 3 months ago

I agree with this proposal! Much more self-documenting. The required base type for method inputs could be Record<string, Provable<any>>

mitschabaude commented 2 months ago

would be awesome to combine this with https://github.com/o1-labs/o1js/issues/510 somehow