Skip to content

Commit

Permalink
Merge pull request #1657 from o1-labs/fix/empty
Browse files Browse the repository at this point in the history
Handle missing extended provable methods
  • Loading branch information
mitschabaude committed May 16, 2024
2 parents 7b4f51f + 6a9d4d3 commit 8cf3b08
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixes

- Fix type inference for `method.returns(Type)`, to require a matching return signature https://github.com/o1-labs/o1js/pull/1653
- Fix `Struct.empty()` returning a garbage object when one of the base types doesn't support `empty()` https://github.com/o1-labs/o1js/pull/1657

## [1.2.0](https://github.com/o1-labs/o1js/compare/4a17de857...6a1012162) - 2024-05-14

Expand Down
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

0 comments on commit 8cf3b08

Please sign in to comment.