Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursion, Pt 2 #250

Merged
merged 25 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f968632
add Program type; handles proofs & private inputs
mitschabaude Jun 17, 2022
97e7ed1
common method preprocessing, tighter types
mitschabaude Jun 20, 2022
94d68ca
use common logic for some method wrapping
mitschabaude Jun 20, 2022
ab18b17
move smart contract-specific stuff to wrapMethod
mitschabaude Jun 20, 2022
c8439b6
move pickles rule wrapper to proof_systems
mitschabaude Jun 20, 2022
86865ee
common logic for compile
mitschabaude Jun 20, 2022
8a39ed0
implement Program.compile
mitschabaude Jun 20, 2022
c785c06
move test code to example
mitschabaude Jun 20, 2022
f136aef
implement proving for Program
mitschabaude Jun 20, 2022
7bfc195
fix compile
mitschabaude Jun 20, 2022
d4b82c1
create example with recursive proof chain
mitschabaude Jun 20, 2022
5cb2315
remove Proof.dummy
mitschabaude Jun 20, 2022
b784561
move hlper stuff to the bottom
mitschabaude Jun 20, 2022
5f7225d
better naming
mitschabaude Jun 20, 2022
f0d5932
update bindings
mitschabaude Jun 20, 2022
6009d9f
add `SelfProof` shortcut and use it in examples
mitschabaude Jun 20, 2022
b624b6d
Program -> ZkProgram
mitschabaude Jun 20, 2022
2b91617
Merge branch 'feature/recursion-2' into feature/recursion-bindings
mitschabaude Jun 21, 2022
36ca782
`privateInput` -> `privateInputs`
mitschabaude Jun 21, 2022
b1fc62d
make isAsFields more general
mitschabaude Jun 21, 2022
05ffd15
fix: method args have to be cloned because method can modify them
mitschabaude Jun 22, 2022
1d39a45
Merge branch 'feature/recursion-2' into feature/recursion-bindings
mitschabaude Jun 22, 2022
b258b01
update bindings
mitschabaude Jun 22, 2022
c1e3d9e
Merge branch 'main' into feature/recursion-2
mitschabaude Jun 27, 2022
efdaee4
Merge branch 'feature/recursion-bindings' into feature/recursion-2
mitschabaude Jun 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/examples/program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Proof, Field, Program } from 'snarkyjs';

class MyProof extends Proof<Field> {
static publicInputType = Field;
static tag: () => { name: string } = () => MyProgram;
}

let MyProgram = Program({
publicInput: Field,

methods: {
baseCase: {
privateInput: [],

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

plus1Case: {
privateInput: [MyProof],

method(publicInput: Field, earlierProof: MyProof) {
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.plus1Case(Field.one, proof);

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

console.log('ok?', proof.publicInput.toString() === '2');
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, Program } 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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type was changed because there is no Party in non-smart-contract proofs

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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment was just moved over from zkapp.ts where it made no sense anymore


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