Skip to content

Commit

Permalink
v2 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Trivo25 committed Oct 22, 2024
1 parent 760ebe6 commit e7fb424
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 51 deletions.
28 changes: 14 additions & 14 deletions docs/zkapps/o1js/recursion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ zkProgram is available as a top-level import. `Experimental.ZkProgram` is deprec

In o1js, you can use `ZkProgram()` to define the steps of a recursive program. Like `SmartContract()` methods, `ZkProgram()` methods execute off-chain.

After performing the desired recursive steps, you can settle the interaction on Mina's blockchain by embedding `ZkProgram` within a `SmartContract` method that verifies the underlying proof of execution and extracts the output that can be used elsewhere in the method (like storing the output in app-state, for example).
After performing the desired recursive steps, you can settle the interaction on Mina's blockchain by embedding `ZkProgram` within a `SmartContract` method that verifies the underlying proof of execution and extracts the output that can be used elsewhere in the method (like storing the output in app-state, for example).

Similar to methods within the `SmartContract` class, inputs to `ZkProgram` are _private by default_ and are never seen by the Mina network. Unlike `SmartContract` methods, as the zkApp developer you choose the shape of the public input to all methods within a `ZkProgram`.
Similar to methods within the `SmartContract` class, inputs to `ZkProgram` are _private by default_ and are never seen by the Mina network. Unlike `SmartContract` methods, as the zkApp developer you choose the shape of the public input to all methods within a `ZkProgram`.

## Example: Recursively verify a simple program in a zkApp

Expand All @@ -47,18 +47,18 @@ This simple example has only one method that proves the public input it received
import { Field, ZkProgram } from 'o1js';

const SimpleProgram = ZkProgram({
name: "simple-program-example",
name: 'simple-program-example',
publicInput: Field,

methods: {
run: {
run: {
privateInputs: [],

async method(publicInput: Field) {
publicInput.assertEquals(Field(0));
},
}
}
},
},
});
```

Expand All @@ -71,7 +71,7 @@ const { verificationKey } = await SimpleProgram.compile();
Now, you can use it to create a proof:

```typescript
const proof = await SimpleProgram.run(Field(0));
const { proof } = await SimpleProgram.run(Field(0));
```

To verify this proof from within any method of your `SmartContract` class:
Expand All @@ -98,7 +98,7 @@ This program describes a recursive operation of adding one repeatedly to a numbe
import { SelfProof, Field, ZkProgram, verify } from 'o1js';

const AddOne = ZkProgram({
name: "add-one-example",
name: 'add-one-example',
publicInput: Field,

methods: {
Expand Down Expand Up @@ -129,19 +129,19 @@ First, compile this program and make the base proof as before:
```typescript
const { verificationKey } = await AddOne.compile();

const proof = await AddOne.baseCase(Field(0));
const { proof } = await AddOne.baseCase(Field(0));
```

This time, use this proof as input to recursively add one again:

```typescript
const proof1 = await AddOne.step(Field(1), proof);
const { proof: proof1 } = await AddOne.step(Field(1), proof);
```

Repeat this as many times as you want:

```typescript
const proof2 = await AddOne.step(Field(2), proof1);
const { proof: proof2 } = await AddOne.step(Field(2), proof1);
```

Finally, verify the proof from within a SmartContract like the earlier example:
Expand All @@ -164,14 +164,14 @@ This example program describes a very simple rollup for adding numbers:
import { SelfProof, Field, ZkProgram, verify } from 'o1js';

let RollupAdd = ZkProgram({
name: "rollup-add-example",
name: 'rollup-add-example',
publicInput: Field,

methods: {
baseCase: {
privateInputs: [],

async method(publicInput: Field) { },
async method(publicInput: Field) {},
},

step: {
Expand Down Expand Up @@ -199,7 +199,7 @@ You can also use ZkProgram directly to prove and verify arbitrary zero knowledge
```typescript
const { verificationKey } = await MyProgram.compile();

