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.
- package/.husky/pre-commit +0 -0
- package/README.md +8 -2
- package/build/src/index.d.ts +5 -5
- package/build/src/index.js +5 -5
- package/build/src/index.js.map +1 -1
- package/build/src/lib/PackingPlant.d.ts +132 -132
- package/build/src/lib/PackingPlant.js +190 -190
- package/build/src/lib/packed-types/PackedBool.d.ts +85 -85
- package/build/src/lib/packed-types/PackedBool.js +49 -49
- 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/PackedString.d.ts +181 -181
- package/build/src/lib/packed-types/PackedString.js +127 -127
- package/build/src/lib/packed-types/PackedString.test.d.ts +1 -1
- package/build/src/lib/packed-types/PackedString.test.js +100 -100
- package/build/src/lib/packed-types/PackedUInt32.d.ts +85 -85
- package/build/src/lib/packed-types/PackedUInt32.js +49 -49
- package/build/src/lib/packed-types/PackedUInt32.test.d.ts +1 -1
- package/build/src/lib/packed-types/PackedUInt32.test.js +83 -83
- package/examples/smart_contract/election/README.md +3 -0
- package/examples/smart_contract/election/contract.ts +79 -0
- package/examples/smart_contract/election/run.ts +98 -0
- package/examples/zk_program/age_gate/README.md +7 -0
- package/examples/zk_program/age_gate/circuit.ts +75 -0
- package/examples/zk_program/age_gate/non-packed-circuit.ts +68 -0
- package/examples/zk_program/age_gate/non-packed-run.ts +36 -0
- package/examples/zk_program/age_gate/run.ts +35 -0
- package/package.json +2 -4
package/.husky/pre-commit
CHANGED
File without changes
|
package/README.md
CHANGED
@@ -8,12 +8,18 @@ o1js Pack is a library for [o1js](https://github.com/o1-labs/o1js/) that allows
|
|
8
8
|
|
9
9
|
### Usage in a zkApp
|
10
10
|
|
11
|
-
TODO
|
12
|
-
|
13
11
|
### Smart Contract
|
14
12
|
|
13
|
+
The primary benefit of using this library in a smart contract is that it allows you to use more of your allotted 8 Fields of storage.
|
14
|
+
|
15
|
+
Check out example usage in a smart contract in [Smart Contract Examples](/examples/smart_contract/).
|
16
|
+
|
15
17
|
### ZK Program
|
16
18
|
|
19
|
+
The benefit to using this library in a zk program is less obvious. In some cases, the number of gates required in a circuit is less using this library than with provable arrays. Especially in applications where the array is hashed many times, packing and unpacking ends up being more effecient than doing a lot of Poseidon hashes. In other cases, you may just prefer the API of o1js Pack compared to using provable arrays.
|
20
|
+
|
21
|
+
Check out example usage in a zk program in [Zk Program Examples](/examples/zk_program/).
|
22
|
+
|
17
23
|
## How to build
|
18
24
|
|
19
25
|
```sh
|
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, MultiPackedStringFactory } from './lib/packed-types/PackedString.js';
|
5
|
+
export { PackedBoolFactory, PackedStringFactory, MultiPackedStringFactory, 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, MultiPackedStringFactory, } from './lib/packed-types/PackedString.js';
|
5
|
+
export { PackedBoolFactory, PackedStringFactory, MultiPackedStringFactory, PackedUInt32Factory, PackingPlant, MultiPackingPlant, };
|
6
6
|
//# sourceMappingURL=index.js.map
|
package/build/src/index.js.map
CHANGED
@@ -1 +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,
|
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,EACL,mBAAmB,EACnB,wBAAwB,GACzB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,wBAAwB,EACxB,mBAAmB,EACnB,YAAY,EACZ,iBAAiB,GAClB,CAAC"}
|
@@ -1,132 +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
|
-
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
|
+
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
|
+
};
|