Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circuit value improvements #306

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/lib/circuit_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export { cloneCircuitValue, circuitValueEquals, circuitArray };

type AnyConstructor = new (...args: any) => any;

type NonMethodKeys<T> = {
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
type NonMethods<T> = Pick<T, NonMethodKeys<T>>;

abstract class CircuitValue {
constructor(...props: any[]) {
const fields = (this.constructor as any).prototype._fields;
Expand All @@ -38,6 +43,13 @@ abstract class CircuitValue {
}
}

static fromObject<T extends AnyConstructor>(
this: T,
value: NonMethods<InstanceType<T>>
): InstanceType<T> {
return Object.assign(Object.create(this.prototype), value);
}

static sizeInFields(): number {
const fields: [string, any][] = (this as any).prototype._fields;
return fields.reduce((acc, [_, typ]) => acc + typ.sizeInFields(), 0);
Expand Down Expand Up @@ -89,17 +101,22 @@ abstract class CircuitValue {
this: T,
xs: Field[]
): InstanceType<T> {
const fields = (this as any).prototype._fields;
const fields: [string, any][] = (this as any).prototype._fields;
if (xs.length < fields.length) {
throw Error(
`${this.name}.ofFields: Expected ${fields.length} field elements, got ${xs?.length}`
);
}
let offset = 0;
const props: any[] = [];
const props: any = {};
for (let i = 0; i < fields.length; ++i) {
const propType = fields[i][1];
const [key, propType] = fields[i];
const propSize = propType.sizeInFields();
const propVal = propType.ofFields(xs.slice(offset, offset + propSize));
props.push(propVal);
props[key] = propVal;
offset += propSize;
}
return new this(...props);
return Object.assign(Object.create(this.prototype), props);
}

static check<T extends AnyConstructor>(this: T, v: InstanceType<T>) {
Expand Down Expand Up @@ -143,7 +160,7 @@ abstract class CircuitValue {
this: T,
value: JSONValue
): InstanceType<T> | null {
const props: any[] = [];
const props: any = {};
const fields: [string, any][] = (this as any).prototype._fields;

switch (typeof value) {
Expand All @@ -162,12 +179,12 @@ abstract class CircuitValue {
if (value[key] === undefined) {
return null;
} else {
props.push(propType.fromJSON(value[key]));
props[key] = propType.fromJSON(value[key]);
}
}
}

return new this(...props);
return Object.assign(Object.create(this.prototype), props);
}
}

Expand Down