Trait Packable
pub trait Packable {
let N: u32;
// Required methods
pub fn pack(self) -> [Field; N];
pub fn unpack(packed: [Field; N]) -> Self;
}
Required methods
pub fn pack(self) -> [Field; N]
Packs the value into a compact Field array.
The original value can be reconstructed by calling Packable::unpack.
pub fn unpack(packed: [Field; N]) -> Self
Unpacks the value from a compact Field array.
packed must be the value returned by Packable::pack.
Implementors
impl Packable for AddressNote
impl Packable for AztecAddress
impl<let M: u32, let N: u32> Packable for CompressedString<N, M>
impl Packable for ContractClassId
impl<let INITIAL_DELAY: u64, T> Packable for DelayedPublicMutableValues<T, INITIAL_DELAY>
where
T: Packable
where
T: Packable
impl Packable for EmbeddedCurvePoint
impl Packable for Empty
impl Packable for EthAddress
impl Packable for Field
impl Packable for FieldCompressedString
impl Packable for FieldNote
impl<let N: u32, T> Packable for HasArrayWithGenerics<T, N>
where
T: Packable
where
T: Packable
impl<Note> Packable for HintedNote<Note>
where
Note: Packable
where
Note: Packable
impl Packable for MockStruct
impl Packable for MockStruct
impl Packable for NoteMetadata
impl Packable for PartialUintNote
impl Packable for Smol
impl Packable for TwoBooleans
impl Packable for UintNote
impl<let M: u32, T> Packable for WithHash<T, M>
where
T: Packable<N = M>
where
T: Packable<N = M>
impl<let M: u32, T> Packable for [T; M]
where
T: Packable
where
T: Packable
Space-efficient value packing and unpacking.
Like
SerializeandDeserialize, this trait is used to convert to a fromFieldarray representations.Packableis used instead of those whenever the length of the resulting array needs to be minimized, for example in order to reduce the number of amount storage access operations, or the number of values that will be hashed together.The associated constant
Nis the length of theFieldarray.Automatic Derivation
The
#[derive]macro can be used on any type that holds primitive values, or types that themselves also derivePackable. The resulting implementation does not attempt to tightly-pack values at all, and instead simply assigns a fullFieldto each value. This makes it suitable for types that are composed solely ofField-sized elements.Manual Implementation
If a type holds at least two elements smaller than a
Field, then manually implementingPackablecan result in reduced costs (assuming the sub-Fieldelements are small enough to be packed together).The nature of cost reduction will depend on what the value is used for. For public storage, it will typically decrease both L2 gas (in the form of fewer
SLOADandSSTOREopcodes) and DA costs (due to there being fewer slots accessed). For notes, it will typically decrease proving times (due to fewer hash operations).Note that packing and unpacking must also be runtime efficient for the previously mentioned gains to not be offset by this overhead. Bit-packing by multiplying and dividing by powers of 2 is often a good strategy, as this maps well to both AVM opcodes (for public contract functions) and proving backend primitives (for private contract functions).