Skip to content

Commit

Permalink
simplify, prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Jan 18, 2024
1 parent 44f44c7 commit 7ae44dc
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/lib/provable-types/packed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,40 @@ class Packed<T> {
packed: Field[];
value: Unconstrained<T>;

/**
* Create a packed representation of `type`. You can then use `PackedType.pack(x)` to pack a value.
*/
static create<T>(type: ProvableExtended<T>): typeof Packed<T> {
// compute size of packed representation
let input = type.toInput(type.empty());
let packedSize = countFields(input);

return class Packed_ extends Packed<T> {
static _innerProvable = type;
static _provable = provableFromClass(Packed_, {
packed: fields(packedSize),
value: Unconstrained.provable,
}) as ProvableHashable<Packed<T>>;
};
}

constructor(packed: Field[], value: Unconstrained<T>) {
this.packed = packed;
this.value = value;
}

/**
* Pack a value.
*/
static pack<T>(x: T): Packed<T> {
let input = this.innerProvable.toInput(x);
let packed = packToFields(input);
return new this(packed, Unconstrained.from(x));
}

/**
* Unpack a value.
*/
unpack(): T {
let value = Provable.witness(this.Constructor.innerProvable, () =>
this.value.get()
Expand All @@ -79,22 +102,7 @@ class Packed<T> {
return this.packed;
}

hash() {
return Poseidon.hash(this.packed);
}

static createProvable<T>(
type: ProvableExtended<T>
): ProvableHashable<Packed<T>> {
let input = type.toInput(type.empty());
let packedSize = countFields(input);

return provableFromClass(this, {
packed: fields(packedSize),
value: Unconstrained.provable,
}) as ProvableHashable<Packed<T>>;
}

// dynamic subclassing infra
static _provable: ProvableHashable<Packed<any>> | undefined;
static _innerProvable: ProvableExtended<any> | undefined;

Expand All @@ -110,13 +118,6 @@ class Packed<T> {
assert(this._innerProvable !== undefined, 'Packed not initialized');
return this._innerProvable;
}

static create<T>(type: ProvableExtended<T>): typeof Packed<T> {
return class Packed_ extends Packed<T> {
static _provable = Packed_.createProvable(type);
static _innerProvable = type;
};
}
}

type ProvableHashable<T> = Provable<T> & { toInput: (x: T) => HashInput };
Expand Down

0 comments on commit 7ae44dc

Please sign in to comment.