Skip to main content
Version: 2.4.0

Packed

Defined in: lib/provable/packed.ts:50

Packed<T> is a "packed" representation of any type T.

"Packed" means that field elements which take up fewer than 254 bits are packed together into as few field elements as possible.

For example, you can pack several Bools (1 bit) or UInt32s (32 bits) into a single field element.

Using a packed representation can make sense in provable code where the number of constraints depends on the number of field elements per value.

For example, Provable.if(bool, x, y) takes O(n) constraints, where n is the number of field elements in x and y.

Usage:

// define a packed type from a type
let PackedType = Packed.create(MyType);

// pack a value
let packed = PackedType.pack(value);

// ... operations on packed values, more efficient than on plain values ...

// unpack a value
let value = packed.unpack();

Warning: Packing only makes sense where packing actually reduces the number of field elements. For example, it doesn't make sense to pack a single Bool, because it will be 1 field element before and after packing. On the other hand, it does makes sense to pack a type that holds 10 or 20 Bools.

Warning: When wrapping a type with Packed, make sure that that type is safe to automatically pack and unpack in provable code. In particular, do not use Packed with types that define a custom toInput() (specifying a certain bit packing) but no corresponding check() method (that constrains the bit lengths of the packed parts).

Type Parameters

T

Constructors

new Packed()

new Packed<T>(packed: Field[], value: Unconstrained<T>): Packed<T>

Defined in: lib/provable/packed.ts:109

Parameters

packed

Field[]

value

Unconstrained<T>

Returns

Packed<T>

Properties

packed

packed: Field[];

Defined in: lib/provable/packed.ts:51


value

value: Unconstrained<T>;

Defined in: lib/provable/packed.ts:52


_innerProvable

static _innerProvable: 
| undefined
| ProvableHashable<any>;

Defined in: lib/provable/packed.ts:136


_provable

static _provable: 
| undefined
| ProvableHashable<Packed<any>>;

Defined in: lib/provable/packed.ts:135

Accessors

Constructor

Get Signature

get Constructor(): typeof Packed

Defined in: lib/provable/packed.ts:138

Returns

typeof Packed


innerProvable

Get Signature

get static innerProvable(): ProvableHashable<any>

Defined in: lib/provable/packed.ts:142

Returns

ProvableHashable<any>

Methods

toFields()

toFields(): Field[]

Defined in: lib/provable/packed.ts:130

Returns

Field[]


unpack()

unpack(): T

Defined in: lib/provable/packed.ts:117

Unpack a value.

Returns

T


create()

static create<T, V>(type: WithProvable<ProvableHashable<T, V>>): typeof Packed & {
provable: ProvableHashable<Packed<T>, V>;
pack: Packed<T>;
}

Defined in: lib/provable/packed.ts:57

Create a packed representation of type. You can then use PackedType.pack(x) to pack a value.

Type Parameters

T

V

Parameters

type

WithProvable<ProvableHashable<T, V>>

Returns

typeof Packed & { provable: ProvableHashable<Packed<T>, V>; pack: Packed<T>; }