Skip to main content

Packed

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>

Parameters

packed: Field[]

value: Unconstrained\<T>

Returns

Packed\<T>

Source

lib/provable/packed.ts:115

Properties

packed

packed: Field[];

Source

lib/provable/packed.ts:51


value

value: Unconstrained<T>;

Source

lib/provable/packed.ts:52


_innerProvable

static _innerProvable: undefined | ProvableHashable<any>;

Source

lib/provable/packed.ts:144


_provable

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

Source

lib/provable/packed.ts:143

Accessors

Constructor

get Constructor(): typeof Packed

Returns

typeof Packed

Source

lib/provable/packed.ts:146


innerProvable

get static innerProvable(): ProvableHashable<any>

Returns

ProvableHashable\<any>

Source

lib/provable/packed.ts:150

Methods

toFields()

toFields(): Field[]

Returns

Field[]

Source

lib/provable/packed.ts:138


unpack()

unpack(): T

Unpack a value.

Returns

T

Source

lib/provable/packed.ts:123


create()

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

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>; }

Source

lib/provable/packed.ts:57