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

Move Provable (formerly Circuit) to JS #889

Merged
merged 34 commits into from
May 10, 2023
Merged

Conversation

mitschabaude
Copy link
Collaborator

@mitschabaude mitschabaude commented May 3, 2023

bindings: o1-labs/o1js-bindings#9

closes #675

related docs update o1-labs/docs2#393

  • re-implement Circuit on top of a new, simpler and less JavaScript-y OCaml interface called Snarky
  • move utility functions such as Circuit.if and Circuit.witness to a new namespace Provable, which lives in provable.ts
  • much of provable.ts is existing code that was moved around.
  • significant changes:
    • Provable.{if, assertEqual, equal} are re-implemented in JS on top of lower-level circuit gadgets, and equipped with more precise typing and better-defined error behavior
    • The Circuit circuit-writing API has some logic (defining input types, & the snarky circuit function) moved to JS, to get rid of Provable<T> handling in OCaml altogether

see comments below for details. I recommend to focus review on the re-written primitives in provable.ts

note: tests are failing because this doesn't have updated bindings yet

Copy link
Collaborator Author

@mitschabaude mitschabaude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments to help reviewing!

Comment on lines +40 to +42
let main = mainFromCircuitData(this._main, privateInput);
let publicInputSize = this._main.publicInputType.sizeInFields();
let publicInputFields = this._main.publicInputType.toFields(publicInput);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of the ocaml logic for the Circuit circuit-writing API is moved to JS

Comment on lines +81 to +83
* @deprecated use {@link Provable.witness}
*/
static witness = SnarkyCircuit.witness;
static witness = Provable.witness;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the methods on Circuit that are general-purpose and not specific to the Circuit-writing API are moved to Provable, and deprecated here

Comment on lines +101 to +103
* @deprecated use {@link Provable.Array}
*/
static array = SnarkyCircuit.array;
static array = Provable.Array;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed Circuit.array to Provable.Array -- upper case because it's analogous to JS builtin Array

Comment on lines +12 to +15
it('Provable.if out of snark', () => {
let x = Provable.if(Bool(false), Int64, Int64.from(-1), Int64.from(-2));
expect(x.toString()).toBe('-2');
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit tests for Provable.if and other rewritten provable methods

@@ -276,97 +259,12 @@ function prop(this: any, target: any, key: string) {
}
}

function circuitArray<A extends FlexibleProvable<any>>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is moved to provable.ts with no changes


function equal<T>(type: FlexibleProvable<T>, x: T, y: T): Bool;
function equal<T extends ToFieldable>(x: T, y: T): Bool;
function equal(typeOrX: any, xOrY: any, yOrUndefined?: any) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equal - new implementation!


function if_<T>(condition: Bool, type: FlexibleProvable<T>, x: T, y: T): T;
function if_<T extends ToFieldable>(condition: Bool, x: T, y: T): T;
function if_(condition: Bool, typeOrX: any, xOrY: any, yOrUndefined?: any) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if - new implementation!

return ifExplicit(condition, type as any as Provable<T>, x, y);
}

function switch_<T, A extends FlexibleProvable<T>>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch is moved here from circuit_value.ts without changes


// runners for provable code

function log(...args: any) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log is moved here from circuit_value.ts without changes

});
}

function asProver(f: () => void) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everything below in this file is moved here without changes, except the new checkLength and clone helpers

@mitschabaude mitschabaude marked this pull request as ready for review May 8, 2023 12:57
Copy link
Member

@Trivo25 Trivo25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Left some questions for my understanding

src/lib/circuit.ts Show resolved Hide resolved
Comment on lines +1 to +5
/**
* {@link Provable} is
* - a namespace with tools for writing provable code
* - the main interface for types that can be used in provable code
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really appreciate these comments!

src/lib/provable.ts Show resolved Hide resolved
src/lib/provable.ts Outdated Show resolved Hide resolved
@mitschabaude mitschabaude merged commit 8374557 into main May 10, 2023
@mitschabaude mitschabaude deleted the refactor/js-circuit branch May 10, 2023 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move the Circuit class to JS, in part as a wrapper
2 participants