#Verifying ZKProgram proof inside another ZKProgram error

14 messages · Page 1 of 1 (latest)

floral crypt
#

The second zk program in this code does not compile. It throws an error. I am verifying a zkp inside a ZKProgram method. The problem seems to arise only if I try to constrain the public inputs of the input zkp. (zk --version = 0.10.2, Node.js v18.15.0)

// run using 
// npm run build && node build/src/demo_error.js
import {
  Field,
  Poseidon,
  SelfProof,
  Experimental, 
  verify, 
  Struct, 
  MerkleMapWitness, 
  Mina, 
  isReady, 
  shutdown, 
} from 'snarkyjs';

class twoFields extends Struct({
  a: Field, 
  b: Field
}){
}

const clientTxnCorrect = Experimental.ZkProgram({
  publicInput: twoFields,
  methods: {
    isCorrect: {
      privateInputs:[Field],
      method(
        clientMsg: twoFields, 
        clientSecret: Field
      ) {
        // really just a void proof to make sure method is not empty
        clientSecret.assertEquals(clientMsg.a);
      }
    }
  }
});

class currStateFields extends Struct({
  x: Field, 
  y: Field
}){
}

const serverTxnCorrect = Experimental.ZkProgram({
  publicInput: currStateFields,
  methods: {
    isValid: {
      privateInputs:[SelfProof<twoFields>],
      method(
        currState: currStateFields, 
        clientProof: SelfProof<twoFields>
      ) {
        // this line does not throw an error
        clientProof.verify();
        // this line throws an error
        clientProof.publicInput.a.assertEquals(currState.x);
        // this line throws an error
        // clientProof.publicInput.b.assertEquals(currState.y);
      }
    }
  }
});

await isReady;

console.log('SnarkyJS loaded');

const _1 = await clientTxnCorrect.compile();
const _2 = await serverTxnCorrect.compile();
console.log("Success");

await shutdown();
broken pecan
#
privateInputs:[SelfProof<twoFields>]

this is not valid typescript

#

I think privateInputs:[SelfProof] should work

#

it's a value, not a type, so it can't have a type argument

floral crypt
#

Doesn't seem to solve it (also wouldn't this be caught during compilation?). I get the error: TypeError: Cannot read properties of undefined (reading 'assertEquals') during runtime

broken pecan
#

well typescript doesn't compile your example for me

#

but you're accessing clientProof.publicInput.a while your public input only has x and y properties, so that's the runtime error

floral crypt
#

clientProof is built over twoField, which has properties (a,b)?

#

I am doing this in VS code, so it should catch all the type mismatches

broken pecan
#

Ok cool, yeah I found my type mismatches where caused by using a newer snarkyjs where Proof has two generic type arguments and not one

#

So, the problem is that you use SelfProof which refers to a proof of that ZkProgram it's used in

#

You need ZkProgram.Proof(OtherProgram) instead in privateInputs

#

And for the type you either extend the above as a named class, or just use Proof<yourType>

floral crypt
#

Okay, this seems to work