o1js-pack 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. package/.husky/pre-commit +0 -0
  2. package/README.md +9 -30
  3. package/build/src/index.d.ts +5 -5
  4. package/build/src/index.js +5 -5
  5. package/build/src/lib/PackingPlant.d.ts +132 -140
  6. package/build/src/lib/PackingPlant.js +190 -191
  7. package/build/src/lib/PackingPlant.js.map +1 -1
  8. package/build/src/lib/packed-types/PackedBool.d.ts +85 -90
  9. package/build/src/lib/packed-types/PackedBool.js +49 -48
  10. package/build/src/lib/packed-types/PackedBool.js.map +1 -1
  11. package/build/src/lib/packed-types/PackedBool.test.d.ts +1 -1
  12. package/build/src/lib/packed-types/PackedBool.test.js +75 -75
  13. package/build/src/lib/packed-types/PackedString.d.ts +181 -92
  14. package/build/src/lib/packed-types/PackedString.js +127 -59
  15. package/build/src/lib/packed-types/PackedString.js.map +1 -1
  16. package/build/src/lib/packed-types/PackedString.test.d.ts +1 -1
  17. package/build/src/lib/packed-types/PackedString.test.js +100 -99
  18. package/build/src/lib/packed-types/PackedString.test.js.map +1 -1
  19. package/build/src/lib/packed-types/PackedUInt32.d.ts +85 -90
  20. package/build/src/lib/packed-types/PackedUInt32.js +49 -48
  21. package/build/src/lib/packed-types/PackedUInt32.js.map +1 -1
  22. package/build/src/lib/packed-types/PackedUInt32.test.d.ts +1 -1
  23. package/build/src/lib/packed-types/PackedUInt32.test.js +83 -83
  24. package/package.json +1 -1
  25. package/tests/provable/end_to_end.test.ts +2 -4
  26. package/tests/provable/example_packed_string_circuit.ts +3 -8
  27. package/tests/provable/example_packed_uint_circuit.ts +6 -12
  28. package/build/src/lib/packed-types/PackedCharacter.d.ts +0 -51
  29. package/build/src/lib/packed-types/PackedCharacter.js +0 -34
  30. package/build/src/lib/packed-types/PackedCharacter.js.map +0 -1
  31. package/build/src/lib/packed-types/PackedCharacter.test.d.ts +0 -1
  32. package/build/src/lib/packed-types/PackedCharacter.test.js +0 -31
  33. package/build/src/lib/packed-types/PackedCharacter.test.js.map +0 -1
  34. package/build/src/lib/scratch.d.ts +0 -1
  35. package/build/src/lib/scratch.js +0 -16
  36. package/build/src/lib/scratch.js.map +0 -1
  37. package/build/src/packed-types/PackedBool.d.ts +0 -51
  38. package/build/src/packed-types/PackedBool.js +0 -34
  39. package/build/src/packed-types/PackedBool.js.map +0 -1
  40. package/build/src/packed-types/PackedBool.test.d.ts +0 -1
  41. package/build/src/packed-types/PackedBool.test.js +0 -101
  42. package/build/src/packed-types/PackedBool.test.js.map +0 -1
  43. package/build/src/packed-types/PackedCharacter.d.ts +0 -51
  44. package/build/src/packed-types/PackedCharacter.js +0 -34
  45. package/build/src/packed-types/PackedCharacter.js.map +0 -1
  46. package/build/src/packed-types/PackedCharacter.test.d.ts +0 -1
  47. package/build/src/packed-types/PackedCharacter.test.js +0 -31
  48. package/build/src/packed-types/PackedCharacter.test.js.map +0 -1
  49. package/build/src/packed-types/PackedUInt32.d.ts +0 -51
  50. package/build/src/packed-types/PackedUInt32.js +0 -34
  51. package/build/src/packed-types/PackedUInt32.js.map +0 -1
  52. package/build/src/packed-types/PackedUInt32.test.d.ts +0 -1
  53. package/build/src/packed-types/PackedUInt32.test.js +0 -68
  54. package/build/src/packed-types/PackedUInt32.test.js.map +0 -1
  55. package/build/src/packed-types/PackingPlant.d.ts +0 -54
  56. package/build/src/packed-types/PackingPlant.js +0 -45
  57. package/build/src/packed-types/PackingPlant.js.map +0 -1
  58. package/build/src/packed-types/scratch.d.ts +0 -1
  59. package/build/src/packed-types/scratch.js +0 -5
  60. package/build/src/packed-types/scratch.js.map +0 -1
  61. package/coverage/clover.xml +0 -166
  62. package/coverage/coverage-final.json +0 -5
  63. package/coverage/lcov-report/PackedBool.ts.html +0 -268
  64. package/coverage/lcov-report/PackedUInt32.ts.html +0 -313
  65. package/coverage/lcov-report/base.css +0 -224
  66. package/coverage/lcov-report/block-navigation.js +0 -87
  67. package/coverage/lcov-report/favicon.png +0 -0
  68. package/coverage/lcov-report/index.html +0 -131
  69. package/coverage/lcov-report/lib/PackingPlant.ts.html +0 -478
  70. package/coverage/lcov-report/lib/index.html +0 -116
  71. package/coverage/lcov-report/lib/packed-types/PackedBool.ts.html +0 -238
  72. package/coverage/lcov-report/lib/packed-types/PackedString.ts.html +0 -364
  73. package/coverage/lcov-report/lib/packed-types/PackedUInt32.ts.html +0 -238
  74. package/coverage/lcov-report/lib/packed-types/index.html +0 -146
  75. package/coverage/lcov-report/prettify.css +0 -1
  76. package/coverage/lcov-report/prettify.js +0 -2
  77. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  78. package/coverage/lcov-report/sorter.js +0 -196
  79. package/coverage/lcov.info +0 -282
