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

Handle missing extended provable methods #1657

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/bindings
Submodule bindings updated 1 files
+104 −58 lib/provable-generic.ts
8 changes: 4 additions & 4 deletions src/lib/provable/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ class Group {
fields: [x.x, x.y],
};
}

static empty() {
return Group.zero;
}
}

// internal helpers
Expand All @@ -360,10 +364,6 @@ function isConstant(g: Group) {
return g.x.isConstant() && g.y.isConstant();
}

function toTuple(g: Group): [0, FieldVar, FieldVar] {
return [0, g.x.value, g.y.value];
}

function toProjective(g: Group) {
return Pallas.fromAffine({
x: g.x.toBigInt(),
Expand Down
4 changes: 4 additions & 0 deletions src/lib/provable/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ class Scalar implements ShiftedScalar {
static fromJSON(x: string) {
return Scalar.from(SignableFq.fromJSON(x));
}

static empty() {
return Scalar.from(0n);
}
}

// internal helpers
Expand Down
24 changes: 23 additions & 1 deletion src/lib/provable/test/struct.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { Bool } from '../bool.js';
import assert from 'assert/strict';
import { FieldType } from '../core/fieldvar.js';
import { From } from '../../../bindings/lib/provable-generic.js';
import { Group } from '../group.js';
import { modifiedField } from '../types/fields.js';

let type = provable({
nested: { a: Number, b: Boolean },
Expand Down Expand Up @@ -85,6 +87,26 @@ expect(jsValue).toEqual({

expect(type.fromValue(jsValue)).toEqual(value);

// empty
let empty = type.empty();
expect(empty).toEqual({
nested: { a: 0, b: false },
other: '',
pk: PublicKey.empty(),
bool: new Bool(false),
uint: [UInt32.zero, UInt32.zero],
});

// empty with Group
expect(provable({ value: Group }).empty()).toEqual({ value: Group.zero });

// fails with a clear error on input without an empty method
const FieldWithoutEmpty = modifiedField({});
delete (FieldWithoutEmpty as any).empty;
expect(() => provable({ value: FieldWithoutEmpty }).empty()).toThrow(
'Expected `empty()` method on anonymous type object'
);

// check
await Provable.runAndCheck(() => {
type.check(value);
Expand Down Expand Up @@ -120,7 +142,7 @@ class MyStructPure extends Struct({
uint: [UInt32, UInt32],
}) {}

// Struct.from() works on both js and provable inputs
// Struct.fromValue() works on both js and provable inputs

let myStructInput = {
nested: { a: Field(1), b: 2n },
Expand Down
1 change: 0 additions & 1 deletion src/lib/provable/types/struct.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Field, Bool, Scalar, Group } from '../wrapped.js';
import {
provable,
provablePure,
provableTuple,
HashInput,
NonMethods,
Expand Down
Loading