o1js-pack 0.3.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- package/.github/workflows/ci.yml +1 -1
- package/.husky/pre-commit +0 -0
- package/README.md +9 -30
- package/build/src/index.d.ts +5 -5
- package/build/src/index.js +5 -5
- package/build/src/lib/PackingPlant.d.ts +132 -92
- package/build/src/lib/PackingPlant.js +190 -74
- package/build/src/lib/PackingPlant.js.map +1 -1
- package/build/src/lib/packed-types/PackedBool.d.ts +85 -77
- package/build/src/lib/packed-types/PackedBool.js +49 -47
- package/build/src/lib/packed-types/PackedBool.js.map +1 -1
- package/build/src/lib/packed-types/PackedBool.test.d.ts +1 -1
- package/build/src/lib/packed-types/PackedBool.test.js +75 -75
- package/build/src/lib/packed-types/PackedBool.test.js.map +1 -1
- package/build/src/lib/packed-types/PackedString.d.ts +181 -78
- package/build/src/lib/packed-types/PackedString.js +127 -82
- package/build/src/lib/packed-types/PackedString.js.map +1 -1
- package/build/src/lib/packed-types/PackedString.test.d.ts +1 -1
- package/build/src/lib/packed-types/PackedString.test.js +100 -99
- package/build/src/lib/packed-types/PackedString.test.js.map +1 -1
- package/build/src/lib/packed-types/PackedUInt32.d.ts +85 -77
- package/build/src/lib/packed-types/PackedUInt32.js +49 -47
- package/build/src/lib/packed-types/PackedUInt32.js.map +1 -1
- package/build/src/lib/packed-types/PackedUInt32.test.d.ts +1 -1
- package/build/src/lib/packed-types/PackedUInt32.test.js +83 -83
- package/build/src/lib/packed-types/PackedUInt32.test.js.map +1 -1
- package/package.json +1 -1
- package/tests/provable/end_to_end.test.ts +65 -0
- package/tests/provable/example_packed_string_circuit.ts +47 -0
- package/tests/provable/example_packed_uint_circuit.ts +39 -0
- package/build/src/lib/packed-types/PackedCharacter.d.ts +0 -51
- package/build/src/lib/packed-types/PackedCharacter.js +0 -34
- package/build/src/lib/packed-types/PackedCharacter.js.map +0 -1
- package/build/src/lib/packed-types/PackedCharacter.test.d.ts +0 -1
- package/build/src/lib/packed-types/PackedCharacter.test.js +0 -31
- package/build/src/lib/packed-types/PackedCharacter.test.js.map +0 -1
- package/build/src/lib/scratch.d.ts +0 -1
- package/build/src/lib/scratch.js +0 -16
- package/build/src/lib/scratch.js.map +0 -1
- package/build/src/packed-types/PackedBool.d.ts +0 -51
- package/build/src/packed-types/PackedBool.js +0 -34
- package/build/src/packed-types/PackedBool.js.map +0 -1
- package/build/src/packed-types/PackedBool.test.d.ts +0 -1
- package/build/src/packed-types/PackedBool.test.js +0 -101
- package/build/src/packed-types/PackedBool.test.js.map +0 -1
- package/build/src/packed-types/PackedCharacter.d.ts +0 -51
- package/build/src/packed-types/PackedCharacter.js +0 -34
- package/build/src/packed-types/PackedCharacter.js.map +0 -1
- package/build/src/packed-types/PackedCharacter.test.d.ts +0 -1
- package/build/src/packed-types/PackedCharacter.test.js +0 -31
- package/build/src/packed-types/PackedCharacter.test.js.map +0 -1
- package/build/src/packed-types/PackedUInt32.d.ts +0 -51
- package/build/src/packed-types/PackedUInt32.js +0 -34
- package/build/src/packed-types/PackedUInt32.js.map +0 -1
- package/build/src/packed-types/PackedUInt32.test.d.ts +0 -1
- package/build/src/packed-types/PackedUInt32.test.js +0 -68
- package/build/src/packed-types/PackedUInt32.test.js.map +0 -1
- package/build/src/packed-types/PackingPlant.d.ts +0 -54
- package/build/src/packed-types/PackingPlant.js +0 -45
- package/build/src/packed-types/PackingPlant.js.map +0 -1
- package/build/src/packed-types/scratch.d.ts +0 -1
- package/build/src/packed-types/scratch.js +0 -5
- package/build/src/packed-types/scratch.js.map +0 -1
- package/coverage/clover.xml +0 -166
- package/coverage/coverage-final.json +0 -5
- package/coverage/lcov-report/PackedBool.ts.html +0 -268
- package/coverage/lcov-report/PackedUInt32.ts.html +0 -313
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -131
- package/coverage/lcov-report/lib/PackingPlant.ts.html +0 -478
- package/coverage/lcov-report/lib/index.html +0 -116
- package/coverage/lcov-report/lib/packed-types/PackedBool.ts.html +0 -238
- package/coverage/lcov-report/lib/packed-types/PackedString.ts.html +0 -364
- package/coverage/lcov-report/lib/packed-types/PackedUInt32.ts.html +0 -238
- package/coverage/lcov-report/lib/packed-types/index.html +0 -146
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -196
- package/coverage/lcov.info +0 -282
package/.github/workflows/ci.yml
CHANGED
package/.husky/pre-commit
CHANGED
File without changes
|
package/README.md
CHANGED
@@ -1,33 +1,18 @@
|
|
1
|
-
#
|
1
|
+
# o1js Pack
|
2
2
|
|
3
|
-
Make the most of your
|
3
|
+
Make the most of your zkApp state!
|
4
4
|
|
5
5
|
## What is it
|
6
6
|
|
7
|
-
|
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
|
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
|
-
|
13
|
+
### Smart Contract
|
27
14
|
|
28
|
-
|
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
|
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
|
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
|
|
package/build/src/index.d.ts
CHANGED
@@ -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, };
|
package/build/src/index.js
CHANGED
@@ -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,92 +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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
packed:
|
10
|
-
}
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
packed:
|
40
|
-
};
|
41
|
-
|
42
|
-
packed:
|
43
|
-
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
packed: import("o1js/dist/node/lib/field").Field[];
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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) => {
|
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
|
+
};
|
@@ -1,75 +1,191 @@
|
|
1
|
-
import { Field, Struct,
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
static
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
}
|
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
|
+
}
|
75
191
|
//# sourceMappingURL=PackingPlant.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"PackingPlant.js","sourceRoot":"","sources":["../../../src/lib/PackingPlant.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,MAAM,
|
1
|
+
{"version":3,"file":"PackingPlant.js","sourceRoot":"","sources":["../../../src/lib/PackingPlant.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,MAAM,EAEN,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;QAKA,YAAY,MAAa;YACvB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACpB,CAAC;QAED,uDAAuD;QACvD,MAAM,CAAC,YAAY,CAAC,KAAQ;YAC1B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;QACD,MAAM,CAAC,UAAU;YACf,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,CAAC;QACD,MAAM,CAAC,MAAM,CAAC,CAAQ;YACpB,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM;QAEN;;;;WAIG;QACH,MAAM,CAAC,SAAS,CAAC,QAAkB;YACjC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;gBACvB,MAAM,IAAI,KAAK,CACb,iBAAiB,QAAQ,CAAC,MAAM,oCAAoC,CAAC,EAAE,CACxE,CAAC;aACH;QACH,CAAC;QAED;;;;WAIG;QACH,MAAM,CAAC,IAAI,CAAC,QAAkB;YAC5B,IAAI,CAAC,SA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