Skip to content

Commit

Permalink
Merge pull request #250 from o1-labs/feature/recursion-2
Browse files Browse the repository at this point in the history
Recursion, Pt 2
  • Loading branch information
mitschabaude committed Jun 27, 2022
2 parents 0cae7ba + efdaee4 commit bad9ec5
Show file tree
Hide file tree
Showing 10 changed files with 559 additions and 290 deletions.
2 changes: 1 addition & 1 deletion MINA_COMMIT
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
The mina commit used to generate the backends for node and chrome is
67a5fc9d0e02e722b09add09740659ba76e7d714
7897c4f2b952bb16c8724440b75bc8fe76ad04c4
94 changes: 47 additions & 47 deletions src/chrome_bindings/snarky_js_chrome.bc.js

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions src/examples/program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { SelfProof, Field, ZkProgram } from 'snarkyjs';

let MyProgram = ZkProgram({
publicInput: Field,

methods: {
baseCase: {
privateInputs: [],

method(publicInput: Field) {
publicInput.assertEquals(Field.zero);
},
},

inductiveCase: {
privateInputs: [SelfProof],

method(publicInput: Field, earlierProof: SelfProof<Field>) {
earlierProof.verify();
earlierProof.publicInput.add(1).assertEquals(publicInput);
},
},
},
});

console.log('compiling MyProgram...');
await MyProgram.compile();

console.log('proving base case...');
let proof = await MyProgram.baseCase(Field.zero);

console.log('proving step 1...');
proof = await MyProgram.inductiveCase(Field.one, proof);

console.log('proving step 2...');
proof = await MyProgram.inductiveCase(Field(2), proof);

console.log('ok?', proof.publicInput.toString() === '2');
7 changes: 2 additions & 5 deletions src/examples/simple_zkapp_with_proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import {
ZkappPublicInput,
Proof,
Ledger,
SelfProof,
} from 'snarkyjs';

await isReady;

class SimpleZkappProof extends Proof<ZkappPublicInput> {
static publicInputType = ZkappPublicInput;
static tag = () => NotSoSimpleZkapp;
}
class TrivialProof extends Proof<ZkappPublicInput> {
static publicInputType = ZkappPublicInput;
static tag = () => TrivialZkapp;
Expand All @@ -34,7 +31,7 @@ class NotSoSimpleZkapp extends SmartContract {

@method update(
y: Field,
oldProof: SimpleZkappProof,
oldProof: SelfProof<ZkappPublicInput>,
trivialProof: TrivialProof
) {
oldProof.verify();
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export * from './lib/int';
export * as Mina from './lib/mina';
export * from './lib/zkapp';
export { state, State, declareState } from './lib/state';
export * from './lib/proof_system';
export { Proof, SelfProof, ZkProgram } from './lib/proof_system';
export * from './lib/party';
export {
fetchAccount,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/global-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export {
let mainContext = undefined as
| {
witnesses?: unknown[];
self: Party;
self?: Party;
expectedAccesses: number | undefined;
actualAccesses: number;
inProver?: boolean;
Expand All @@ -24,7 +24,7 @@ let mainContext = undefined as
| undefined;
type PartialContext = {
witnesses?: unknown[];
self: Party;
self?: Party;
expectedAccesses?: number;
actualAccesses?: number;
inProver?: boolean;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,14 @@ type PartiesProved = {
memo: string;
};

/**
* The public input for zkApps consists of certain hashes of the transaction and of the proving Party which is constructed during method execution.
In SmartContract.prove, a method is run twice: First outside the proof, to obtain the public input, and once in the prover,
which takes the public input as input. The current transaction is hashed again inside the prover, which asserts that the result equals the input public input,
as part of the snark circuit. The block producer will also hash the transaction they receive and pass it as a public input to the verifier.
Thus, the transaction is fully constrained by the proof - the proof couldn't be used to attest to a different transaction.
*/
type ZkappPublicInput = [transaction: Field, atParty: Field];
let ZkappPublicInput = circuitValue<ZkappPublicInput>([Field, Field]);

Expand Down
Loading

0 comments on commit bad9ec5

Please sign in to comment.