o1js-pack 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. package/README.md +14 -29
  2. package/build/src/index.d.ts +2 -2
  3. package/build/src/index.js +2 -2
  4. package/build/src/index.js.map +1 -1
  5. package/build/src/lib/PackingPlant.d.ts +4 -12
  6. package/build/src/lib/PackingPlant.js +11 -12
  7. package/build/src/lib/PackingPlant.js.map +1 -1
  8. package/build/src/lib/packed-types/PackedBool.d.ts +1 -6
  9. package/build/src/lib/packed-types/PackedBool.js +1 -0
  10. package/build/src/lib/packed-types/PackedBool.js.map +1 -1
  11. package/build/src/lib/packed-types/PackedString.d.ts +96 -7
  12. package/build/src/lib/packed-types/PackedString.js +78 -10
  13. package/build/src/lib/packed-types/PackedString.js.map +1 -1
  14. package/build/src/lib/packed-types/PackedString.test.js +11 -10
  15. package/build/src/lib/packed-types/PackedString.test.js.map +1 -1
  16. package/build/src/lib/packed-types/PackedUInt32.d.ts +1 -6
  17. package/build/src/lib/packed-types/PackedUInt32.js +1 -0
  18. package/build/src/lib/packed-types/PackedUInt32.js.map +1 -1
  19. package/examples/smart_contract/election/README.md +3 -0
  20. package/examples/smart_contract/election/contract.ts +79 -0
  21. package/examples/smart_contract/election/run.ts +98 -0
  22. package/examples/zk_program/age_gate/README.md +7 -0
  23. package/examples/zk_program/age_gate/circuit.ts +75 -0
  24. package/examples/zk_program/age_gate/non-packed-circuit.ts +68 -0
  25. package/examples/zk_program/age_gate/non-packed-run.ts +36 -0
  26. package/examples/zk_program/age_gate/run.ts +35 -0
  27. package/package.json +1 -1
  28. package/tests/provable/end_to_end.test.ts +2 -4
  29. package/tests/provable/example_packed_string_circuit.ts +3 -8
  30. package/tests/provable/example_packed_uint_circuit.ts +6 -12
  31. package/build/src/lib/packed-types/PackedCharacter.d.ts +0 -51
  32. package/build/src/lib/packed-types/PackedCharacter.js +0 -34
  33. package/build/src/lib/packed-types/PackedCharacter.js.map +0 -1
  34. package/build/src/lib/packed-types/PackedCharacter.test.d.ts +0 -1
  35. package/build/src/lib/packed-types/PackedCharacter.test.js +0 -31
  36. package/build/src/lib/packed-types/PackedCharacter.test.js.map +0 -1
  37. package/build/src/lib/scratch.d.ts +0 -1
  38. package/build/src/lib/scratch.js +0 -16
  39. package/build/src/lib/scratch.js.map +0 -1
  40. package/build/src/packed-types/PackedBool.d.ts +0 -51
  41. package/build/src/packed-types/PackedBool.js +0 -34
  42. package/build/src/packed-types/PackedBool.js.map +0 -1
  43. package/build/src/packed-types/PackedBool.test.d.ts +0 -1
  44. package/build/src/packed-types/PackedBool.test.js +0 -101
  45. package/build/src/packed-types/PackedBool.test.js.map +0 -1
  46. package/build/src/packed-types/PackedCharacter.d.ts +0 -51
  47. package/build/src/packed-types/PackedCharacter.js +0 -34
  48. package/build/src/packed-types/PackedCharacter.js.map +0 -1
  49. package/build/src/packed-types/PackedCharacter.test.d.ts +0 -1
  50. package/build/src/packed-types/PackedCharacter.test.js +0 -31
  51. package/build/src/packed-types/PackedCharacter.test.js.map +0 -1
  52. package/build/src/packed-types/PackedUInt32.d.ts +0 -51
  53. package/build/src/packed-types/PackedUInt32.js +0 -34
  54. package/build/src/packed-types/PackedUInt32.js.map +0 -1
  55. package/build/src/packed-types/PackedUInt32.test.d.ts +0 -1
  56. package/build/src/packed-types/PackedUInt32.test.js +0 -68
  57. package/build/src/packed-types/PackedUInt32.test.js.map +0 -1
  58. package/build/src/packed-types/PackingPlant.d.ts +0 -54
  59. package/build/src/packed-types/PackingPlant.js +0 -45
  60. package/build/src/packed-types/PackingPlant.js.map +0 -1
  61. package/build/src/packed-types/scratch.d.ts +0 -1
  62. package/build/src/packed-types/scratch.js +0 -5
  63. package/build/src/packed-types/scratch.js.map +0 -1
  64. package/coverage/clover.xml +0 -166
  65. package/coverage/coverage-final.json +0 -5
  66. package/coverage/lcov-report/PackedBool.ts.html +0 -268
  67. package/coverage/lcov-report/PackedUInt32.ts.html +0 -313
  68. package/coverage/lcov-report/base.css +0 -224
  69. package/coverage/lcov-report/block-navigation.js +0 -87
  70. package/coverage/lcov-report/favicon.png +0 -0
  71. package/coverage/lcov-report/index.html +0 -131
  72. package/coverage/lcov-report/lib/PackingPlant.ts.html +0 -478
  73. package/coverage/lcov-report/lib/index.html +0 -116
  74. package/coverage/lcov-report/lib/packed-types/PackedBool.ts.html +0 -238
  75. package/coverage/lcov-report/lib/packed-types/PackedString.ts.html +0 -364
  76. package/coverage/lcov-report/lib/packed-types/PackedUInt32.ts.html +0 -238
  77. package/coverage/lcov-report/lib/packed-types/index.html +0 -146
  78. package/coverage/lcov-report/prettify.css +0 -1
  79. package/coverage/lcov-report/prettify.js +0 -2
  80. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  81. package/coverage/lcov-report/sorter.js +0 -196
  82. package/coverage/lcov.info +0 -282
