Skip to content

Commit 8120d96

Browse files
committed
Single recursive function (WIP)
1 parent 2558dc9 commit 8120d96

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
use dep::std;
22

3+
// recursive summation
4+
#[recursive]
35
fn main(
46
verification_key: [Field; 114],
7+
proof: [Field; 93],
58
public_inputs: [Field; 1],
69
key_hash: Field,
7-
proof1: [Field; 93],
8-
proof2: [Field; 93]
9-
) {
10-
std::verify_proof(
11-
verification_key.as_slice(),
12-
proof1.as_slice(),
13-
public_inputs.as_slice(),
14-
key_hash
15-
);
16-
std::verify_proof(
17-
verification_key.as_slice(),
18-
proof2.as_slice(),
19-
public_inputs.as_slice(),
20-
key_hash
21-
);
10+
sum: Field,
11+
n: Field
12+
) -> pub u64 {
13+
if (key_hash != 0) {
14+
std::verify_proof(
15+
verification_key.as_slice(),
16+
proof.as_slice(),
17+
public_inputs.as_slice(),
18+
key_hash
19+
);
20+
}
21+
sum as u64 + n as u64
2222
}

how-to/recursive-proofs/main.ts

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,55 @@ type FullNoir = {
3535
}
3636

3737
async function start() {
38-
const simple: FullNoir = await fullNoirFromCircuit('not_odd');
39-
const outer: FullNoir = await fullNoirFromCircuit('recurse');
38+
const recursive: FullNoir = await fullNoirFromCircuit('recurse');
4039

41-
// Generate inner proof artifacts
42-
console.log("Generating intermediate proof artifacts 1...");
43-
const innerWitness1 = (await simple.noir.execute({ n: 2 })).witness;
44-
const innerProof1: ProofData = await simple.backend.generateProof(innerWitness1);
45-
const artifacts1 = await simple.backend.generateRecursiveProofArtifacts(innerProof1, 1);
46-
47-
console.log("Generating intermediate proof artifacts 2...");
48-
const innerWitness2 = (await simple.noir.execute({ n: 4 })).witness;
49-
const innerProof2: ProofData = await simple.backend.generateProof(innerWitness2);
50-
const artifacts2 = await simple.backend.generateRecursiveProofArtifacts(innerProof2, 1);
51-
52-
simple.backend.destroy();
53-
54-
// Generate and verify outer proof
55-
const outerInput = {
56-
verification_key: artifacts1.vkAsFields,
57-
public_inputs: ["1"], // expect output of inner call to be "true"
58-
key_hash: artifacts1.vkHash,
59-
proof1: artifacts1.proofAsFields,
60-
proof2: artifacts2.proofAsFields
40+
// Generate artifacts recursively
41+
let verificationProof = {
42+
verification_key: Array<string>(114).fill("0x00"),
43+
proof: Array<string>(93).fill("0x00"),
44+
public_inputs: ["0x06"],
45+
key_hash: "0x00" as string, // circuit should skip `verify_proof` when key_hash is 0.
6146
};
62-
const outerWitness = (await outer.noir.execute(
63-
outerInput
64-
)).witness;
65-
try {
66-
console.log("Generating outer proof...");
67-
const outerProof: ProofData = await outer.backend.generateProof(outerWitness);
68-
console.log("Verifying outer proof...");
69-
const res: boolean = await outer.backend.verifyProof(outerProof);
70-
console.log("Verification", res ? "PASSED" : "failed");
71-
}
72-
catch (e) {
73-
console.log(e);
47+
let sum = 1;
48+
const nodes = [3, 7];
49+
for (const [i, n] of nodes.entries()) {
50+
console.log("\n\nExecuting (with key_hash %s, sum %d, n %d)", verificationProof.key_hash, sum, n);
51+
verificationProof.public_inputs = ["4"];
52+
const { witness, returnValue } = await recursive.noir.execute({
53+
...verificationProof,
54+
sum,
55+
n
56+
});
57+
console.log("RESULT:", returnValue, witness);
58+
sum = Number(returnValue) as number;
59+
console.log("Generating proof data...");
60+
const pd: ProofData = await recursive.backend.generateProof(witness);
61+
console.log(pd);
62+
if (i < nodes.length - 1) {
63+
console.log("Generating recursive proof artifacts (depth %d)...", i);
64+
({
65+
proofAsFields: verificationProof.proof,
66+
vkAsFields: verificationProof.verification_key,
67+
vkHash: verificationProof.key_hash
68+
} = await recursive.backend.generateRecursiveProofArtifacts(
69+
pd,
70+
Number(verificationProof.public_inputs[0])
71+
));
72+
}
73+
else {
74+
console.log("Verifying final proof...");
75+
try {
76+
const res: boolean = await recursive.backend.verifyProof(pd);
77+
console.log("Verification", res ? "PASSED" : "failed");
78+
}
79+
catch (e) {
80+
console.log(e);
81+
}
82+
83+
}
7484
}
7585

76-
outer.backend.destroy();
86+
recursive.backend.destroy();
7787
}
7888

7989
start();

0 commit comments

Comments
 (0)