package/.husky/pre-commit CHANGED
File without changes
package/README.md CHANGED
@@ -1,33 +1,18 @@
1
- # O1JS Pack
1
+ # o1js Pack
2
2
 
3
- Make the most of your ZKApp state!
3
+ Make the most of your zkApp state!
4
4
 
5
5
  ## What is it
6
6
 
7
- O1JS Pack is a library for [O1JS](https://github.com/o1-labs/o1js/) that allows a zkapp developer to pack extra data into a single Field.
7
+ o1js Pack is a library for [o1js](https://github.com/o1-labs/o1js/) that allows a zkApp developer to pack extra data into a single Field.
8
8
 
9
- ### Usage in a ZKApp
9
+ ### Usage in a zkApp
10
10
 
11
- ```
12
- import {
13
- PackedBoolFactory,
14
- PackedStringFactory,
15
- PackedUInt32Factory,
16
- } from 'o1js-pack';
17
-
18
- const MyPackedBools = PackedBoolFactory(); // Max of 254 Bools
19
- const MyPackedString = PackedStringFactory(); // Max of 120 Characters
20
- const MyPackedUInts = PackedUInt32Factory(); // Max of 7 UInt32s
21
-
22
- const sevenUints = [1, 2, 3, 4, 5, 6, 7].map(x => UInt32.from(x));
23
- const myPackedUInts = new MyPackedUInts(MyPackedUInts.pack(sevenUints), sevenUints);
24
- ```
11
+ TODO
25
12
 
26
- ## What's the catch?
13
+ ### Smart Contract
27
14
 
28
- If a zkapp has two or more independent Fields of state, then preconditions can be set on just one, or on any combination of the Fields. With SnarkyPack, one precondition will be set on the packed Field, which means that independent updates cannot happen asynchronously.
29
-
30
- For example, let's say there is a voting app with two independent Fields of state: `yesCount` and `noCount`. If one user issues an update to `yesCount` and another user issues an update to `noCount` at the same time, both proofs may be valid at the same time. Both transactions may succeed during the same block, since they don't interfere with each others' preconditions. With SnarkyPack, a devolper can save state by putting both `yesCount` and `noCount` into the same Field of state, making room for other state to exist on chain. The tradeoff is that two votes will no longer be able to be cast independently. `yesCount` and `noCount` depend on the same precontition, meaning that to update one or the other, you must lock both.
15
+ ### ZK Program
31
16
 
32
17
  ## How to build
33
18
 
@@ -39,18 +24,12 @@ npm run build
39
24
 
40
25
  ```sh
41
26
  npm run test src # non-proof tests
42
- npm run testw # watch mode
43
- ```
44
-
45
- ## How to run coverage
46
-
47
- ```sh
48
- npm run coverage
27
+ npm run test tests/provable # provable tests
49
28
  ```
50
29
 
51
30
  ## Credits
52
31
 
53
- Thanks to @mario_zito for seeding the idea for this library on discord ([Thread 1](https://discord.com/channels/484437221055922177/1128509274465779822), [Thread 2](https://discord.com/channels/484437221055922177/1128501705173106698)), and to @gregor for sounding out the early implementation.
32
+ Thanks to @mario_zito for seeding the idea for this library on Discord ([Thread 1](https://discord.com/channels/484437221055922177/1128509274465779822), [Thread 2](https://discord.com/channels/484437221055922177/1128501705173106698)), and to @gregor for sounding out the early implementation.
54
33
 
55
34
  Thanks to @iam-dev for early adoption!
56
35
 
@@ -1,5 +1,5 @@
1
- import { PackedBoolFactory } from './lib/packed-types/PackedBool.js';
2
- import { PackedUInt32Factory } from './lib/packed-types/PackedUInt32.js';
3
- import { PackingPlant, MultiPackingPlant } from './lib/PackingPlant.js';
4
- import { PackedStringFactory } from './lib/packed-types/PackedString.js';
5
- export { PackedBoolFactory, PackedStringFactory, PackedUInt32Factory, PackingPlant, MultiPackingPlant, };
1
+ import { PackedBoolFactory } from './lib/packed-types/PackedBool.js';
2
+ import { PackedUInt32Factory } from './lib/packed-types/PackedUInt32.js';
3
+ import { PackingPlant, MultiPackingPlant } from './lib/PackingPlant.js';
4
+ import { PackedStringFactory } from './lib/packed-types/PackedString.js';
5
+ export { PackedBoolFactory, PackedStringFactory, PackedUInt32Factory, PackingPlant, MultiPackingPlant, };
@@ -1,6 +1,6 @@
1
- import { PackedBoolFactory } from './lib/packed-types/PackedBool.js';
2
- import { PackedUInt32Factory } from './lib/packed-types/PackedUInt32.js';
3
- import { PackingPlant, MultiPackingPlant } from './lib/PackingPlant.js';
4
- import { PackedStringFactory } from './lib/packed-types/PackedString.js';
5
- export { PackedBoolFactory, PackedStringFactory, PackedUInt32Factory, PackingPlant, MultiPackingPlant, };
1
+ import { PackedBoolFactory } from './lib/packed-types/PackedBool.js';
2
+ import { PackedUInt32Factory } from './lib/packed-types/PackedUInt32.js';
3
+ import { PackingPlant, MultiPackingPlant } from './lib/PackingPlant.js';
4
+ import { PackedStringFactory } from './lib/packed-types/PackedString.js';
5
+ export { PackedBoolFactory, PackedStringFactory, PackedUInt32Factory, PackingPlant, MultiPackingPlant, };
6
6
  //# sourceMappingURL=index.js.map
@@ -1,140 +1,132 @@
1
- import { Field, InferProvable } from 'o1js';
2
- export declare function PackingPlant<A, T extends InferProvable<A> = InferProvable<A>>(elementType: A, l: number, bitSize: bigint): (abstract new (packed: Field) => {
3
- bitSize: bigint;
4
- assertEquals(other: any): void;
5
- packed: import("o1js/dist/node/lib/field").Field;
6
- }) & {
7
- type: import("o1js/dist/node/bindings/lib/provable-snarky").ProvableExtended<{
8
- packed: import("o1js/dist/node/lib/field").Field;
9
- }, {
10
- packed: string;
11
- }>;
12
- l: number;
13
- extractField(input: T): Field;
14
- sizeInBits(): bigint;
15
- unpack(f: Field): Array<T>;
16
- /**
17
- *
18
- * @param unpacked Array of the implemented packed type
19
- * @throws if the length of the array is longer than the length of the implementing factory config
20
- */
21
- checkPack(unpacked: Array<T>): void;
22
- /**
23
- *
24
- * @param unpacked Array of the implemented packed type, must be shorter than the max allowed, which varies by type, will throw if the input is too long
25
- * @returns Field, packed with the information from the unpacked input
26
- */
27
- pack(unpacked: Array<T>): Field;
28
- /**
29
- *
30
- * @param f Field, packed with the information, as returned by #pack
31
- * @returns Array of bigints, which can be decoded by the implementing class into the final type
32
- */
33
- unpackToBigints(f: Field): Array<bigint>;
34
- _isStruct: true; /**
35
- *
36
- * @param fields Array of Fields, packed such that each Field has as much information as possible, as returned in #pack
37
- * @returns Array of bigints, which can be decoded by the implementing class into the final type
38
- */
39
- toFields: (value: {
40
- packed: import("o1js/dist/node/lib/field").Field;
41
- }) => import("o1js/dist/node/lib/field").Field[];
42
- toAuxiliary: (value?: {
43
- packed: import("o1js/dist/node/lib/field").Field;
44
- } | undefined) => any[];
45
- fromFields: (fields: import("o1js/dist/node/lib/field").Field[]) => {
46
- packed: import("o1js/dist/node/lib/field").Field;
47
- };
48
- sizeInFields(): number;
49
- check: (value: {
50
- packed: import("o1js/dist/node/lib/field").Field;
51
- }) => void;
52
- toInput: (x: {
53
- packed: import("o1js/dist/node/lib/field").Field;
54
- }) => {
55
- fields?: import("o1js/dist/node/lib/field").Field[] | undefined;
56
- packed?: [import("o1js/dist/node/lib/field").Field, number][] | undefined;
57
- };
58
- toJSON: (x: {
59
- packed: import("o1js/dist/node/lib/field").Field;
60
- }) => {
61
- packed: string;
62
- };
63
- fromJSON: (x: {
64
- packed: string;
65
- }) => {
66
- packed: import("o1js/dist/node/lib/field").Field;
67
- };
68
- };
69
- export declare function MultiPackingPlant<A, T extends InferProvable<A> = InferProvable<A>>(elementType: A, l: number, bitSize: bigint): (abstract new (packed: Array<Field>) => {
70
- bitSize: bigint;
71
- assertEquals(other: any): void;
72
- packed: import("o1js/dist/node/lib/field").Field[];
73
- }) & {
74
- type: import("o1js/dist/node/bindings/lib/provable-snarky").ProvableExtended<{
75
- packed: import("o1js/dist/node/lib/field").Field[];
76
- }, {
77
- packed: string[];
78
- }>;
79
- l: number;
80
- n: number;
81
- extractField(input: T | undefined): Field;
82
- sizeInBits(): bigint;
83
- elementsPerField(): number;
84
- unpack(fields: Array<Field>): Array<T>;
85
- /**
86
- *
87
- * @param unpacked Array of the implemented packed type
88
- * @throws if the length of the array is longer than the length of the implementing factory config
89
- */
90
- checkPack(unpacked: Array<T>): void;
91
- /**
92
- *
93
- * @param unpacked Array of the implemented packed type, must be shorter than the max allowed, which varies by type, will throw if the input is too long
94
- * @returns Array of Fields, packed such that each Field has as much information as possible
95
- *
96
- * e.g. 15 Characters pack into 1 Field. 15 or fewer Characters will return an array of 1 Field
97
- * 30 of fewer Characters will return an aray of 2 Fields
98
- */
99
- pack(unpacked: Array<T>): Array<Field>;
100
- /**
101
- *
102
- * @param fields Array of Fields, packed such that each Field has as much information as possible, as returned in #pack
103
- * @returns Array of bigints, which can be decoded by the implementing class into the final type
104
- */
105
- unpackToBigints(fields: Array<Field>): Array<bigint>;
106
- _isStruct: true; /**
107
- *
108
- * @param fields Array of Fields, packed such that each Field has as much information as possible, as returned in #pack
109
- * @returns Array of bigints, which can be decoded by the implementing class into the final type
110
- */
111
- toFields: (value: {
112
- packed: import("o1js/dist/node/lib/field").Field[];
113
- }) => import("o1js/dist/node/lib/field").Field[];
114
- toAuxiliary: (value?: {
115
- packed: import("o1js/dist/node/lib/field").Field[];
116
- } | undefined) => any[];
117
- fromFields: (fields: import("o1js/dist/node/lib/field").Field[]) => {
118
- packed: import("o1js/dist/node/lib/field").Field[];
119
- };
120
- sizeInFields(): number;
121
- check: (value: {
122
- packed: import("o1js/dist/node/lib/field").Field[];
123
- }) => void;
124
- toInput: (x: {
125
- packed: import("o1js/dist/node/lib/field").Field[];
126
- }) => {
127
- fields?: import("o1js/dist/node/lib/field").Field[] | undefined;
128
- packed?: [import("o1js/dist/node/lib/field").Field, number][] | undefined;
129
- };
130
- toJSON: (x: {
131
- packed: import("o1js/dist/node/lib/field").Field[];
132
- }) => {
133
- packed: string[];
134
- };
135
- fromJSON: (x: {
136
- packed: string[];
137
- }) => {
138
- packed: import("o1js/dist/node/lib/field").Field[];
139
- };
140
- };
1
+ import { Field, InferProvable } from 'o1js';
2
+ export declare function PackingPlant<A, T extends InferProvable<A> = InferProvable<A>>(elementType: A, l: number, bitSize: bigint): (abstract new (packed: Field) => {
3
+ assertEquals(other: any): void;
4
+ packed: import("o1js/dist/node/lib/field").Field;
5
+ }) & {
6
+ type: import("o1js/dist/node/bindings/lib/provable-snarky").ProvableExtended<{
7
+ packed: import("o1js/dist/node/lib/field").Field;
8
+ }, {
9
+ packed: string;
10
+ }>;
11
+ l: number;
12
+ bitSize: bigint;
13
+ extractField(input: T): Field;
14
+ sizeInBits(): bigint;
15
+ unpack(f: Field): Array<T>;
16
+ /**
17
+ *
18
+ * @param unpacked Array of the implemented packed type
19
+ * @throws if the length of the array is longer than the length of the implementing factory config
20
+ */
21
+ checkPack(unpacked: Array<T>): void;
22
+ /**
23
+ *
24
+ * @param unpacked Array of the implemented packed type, must be shorter than the max allowed, which varies by type, will throw if the input is too long
25
+ * @returns Field, packed with the information from the unpacked input
26
+ */
27
+ pack(unpacked: Array<T>): Field;
28
+ /**
29
+ *
30
+ * @param f Field, packed with the information, as returned by #pack
31
+ * @returns Array of bigints, which can be decoded by the implementing class into the final type
32
+ */
33
+ unpackToBigints(f: Field): Array<bigint>;
34
+ _isStruct: true;
35
+ toFields: (value: {
36
+ packed: import("o1js/dist/node/lib/field").Field;
37
+ }) => import("o1js/dist/node/lib/field").Field[];
38
+ toAuxiliary: (value?: {
39
+ packed: import("o1js/dist/node/lib/field").Field;
40
+ } | undefined) => any[];
41
+ fromFields: (fields: import("o1js/dist/node/lib/field").Field[]) => {
42
+ packed: import("o1js/dist/node/lib/field").Field;
43
+ };
44
+ sizeInFields(): number;
45
+ check: (value: {
46
+ packed: import("o1js/dist/node/lib/field").Field;
47
+ }) => void;
48
+ toInput: (x: {
49
+ packed: import("o1js/dist/node/lib/field").Field;
50
+ }) => {
51
+ fields?: import("o1js/dist/node/lib/field").Field[] | undefined;
52
+ packed?: [import("o1js/dist/node/lib/field").Field, number][] | undefined;
53
+ };
54
+ toJSON: (x: {
55
+ packed: import("o1js/dist/node/lib/field").Field;
56
+ }) => {
57
+ packed: string;
58
+ };
59
+ fromJSON: (x: {
60
+ packed: string;
61
+ }) => {
62
+ packed: import("o1js/dist/node/lib/field").Field;
63
+ };
64
+ };
65
+ export declare function MultiPackingPlant<A, T extends InferProvable<A> = InferProvable<A>>(elementType: A, l: number, bitSize: bigint): (abstract new (packed: Array<Field>) => {
66
+ assertEquals(other: any): void;
67
+ packed: import("o1js/dist/node/lib/field").Field[];
68
+ }) & {
69
+ type: import("o1js/dist/node/bindings/lib/provable-snarky").ProvableExtended<{
70
+ packed: import("o1js/dist/node/lib/field").Field[];
71
+ }, {
72
+ packed: string[];
73
+ }>;
74
+ l: number;
75
+ n: number;
76
+ bitSize: bigint;
77
+ extractField(input: T | undefined): Field;
78
+ sizeInBits(): bigint;
79
+ elementsPerField(): number;
80
+ unpack(fields: Array<Field>): Array<T>;
81
+ /**
82
+ *
83
+ * @param unpacked Array of the implemented packed type
84
+ * @throws if the length of the array is longer than the length of the implementing factory config
85
+ */
86
+ checkPack(unpacked: Array<T>): void;
87
+ /**
88
+ *
89
+ * @param unpacked Array of the implemented packed type, must be shorter than the max allowed, which varies by type, will throw if the input is too long
90
+ * @returns Array of Fields, packed such that each Field has as much information as possible
91
+ *
92
+ * e.g. 15 Characters pack into 1 Field. 15 or fewer Characters will return an array of 1 Field
93
+ * 30 of fewer Characters will return an aray of 2 Fields
94
+ */
95
+ pack(unpacked: Array<T>): Array<Field>;
96
+ /**
97
+ *
98
+ * @param fields Array of Fields, packed such that each Field has as much information as possible, as returned in #pack
99
+ * @returns Array of bigints, which can be decoded by the implementing class into the final type
100
+ */
101
+ unpackToBigints(fields: Array<Field>): Array<bigint>;
102
+ _isStruct: true;
103
+ toFields: (value: {
104
+ packed: import("o1js/dist/node/lib/field").Field[];
105
+ }) => import("o1js/dist/node/lib/field").Field[];
106
+ toAuxiliary: (value?: {
107
+ packed: import("o1js/dist/node/lib/field").Field[];
108
+ } | undefined) => any[];
109
+ fromFields: (fields: import("o1js/dist/node/lib/field").Field[]) => {
110
+ packed: import("o1js/dist/node/lib/field").Field[];
111
+ };
112
+ sizeInFields(): number;
113
+ check: (value: {
114
+ packed: import("o1js/dist/node/lib/field").Field[];
115
+ }) => void;
116
+ toInput: (x: {
117
+ packed: import("o1js/dist/node/lib/field").Field[];
118
+ }) => {
119
+ fields?: import("o1js/dist/node/lib/field").Field[] | undefined;
120
+ packed?: [import("o1js/dist/node/lib/field").Field, number][] | undefined;
121
+ };
122
+ toJSON: (x: {
123
+ packed: import("o1js/dist/node/lib/field").Field[];
124
+ }) => {
125
+ packed: string[];
126
+ };
127
+ fromJSON: (x: {
128
+ packed: string[];
129
+ }) => {
130
+ packed: import("o1js/dist/node/lib/field").Field[];
131
+ };
132
+ };