@@ -1,9 +1,9 @@
1
1
  import { Character, Provable } from 'o1js';
2
- import { PackedStringFactory } from './PackedString';
2
+ import { PackedStringFactory, MultiPackedStringFactory } from './PackedString';
3
3
  describe('PackedString', () => {
4
4
  describe('Outside of the Circuit', () => {
5
5
  const vitalik_dot_eth = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
6
- class EthAddressString extends PackedStringFactory(42) {
6
+ class EthAddressString extends MultiPackedStringFactory(3) {
7
7
  }
8
8
  let characters = [];
9
9
  beforeEach(() => {
@@ -21,20 +21,21 @@ describe('PackedString', () => {
21
21
  expect(myEthAddress.toString()).toBe(vitalik_dot_eth);
22
22
  });
23
23
  it('#pack and #unPack', () => {
24
- const packed = EthAddressString.pack(characters);
25
- const unpacked = EthAddressString.unpack(packed);
24
+ const unpacked = EthAddressString.unpack(EthAddressString.fromCharacters(characters).packed);
25
+ const packed = EthAddressString.pack(unpacked);
26
26
  expect(packed.length).toBe(Math.ceil(vitalik_dot_eth.length / 15));
27
- expect(unpacked.length).toBe(vitalik_dot_eth.length);
28
- expect(unpacked.toString()).toBe(characters.toString());
27
+ expect(unpacked.length).toBe(EthAddressString.l);
28
+ expect(unpacked.length).toBe(45);
29
+ expect(unpacked.slice(0, characters.length).toString()).toBe(characters.toString());
29
30
  });
30
31
  });
31
32
  describe('Provable Properties', () => {
32
33
  it('#sizeInFields', () => {
33
34
  class one extends PackedStringFactory(15) {
34
35
  }
35
- class two extends PackedStringFactory(16) {
36
+ class two extends MultiPackedStringFactory(2) {
36
37
  }
37
- class three extends PackedStringFactory(40) {
38
+ class three extends MultiPackedStringFactory(3) {
38
39
  }
39
40
  expect(one.sizeInFields()).toBe(1);
40
41
  expect(two.sizeInFields()).toBe(2);
@@ -59,7 +60,7 @@ describe('PackedString', () => {
59
60
  });
60
61
  it('Exceeds maximum size string', () => {
61
62
  const tooLong = 'too long!'.repeat(20);
62
- class MaxString extends PackedStringFactory(120) {
63
+ class MaxString extends MultiPackedStringFactory(8) {
63
64
  }
64
65
  expect(() => {
65
66
  MaxString.fromString(tooLong);
@@ -68,7 +69,7 @@ describe('PackedString', () => {
68
69
  });
69
70
  describe('In the circuit', () => {
70
71
  const vitalik_dot_eth = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
71
- class EthAddressString extends PackedStringFactory(42) {
72
+ class EthAddressString extends MultiPackedStringFactory(3) {
72
73
  }
73
74
  const outsideEthAddress = EthAddressString.fromString(vitalik_dot_eth);
74
75
  it('Initializes a string', () => {
@@ -1 +1 @@
1
- {"version":3,"file":"PackedString.test.js","sourceRoot":"","sources":["../../../../src/lib/packed-types/PackedString.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAS,QAAQ,EAAE,MAAM,MAAM,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAErD,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;QACtC,MAAM,eAAe,GAAG,4CAA4C,CAAC;QACrE,MAAM,gBAAiB,SAAQ,mBAAmB,CAAC,EAAE,CAAC;SAAG;QACzD,IAAI,UAAU,GAAqB,E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MAAM,YAAY,GAAG,IAAI,gBAAgB,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;oBAEpE,YAAY,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;gBAC/C,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;YAEjB,MAAM,CAAC,GAAG,EAAE;gBACV,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE;oBACxB,MAAM,UAAU,GAAG,CAAC,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;oBACjD,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;oBACrC,MAAM,YAAY,GAAG,IAAI,gBAAgB,CAAC,UAAU,CAAC,CAAC;oBAEtD,YAAY,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;gBAC/C,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -2,9 +2,7 @@ import { Field, UInt32 } from 'o1js';
2
2
  export declare function PackedUInt32Factory(l?: number): {
3
3
  new (packed: import("o1js/dist/node/lib/field.js").Field): {
4
4
  toBigInts(): Array<bigint>;
5
- bitSize: bigint;
6
5
  assertEquals(other: {
7
- bitSize: bigint;
8
6
  assertEquals(other: any): void;
9
7
  packed: import("o1js/dist/node/lib/field.js").Field;
10
8
  }): void;
@@ -25,9 +23,7 @@ export declare function PackedUInt32Factory(l?: number): {
25
23
  */
26
24
  fromUInt32s(uint32s: Array<UInt32>): {
27
25
  toBigInts(): Array<bigint>;
28
- bitSize: bigint;
29
26
  assertEquals(other: {
30
- bitSize: bigint;
31
27
  assertEquals(other: any): void;
32
28
  packed: import("o1js/dist/node/lib/field.js").Field;
33
29
  }): void;
@@ -40,9 +36,7 @@ export declare function PackedUInt32Factory(l?: number): {
40
36
  */
41
37
  fromBigInts(bigints: Array<bigint>): {
42
38
  toBigInts(): Array<bigint>;
43
- bitSize: bigint;
44
39
  assertEquals(other: {
45
- bitSize: bigint;
46
40
  assertEquals(other: any): void;
47
41
  packed: import("o1js/dist/node/lib/field.js").Field;
48
42
  }): void;
@@ -54,6 +48,7 @@ export declare function PackedUInt32Factory(l?: number): {
54
48
  packed: string;
55
49
  }>;
56
50
  l: number;
51
+ bitSize: bigint;
57
52
  checkPack(unpacked: UInt32[]): void;
58
53
  pack(unpacked: UInt32[]): import("o1js/dist/node/lib/field.js").Field;
59
54
  unpackToBigints(f: import("o1js/dist/node/lib/field.js").Field): bigint[];
@@ -20,6 +20,7 @@ export function PackedUInt32Factory(l = L) {
20
20
  const unpacked = this.unpackToBigints(f);
21
21
  return unpacked.map((x) => UInt32.from(x));
22
22
  });
23
+ f.assertEquals(PackedUInt32_.pack(unpacked));
23
24
  return unpacked;
24
25
  }
25
26
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"PackedUInt32.js","sourceRoot":"","sources":["../../../../src/lib/packed-types/PackedUInt32.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,QAAQ,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,kCAAkC;AAC/C,MAAM,YAAY,GAAG,GAAG,CAAC;AAEzB,MAAM,UAAU,mBAAmB,CAAC,IAAY,CAAC;IAC/C,MAAM,aAAc,SAAQ,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,YAAY,CAAC;QAC/D,MAAM,CAAC,YAAY,CAAC,KAAa;YAC/B,OAAO,KAAK,CAAC,KAAK,CAAC;QACrB,CAAC;QAED,MAAM,CAAC,UAAU;YACf,OAAO,YAAY,CAAC;QACtB,CAAC;QAED;;;;WAIG;QACH,MAAM,CAAC,MAAM,CAAC,CAAQ;YACpB,MAAM,QA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;YACP,OAAO,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;QACpE,CAAC;KACF;IACD,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -0,0 +1,3 @@
1
+ ## Election Smart Contract
2
+
3
+ An election has multiple ballots, and a ballot has multiple options. We want to build an app that can track the state of the election, but there are more than 8 pieces of state. We'll have to use packing to store all of the results on chain.
@@ -0,0 +1,79 @@
1
+ import { SmartContract, state, State, method, UInt32 } from 'o1js';
2
+ import { PackedUInt32Factory } from '../../../src/index.js';
3
+
4
+ export class Ballot extends PackedUInt32Factory() {}
5
+
6
+ export class Election extends SmartContract {
7
+ @state(Ballot) ballot1 = State<Ballot>();
8
+ @state(Ballot) ballot2 = State<Ballot>();
9
+ @state(Ballot) ballot3 = State<Ballot>();
10
+ @state(Ballot) ballot4 = State<Ballot>();
11
+
12
+ init() {
13
+ super.init();
14
+ this.ballot1.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n]));
15
+ this.ballot2.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n]));
16
+ this.ballot3.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n]));
17
+ this.ballot4.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n]));
18
+ }
19
+
20
+ @method
21
+ castBallot1(vote: Ballot) {
22
+ const unpackedVote = Ballot.unpack(vote.packed);
23
+ const ballot1 = this.ballot1.getAndAssertEquals();
24
+ const unpackedBallot1 = Ballot.unpack(ballot1.packed);
25
+
26
+ let voteSum = UInt32.from(0);
27
+ for (let i = 0; i < Ballot.l; i++) {
28
+ voteSum = voteSum.add(unpackedVote[i]);
29
+ unpackedBallot1[i] = unpackedBallot1[i].add(unpackedVote[i]);
30
+ }
31
+ voteSum.value.assertEquals(1); // vote must be exactly one vote for one choice
32
+ this.ballot1.set(Ballot.fromUInt32s(unpackedBallot1));
33
+ }
34
+
35
+ @method
36
+ castBallot2(vote: Ballot) {
37
+ const unpackedVote = Ballot.unpack(vote.packed);
38
+ const ballot2 = this.ballot2.getAndAssertEquals();
39
+ const unpackedBallot2 = Ballot.unpack(ballot2.packed);
40
+
41
+ let voteSum = UInt32.from(0);
42
+ for (let i = 0; i < Ballot.l; i++) {
43
+ voteSum = voteSum.add(unpackedVote[i]);
44
+ unpackedBallot2[i] = unpackedBallot2[i].add(unpackedVote[i]);
45
+ }
46
+ voteSum.value.assertEquals(1); // vote must be exactly one vote for one choice
47
+ this.ballot2.set(Ballot.fromUInt32s(unpackedBallot2));
48
+ }
49
+
50
+ @method
51
+ castBallot3(vote: Ballot) {
52
+ const unpackedVote = Ballot.unpack(vote.packed);
53
+ const ballot3 = this.ballot3.getAndAssertEquals();
54
+ const unpackedBallot3 = Ballot.unpack(ballot3.packed);
55
+
56
+ let voteSum = UInt32.from(0);
57
+ for (let i = 0; i < Ballot.l; i++) {
58
+ voteSum = voteSum.add(unpackedVote[i]);
59
+ unpackedBallot3[i] = unpackedBallot3[i].add(unpackedVote[i]);
60
+ }
61
+ voteSum.value.assertEquals(1); // vote must be exactly one vote for one choice
62
+ this.ballot3.set(Ballot.fromUInt32s(unpackedBallot3));
63
+ }
64
+
65
+ @method
66
+ castBallot4(vote: Ballot) {
67
+ const unpackedVote = Ballot.unpack(vote.packed);
68
+ const ballot4 = this.ballot4.getAndAssertEquals();
69
+ const unpackedBallot4 = Ballot.unpack(ballot4.packed);
70
+
71
+ let voteSum = UInt32.from(0);
72
+ for (let i = 0; i < Ballot.l; i++) {
73
+ voteSum = voteSum.add(unpackedVote[i]);
74
+ unpackedBallot4[i] = unpackedBallot4[i].add(unpackedVote[i]);
75
+ }
76
+ voteSum.value.assertEquals(1); // vote must be exactly one vote for one choice
77
+ this.ballot4.set(Ballot.fromUInt32s(unpackedBallot4));
78
+ }
79
+ }
@@ -0,0 +1,98 @@
1
+ import { AccountUpdate, Mina, PrivateKey } from 'o1js';
2
+ import { Ballot, Election } 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 Election(zkappAddress);
20
+
21
+ console.time('compile');
22
+ await Election.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
+ console.log(
36
+ 'initial state: ',
37
+ zkapp.ballot1.get().toBigInts(),
38
+ zkapp.ballot2.get().toBigInts(),
39
+ zkapp.ballot3.get().toBigInts(),
40
+ zkapp.ballot4.get().toBigInts()
41
+ );
42
+
43
+ console.time('vote on ballot 1');
44
+ tx = await Mina.transaction(sender, () => {
45
+ const myVote = Ballot.fromBigInts([0n, 0n, 1n, 0n, 0n, 0n, 0n]);
46
+ zkapp.castBallot1(myVote);
47
+ });
48
+ await tx.prove();
49
+ await tx.sign([senderKey]).send();
50
+ console.timeEnd('vote on ballot 1');
51
+
52
+ console.time('vote on ballot 3');
53
+ tx = await Mina.transaction(sender, () => {
54
+ const myVote = Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 1n]);
55
+ zkapp.castBallot3(myVote);
56
+ });
57
+ await tx.prove();
58
+ await tx.sign([senderKey]).send();
59
+ console.timeEnd('vote on ballot 3');
60
+
61
+ console.log(
62
+ 'final state: ',
63
+ zkapp.ballot1.get().toBigInts(),
64
+ zkapp.ballot2.get().toBigInts(),
65
+ zkapp.ballot3.get().toBigInts(),
66
+ zkapp.ballot4.get().toBigInts()
67
+ );
68
+
69
+ // compile: 28.710s
70
+ // deploy: 1.848s
71
+ // initial state: [
72
+ // 0n, 0n, 0n, 0n,
73
+ // 0n, 0n, 0n
74
+ // ] [
75
+ // 0n, 0n, 0n, 0n,
76
+ // 0n, 0n, 0n
77
+ // ] [
78
+ // 0n, 0n, 0n, 0n,
79
+ // 0n, 0n, 0n
80
+ // ] [
81
+ // 0n, 0n, 0n, 0n,
82
+ // 0n, 0n, 0n
83
+ // ]
84
+ // vote on ballot 1: 24.752s
85
+ // vote on ballot 3: 23.132s
86
+ // final state: [
87
+ // 0n, 0n, 1n, 0n,
88
+ // 0n, 0n, 0n
89
+ // ] [
90
+ // 0n, 0n, 0n, 0n,
91
+ // 0n, 0n, 0n
92
+ // ] [
93
+ // 0n, 0n, 0n, 0n,
94
+ // 0n, 0n, 1n
95
+ // ] [
96
+ // 0n, 0n, 0n, 0n,
97
+ // 0n, 0n, 0n
98
+ // ]
@@ -0,0 +1,7 @@
1
+ ## Age Gate
2
+
3
+ This example Zk Program stores a counter of accepted, a counter of rejected, and a method to check an input age.
4
+
5
+ If the age is above a required limit, the accepted counter is incremented, else the rejected counter is incremented.
6
+
7
+ The 2 counters are stored in a single packed UInt, and the data type for age is a packed string.
@@ -0,0 +1,75 @@
1
+ import {
2
+ Field,
3
+ Experimental,
4
+ UInt32,
5
+ Provable,
6
+ Poseidon,
7
+ SelfProof,
8
+ Character,
9
+ Bool,
10
+ Empty,
11
+ } from 'o1js';
12
+ import {
13
+ PackedUInt32Factory,
14
+ PackedStringFactory,
15
+ } from '../../../src/index.js';
16
+
17
+ export class PackedCounters extends PackedUInt32Factory(2) {}
18
+
19
+ export class PackedString extends PackedStringFactory(8) {}
20
+
21
+ const isOlderThan = (reference: PackedString, toVerify: PackedString): Bool => {
22
+ let ref = Field(0);
23
+ let ver = Field(0);
24
+
25
+ const referenceDate = PackedString.unpack(reference.packed);
26
+ const dateToVerify = PackedString.unpack(toVerify.packed);
27
+
28
+ for (let i = 0; i < PackedString.l; i++) {
29
+ const exp = PackedString.l - i;
30
+ ref = ref.add(referenceDate[i].value.mul(10 ** exp));
31
+ ver = ver.add(dateToVerify[i].value.mul(10 ** exp));
32
+ }
33
+
34
+ return ver.greaterThan(ref);
35
+ };
36
+
37
+ export const AgeGateCircuit = Experimental.ZkProgram({
38
+ publicOutput: PackedCounters,
39
+
40
+ methods: {
41
+ init: {
42
+ privateInputs: [],
43
+ method() {
44
+ return PackedCounters.fromBigInts([0n, 0n]);
45
+ },
46
+ },
47
+ verifyAge: {
48
+ privateInputs: [SelfProof, PackedString],
49
+ method(
50
+ oldProof: SelfProof<Empty, PackedCounters>,
51
+ dateToVerify: PackedString
52
+ ) {
53
+ oldProof.verify();
54
+ const counters = PackedCounters.unpack(oldProof.publicOutput.packed);
55
+ const referenceDate = PackedString.fromString('20000101'); // hard-coded - date to verify must be older than new years in the year 2000
56
+ const isOlder = isOlderThan(referenceDate, dateToVerify);
57
+ const deltaCounter0 = Provable.if(
58
+ isOlder,
59
+ UInt32.from(1),
60
+ UInt32.from(0)
61
+ );
62
+ const deltaCounter1 = Provable.if(
63
+ isOlder,
64
+ UInt32.from(0),
65
+ UInt32.from(1)
66
+ );
67
+ counters[0] = counters[0].add(deltaCounter0);
68
+ counters[1] = counters[1].add(deltaCounter1);
69
+ return PackedCounters.fromUInt32s(counters);
70
+ },
71
+ },
72
+ },
73
+ });
74
+
75
+ export const AgeGateCircuitProof = Experimental.ZkProgram.Proof(AgeGateCircuit);
@@ -0,0 +1,68 @@
1
+ import {
2
+ Field,
3
+ Experimental,
4
+ UInt32,
5
+ Provable,
6
+ Poseidon,
7
+ SelfProof,
8
+ Character,
9
+ Bool,
10
+ Empty,
11
+ CircuitString,
12
+ } from 'o1js';
13
+
14
+ const isOlderThan = (
15
+ reference: CircuitString,
16
+ toVerify: CircuitString
17
+ ): Bool => {
18
+ let ref = Field(0);
19
+ let ver = Field(0);
20
+
21
+ for (let i = 0; i < 8; i++) {
22
+ const exp = 8 - i;
23
+ ref = ref.add(reference.values[i].value.mul(10 ** exp));
24
+ ver = ver.add(toVerify.values[i].value.mul(10 ** exp));
25
+ }
26
+
27
+ return ver.greaterThan(ref);
28
+ };
29
+
30
+ export const AgeGateCircuit = Experimental.ZkProgram({
31
+ publicOutput: Provable.Array(UInt32, 2),
32
+
33
+ methods: {
34
+ init: {
35
+ privateInputs: [],
36
+ method() {
37
+ return [UInt32.from(0), UInt32.from(0)];
38
+ },
39
+ },
40
+ verifyAge: {
41
+ privateInputs: [SelfProof, CircuitString],
42
+ method(
43
+ oldProof: SelfProof<Empty, Array<UInt32>>,
44
+ dateToVerify: CircuitString
45
+ ) {
46
+ oldProof.verify();
47
+ const counters = oldProof.publicOutput;
48
+ const referenceDate = CircuitString.fromString('20000101'); // hard-coded - date to verify must be older than new years in the year 2000
49
+ const isOlder = isOlderThan(referenceDate, dateToVerify);
50
+ const deltaCounter0 = Provable.if(
51
+ isOlder,
52
+ UInt32.from(1),
53
+ UInt32.from(0)
54
+ );
55
+ const deltaCounter1 = Provable.if(
56
+ isOlder,
57
+ UInt32.from(0),
58
+ UInt32.from(1)
59
+ );
60
+ counters[0] = counters[0].add(deltaCounter0);
61
+ counters[1] = counters[1].add(deltaCounter1);
62
+ return counters;
63
+ },
64
+ },
65
+ },
66
+ });
67
+
68
+ export const AgeGateCircuitProof = Experimental.ZkProgram.Proof(AgeGateCircuit);
@@ -0,0 +1,36 @@
1
+ import { AgeGateCircuit } from './non-packed-circuit.js';
2
+ import { CircuitString } from 'o1js';
3
+
4
+ console.time('Compiling...');
5
+ await AgeGateCircuit.compile();
6
+ console.timeEnd('Compiling...');
7
+
8
+ console.time('Running...');
9
+ let old_proof = await AgeGateCircuit.init();
10
+ old_proof = await AgeGateCircuit.verifyAge(
11
+ old_proof,
12
+ CircuitString.fromString('20210927')
13
+ );
14
+ old_proof = await AgeGateCircuit.verifyAge(
15
+ old_proof,
16
+ CircuitString.fromString('19011225')
17
+ );
18
+ old_proof = await AgeGateCircuit.verifyAge(
19
+ old_proof,
20
+ CircuitString.fromString('20071031')
21
+ );
22
+ console.timeEnd('Running...');
23
+
24
+ // Now we expect old_proof to have a counter of [2, 1] - 2 dates were older than 2000-01-01, 1 date was younger
25
+ const counters = old_proof.publicOutput;
26
+ console.log(counters.toString());
27
+
28
+ const circuitAnalysis = AgeGateCircuit.analyzeMethods();
29
+ console.log('Number of gates in method 0: ', circuitAnalysis[0].rows);
30
+ console.log('Number of gates in method 1: ', circuitAnalysis[1].rows);
31
+
32
+ // Compiling...: 1:00.331 (m:ss.mmm)
33
+ // Running...: 3:31.711 (m:ss.mmm)
34
+ // 2,1
35
+ // Number of gates in method 0: 0
36
+ // Number of gates in method 1: 651
@@ -0,0 +1,35 @@
1
+ import { AgeGateCircuit, PackedCounters, PackedString } from './circuit.js';
2
+
3
+ console.time('Compiling...');
4
+ await AgeGateCircuit.compile();
5
+ console.timeEnd('Compiling...');
6
+
7
+ console.time('Running...');
8
+ let old_proof = await AgeGateCircuit.init();
9
+ old_proof = await AgeGateCircuit.verifyAge(
10
+ old_proof,
11
+ PackedString.fromString('20210927')
12
+ );
13
+ old_proof = await AgeGateCircuit.verifyAge(
14
+ old_proof,
15
+ PackedString.fromString('19011225')
16
+ );
17
+ old_proof = await AgeGateCircuit.verifyAge(
18
+ old_proof,
19
+ PackedString.fromString('20071031')
20
+ );
21
+ console.timeEnd('Running...');
22
+
23
+ // Now we expect old_proof to have a counter of [2, 1] - 2 dates were older than 2000-01-01, 1 date was younger
24
+ const counters = PackedCounters.unpack(old_proof.publicOutput.packed);
25
+ console.log(counters.toString());
26
+
27
+ const circuitAnalysis = AgeGateCircuit.analyzeMethods();
28
+ console.log('Number of gates in method 0: ', circuitAnalysis[0].rows);
29
+ console.log('Number of gates in method 1: ', circuitAnalysis[1].rows);
30
+
31
+ // Compiling...: 59.405s
32
+ // Running...: 3:31.839 (m:ss.mmm)
33
+ // 2,1
34
+ // Number of gates in method 0: 0
35
+ // Number of gates in method 1: 551
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "o1js-pack",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "",
5
5
  "author": "45930",
6
6
  "license": "Apache-2.0",
@@ -56,12 +56,10 @@ describe('End to End Text Input Test', () => {
56
56
  const p1 = await TextInputProgram.changeFirstLetter(
57
57
  proofTextInput,
58
58
  initProof,
59
- TextInput.unpack(new TextInput(initProof.publicInput.packed).packed),
59
+ TextInput.unpack(initProof.publicInput.packed),
60
60
  Character.fromString('Z')
61
61
  );
62
- Poseidon.hash(proofTextInput.packed).assertEquals(
63
- Poseidon.hash(p1.publicInput.packed)
64
- );
62
+ proofTextInput.assertEquals(p1.publicInput);
65
63
  });
66
64
  });
67
65
  });
@@ -6,10 +6,9 @@ import {
6
6
  Provable,
7
7
  Field,
8
8
  } from 'o1js';
9
- import { PackedStringFactory } from '../../src/lib/packed-types/PackedString';
9
+ import { MultiPackedStringFactory } from '../../src/lib/packed-types/PackedString';
10
10
 
11
- const MAX_LENGTH = 32;
12
- export class TextInput extends PackedStringFactory(MAX_LENGTH) {}
11
+ export class TextInput extends MultiPackedStringFactory(3) {}
13
12
 
14
13
  export const TextInputProgram = Experimental.ZkProgram({
15
14
  publicInput: TextInput,
@@ -23,11 +22,7 @@ export const TextInputProgram = Experimental.ZkProgram({
23
22
  },
24
23
  },
25
24
  changeFirstLetter: {
26
- privateInputs: [
27
- SelfProof,
28
- Provable.Array(Character, MAX_LENGTH),
29
- Character,
30
- ],
25
+ privateInputs: [SelfProof, Provable.Array(Character, 45), Character],
31
26
  method(
32
27
  newState: TextInput,
33
28
  oldProof: SelfProof<TextInput, TextInput>,
@@ -18,24 +18,18 @@ export const VotesProgram = Experimental.ZkProgram({
18
18
  privateInputs: [SelfProof],
19
19
  method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
20
20
  oldProof.verify();
21
- const expectedUnpacked = Votes.unpack(oldProof.publicInput.packed);
22
- oldProof.publicInput.packed.assertEquals(
23
- Votes.fromUInt32s(expectedUnpacked).packed
24
- );
25
- expectedUnpacked[0] = expectedUnpacked[0].add(1);
26
- newState.assertEquals(Votes.fromUInt32s(expectedUnpacked));
21
+ const unpacked = Votes.unpack(oldProof.publicInput.packed);
22
+ unpacked[0] = unpacked[0].add(1);
23
+ newState.assertEquals(Votes.fromUInt32s(unpacked));
27
24
  },
28
25
  },
29
26
  incrementIndex1: {
30
27
  privateInputs: [SelfProof],
31
28
  method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
32
29
  oldProof.verify();
33
- const expectedUnpacked = Votes.unpack(oldProof.publicInput.packed);
34
- oldProof.publicInput.packed.assertEquals(
35
- Votes.fromUInt32s(expectedUnpacked).packed
36
- );
37
- expectedUnpacked[1] = expectedUnpacked[1].add(1);
38
- newState.assertEquals(Votes.fromUInt32s(expectedUnpacked));
30
+ const unpacked = Votes.unpack(oldProof.publicInput.packed);
31
+ unpacked[1] = unpacked[1].add(1);
32
+ newState.assertEquals(Votes.fromUInt32s(unpacked));
39
33
  },
40
34
  },
41
35
  },