o1js-pack 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. package/.eslintrc.cjs +24 -0
  2. package/.gitattributes +3 -0
  3. package/.github/workflows/ci.yml +26 -0
  4. package/.husky/pre-commit +4 -0
  5. package/.prettierignore +14 -0
  6. package/.prettierrc +6 -0
  7. package/LICENSE +199 -0
  8. package/README.md +59 -0
  9. package/babel.config.cjs +3 -0
  10. package/build/src/index.d.ts +5 -0
  11. package/build/src/index.js +6 -0
  12. package/build/src/index.js.map +1 -0
  13. package/build/src/lib/PackingPlant.d.ts +92 -0
  14. package/build/src/lib/PackingPlant.js +75 -0
  15. package/build/src/lib/PackingPlant.js.map +1 -0
  16. package/build/src/lib/packed-types/PackedBool.d.ts +77 -0
  17. package/build/src/lib/packed-types/PackedBool.js +48 -0
  18. package/build/src/lib/packed-types/PackedBool.js.map +1 -0
  19. package/build/src/lib/packed-types/PackedBool.test.d.ts +1 -0
  20. package/build/src/lib/packed-types/PackedBool.test.js +76 -0
  21. package/build/src/lib/packed-types/PackedBool.test.js.map +1 -0
  22. package/build/src/lib/packed-types/PackedCharacter.d.ts +51 -0
  23. package/build/src/lib/packed-types/PackedCharacter.js +34 -0
  24. package/build/src/lib/packed-types/PackedCharacter.js.map +1 -0
  25. package/build/src/lib/packed-types/PackedCharacter.test.d.ts +1 -0
  26. package/build/src/lib/packed-types/PackedCharacter.test.js +31 -0
  27. package/build/src/lib/packed-types/PackedCharacter.test.js.map +1 -0
  28. package/build/src/lib/packed-types/PackedString.d.ts +78 -0
  29. package/build/src/lib/packed-types/PackedString.js +83 -0
  30. package/build/src/lib/packed-types/PackedString.js.map +1 -0
  31. package/build/src/lib/packed-types/PackedString.test.d.ts +1 -0
  32. package/build/src/lib/packed-types/PackedString.test.js +100 -0
  33. package/build/src/lib/packed-types/PackedString.test.js.map +1 -0
  34. package/build/src/lib/packed-types/PackedUInt32.d.ts +77 -0
  35. package/build/src/lib/packed-types/PackedUInt32.js +48 -0
  36. package/build/src/lib/packed-types/PackedUInt32.js.map +1 -0
  37. package/build/src/lib/packed-types/PackedUInt32.test.d.ts +1 -0
  38. package/build/src/lib/packed-types/PackedUInt32.test.js +84 -0
  39. package/build/src/lib/packed-types/PackedUInt32.test.js.map +1 -0
  40. package/build/src/lib/scratch.d.ts +1 -0
  41. package/build/src/lib/scratch.js +16 -0
  42. package/build/src/lib/scratch.js.map +1 -0
  43. package/build/src/packed-types/PackedBool.d.ts +51 -0
  44. package/build/src/packed-types/PackedBool.js +34 -0
  45. package/build/src/packed-types/PackedBool.js.map +1 -0
  46. package/build/src/packed-types/PackedBool.test.d.ts +1 -0
  47. package/build/src/packed-types/PackedBool.test.js +101 -0
  48. package/build/src/packed-types/PackedBool.test.js.map +1 -0
  49. package/build/src/packed-types/PackedCharacter.d.ts +51 -0
  50. package/build/src/packed-types/PackedCharacter.js +34 -0
  51. package/build/src/packed-types/PackedCharacter.js.map +1 -0
  52. package/build/src/packed-types/PackedCharacter.test.d.ts +1 -0
  53. package/build/src/packed-types/PackedCharacter.test.js +31 -0
  54. package/build/src/packed-types/PackedCharacter.test.js.map +1 -0
  55. package/build/src/packed-types/PackedUInt32.d.ts +51 -0
  56. package/build/src/packed-types/PackedUInt32.js +34 -0
  57. package/build/src/packed-types/PackedUInt32.js.map +1 -0
  58. package/build/src/packed-types/PackedUInt32.test.d.ts +1 -0
  59. package/build/src/packed-types/PackedUInt32.test.js +68 -0
  60. package/build/src/packed-types/PackedUInt32.test.js.map +1 -0
  61. package/build/src/packed-types/PackingPlant.d.ts +54 -0
  62. package/build/src/packed-types/PackingPlant.js +45 -0
  63. package/build/src/packed-types/PackingPlant.js.map +1 -0
  64. package/build/src/packed-types/scratch.d.ts +1 -0
  65. package/build/src/packed-types/scratch.js +5 -0
  66. package/build/src/packed-types/scratch.js.map +1 -0
  67. package/config.json +4 -0
  68. package/coverage/clover.xml +166 -0
  69. package/coverage/coverage-final.json +5 -0
  70. package/coverage/lcov-report/PackedBool.ts.html +268 -0
  71. package/coverage/lcov-report/PackedUInt32.ts.html +313 -0
  72. package/coverage/lcov-report/base.css +224 -0
  73. package/coverage/lcov-report/block-navigation.js +87 -0
  74. package/coverage/lcov-report/favicon.png +0 -0
  75. package/coverage/lcov-report/index.html +131 -0
  76. package/coverage/lcov-report/lib/PackingPlant.ts.html +478 -0
  77. package/coverage/lcov-report/lib/index.html +116 -0
  78. package/coverage/lcov-report/lib/packed-types/PackedBool.ts.html +238 -0
  79. package/coverage/lcov-report/lib/packed-types/PackedString.ts.html +364 -0
  80. package/coverage/lcov-report/lib/packed-types/PackedUInt32.ts.html +238 -0
  81. package/coverage/lcov-report/lib/packed-types/index.html +146 -0
  82. package/coverage/lcov-report/prettify.css +1 -0
  83. package/coverage/lcov-report/prettify.js +2 -0
  84. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  85. package/coverage/lcov-report/sorter.js +196 -0
  86. package/coverage/lcov.info +282 -0
  87. package/jest-resolver.cjs +21 -0
  88. package/jest.config.js +24 -0
  89. package/package.json +50 -0
  90. package/tsconfig.json +23 -0
