o1js-pack 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.6
2
+
3
+ - Upgrading to o1js version 0.18.0
4
+ - Updated tests and examples to use async circuits
5
+
1
6
  ## v0.5
2
7
 
3
8
  - Making PackedString class 31 characters instead of 15
@@ -9,7 +9,7 @@ export class Election extends SmartContract {
9
9
  @state(Ballot) ballot3 = State<Ballot>();
10
10
  @state(Ballot) ballot4 = State<Ballot>();
11
11
 
12
- init() {
12
+ async init() {
13
13
  super.init();
14
14
  this.ballot1.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n]));
15
15
  this.ballot2.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n]));
@@ -18,9 +18,9 @@ export class Election extends SmartContract {
18
18
  }
19
19
 
20
20
  @method
21
- castBallot1(vote: Ballot) {
21
+ async castBallot1(vote: Ballot) {
22
22
  const unpackedVote = Ballot.unpack(vote.packed);
23
- const ballot1 = this.ballot1.getAndAssertEquals();
23
+ const ballot1 = this.ballot1.getAndRequireEquals();
24
24
  const unpackedBallot1 = Ballot.unpack(ballot1.packed);
25
25
 
26
26
  let voteSum = UInt32.from(0);
@@ -33,9 +33,9 @@ export class Election extends SmartContract {
33
33
  }
34
34
 
35
35
  @method
36
- castBallot2(vote: Ballot) {
36
+ async castBallot2(vote: Ballot) {
37
37
  const unpackedVote = Ballot.unpack(vote.packed);
38
- const ballot2 = this.ballot2.getAndAssertEquals();
38
+ const ballot2 = this.ballot2.getAndRequireEquals();
39
39
  const unpackedBallot2 = Ballot.unpack(ballot2.packed);
40
40
 
41
41
  let voteSum = UInt32.from(0);
@@ -50,7 +50,7 @@ export class Election extends SmartContract {
50
50
  @method
51
51
  castBallot3(vote: Ballot) {
52
52
  const unpackedVote = Ballot.unpack(vote.packed);
53
- const ballot3 = this.ballot3.getAndAssertEquals();
53
+ const ballot3 = this.ballot3.getAndRequireEquals();
54
54
  const unpackedBallot3 = Ballot.unpack(ballot3.packed);
55
55
 
56
56
  let voteSum = UInt32.from(0);
@@ -63,9 +63,9 @@ export class Election extends SmartContract {
63
63
  }
64
64
 
65
65
  @method
66
- castBallot4(vote: Ballot) {
66
+ async castBallot4(vote: Ballot) {
67
67
  const unpackedVote = Ballot.unpack(vote.packed);
68
- const ballot4 = this.ballot4.getAndAssertEquals();
68
+ const ballot4 = this.ballot4.getAndRequireEquals();
69
69
  const unpackedBallot4 = Ballot.unpack(ballot4.packed);
70
70
 
71
71
  let voteSum = UInt32.from(0);
@@ -1,11 +1,9 @@
1
1
  import {
2
2
  Field,
3
- Experimental,
3
+ ZkProgram,
4
4
  UInt32,
5
5
  Provable,
6
- Poseidon,
7
6
  SelfProof,
8
- Character,
9
7
  Bool,
10
8
  Empty,
11
9
  } from 'o1js';
@@ -34,19 +32,20 @@ const isOlderThan = (reference: PackedString, toVerify: PackedString): Bool => {
34
32
  return ver.greaterThan(ref);
35
33
  };
36
34
 
37
- export const AgeGateCircuit = Experimental.ZkProgram({
35
+ export const AgeGateCircuit = ZkProgram({
36
+ name: 'AgeGateCircuit',
38
37
  publicOutput: PackedCounters,
39
38
 
40
39
  methods: {
41
40
  init: {
42
41
  privateInputs: [],
43
- method() {
42
+ async method() {
44
43
  return PackedCounters.fromBigInts([0n, 0n]);
45
44
  },
46
45
  },
47
46
  verifyAge: {
48
47
  privateInputs: [SelfProof, PackedString],
49
- method(
48
+ async method(
50
49
  oldProof: SelfProof<Empty, PackedCounters>,
51
50
  dateToVerify: PackedString
52
51
  ) {
@@ -72,4 +71,4 @@ export const AgeGateCircuit = Experimental.ZkProgram({
72
71
  },
73
72
  });
74
73
 
75
- export const AgeGateCircuitProof = Experimental.ZkProgram.Proof(AgeGateCircuit);
74
+ export const AgeGateCircuitProof = ZkProgram.Proof(AgeGateCircuit);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "o1js-pack",
3
- "version": "0.5.3",
3
+ "version": "0.6.0",
4
4
  "description": "",
5
5
  "author": "45930",
6
6
  "license": "Apache-2.0",
@@ -45,6 +45,6 @@
45
45
  "typescript": "^4.7.2"
46
46
  },
47
47
  "peerDependencies": {
48
- "o1js": "0.16.1"
48
+ "o1js": "^0.18.0"
49
49
  }
50
50
  }
@@ -4,7 +4,7 @@ import {
4
4
  TextInputProgram,
5
5
  TextInputProof,
6
6
  } from './example_packed_string_circuit';
7
- import { Character, Poseidon } from 'o1js';
7
+ import { Character } from 'o1js';
8
8
 
9
9
  describe('End to End Votes Test', () => {
10
10
  const init = [0n, 0n];
@@ -1,11 +1,4 @@
1
- import {
2
- ZkProgram,
3
- SelfProof,
4
- Character,
5
- Poseidon,
6
- Provable,
7
- Field,
8
- } from 'o1js';
1
+ import { ZkProgram, SelfProof, Character, Provable } from 'o1js';
9
2
  import { PackedStringFactory } from '../../src/lib/packed-types/PackedString';
10
3
 
11
4
  export class TextInput extends PackedStringFactory() {}
@@ -17,14 +10,14 @@ export const TextInputProgram = ZkProgram({
17
10
  methods: {
18
11
  init: {
19
12
  privateInputs: [],
20
- method(state: TextInput) {
13
+ async method(state: TextInput) {
21
14
  const initState = TextInput.fromString('Mina Protocol');
22
15
  state.assertEquals(initState);
23
16
  },
24
17
  },
25
18
  changeFirstLetter: {
26
19
  privateInputs: [SelfProof, Provable.Array(Character, 31), Character],
27
- method(
20
+ async method(
28
21
  newState: TextInput,
29
22
  oldProof: SelfProof<TextInput, TextInput>,
30
23
  oldCharacters: Array<Character>,
@@ -10,14 +10,14 @@ export const VotesProgram = ZkProgram({
10
10
  methods: {
11
11
  init: {
12
12
  privateInputs: [],
13
- method(state: Votes) {
13
+ async method(publicInput: Votes) {
14
14
  const initState = Votes.fromBigInts([0n, 0n]);
15
- state.assertEquals(initState);
15
+ publicInput.assertEquals(initState);
16
16
  },
17
17
  },
18
18
  incrementIndex0: {
19
19
  privateInputs: [SelfProof],
20
- method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
20
+ async method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
21
21
  oldProof.verify();
22
22
  const unpacked = Votes.unpack(oldProof.publicInput.packed);
23
23
  unpacked[0] = unpacked[0].add(1);
@@ -26,7 +26,7 @@ export const VotesProgram = ZkProgram({
26
26
  },
27
27
  incrementIndex1: {
28
28
  privateInputs: [SelfProof],
29
- method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
29
+ async method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
30
30
  oldProof.verify();
31
31
  const unpacked = Votes.unpack(oldProof.publicInput.packed);
32
32
  unpacked[1] = unpacked[1].add(1);
@@ -1,7 +0,0 @@
1
- ## Using Tokens as State Smart Contract
2
-
3
- Using o1js-pack, a token can actually be a packed value. Token balances are natively a `UInt64`, so there is not as much room as a full `Field`, but we can still pack 2 `UInt32`, or we could pack 8 characters, or 64 booleans.
4
-
5
- The `mint` and `burn` mechanisms are used like an unspent transaction output system. The state going in is always burnt, and the state coming out is unrelated numerically. In the example in `run.ts`, the token balance is set to 5 when the state is (5, 0). But when the state is (5, 10), the token balance becomes 42949672965.
6
-
7
- This can be useful for per-user state in a zkapp. Booleans could be used to track feature flags or whether or not a user account is active, in good standing, etc... Characters could be used to store shore strings on a per-user basis, perhaps a country code? UInts can be used like in this example to track the first and last time a user has used an app. Or you could use half of the state to track an actual user balance, and the other half to track another number like number of token transactions.
@@ -1,62 +0,0 @@
1
- import {
2
- SmartContract,
3
- state,
4
- State,
5
- method,
6
- UInt32,
7
- UInt64,
8
- Account,
9
- Provable,
10
- } from 'o1js';
11
- import { PackedUInt32Factory } from '../../../src/index.js';
12
-
13
- /**
14
- * 2 UInt32s representing the first-joined block and the last-updated block
15
- * Could be used in conjunction with some other app logic to ensure only active users can access some resource, or similar
16
- */
17
- export class UserBlockStamps extends PackedUInt32Factory(2) {}
18
-
19
- export class UserBlockStampsToken extends SmartContract {
20
- /**
21
- * Initializes the user state by setting the first-joined blockstamp to the current blockheight
22
- */
23
- @method
24
- joinSystem() {
25
- const account = Account(this.sender, this.token.id);
26
- const currentTokenBalance = account.balance.getAndAssertEquals();
27
- currentTokenBalance.assertEquals(UInt64.from(0));
28
-
29
- const userBlockStamps = UserBlockStamps.fromUInt32s([
30
- this.network.blockchainLength.getAndAssertEquals(),
31
- UInt32.from(0),
32
- ]);
33
- this.token.mint({
34
- address: this.sender,
35
- amount: UInt64.from(userBlockStamps.packed),
36
- });
37
- }
38
-
39
- /**
40
- * Updates the user state by setting the last-updated blockstamp to the current blockheight
41
- */
42
- @method
43
- interactWithSystem() {
44
- const account = Account(this.sender, this.token.id);
45
- const currentTokenBalance = account.balance.getAndAssertEquals();
46
- const existingUserBlockstamps = UserBlockStamps.unpack(
47
- currentTokenBalance.value
48
- );
49
-
50
- this.token.burn({
51
- address: this.sender,
52
- amount: currentTokenBalance,
53
- });
54
-
55
- existingUserBlockstamps[1] =
56
- this.network.blockchainLength.getAndAssertEquals();
57
- this.token.mint({
58
- address: this.sender,
59
- amount: UInt64.from(UserBlockStamps.pack(existingUserBlockstamps)),
60
- });
61
- }
62
- }
@@ -1,71 +0,0 @@
1
- import { AccountUpdate, Mina, PrivateKey, UInt32 } from 'o1js';
2
- import { UserBlockStamps, UserBlockStampsToken } from './contract.js';
3
-
4
- let Local = Mina.LocalBlockchain({ proofsEnabled: true });
5
- Mina.setActiveInstance(Local);
6
-
7
- // a test account that pays all the fees, and puts additional funds into the zkapp
8
- let { privateKey: senderKey, publicKey: sender } = Local.testAccounts[0];
9
-
10
- // the zkapp account
11
- let zkappKey = PrivateKey.random();
12
- let zkappAddress = zkappKey.toPublicKey();
13
-
14
- // a special account that is allowed to pull out half of the zkapp balance, once
15
- let privilegedKey = PrivateKey.random();
16
- let privilegedAddress = privilegedKey.toPublicKey();
17
-
18
- let initialBalance = 10_000_000_000;
19
- let zkapp = new UserBlockStampsToken(zkappAddress);
20
-
21
- console.time('compile');
22
- await UserBlockStampsToken.compile();
23
- console.timeEnd('compile');
24
-
25
- console.time('deploy');
26
- let tx = await Mina.transaction(sender, () => {
27
- let senderUpdate = AccountUpdate.fundNewAccount(sender);
28
- senderUpdate.send({ to: zkappAddress, amount: initialBalance });
29
- zkapp.deploy({ zkappKey });
30
- });
31
- await tx.prove();
32
- await tx.sign([senderKey]).send();
33
- console.timeEnd('deploy');
34
-
35
- Local.setBlockchainLength(UInt32.from(5));
36
-
37
- console.time('join system');
38
- tx = await Mina.transaction(sender, () => {
39
- AccountUpdate.fundNewAccount(sender);
40
- zkapp.joinSystem();
41
- });
42
- await tx.prove();
43
- await tx.sign([senderKey]).send();
44
- console.timeEnd('join system');
45
-
46
- let tokenBalance = Mina.getBalance(sender, zkapp.token.id);
47
- console.log(tokenBalance.toString());
48
- console.log(UserBlockStamps.unpack(tokenBalance.value).toString());
49
-
50
- Local.setBlockchainLength(UInt32.from(10));
51
-
52
- console.time('interact with system');
53
- tx = await Mina.transaction(sender, () => {
54
- zkapp.interactWithSystem();
55
- });
56
- await tx.prove();
57
- await tx.sign([senderKey]).send();
58
- console.timeEnd('interact with system');
59
-
60
- tokenBalance = Mina.getBalance(sender, zkapp.token.id);
61
- console.log(tokenBalance.toString());
62
- console.log(UserBlockStamps.unpack(tokenBalance.value).toString());
63
-
64
- // compile: 1.818s
65
- // deploy: 615.559ms
66
- // join system: 23.085s
67
- // 5
68
- // 5,0
69
- // interact with system: 15.263s
70
- // 42949672965
71
- // 5,10