Skip to content

Instantly share code, notes, and snippets.

@LucidSamuel
Last active June 29, 2023 17:40
Show Gist options
  • Save LucidSamuel/af3f118fc5a1e10cf3bf8543cd4b50c8 to your computer and use it in GitHub Desktop.
Save LucidSamuel/af3f118fc5a1e10cf3bf8543cd4b50c8 to your computer and use it in GitHub Desktop.
A simple ZkProgram that uses recursion -
import {
isReady,
shutdown,
Field,
Experimental,
SelfProof,
verify,
} from 'snarkyjs';
const Factorial = Experimental.ZkProgram({
publicInput: Field,
calculateFactorial(result: Field, n: Field): SelfProof<Field> {
const isZero: Bool = n.isZero();
if (isZero.toField().equals(Field(1))) {
result.assertEquals(Field(1));
return Experimental.SelfProof(result);
} else {
const prevResult = this.calculateFactorial(result, n.sub(Field(1)));
result.assertEquals(n.mul(prevResult.publicInput));
prevResult.verify();
return Experimental.SelfProof(result);
}
},
});
async function main() {
await isReady;
console.log('SnarkyJS loaded');
console.log('compiling...');
const { verificationKey } = await Factorial.compile();
console.log('making proof for factorial of 5');
const result: Field = Field(1);
const proof = Factorial.calculateFactorial(result, Field(5));
/*
* Please note that this code snippet assumes that the necessary dependencies are properly installed and that the required import statements and variable declarations are present.
*/
@LucidSamuel
Copy link
Author

LucidSamuel commented Jun 29, 2023

In the provided code snippet, the Factorial zkProgram calculates the factorial of a given number using recursion, enabling efficient and secure proof generation.

The calculateFactorial method within the Factorial zkProgram takes two inputs: result (representing the factorial result) and n (representing the current number in the factorial sequence). Here's how the recursion and factorial calculation work:

  1. If the current number n is zero (n.isZero()), it means we have reached the base case where the factorial of zero is defined as 1. In this case, we set result to 1 (result.assertEquals(Field(1))).

  2. If n is not zero, it means we are in the recursive step. The method recursively calls itself with the updated inputs:

  • result remains the same.
  • n.sub(Field(1)) reduces the current number by 1, moving closer to the base case.
    Inside the else block, the previous result (prevResult) of the recursive call is stored. We multiply the current number n with the previous result (n.mul(prevResult.publicInput)) to calculate the factorial of the current number.

Before accepting the new result, we verify the correctness of the previous result using prevResult.verify().

By utilizing recursion, the calculateFactorial method breaks down the factorial calculation into smaller subproblems, solving them recursively until it reaches the base case of zero. This approach allows for efficient computation and enables the generation of secure proofs for factorial calculations within the zkProgram.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment