o1js-pack 0.4.1 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. package/.husky/pre-commit +0 -0
  2. package/README.md +8 -2
  3. package/build/src/index.d.ts +5 -5
  4. package/build/src/index.js +5 -5
  5. package/build/src/index.js.map +1 -1
  6. package/build/src/lib/PackingPlant.d.ts +132 -132
  7. package/build/src/lib/PackingPlant.js +190 -190
  8. package/build/src/lib/packed-types/PackedBool.d.ts +85 -85
  9. package/build/src/lib/packed-types/PackedBool.js +49 -49
  10. package/build/src/lib/packed-types/PackedBool.test.d.ts +1 -1
  11. package/build/src/lib/packed-types/PackedBool.test.js +75 -75
  12. package/build/src/lib/packed-types/PackedString.d.ts +181 -181
  13. package/build/src/lib/packed-types/PackedString.js +127 -127
  14. package/build/src/lib/packed-types/PackedString.test.d.ts +1 -1
  15. package/build/src/lib/packed-types/PackedString.test.js +100 -100
  16. package/build/src/lib/packed-types/PackedUInt32.d.ts +85 -85
  17. package/build/src/lib/packed-types/PackedUInt32.js +49 -49
  18. package/build/src/lib/packed-types/PackedUInt32.test.d.ts +1 -1
  19. package/build/src/lib/packed-types/PackedUInt32.test.js +83 -83
  20. package/examples/smart_contract/election/README.md +3 -0
  21. package/examples/smart_contract/election/contract.ts +79 -0
  22. package/examples/smart_contract/election/run.ts +98 -0
  23. package/examples/zk_program/age_gate/README.md +7 -0
  24. package/examples/zk_program/age_gate/circuit.ts +75 -0
  25. package/examples/zk_program/age_gate/non-packed-circuit.ts +68 -0
  26. package/examples/zk_program/age_gate/non-packed-run.ts +36 -0
  27. package/examples/zk_program/age_gate/run.ts +35 -0
  28. package/package.json +2 -4
@@ -1,191 +1,191 @@
1
- import { Field, Struct, 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) {
11
- super({ packed });
12
- }
13
- // Must implement these in type-specific implementation
14
- static extractField(input) {
15
- throw new Error('Must implement extractField');
16
- }
17
- static sizeInBits() {
18
- throw new Error('Must implement sizeInBits');
19
- }
20
- static unpack(f) {
21
- throw new Error('Must implement unpack');
22
- }
23
- // End
24
- /**
25
- *
26
- * @param unpacked Array of the implemented packed type
27
- * @throws if the length of the array is longer than the length of the implementing factory config
28
- */
29
- static checkPack(unpacked) {
30
- if (unpacked.length > l) {
31
- throw new Error(`Input of size ${unpacked.length} is larger than expected size of ${l}`);
32
- }
33
- }
34
- /**
35
- *
36
- * @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
37
- * @returns Field, packed with the information from the unpacked input
38
- */
39
- static pack(unpacked) {
40
- this.checkPack(unpacked);
41
- let f = this.extractField(unpacked[0]);
42
- const n = Math.min(unpacked.length, l);
43
- for (let i = 1; i < n; i++) {
44
- const c = Field((2n ** this.sizeInBits()) ** BigInt(i));
45
- f = f.add(this.extractField(unpacked[i]).mul(c));
46
- }
47
- return f;
48
- }
49
- /**
50
- *
51
- * @param f Field, packed with the information, as returned by #pack
52
- * @returns Array of bigints, which can be decoded by the implementing class into the final type
53
- */
54
- static unpackToBigints(f) {
55
- let unpacked = new Array(l);
56
- unpacked.fill(0n);
57
- let packedN;
58
- if (f) {
59
- packedN = f.toBigInt();
60
- }
61
- else {
62
- throw new Error('No Packed Value Provided');
63
- }
64
- for (let i = 0; i < l; i++) {
65
- unpacked[i] = packedN & ((1n << this.sizeInBits()) - 1n);
66
- packedN >>= this.sizeInBits();
67
- }
68
- return unpacked;
69
- }
70
- assertEquals(other) {
71
- this.packed.assertEquals(other.packed);
72
- }
73
- }
74
- Packed_.type = provable({ packed: Field }, {});
75
- Packed_.l = l;
76
- Packed_.bitSize = bitSize;
77
- return Packed_;
78
- }
79
- export function MultiPackingPlant(elementType, l, bitSize) {
80
- if (bitSize * BigInt(l) > 8n * MAX_BITS_PER_FIELD) {
81
- throw new Error(`The Packing Plant is only accepting orders that can fit into eight Fields`);
82
- }
83
- const n = Math.ceil(Number(bitSize * BigInt(l)) / Number(255n));
84
- class Packed_ extends Struct({
85
- packed: Provable.Array(Field, n),
86
- }) {
87
- constructor(packed) {
88
- super({ packed });
89
- }
90
- // Must implement these in type-specific implementation
91
- static extractField(input) {
92
- throw new Error('Must implement extractField');
93
- }
94
- static sizeInBits() {
95
- throw new Error('Must implement sizeInBits');
96
- }
97
- static elementsPerField() {
98
- throw new Error('Must implement elementsPerField');
99
- }
100
- static unpack(fields) {
101
- throw new Error('Must implement unpack');
102
- }
103
- // End
104
- /**
105
- *
106
- * @param unpacked Array of the implemented packed type
107
- * @throws if the length of the array is longer than the length of the implementing factory config
108
- */
109
- static checkPack(unpacked) {
110
- if (unpacked.length > this.l) {
111
- throw new Error(`Input of size ${unpacked.length} is larger than expected size of ${l}`);
112
- }
113
- }
114
- /**
115
- *
116
- * @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
117
- * @returns Array of Fields, packed such that each Field has as much information as possible
118
- *
119
- * e.g. 15 Characters pack into 1 Field. 15 or fewer Characters will return an array of 1 Field
120
- * 30 of fewer Characters will return an aray of 2 Fields
121
- */
122
- static pack(unpacked) {
123
- this.checkPack(unpacked);
124
- const q = this.elementsPerField();
125
- let fields = [];
126
- let mutableUnpacked = [...unpacked];
127
- for (let i = 0; i < this.n; i++) {
128
- let f = this.extractField(mutableUnpacked[i * q]);
129
- if (!f) {
130
- throw new Error('Unexpected Array Length');
131
- }
132
- for (let j = 1; j < q; j++) {
133
- const idx = i * q + j;
134
- let value = this.extractField(mutableUnpacked[idx]);
135
- if (!value) {
136
- throw new Error('Unexpected Array Length');
137
- }
138
- value = value || Field(0);
139
- const c = Field((2n ** this.sizeInBits()) ** BigInt(j));
140
- f = f.add(value.mul(c));
141
- }
142
- fields.push(f);
143
- }
144
- return fields;
145
- }
146
- /**
147
- *
148
- * @param fields Array of Fields, packed such that each Field has as much information as possible, as returned in #pack
149
- * @returns Array of bigints, which can be decoded by the implementing class into the final type
150
- */
151
- static unpackToBigints(fields) {
152
- let uints_ = new Array(this.l); // array length is number of elements per field * number of fields
153
- uints_.fill(0n);
154
- let packedNs = new Array(this.n);
155
- packedNs.fill(0n);
156
- const packedArg = new Array(this.n);
157
- packedArg.fill(Field(0), 0, this.n);
158
- for (let i = 0; i < this.n; i++) {
159
- if (fields[i]) {
160
- packedArg[i] = fields[i];
161
- }
162
- }
163
- if (packedArg.length !== this.n) {
164
- throw new Error(`Packed value must be exactly ${this.n} in length`);
165
- }
166
- for (let i = 0; i < this.n; i++) {
167
- packedNs[i] = packedArg[i].toConstant().toBigInt();
168
- }
169
- for (let i = 0; i < packedNs.length; i++) {
170
- let packedN = packedNs[i];
171
- for (let j = 0; j < this.elementsPerField(); j++) {
172
- const k = i * this.elementsPerField() + j;
173
- uints_[k] = packedN & ((1n << this.sizeInBits()) - 1n);
174
- packedN >>= this.sizeInBits();
175
- }
176
- }
177
- return uints_;
178
- }
179
- assertEquals(other) {
180
- for (let x = 0; x < n; x++) {
181
- this.packed[x].assertEquals(other.packed[x]);
182
- }
183
- }
184
- }
185
- Packed_.type = provable({ packed: Provable.Array(Field, n) }, {});
186
- Packed_.l = l;
187
- Packed_.n = n;
188
- Packed_.bitSize = bitSize;
189
- return Packed_;
190
- }
1
+ import { Field, Struct, 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) {
11
+ super({ packed });
12
+ }
13
+ // Must implement these in type-specific implementation
14
+ static extractField(input) {
15
+ throw new Error('Must implement extractField');
16
+ }
17
+ static sizeInBits() {
18
+ throw new Error('Must implement sizeInBits');
19
+ }
20
+ static unpack(f) {
21
+ throw new Error('Must implement unpack');
22
+ }
23
+ // End
24
+ /**
25
+ *
26
+ * @param unpacked Array of the implemented packed type
27
+ * @throws if the length of the array is longer than the length of the implementing factory config
28
+ */
29
+ static checkPack(unpacked) {
30
+ if (unpacked.length > l) {
31
+ throw new Error(`Input of size ${unpacked.length} is larger than expected size of ${l}`);
32
+ }
33
+ }
34
+ /**
35
+ *
36
+ * @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
37
+ * @returns Field, packed with the information from the unpacked input
38
+ */
39
+ static pack(unpacked) {
40
+ this.checkPack(unpacked);
41
+ let f = this.extractField(unpacked[0]);
42
+ const n = Math.min(unpacked.length, l);
43
+ for (let i = 1; i < n; i++) {
44
+ const c = Field((2n ** this.sizeInBits()) ** BigInt(i));
45
+ f = f.add(this.extractField(unpacked[i]).mul(c));
46
+ }
47
+ return f;
48
+ }
49
+ /**
50
+ *
51
+ * @param f Field, packed with the information, as returned by #pack
52
+ * @returns Array of bigints, which can be decoded by the implementing class into the final type
53
+ */
54
+ static unpackToBigints(f) {
55
+ let unpacked = new Array(l);
56
+ unpacked.fill(0n);
57
+ let packedN;
58
+ if (f) {
59
+ packedN = f.toBigInt();
60
+ }
61
+ else {
62
+ throw new Error('No Packed Value Provided');
63
+ }
64
+ for (let i = 0; i < l; i++) {
65
+ unpacked[i] = packedN & ((1n << this.sizeInBits()) - 1n);
66
+ packedN >>= this.sizeInBits();
67
+ }
68
+ return unpacked;
69
+ }
70
+ assertEquals(other) {
71
+ this.packed.assertEquals(other.packed);
72
+ }
73
+ }
74
+ Packed_.type = provable({ packed: Field }, {});
75
+ Packed_.l = l;
76
+ Packed_.bitSize = bitSize;
77
+ return Packed_;
78
+ }
79
+ export function MultiPackingPlant(elementType, l, bitSize) {
80
+ if (bitSize * BigInt(l) > 8n * MAX_BITS_PER_FIELD) {
81
+ throw new Error(`The Packing Plant is only accepting orders that can fit into eight Fields`);
82
+ }
83
+ const n = Math.ceil(Number(bitSize * BigInt(l)) / Number(255n));
84
+ class Packed_ extends Struct({
85
+ packed: Provable.Array(Field, n),
86
+ }) {
87
+ constructor(packed) {
88
+ super({ packed });
89
+ }
90
+ // Must implement these in type-specific implementation
91
+ static extractField(input) {
92
+ throw new Error('Must implement extractField');
93
+ }
94
+ static sizeInBits() {
95
+ throw new Error('Must implement sizeInBits');
96
+ }
97
+ static elementsPerField() {
98
+ throw new Error('Must implement elementsPerField');
99
+ }
100
+ static unpack(fields) {
101
+ throw new Error('Must implement unpack');
102
+ }
103
+ // End
104
+ /**
105
+ *
106
+ * @param unpacked Array of the implemented packed type
107
+ * @throws if the length of the array is longer than the length of the implementing factory config
108
+ */
109
+ static checkPack(unpacked) {
110
+ if (unpacked.length > this.l) {
111
+ throw new Error(`Input of size ${unpacked.length} is larger than expected size of ${l}`);
112
+ }
113
+ }
114
+ /**
115
+ *
116
+ * @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
117
+ * @returns Array of Fields, packed such that each Field has as much information as possible
118
+ *
119
+ * e.g. 15 Characters pack into 1 Field. 15 or fewer Characters will return an array of 1 Field
120
+ * 30 of fewer Characters will return an aray of 2 Fields
121
+ */
122
+ static pack(unpacked) {
123
+ this.checkPack(unpacked);
124
+ const q = this.elementsPerField();
125
+ let fields = [];
126
+ let mutableUnpacked = [...unpacked];
127
+ for (let i = 0; i < this.n; i++) {
128
+ let f = this.extractField(mutableUnpacked[i * q]);
129
+ if (!f) {
130
+ throw new Error('Unexpected Array Length');
131
+ }
132
+ for (let j = 1; j < q; j++) {
133
+ const idx = i * q + j;
134
+ let value = this.extractField(mutableUnpacked[idx]);
135
+ if (!value) {
136
+ throw new Error('Unexpected Array Length');
137
+ }
138
+ value = value || Field(0);
139
+ const c = Field((2n ** this.sizeInBits()) ** BigInt(j));
140
+ f = f.add(value.mul(c));
141
+ }
142
+ fields.push(f);
143
+ }
144
+ return fields;
145
+ }
146
+ /**
147
+ *
148
+ * @param fields Array of Fields, packed such that each Field has as much information as possible, as returned in #pack
149
+ * @returns Array of bigints, which can be decoded by the implementing class into the final type
150
+ */
151
+ static unpackToBigints(fields) {
152
+ let uints_ = new Array(this.l); // array length is number of elements per field * number of fields
153
+ uints_.fill(0n);
154
+ let packedNs = new Array(this.n);
155
+ packedNs.fill(0n);
156
+ const packedArg = new Array(this.n);
157
+ packedArg.fill(Field(0), 0, this.n);
158
+ for (let i = 0; i < this.n; i++) {
159
+ if (fields[i]) {
160
+ packedArg[i] = fields[i];
161
+ }
162
+ }
163
+ if (packedArg.length !== this.n) {
164
+ throw new Error(`Packed value must be exactly ${this.n} in length`);
165
+ }
166
+ for (let i = 0; i < this.n; i++) {
167
+ packedNs[i] = packedArg[i].toConstant().toBigInt();
168
+ }
169
+ for (let i = 0; i < packedNs.length; i++) {
170
+ let packedN = packedNs[i];
171
+ for (let j = 0; j < this.elementsPerField(); j++) {
172
+ const k = i * this.elementsPerField() + j;
173
+ uints_[k] = packedN & ((1n << this.sizeInBits()) - 1n);
174
+ packedN >>= this.sizeInBits();
175
+ }
176
+ }
177
+ return uints_;
178
+ }
179
+ assertEquals(other) {
180
+ for (let x = 0; x < n; x++) {
181
+ this.packed[x].assertEquals(other.packed[x]);
182
+ }
183
+ }
184
+ }
185
+ Packed_.type = provable({ packed: Provable.Array(Field, n) }, {});
186
+ Packed_.l = l;
187
+ Packed_.n = n;
188
+ Packed_.bitSize = bitSize;
189
+ return Packed_;
190
+ }
191
191
  //# sourceMappingURL=PackingPlant.js.map