package/.eslintrc.cjs ADDED
@@ -0,0 +1,24 @@
1
+ module.exports = {
2
+ root: true,
3
+ env: {
4
+ browser: true,
5
+ node: true,
6
+ jest: true,
7
+ },
8
+ extends: [
9
+ 'eslint:recommended',
10
+ 'plugin:@typescript-eslint/eslint-recommended',
11
+ 'plugin:@typescript-eslint/recommended',
12
+ 'plugin:o1js/recommended',
13
+ ],
14
+ parser: '@typescript-eslint/parser',
15
+ parserOptions: {
16
+ ecmaVersion: 'latest',
17
+ },
18
+ plugins: ['@typescript-eslint', 'o1js'],
19
+ ignorePatterns: ['README.md'],
20
+ rules: {
21
+ 'no-constant-condition': 'off',
22
+ 'prefer-const': 'off',
23
+ },
24
+ };
package/.gitattributes ADDED
@@ -0,0 +1,3 @@
1
+ # Use line endings appropriate for the system. This prevents Git from
2
+ # complaining about project template line endings when committing on Windows.
3
+ * text=auto eol=lf
@@ -0,0 +1,26 @@
1
+ #
2
+ # ci.yml
3
+ #
4
+ # Run tests for all pushed commits and opened pull requests on Github.
5
+ #
6
+
7
+ name: CI
8
+ on: [push, pull_request]
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ timeout-minutes: 10
13
+ steps:
14
+ - name: Set up NodeJS
15
+ uses: actions/setup-node@v2
16
+ with:
17
+ node-version: '16'
18
+ - name: Git checkout
19
+ uses: actions/checkout@v2
20
+ - name: NPM ci, build, & test
21
+ run: |
22
+ npm install
23
+ npm run build --if-present
24
+ npm test
25
+ env:
26
+ CI: true
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ npx lint-staged
@@ -0,0 +1,14 @@
1
+ # NodeJS
2
+ node_modules
3
+ build
4
+ coverage
5
+ .husky
6
+
7
+ # Editor
8
+ .vscode
9
+
10
+ # System
11
+ .DS_Store
12
+
13
+ # Misc
14
+ LICENSE
package/.prettierrc ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": true,
4
+ "tabWidth": 2,
5
+ "trailingComma": "es5"
6
+ }
package/LICENSE ADDED
@@ -0,0 +1,199 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Licensed under the Apache License, Version 2.0 (the "License");
190
+ you may not use this file except in compliance with the License.
191
+ You may obtain a copy of the License at
192
+
193
+ http://www.apache.org/licenses/LICENSE-2.0
194
+
195
+ Unless required by applicable law or agreed to in writing, software
196
+ distributed under the License is distributed on an "AS IS" BASIS,
197
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
198
+ See the License for the specific language governing permissions and
199
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # O1JS Pack
2
+
3
+ Make the most of your ZKApp state!
4
+
5
+ ## What is it
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.
8
+
9
+ ### Usage in a ZKApp
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
+ ```
25
+
26
+ ## What's the catch?
27
+
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.
31
+
32
+ ## How to build
33
+
34
+ ```sh
35
+ npm run build
36
+ ```
37
+
38
+ ## How to run tests
39
+
40
+ ```sh
41
+ 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
49
+ ```
50
+
51
+ ## Credits
52
+
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.
54
+
55
+ Thanks to @iam-dev for early adoption!
56
+
57
+ ## License
58
+
59
+ [Apache-2.0](LICENSE)
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
3
+ };
@@ -0,0 +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, };
@@ -0,0 +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, };
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AAEzE,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,YAAY,EACZ,iBAAiB,GAClB,CAAC"}
@@ -0,0 +1,92 @@
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, aux: Array<T>) => {
3
+ packed: Field;
4
+ aux: Array<T>;
5
+ bitSize: bigint;
6
+ assertEquals(other: any): void;
7
+ }) & {
8
+ type: import("o1js/dist/node/bindings/lib/provable-snarky").ProvableExtended<{
9
+ packed: import("o1js/dist/node/lib/field").Field;
10
+ }, {
11
+ packed: string;
12
+ }>;
13
+ l: number;
14
+ toAuxiliary(value?: {
15
+ packed: Field;
16
+ } | undefined): Array<T>;
17
+ pack(aux: Array<T>): Field;
18
+ unpack(f: Field): T[];
19
+ _isStruct: true;
20
+ toFields: (value: {
21
+ packed: import("o1js/dist/node/lib/field").Field;
22
+ }) => import("o1js/dist/node/lib/field").Field[];
23
+ fromFields: (fields: import("o1js/dist/node/lib/field").Field[]) => {
24
+ packed: import("o1js/dist/node/lib/field").Field;
25
+ };
26
+ sizeInFields(): number;
27
+ check: (value: {
28
+ packed: import("o1js/dist/node/lib/field").Field;
29
+ }) => void;
30
+ toInput: (x: {
31
+ packed: import("o1js/dist/node/lib/field").Field;
32
+ }) => {
33
+ fields?: import("o1js/dist/node/lib/field").Field[] | undefined;
34
+ packed?: [import("o1js/dist/node/lib/field").Field, number][] | undefined;
35
+ };
36
+ toJSON: (x: {
37
+ packed: import("o1js/dist/node/lib/field").Field;
38
+ }) => {
39
+ packed: string;
40
+ };
41
+ fromJSON: (x: {
42
+ packed: string;
43
+ }) => {
44
+ packed: import("o1js/dist/node/lib/field").Field;
45
+ };
46
+ };
47
+ export declare function MultiPackingPlant<A, T extends InferProvable<A> = InferProvable<A>>(elementType: A, l: number, bitSize: bigint): (abstract new (packed: Array<Field>, aux: Array<T>) => {
48
+ packed: Array<Field>;
49
+ aux: Array<T>;
50
+ bitSize: bigint;
51
+ assertEquals(other: any): void;
52
+ }) & {
53
+ type: import("o1js/dist/node/bindings/lib/provable-snarky").ProvableExtended<{
54
+ packed: import("o1js/dist/node/lib/field").Field[];
55
+ }, {
56
+ packed: string[];
57
+ }>;
58
+ l: number;
59
+ n: number;
60
+ toAuxiliary(value?: {
61
+ packed: Array<Field>;
62
+ } | undefined): Array<T>;
63
+ pack(aux: Array<T>): Array<Field>;
64
+ unpack(fields: Array<Field>): T[];
65
+ _isStruct: true;
66
+ toFields: (value: {
67
+ packed: import("o1js/dist/node/lib/field").Field[];
68
+ }) => import("o1js/dist/node/lib/field").Field[];
69
+ fromFields: (fields: import("o1js/dist/node/lib/field").Field[]) => {
70
+ packed: import("o1js/dist/node/lib/field").Field[];
71
+ };
72
+ sizeInFields(): number;
73
+ check: (value: {
74
+ packed: import("o1js/dist/node/lib/field").Field[];
75
+ }) => void;
76
+ toInput: (x: {
77
+ packed: import("o1js/dist/node/lib/field").Field[];
78
+ }) => {
79
+ fields?: import("o1js/dist/node/lib/field").Field[] | undefined;
80
+ packed?: [import("o1js/dist/node/lib/field").Field, number][] | undefined;
81
+ };
82
+ toJSON: (x: {
83
+ packed: import("o1js/dist/node/lib/field").Field[];
84
+ }) => {
85
+ packed: string[];
86
+ };
87
+ fromJSON: (x: {
88
+ packed: string[];
89
+ }) => {
90
+ packed: import("o1js/dist/node/lib/field").Field[];
91
+ };
92
+ };
@@ -0,0 +1,75 @@
1
+ import { Field, Struct, Poseidon, provable, Provable, } from 'o1js';
2
+ const MAX_BITS_PER_FIELD = 254n;
3
+ export function PackingPlant(elementType, l, bitSize) {
4
+ if (bitSize * BigInt(l) > MAX_BITS_PER_FIELD) {
5
+ throw new Error(`The Packing Plant is only accepting orders that can fit into one Field, try using MultiPackingPlant`);
6
+ }
7
+ class Packed_ extends Struct({
8
+ packed: Field,
9
+ }) {
10
+ constructor(packed, aux) {
11
+ if (aux.length > l) {
12
+ throw new Error(`Length of aux data is too long, input of size ${aux.length} is larger than max allowed ${l}`);
13
+ }
14
+ super({ packed });
15
+ this.bitSize = bitSize;
16
+ this.aux = aux;
17
+ }
18
+ static toAuxiliary(value) {
19
+ throw new Error('Must implement toAuxiliary');
20
+ return [];
21
+ }
22
+ static pack(aux) {
23
+ throw new Error('Must implement pack');
24
+ let f = Field(0);
25
+ return f;
26
+ }
27
+ static unpack(f) {
28
+ return this.toAuxiliary({ packed: f });
29
+ }
30
+ assertEquals(other) {
31
+ this.packed.assertEquals(other.packed);
32
+ }
33
+ }
34
+ Packed_.type = provable({ packed: Field }, {});
35
+ Packed_.l = l;
36
+ return Packed_;
37
+ }
38
+ export function MultiPackingPlant(elementType, l, bitSize) {
39
+ if (bitSize * BigInt(l) > 8n * MAX_BITS_PER_FIELD) {
40
+ throw new Error(`The Packing Plant is only accepting orders that can fit into eight Fields`);
41
+ }
42
+ const n = Math.ceil(Number(bitSize * BigInt(l)) / Number(255n));
43
+ class Packed_ extends Struct({
44
+ packed: Provable.Array(Field, n),
45
+ }) {
46
+ constructor(packed, aux) {
47
+ if (aux.length > l) {
48
+ throw new Error('Length of aux data is too long');
49
+ }
50
+ super({ packed });
51
+ this.bitSize = bitSize;
52
+ this.aux = aux;
53
+ }
54
+ static toAuxiliary(value) {
55
+ throw new Error('Must implement toAuxiliary');
56
+ return [];
57
+ }
58
+ static pack(aux) {
59
+ throw new Error('Must implement pack');
60
+ let f = [Field(0)];
61
+ return f;
62
+ }
63
+ static unpack(fields) {
64
+ return this.toAuxiliary({ packed: fields });
65
+ }
66
+ assertEquals(other) {
67
+ Poseidon.hash(this.packed).assertEquals(Poseidon.hash(other.packed));
68
+ }
69
+ }
70
+ Packed_.type = provable({ packed: Provable.Array(Field, n) }, {});
71
+ Packed_.l = l;
72
+ Packed_.n = n;
73
+ return Packed_;
74
+ }
75
+ //# sourceMappingURL=PackingPlant.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PackingPlant.js","sourceRoot":"","sources":["../../../src/lib/PackingPlant.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,MAAM,EACN,QAAQ,EACR,QAAQ,EAER,QAAQ,GACT,MAAM,MAAM,CAAC;AAEd,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAEhC,MAAM,UAAU,YAAY,CAC1B,WAAc,EACd,CAAS,EACT,OAAe;IAEf,IAAI,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,kBAAkB,EAAE;QAC5C,MAAM,IAAI,KAAK,CACb,qGAAqG,CACtG,CAAC;KACH;IACD,MAAe,OAAQ,SAAQ,MAAM,CAAC;QACpC,MAAM,EAAE,KAAK;KACd,CAAC;QAOA,YAAY,MAAa,EAAE,GAAa;YACtC,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE;gBAClB,MAAM,IAAI,KAAK,CACb,iDAAiD,GAAG,CAAC,MAAM,+BAA+B,CAAC,EAAE,CAC9F,CAAC;aACH;YACD,KAAK,CAAC,EAAE,MAAM,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field.js").Field, aux: import("o1js/dist/node/lib/bool.js").Bool[]): {
4
+ toBooleans(): Array<boolean>;
5
+ packed: import("o1js/dist/node/lib/field.js").Field;
6
+ aux: import("o1js/dist/node/lib/bool.js").Bool[];
7
+ bitSize: bigint;
8
+ assertEquals(other: {
9
+ packed: import("o1js/dist/node/lib/field.js").Field;
10
+ aux: import("o1js/dist/node/lib/bool.js").Bool[];
11
+ bitSize: bigint;
12
+ assertEquals(other: any): void;
13
+ }): void;
14
+ };
15
+ toAuxiliary(value?: {
16
+ packed: Field;
17
+ } | undefined): Array<Bool>;
18
+ pack(aux: Array<Bool>): Field;
19
+ fromAuxiliary(aux: Array<Bool>): {
20
+ toBooleans(): Array<boolean>;
21
+ packed: import("o1js/dist/node/lib/field.js").Field;
22
+ aux: import("o1js/dist/node/lib/bool.js").Bool[];
23
+ bitSize: bigint;
24
+ assertEquals(other: {
25
+ packed: import("o1js/dist/node/lib/field.js").Field;
26
+ aux: import("o1js/dist/node/lib/bool.js").Bool[];
27
+ bitSize: bigint;
28
+ assertEquals(other: any): void;
29
+ }): void;
30
+ };
31
+ fromBooleans(bigints: Array<boolean>): {
32
+ toBooleans(): Array<boolean>;
33
+ packed: import("o1js/dist/node/lib/field.js").Field;
34
+ aux: import("o1js/dist/node/lib/bool.js").Bool[];
35
+ bitSize: bigint;
36
+ assertEquals(other: {
37
+ packed: import("o1js/dist/node/lib/field.js").Field;
38
+ aux: import("o1js/dist/node/lib/bool.js").Bool[];
39
+ bitSize: bigint;
40
+ assertEquals(other: any): void;
41
+ }): void;
42
+ };
43
+ type: import("o1js/dist/node/bindings/lib/provable-snarky.js").ProvableExtended<{
44
+ packed: import("o1js/dist/node/lib/field.js").Field;
45
+ }, {
46
+ packed: string;
47
+ }>;
48
+ l: number;
49
+ unpack(f: import("o1js/dist/node/lib/field.js").Field): import("o1js/dist/node/lib/bool.js").Bool[];
50
+ _isStruct: true;
51
+ toFields: (value: {
52
+ packed: import("o1js/dist/node/lib/field.js").Field;
53
+ }) => import("o1js/dist/node/lib/field.js").Field[];
54
+ fromFields: (fields: import("o1js/dist/node/lib/field.js").Field[]) => {
55
+ packed: import("o1js/dist/node/lib/field.js").Field;
56
+ };
57
+ sizeInFields(): number;
58
+ check: (value: {
59
+ packed: import("o1js/dist/node/lib/field.js").Field;
60
+ }) => void;
61
+ toInput: (x: {
62
+ packed: import("o1js/dist/node/lib/field.js").Field;
63
+ }) => {
64
+ fields?: import("o1js/dist/node/lib/field.js").Field[] | undefined;
65
+ packed?: [import("o1js/dist/node/lib/field.js").Field, number][] | undefined;
66
+ };
67
+ toJSON: (x: {
68
+ packed: import("o1js/dist/node/lib/field.js").Field;
69
+ }) => {
70
+ packed: string;
71
+ };
72
+ fromJSON: (x: {
73
+ packed: string;
74
+ }) => {
75
+ packed: import("o1js/dist/node/lib/field.js").Field;
76
+ };
77
+ };
@@ -0,0 +1,48 @@
1
+ import { Field, Provable, Bool } from 'o1js';
2
+ import { PackingPlant } from '../PackingPlant.js';
3
+ const SIZE_IN_BITS = 1n;
4
+ export function PackedBoolFactory(l) {
5
+ class PackedBool_ extends PackingPlant(Bool, l, SIZE_IN_BITS) {
6
+ static toAuxiliary(value) {
7
+ const auxiliary = Provable.witness(Provable.Array(Bool, l), () => {
8
+ let bools_ = new Array(l);
9
+ bools_.fill(0n);
10
+ let packedN;
11
+ if (value && value.packed) {
12
+ packedN = value.packed.toBigInt();
13
+ }
14
+ else {
15
+ throw new Error('No Packed Value Provided');
16
+ }
17
+ for (let i = 0; i < l; i++) {
18
+ bools_[i] = packedN & ((1n << SIZE_IN_BITS) - 1n);
19
+ packedN >>= SIZE_IN_BITS;
20
+ }
21
+ return bools_.map((x) => Bool.fromJSON(Boolean(x)));
22
+ });
23
+ return auxiliary;
24
+ }
25
+ static pack(aux) {
26
+ let f = aux[0].toField();
27
+ const n = Math.min(aux.length, l);
28
+ for (let i = 1; i < n; i++) {
29
+ const c = Field((2n ** SIZE_IN_BITS) ** BigInt(i));
30
+ f = f.add(aux[i].toField().mul(c));
31
+ }
32
+ return f;
33
+ }
34
+ static fromAuxiliary(aux) {
35
+ const packed = PackedBool_.pack(aux);
36
+ return new PackedBool_(packed, aux);
37
+ }
38
+ static fromBooleans(bigints) {
39
+ const uint32s = bigints.map((x) => Bool(x));
40
+ return this.fromAuxiliary(uint32s);
41
+ }
42
+ toBooleans() {
43
+ return PackedBool_.unpack(this.packed).map((x) => x.toBoolean());
44
+ }
45
+ }
46
+ return PackedBool_;
47
+ }
48
+ //# sourceMappingURL=PackedBool.js.map