const proof = await MyProgram.base(Field(0));
const { proof } = await MyProgram.base(Field(0));
```

Now you can directly verify a JSON-encoded version of the proof to get back a boolean value that tells you if the proof is valid:
Expand Down
6 changes: 3 additions & 3 deletions docs/zkapps/tutorials/09-recursion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ async function main() {
console.log('making proof 0');
const proof0 = await Add.init(Field(0));
const { proof: proof0 } = await Add.init(Field(0));
console.log('making proof 1');
const proof1 = await Add.addNumber(Field(4), proof0, Field(4));
const { proof: proof1 } = await Add.addNumber(Field(4), proof0, Field(4));
console.log('making proof 2');
const proof2 = await Add.add(Field(4), proof1, proof0);
const { proof: proof2 } = await Add.add(Field(4), proof1, proof0);
console.log('verifying proof 2');
console.log('proof 2 data', proof2.publicInput.toString());
Expand Down
9 changes: 5 additions & 4 deletions examples/zkapps/01-hello-world/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/zkapps/01-hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"typescript": "^5.6"
},
"peerDependencies": {
"o1js": "1.*"
"o1js": "https://pkg.pr.new/o1-labs/o1js@b04520d"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"typescript": "^5.6.2"
},
"peerDependencies": {
"o1js": "1.*"
"o1js": "https://pkg.pr.new/o1-labs/o1js@b04520d"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/zkapps/04-zkapp-browser-ui/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"typescript": "^5.6"
},
"peerDependencies": {
"o1js": "^1.*"
"o1js": "https://pkg.pr.new/o1-labs/o1js@b04520d"
},
"engines": {
"node": ">=18.14.0"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/zkapps/05-common-types-and-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"typescript": "^5.6.3"
},
"peerDependencies": {
"o1js": "1.8.0"
"o1js": "https://pkg.pr.new/o1-labs/o1js@b04520d"
}
}
2 changes: 1 addition & 1 deletion examples/zkapps/07-oracles/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"typescript": "^5.6.2"
},
"peerDependencies": {
"o1js": "1.0.*"
"o1js": "https://pkg.pr.new/o1-labs/o1js@b04520d"
}
}
9 changes: 5 additions & 4 deletions examples/zkapps/09-recursion/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/zkapps/09-recursion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"typescript": "^5.6.2"
},
"peerDependencies": {
"o1js": "1.*"
"o1js": "https://pkg.pr.new/o1-labs/o1js@b04520d"
}
}
6 changes: 3 additions & 3 deletions examples/zkapps/09-recursion/src/Add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ async function main() {

console.log('making proof 0');

const proof0 = await Add.init(Field(0));
const { proof: proof0 } = await Add.init(Field(0));

console.log('making proof 1');

const proof1 = await Add.addNumber(Field(4), proof0, Field(4));
const { proof: proof1 } = await Add.addNumber(Field(4), proof0, Field(4));

console.log('making proof 2');

const proof2 = await Add.add(Field(4), proof1, proof0);
const { proof: proof2 } = await Add.add(Field(4), proof1, proof0);

console.log('verifying proof 2');
console.log('proof 2 data', proof2.publicInput.toString());
Expand Down
8 changes: 6 additions & 2 deletions examples/zkapps/09-recursion/src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function main() {
increment,
witness
);
const proof = await Rollup.oneStep(
const { proof } = await Rollup.oneStep(
rollup,
initialRoot,
latestRoot,
Expand All @@ -110,7 +110,11 @@ async function main() {
proof.publicInput,
rollupProofs[i].publicInput
);
let mergedProof = await Rollup.merge(rollup, proof, rollupProofs[i]);
let { proof: mergedProof } = await Rollup.merge(
rollup,
proof,
rollupProofs[i]
);
proof = mergedProof;
}

Expand Down
6 changes: 3 additions & 3 deletions examples/zkapps/09-recursion/src/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function main() {
);

const vote0 = VoteState.newVote(votersTree.getRoot());
const proof0 = await Vote.create(vote0);
const { proof: proof0 } = await Vote.create(vote0);

console.log('making proof 1');

Expand All @@ -51,7 +51,7 @@ async function main() {
voterTreeWitness1,
nullifierWitness1
);
const proof1 = await Vote.applyVote(
const { proof: proof1 } = await Vote.applyVote(
vote1,
proof0,
Bool(true),
Expand All @@ -77,7 +77,7 @@ async function main() {
voterTreeWitness2,
nullifierWitness2
);
const proof2 = await Vote.applyVote(
const { proof: proof2 } = await Vote.applyVote(
vote2,
proof1,
Bool(false),
Expand Down
Loading

0 comments on commit e7fb424

Please sign in to comment.