@@ -1,85 +1,85 @@
1
- import { Field, Bool } from 'o1js';
2
- export declare function PackedBoolFactory(l?: number): {
3
- new (packed: import("o1js/dist/node/lib/field.js").Field): {
4
- toBooleans(): Array<boolean>;
5
- assertEquals(other: {
6
- assertEquals(other: any): void;
7
- packed: import("o1js/dist/node/lib/field.js").Field;
8
- }): void;
9
- packed: import("o1js/dist/node/lib/field.js").Field;
10
- };
11
- extractField(input: Bool): Field;
12
- sizeInBits(): bigint;
13
- /**
14
- *
15
- * @param f Field, packed with the information, as returned by #pack
16
- * @returns Array of Bool
17
- */
18
- unpack(f: Field): Bool[];
19
- /**
20
- *
21
- * @param bools Array of Bools to be packed
22
- * @returns Instance of the implementing class
23
- */
24
- fromBools(bools: Array<Bool>): {
25
- toBooleans(): Array<boolean>;
26
- assertEquals(other: {
27
- assertEquals(other: any): void;
28
- packed: import("o1js/dist/node/lib/field.js").Field;
29
- }): void;
30
- packed: import("o1js/dist/node/lib/field.js").Field;
31
- };
32
- /**
33
- *
34
- * @param booleans Array of booleans to be packed
35
- * @returns Instance of the implementing class
36
- */
37
- fromBooleans(booleans: Array<boolean>): {
38
- toBooleans(): Array<boolean>;
39
- assertEquals(other: {
40
- assertEquals(other: any): void;
41
- packed: import("o1js/dist/node/lib/field.js").Field;
42
- }): void;
43
- packed: import("o1js/dist/node/lib/field.js").Field;
44
- };
45
- type: import("o1js/dist/node/bindings/lib/provable-snarky.js").ProvableExtended<{
46
- packed: import("o1js/dist/node/lib/field.js").Field;
47
- }, {
48
- packed: string;
49
- }>;
50
- l: number;
51
- bitSize: bigint;
52
- checkPack(unpacked: import("o1js/dist/node/lib/bool.js").Bool[]): void;
53
- pack(unpacked: import("o1js/dist/node/lib/bool.js").Bool[]): import("o1js/dist/node/lib/field.js").Field;
54
- unpackToBigints(f: import("o1js/dist/node/lib/field.js").Field): bigint[];
55
- _isStruct: true;
56
- toFields: (value: {
57
- packed: import("o1js/dist/node/lib/field.js").Field;
58
- }) => import("o1js/dist/node/lib/field.js").Field[];
59
- toAuxiliary: (value?: {
60
- packed: import("o1js/dist/node/lib/field.js").Field;
61
- } | undefined) => any[];
62
- fromFields: (fields: import("o1js/dist/node/lib/field.js").Field[]) => {
63
- packed: import("o1js/dist/node/lib/field.js").Field;
64
- };
65
- sizeInFields(): number;
66
- check: (value: {
67
- packed: import("o1js/dist/node/lib/field.js").Field;
68
- }) => void;
69
- toInput: (x: {
70
- packed: import("o1js/dist/node/lib/field.js").Field;
71
- }) => {
72
- fields?: import("o1js/dist/node/lib/field.js").Field[] | undefined;
73
- packed?: [import("o1js/dist/node/lib/field.js").Field, number][] | undefined;
74
- };
75
- toJSON: (x: {
76
- packed: import("o1js/dist/node/lib/field.js").Field;
77
- }) => {
78
- packed: string;
79
- };
80
- fromJSON: (x: {
81
- packed: string;
82
- }) => {
83
- packed: import("o1js/dist/node/lib/field.js").Field;
84
- };
85
- };
1
+ import { Field, Bool } from 'o1js';
2
+ export declare function PackedBoolFactory(l?: number): {
3
+ new (packed: import("o1js/dist/node/lib/field.js").Field): {
4
+ toBooleans(): Array<boolean>;
5
+ assertEquals(other: {
6
+ assertEquals(other: any): void;
7
+ packed: import("o1js/dist/node/lib/field.js").Field;
8
+ }): void;
9
+ packed: import("o1js/dist/node/lib/field.js").Field;
10
+ };
11
+ extractField(input: Bool): Field;
12
+ sizeInBits(): bigint;
13
+ /**
14
+ *
15
+ * @param f Field, packed with the information, as returned by #pack
16
+ * @returns Array of Bool
17
+ */
18
+ unpack(f: Field): Bool[];
19
+ /**
20
+ *
21
+ * @param bools Array of Bools to be packed
22
+ * @returns Instance of the implementing class
23
+ */
24
+ fromBools(bools: Array<Bool>): {
25
+ toBooleans(): Array<boolean>;
26
+ assertEquals(other: {
27
+ assertEquals(other: any): void;
28
+ packed: import("o1js/dist/node/lib/field.js").Field;
29
+ }): void;
30
+ packed: import("o1js/dist/node/lib/field.js").Field;
31
+ };
32
+ /**
33
+ *
34
+ * @param booleans Array of booleans to be packed
35
+ * @returns Instance of the implementing class
36
+ */
37
+ fromBooleans(booleans: Array<boolean>): {
38
+ toBooleans(): Array<boolean>;
39
+ assertEquals(other: {
40
+ assertEquals(other: any): void;
41
+ packed: import("o1js/dist/node/lib/field.js").Field;
42
+ }): void;
43
+ packed: import("o1js/dist/node/lib/field.js").Field;
44
+ };
45
+ type: import("o1js/dist/node/bindings/lib/provable-snarky.js").ProvableExtended<{
46
+ packed: import("o1js/dist/node/lib/field.js").Field;
47
+ }, {
48
+ packed: string;
49
+ }>;
50
+ l: number;
51
+ bitSize: bigint;
52
+ checkPack(unpacked: import("o1js/dist/node/lib/bool.js").Bool[]): void;
53
+ pack(unpacked: import("o1js/dist/node/lib/bool.js").Bool[]): import("o1js/dist/node/lib/field.js").Field;
54
+ unpackToBigints(f: import("o1js/dist/node/lib/field.js").Field): bigint[];
55
+ _isStruct: true;
56
+ toFields: (value: {
57
+ packed: import("o1js/dist/node/lib/field.js").Field;
58
+ }) => import("o1js/dist/node/lib/field.js").Field[];
59
+ toAuxiliary: (value?: {
60
+ packed: import("o1js/dist/node/lib/field.js").Field;
61
+ } | undefined) => any[];
62
+ fromFields: (fields: import("o1js/dist/node/lib/field.js").Field[]) => {
63
+ packed: import("o1js/dist/node/lib/field.js").Field;
64
+ };
65
+ sizeInFields(): number;
66
+ check: (value: {
67
+ packed: import("o1js/dist/node/lib/field.js").Field;
68
+ }) => void;
69
+ toInput: (x: {
70
+ packed: import("o1js/dist/node/lib/field.js").Field;
71
+ }) => {
72
+ fields?: import("o1js/dist/node/lib/field.js").Field[] | undefined;
73
+ packed?: [import("o1js/dist/node/lib/field.js").Field, number][] | undefined;
74
+ };
75
+ toJSON: (x: {
76
+ packed: import("o1js/dist/node/lib/field.js").Field;
77
+ }) => {
78
+ packed: string;
79
+ };
80
+ fromJSON: (x: {
81
+ packed: string;
82
+ }) => {
83
+ packed: import("o1js/dist/node/lib/field.js").Field;
84
+ };
85
+ };
@@ -1,50 +1,50 @@
1
- import { Provable, Bool } from 'o1js';
2
- import { PackingPlant } from '../PackingPlant.js';
3
- const L = 254; // 254 1-bit booleans fit in one Field
4
- const SIZE_IN_BITS = 1n;
5
- export function PackedBoolFactory(l = L) {
6
- class PackedBool_ extends PackingPlant(Bool, l, SIZE_IN_BITS) {
7
- static extractField(input) {
8
- return input.toField();
9
- }
10
- static sizeInBits() {
11
- return SIZE_IN_BITS;
12
- }
13
- /**
14
- *
15
- * @param f Field, packed with the information, as returned by #pack
16
- * @returns Array of Bool
17
- */
18
- static unpack(f) {
19
- const unpacked = Provable.witness(Provable.Array(Bool, l), () => {
20
- const unpacked = this.unpackToBigints(f);
21
- return unpacked.map((x) => Bool.fromJSON(Boolean(x)));
22
- });
23
- f.assertEquals(PackedBool_.pack(unpacked));
24
- return unpacked;
25
- }
26
- /**
27
- *
28
- * @param bools Array of Bools to be packed
29
- * @returns Instance of the implementing class
30
- */
31
- static fromBools(bools) {
32
- const packed = PackedBool_.pack(bools);
33
- return new PackedBool_(packed);
34
- }
35
- /**
36
- *
37
- * @param booleans Array of booleans to be packed
38
- * @returns Instance of the implementing class
39
- */
40
- static fromBooleans(booleans) {
41
- const bools = booleans.map((x) => Bool(x));
42
- return PackedBool_.fromBools(bools);
43
- }
44
- toBooleans() {
45
- return PackedBool_.unpack(this.packed).map((x) => x.toBoolean());
46
- }
47
- }
48
- return PackedBool_;
49
- }
1
+ import { Provable, Bool } from 'o1js';
2
+ import { PackingPlant } from '../PackingPlant.js';
3
+ const L = 254; // 254 1-bit booleans fit in one Field
4
+ const SIZE_IN_BITS = 1n;
5
+ export function PackedBoolFactory(l = L) {
6
+ class PackedBool_ extends PackingPlant(Bool, l, SIZE_IN_BITS) {
7
+ static extractField(input) {
8
+ return input.toField();
9
+ }
10
+ static sizeInBits() {
11
+ return SIZE_IN_BITS;
12
+ }
13
+ /**
14
+ *
15
+ * @param f Field, packed with the information, as returned by #pack
16
+ * @returns Array of Bool
17
+ */
18
+ static unpack(f) {
19
+ const unpacked = Provable.witness(Provable.Array(Bool, l), () => {
20
+ const unpacked = this.unpackToBigints(f);
21
+ return unpacked.map((x) => Bool.fromJSON(Boolean(x)));
22
+ });
23
+ f.assertEquals(PackedBool_.pack(unpacked));
24
+ return unpacked;
25
+ }
26
+ /**
27
+ *
28
+ * @param bools Array of Bools to be packed
29
+ * @returns Instance of the implementing class
30
+ */
31
+ static fromBools(bools) {
32
+ const packed = PackedBool_.pack(bools);
33
+ return new PackedBool_(packed);
34
+ }
35
+ /**
36
+ *
37
+ * @param booleans Array of booleans to be packed
38
+ * @returns Instance of the implementing class
39
+ */
40
+ static fromBooleans(booleans) {
41
+ const bools = booleans.map((x) => Bool(x));
42
+ return PackedBool_.fromBools(bools);
43
+ }
44
+ toBooleans() {
45
+ return PackedBool_.unpack(this.packed).map((x) => x.toBoolean());
46
+ }
47
+ }
48
+ return PackedBool_;
49
+ }
50
50
  //# sourceMappingURL=PackedBool.js.map
@@ -1 +1 @@
1
- export {};
1
+ export {};