o1js-pack 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- 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 -140
- package/build/src/lib/PackingPlant.js +190 -191
- package/build/src/lib/PackingPlant.js.map +1 -1
- package/build/src/lib/packed-types/PackedBool.d.ts +85 -90
- package/build/src/lib/packed-types/PackedBool.js +49 -48
- 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/PackedString.d.ts +181 -92
- package/build/src/lib/packed-types/PackedString.js +127 -59
- 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 -90
- package/build/src/lib/packed-types/PackedUInt32.js +49 -48
- 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/package.json +1 -1
- package/tests/provable/end_to_end.test.ts +2 -4
- package/tests/provable/example_packed_string_circuit.ts +3 -8
- package/tests/provable/example_packed_uint_circuit.ts +6 -12
- 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
@@ -1,60 +1,128 @@
|
|
1
|
-
import { Field, Provable, Character } from 'o1js';
|
2
|
-
import { MultiPackingPlant } from '../PackingPlant.js';
|
3
|
-
const SIZE_IN_BITS = 16n;
|
4
|
-
const L = 15; // Default to one-field worth of characters
|
5
|
-
const CHARS_PER_FIELD = 15;
|
6
|
-
export function PackedStringFactory(l = L) {
|
7
|
-
class PackedString_ extends
|
8
|
-
static extractField(input) {
|
9
|
-
return input.value;
|
10
|
-
}
|
11
|
-
static sizeInBits() {
|
12
|
-
return SIZE_IN_BITS;
|
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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
}
|
1
|
+
import { Field, Provable, Character, Poseidon } from 'o1js';
|
2
|
+
import { PackingPlant, MultiPackingPlant } from '../PackingPlant.js';
|
3
|
+
const SIZE_IN_BITS = 16n;
|
4
|
+
const L = 15; // Default to one-field worth of characters
|
5
|
+
const CHARS_PER_FIELD = 15;
|
6
|
+
export function PackedStringFactory(l = L) {
|
7
|
+
class PackedString_ extends PackingPlant(Character, l, SIZE_IN_BITS) {
|
8
|
+
static extractField(input) {
|
9
|
+
return input.value;
|
10
|
+
}
|
11
|
+
static sizeInBits() {
|
12
|
+
return SIZE_IN_BITS;
|
13
|
+
}
|
14
|
+
/**
|
15
|
+
*
|
16
|
+
* @param f Field, packed with the information, as returned by #pack
|
17
|
+
* @returns Array of Character
|
18
|
+
*/
|
19
|
+
static unpack(f) {
|
20
|
+
const unpacked = Provable.witness(Provable.Array(Character, l), () => {
|
21
|
+
const unpacked = this.unpackToBigints(f);
|
22
|
+
return unpacked.map((x) => Character.fromString(String.fromCharCode(Number(x))));
|
23
|
+
});
|
24
|
+
f.assertEquals(PackedString_.pack(unpacked));
|
25
|
+
return unpacked;
|
26
|
+
}
|
27
|
+
/**
|
28
|
+
*
|
29
|
+
* @param characters Array of Character to be packed
|
30
|
+
* @returns Instance of the implementing class
|
31
|
+
*/
|
32
|
+
static fromCharacters(input) {
|
33
|
+
let characters = new Array(this.l);
|
34
|
+
characters.fill(new Character(Field(0)), 0, this.l);
|
35
|
+
for (let i = 0; i < input.length; i++) {
|
36
|
+
characters[i] = input[i];
|
37
|
+
}
|
38
|
+
const packed = this.pack(characters);
|
39
|
+
return new PackedString_(packed);
|
40
|
+
}
|
41
|
+
/**
|
42
|
+
*
|
43
|
+
* @param str string to be packed
|
44
|
+
* @returns Instance of the implementing class
|
45
|
+
*/
|
46
|
+
static fromString(str) {
|
47
|
+
let characters = new Array(this.l);
|
48
|
+
characters.fill(new Character(Field(0)), 0, this.l);
|
49
|
+
for (let i = 0; i < str.length; i++) {
|
50
|
+
characters[i] = Character.fromString(str[i]);
|
51
|
+
}
|
52
|
+
return this.fromCharacters(characters);
|
53
|
+
}
|
54
|
+
toString() {
|
55
|
+
const nullChar = String.fromCharCode(0);
|
56
|
+
return PackedString_.unpack(this.packed)
|
57
|
+
.filter((c) => c.toString() !== nullChar)
|
58
|
+
.join('');
|
59
|
+
}
|
60
|
+
}
|
61
|
+
return PackedString_;
|
62
|
+
}
|
63
|
+
/**
|
64
|
+
*
|
65
|
+
* @param n number of fields to employ to store the string
|
66
|
+
* @returns MultiPackedString_ class with length of n * CHARS_PER_FIELD
|
67
|
+
*/
|
68
|
+
export function MultiPackedStringFactory(n = 8) {
|
69
|
+
class MultiPackedString_ extends MultiPackingPlant(Character, n * CHARS_PER_FIELD, SIZE_IN_BITS) {
|
70
|
+
static extractField(input) {
|
71
|
+
return input.value;
|
72
|
+
}
|
73
|
+
static sizeInBits() {
|
74
|
+
return SIZE_IN_BITS;
|
75
|
+
}
|
76
|
+
static elementsPerField() {
|
77
|
+
return CHARS_PER_FIELD;
|
78
|
+
}
|
79
|
+
/**
|
80
|
+
*
|
81
|
+
* @param fields Array of Fields, containing packed Characters
|
82
|
+
* @returns Array of Character
|
83
|
+
*/
|
84
|
+
static unpack(fields) {
|
85
|
+
const unpacked = Provable.witness(Provable.Array(Character, this.l), () => {
|
86
|
+
let unpacked = this.unpackToBigints(fields);
|
87
|
+
return unpacked.map((x) => Character.fromString(String.fromCharCode(Number(x))));
|
88
|
+
});
|
89
|
+
Poseidon.hash(fields).assertEquals(Poseidon.hash(MultiPackedString_.pack(unpacked)));
|
90
|
+
return unpacked;
|
91
|
+
}
|
92
|
+
/**
|
93
|
+
*
|
94
|
+
* @param characters Array of Character to be packed
|
95
|
+
* @returns Instance of the implementing class
|
96
|
+
*/
|
97
|
+
static fromCharacters(input) {
|
98
|
+
let characters = new Array(this.l);
|
99
|
+
characters.fill(new Character(Field(0)), 0, this.l);
|
100
|
+
for (let i = 0; i < input.length; i++) {
|
101
|
+
characters[i] = input[i];
|
102
|
+
}
|
103
|
+
const packed = this.pack(characters);
|
104
|
+
return new MultiPackedString_(packed);
|
105
|
+
}
|
106
|
+
/**
|
107
|
+
*
|
108
|
+
* @param str string to be packed
|
109
|
+
* @returns Instance of the implementing class
|
110
|
+
*/
|
111
|
+
static fromString(str) {
|
112
|
+
let characters = new Array(this.l);
|
113
|
+
characters.fill(new Character(Field(0)), 0, this.l);
|
114
|
+
for (let i = 0; i < str.length; i++) {
|
115
|
+
characters[i] = Character.fromString(str[i]);
|
116
|
+
}
|
117
|
+
return this.fromCharacters(characters);
|
118
|
+
}
|
119
|
+
toString() {
|
120
|
+
const nullChar = String.fromCharCode(0);
|
121
|
+
return MultiPackedString_.unpack(this.packed)
|
122
|
+
.filter((c) => c.toString() !== nullChar)
|
123
|
+
.join('');
|
124
|
+
}
|
125
|
+
}
|
126
|
+
return MultiPackedString_;
|
127
|
+
}
|
60
128
|
//# sourceMappingURL=PackedString.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"PackedString.js","sourceRoot":"","sources":["../../../../src/lib/packed-types/PackedString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;
|
1
|
+
{"version":3,"file":"PackedString.js","sourceRoot":"","sources":["../../../../src/lib/packed-types/PackedString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAErE,MAAM,YAAY,GAAG,GAAG,CAAC;AACzB,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,2CAA2C;AACzD,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B,MAAM,UAAU,mBAAmB,CAAC,IAAY,CAAC;IAC/C,MAAM,aAAc,SAAQ,YAAY,CAAC,SAAS,EAAE,CAAC,EAAE,YAAY,CAAC;QAClE,MAAM,CAAC,YAAY,CAAC,KAAgB;YAClC,OAAO,KAAK,CAAC,KAAK,CAAC;QACrB,CAAC;QAED,MAAM,CAAC,UAAU;YACf,OAAO,YAAY,CAAC;QACtB,CAAC;QAED;;;;WAIG;QACH,MAAM,CAAC,MAAM,CAAC,CAAQ;YACpB,MAA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AG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;gBACnC,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;aAC9C;YACD,OAAO,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;QACzC,CAAC;QAED,QAAQ;YACN,MAAM,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;YACxC,OAAO,kBAAkB,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;iBAC1C,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,QAAQ,CAAC;iBACxC,IAAI,CAAC,EAAE,CAAC,CAAC;QACd,CAAC;KACF;IACD,OAAO,kBAAkB,CAAC;AAC5B,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
export {};
|
1
|
+
export {};
|
@@ -1,100 +1,101 @@
|
|
1
|
-
import { Character, Provable } from 'o1js';
|
2
|
-
import { PackedStringFactory } from './PackedString';
|
3
|
-
describe('PackedString', () => {
|
4
|
-
describe('Outside of the Circuit', () => {
|
5
|
-
const vitalik_dot_eth = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
|
6
|
-
class EthAddressString extends
|
7
|
-
}
|
8
|
-
let characters = [];
|
9
|
-
beforeEach(() => {
|
10
|
-
characters = [];
|
11
|
-
for (let i = 0; i < vitalik_dot_eth.length; i++) {
|
12
|
-
characters.push(Character.fromString(vitalik_dot_eth[i]));
|
13
|
-
}
|
14
|
-
});
|
15
|
-
it('#fromString', () => {
|
16
|
-
const myEthAddress = EthAddressString.fromString(vitalik_dot_eth);
|
17
|
-
expect(myEthAddress.toString()).toBe(vitalik_dot_eth);
|
18
|
-
});
|
19
|
-
it('#fromCharacters', () => {
|
20
|
-
const myEthAddress = EthAddressString.fromCharacters(characters);
|
21
|
-
expect(myEthAddress.toString()).toBe(vitalik_dot_eth);
|
22
|
-
});
|
23
|
-
it('#pack and #unPack', () => {
|
24
|
-
const
|
25
|
-
const
|
26
|
-
expect(packed.length).toBe(Math.ceil(vitalik_dot_eth.length / 15));
|
27
|
-
expect(unpacked.length).toBe(
|
28
|
-
expect(unpacked.
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
expect(
|
41
|
-
expect(
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
const
|
50
|
-
|
51
|
-
expect(
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
EthAddressString
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
myEthAddress
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
fakePacked
|
93
|
-
|
94
|
-
myEthAddress
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
});
|
1
|
+
import { Character, Provable } from 'o1js';
|
2
|
+
import { PackedStringFactory, MultiPackedStringFactory } from './PackedString';
|
3
|
+
describe('PackedString', () => {
|
4
|
+
describe('Outside of the Circuit', () => {
|
5
|
+
const vitalik_dot_eth = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
|
6
|
+
class EthAddressString extends MultiPackedStringFactory(3) {
|
7
|
+
}
|
8
|
+
let characters = [];
|
9
|
+
beforeEach(() => {
|
10
|
+
characters = [];
|
11
|
+
for (let i = 0; i < vitalik_dot_eth.length; i++) {
|
12
|
+
characters.push(Character.fromString(vitalik_dot_eth[i]));
|
13
|
+
}
|
14
|
+
});
|
15
|
+
it('#fromString', () => {
|
16
|
+
const myEthAddress = EthAddressString.fromString(vitalik_dot_eth);
|
17
|
+
expect(myEthAddress.toString()).toBe(vitalik_dot_eth);
|
18
|
+
});
|
19
|
+
it('#fromCharacters', () => {
|
20
|
+
const myEthAddress = EthAddressString.fromCharacters(characters);
|
21
|
+
expect(myEthAddress.toString()).toBe(vitalik_dot_eth);
|
22
|
+
});
|
23
|
+
it('#pack and #unPack', () => {
|
24
|
+
const unpacked = EthAddressString.unpack(EthAddressString.fromCharacters(characters).packed);
|
25
|
+
const packed = EthAddressString.pack(unpacked);
|
26
|
+
expect(packed.length).toBe(Math.ceil(vitalik_dot_eth.length / 15));
|
27
|
+
expect(unpacked.length).toBe(EthAddressString.l);
|
28
|
+
expect(unpacked.length).toBe(45);
|
29
|
+
expect(unpacked.slice(0, characters.length).toString()).toBe(characters.toString());
|
30
|
+
});
|
31
|
+
});
|
32
|
+
describe('Provable Properties', () => {
|
33
|
+
it('#sizeInFields', () => {
|
34
|
+
class one extends PackedStringFactory(15) {
|
35
|
+
}
|
36
|
+
class two extends MultiPackedStringFactory(2) {
|
37
|
+
}
|
38
|
+
class three extends MultiPackedStringFactory(3) {
|
39
|
+
}
|
40
|
+
expect(one.sizeInFields()).toBe(1);
|
41
|
+
expect(two.sizeInFields()).toBe(2);
|
42
|
+
expect(three.sizeInFields()).toBe(3);
|
43
|
+
});
|
44
|
+
});
|
45
|
+
describe('Defensive Cases', () => {
|
46
|
+
it('Initializes a class with fewer than allowed characters', () => {
|
47
|
+
class String10 extends PackedStringFactory(10) {
|
48
|
+
}
|
49
|
+
const string0 = String10.fromString('');
|
50
|
+
const string2 = String10.fromString('ab');
|
51
|
+
expect(string0.toString()).toBe('');
|
52
|
+
expect(string2.toString()).toBe('ab');
|
53
|
+
});
|
54
|
+
it('Initializes a class with more than allowed characters', () => {
|
55
|
+
class String10 extends PackedStringFactory(10) {
|
56
|
+
}
|
57
|
+
expect(() => {
|
58
|
+
String10.fromString('abcdefghijk');
|
59
|
+
}).toThrow();
|
60
|
+
});
|
61
|
+
it('Exceeds maximum size string', () => {
|
62
|
+
const tooLong = 'too long!'.repeat(20);
|
63
|
+
class MaxString extends MultiPackedStringFactory(8) {
|
64
|
+
}
|
65
|
+
expect(() => {
|
66
|
+
MaxString.fromString(tooLong);
|
67
|
+
}).toThrow();
|
68
|
+
});
|
69
|
+
});
|
70
|
+
describe('In the circuit', () => {
|
71
|
+
const vitalik_dot_eth = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
|
72
|
+
class EthAddressString extends MultiPackedStringFactory(3) {
|
73
|
+
}
|
74
|
+
const outsideEthAddress = EthAddressString.fromString(vitalik_dot_eth);
|
75
|
+
it('Initializes a string', () => {
|
76
|
+
expect(() => {
|
77
|
+
Provable.runAndCheck(() => {
|
78
|
+
const myEthAddress = new EthAddressString(outsideEthAddress.packed);
|
79
|
+
EthAddressString.check({ packed: myEthAddress.packed });
|
80
|
+
});
|
81
|
+
}).not.toThrow();
|
82
|
+
});
|
83
|
+
it('#assertEquals', () => {
|
84
|
+
expect(() => {
|
85
|
+
Provable.runAndCheck(() => {
|
86
|
+
const myEthAddress = new EthAddressString(outsideEthAddress.packed);
|
87
|
+
myEthAddress.assertEquals(outsideEthAddress);
|
88
|
+
});
|
89
|
+
}).not.toThrow();
|
90
|
+
expect(() => {
|
91
|
+
Provable.runAndCheck(() => {
|
92
|
+
const fakePacked = [...outsideEthAddress.packed];
|
93
|
+
fakePacked[0] = fakePacked[0].add(1);
|
94
|
+
const myEthAddress = new EthAddressString(fakePacked);
|
95
|
+
myEthAddress.assertEquals(outsideEthAddress);
|
96
|
+
});
|
97
|
+
}).toThrow();
|
98
|
+
});
|
99
|
+
});
|
100
|
+
});
|
100
101
|
//# sourceMappingURL=PackedString.test.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"PackedString.test.js","sourceRoot":"","sources":["../../../../src/lib/packed-types/PackedString.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAS,QAAQ,EAAE,MAAM,MAAM,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;
|
1
|
+
{"version":3,"file":"PackedString.test.js","sourceRoot":"","sources":["../../../../src/lib/packed-types/PackedString.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAS,QAAQ,EAAE,MAAM,MAAM,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAE/E,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;QACtC,MAAM,eAAe,GAAG,4CAA4C,CAAC;QACrE,MAAM,gBAAiB,SAAQ,wBAAwB,CAAC,CAAC,CAAC;SAAG;QAC7D,IAAI,UAAU,GAAqB,EAAE,CAAC;QAEtC,UAAU,CAAC,GAAG,EAAE;YACd,UAAU,GAAG,EAAE,CAAC;YAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;gBAC/C,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;aAC3D;QACH,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,aAAa,EAAE,GAAG,EAAE;YACrB,MAAM,YAAY,GAAG,gBAAgB,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC;YAClE,MAAM,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QACxD,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,iBAAiB,EAAE,GAAG,EAAE;YACzB,MAAM,YAAY,GAAG,gBAAgB,CAAC,cAA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GAAG,IAAI,gBAAgB,CAAC,UAAU,CAAC,CAAC;oBAEtD,YAAY,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;gBAC/C,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
@@ -1,90 +1,85 @@
|
|
1
|
-
import { Field, UInt32 } from 'o1js';
|
2
|
-
export declare function PackedUInt32Factory(l?: number): {
|
3
|
-
new (packed: import("o1js/dist/node/lib/field.js").Field): {
|
4
|
-
toBigInts(): Array<bigint>;
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
packed
|
79
|
-
};
|
80
|
-
|
81
|
-
packed:
|
82
|
-
}) => {
|
83
|
-
packed:
|
84
|
-
};
|
85
|
-
|
86
|
-
packed: string;
|
87
|
-
}) => {
|
88
|
-
packed: import("o1js/dist/node/lib/field.js").Field;
|
89
|
-
};
|
90
|
-
};
|
1
|
+
import { Field, UInt32 } from 'o1js';
|
2
|
+
export declare function PackedUInt32Factory(l?: number): {
|
3
|
+
new (packed: import("o1js/dist/node/lib/field.js").Field): {
|
4
|
+
toBigInts(): Array<bigint>;
|
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: UInt32): Field;
|
12
|
+
sizeInBits(): bigint;
|
13
|
+
/**
|
14
|
+
*
|
15
|
+
* @param f Field, packed with the information, as returned by #pack
|
16
|
+
* @returns Array of UInt32
|
17
|
+
*/
|
18
|
+
unpack(f: Field): UInt32[];
|
19
|
+
/**
|
20
|
+
*
|
21
|
+
* @param uint32s Array of UInt32s to be packed
|
22
|
+
* @returns Instance of the implementing class
|
23
|
+
*/
|
24
|
+
fromUInt32s(uint32s: Array<UInt32>): {
|
25
|
+
toBigInts(): Array<bigint>;
|
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 bigints Array of bigints to be packed
|
35
|
+
* @returns Instance of the implementing class
|
36
|
+
*/
|
37
|
+
fromBigInts(bigints: Array<bigint>): {
|
38
|
+
toBigInts(): Array<bigint>;
|
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: UInt32[]): void;
|
53
|
+
pack(unpacked: UInt32[]): 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
|
+
};
|