Skip to content

Commit

Permalink
move provable derivers into o1js
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Mar 22, 2024
1 parent 48ce85d commit b2f48f5
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/bindings
Submodule bindings updated 1 files
+0 −105 lib/provable-snarky.ts
2 changes: 1 addition & 1 deletion src/lib/foreign-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EllipticCurve, Point } from './gadgets/elliptic-curve.js';
import { Field3 } from './gadgets/foreign-field.js';
import { assert } from './gadgets/common.js';
import { Provable } from './provable.js';
import { provableFromClass } from '../bindings/lib/provable-snarky.js';
import { provableFromClass } from './provable-types/provable-derivers.js';

// external API
export { createForeignCurve, ForeignCurve };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/foreign-ecdsa.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { provableFromClass } from '../bindings/lib/provable-snarky.js';
import { provableFromClass } from './provable-types/provable-derivers.js';
import { CurveParams } from '../bindings/crypto/elliptic-curve.js';
import { ProvablePureExtended } from './provable-types/struct.js';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/gadgets/foreign-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
inverse as modInverse,
mod,
} from '../../bindings/crypto/finite-field.js';
import { provableTuple } from '../../bindings/lib/provable-snarky.js';
import { provableTuple } from '../provable-types/provable-derivers.js';
import { Bool } from '../bool.js';
import { Unconstrained } from '../provable-types/struct.js';
import { Field } from '../field.js';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/provable-types/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { provableFromClass } from '../../bindings/lib/provable-snarky.js';
import { provableFromClass } from './provable-derivers.js';
import type { ProvablePureExtended } from './struct.js';
import { assert } from '../gadgets/common.js';
import { chunkString } from '../util/arrays.js';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/provable-types/circuit-value.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import { Field } from '../core.js';
import { HashInput, NonMethods } from '../../bindings/lib/provable-snarky.js';
import { HashInput, NonMethods } from './provable-derivers.js';
import { Provable } from '../provable.js';
import { AnyConstructor, FlexibleProvable } from './struct.js';

Expand Down
2 changes: 1 addition & 1 deletion src/lib/provable-types/merkle-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Bool, Field } from '../core.js';
import { Provable } from '../provable.js';
import { Struct, Unconstrained } from './struct.js';
import { assert } from '../gadgets/common.js';
import { provableFromClass } from '../../bindings/lib/provable-snarky.js';
import { provableFromClass } from './provable-derivers.js';
import { Poseidon, packToFields, ProvableHashable } from '../hash.js';

export {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/provable-types/packed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { provableFromClass } from '../../bindings/lib/provable-snarky.js';
import { provableFromClass } from './provable-derivers.js';
import { HashInput, ProvableExtended, Unconstrained } from './struct.js';
import { Field } from '../field.js';
import { assert } from '../gadgets/common.js';
Expand Down
102 changes: 102 additions & 0 deletions src/lib/provable-types/provable-derivers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Provable, ProvablePure } from './provable-intf.js';
import { Field } from '../core.js';
import {
createDerivers,
NonMethods,
InferProvable as GenericInferProvable,
InferJson,
InferredProvable as GenericInferredProvable,
IsPure as GenericIsPure,
createHashInput,
Constructor,
} from '../../bindings/lib/provable-generic.js';
import { Tuple } from '../util/types.js';
import { GenericHashInput } from '../../bindings/lib/generic.js';

// external API
export {
ProvableExtended,
provable,
provablePure,
provableTuple,
provableFromClass,
};

// internal API
export {
NonMethods,
HashInput,
InferProvable,
InferJson,
InferredProvable,
IsPure,
};

type ProvableExtension<T, TJson = any> = {
toInput: (x: T) => { fields?: Field[]; packed?: [Field, number][] };
toJSON: (x: T) => TJson;
fromJSON: (x: TJson) => T;
empty: () => T;
};
type ProvableExtended<T, TJson = any> = Provable<T> &
ProvableExtension<T, TJson>;
type ProvablePureExtended<T, TJson = any> = ProvablePure<T> &
ProvableExtension<T, TJson>;

type InferProvable<T> = GenericInferProvable<T, Field>;
type InferredProvable<T> = GenericInferredProvable<T, Field>;
type IsPure<T> = GenericIsPure<T, Field>;

type HashInput = GenericHashInput<Field>;
const HashInput = createHashInput<Field>();

const { provable } = createDerivers<Field>();

function provablePure<A>(
typeObj: A
): ProvablePureExtended<InferProvable<A>, InferJson<A>> {
return provable(typeObj, { isPure: true }) as any;
}

function provableTuple<T extends Tuple<any>>(types: T): InferredProvable<T> {
return provable(types) as any;
}

function provableFromClass<A, T extends InferProvable<A>>(
Class: Constructor<T> & { check?: (x: T) => void; empty?: () => T },
typeObj: A
): IsPure<A> extends true
? ProvablePureExtended<T, InferJson<A>>
: ProvableExtended<T, InferJson<A>> {
let raw = provable(typeObj);
return {
sizeInFields: raw.sizeInFields,
toFields: raw.toFields,
toAuxiliary: raw.toAuxiliary,
fromFields(fields, aux) {
return construct(Class, raw.fromFields(fields, aux));
},
check(value) {
if (Class.check !== undefined) {
Class.check(value);
} else {
raw.check(value);
}
},
toInput: raw.toInput,
toJSON: raw.toJSON,
fromJSON(x) {
return construct(Class, raw.fromJSON(x));
},
empty() {
return Class.empty !== undefined
? Class.empty()
: construct(Class, raw.empty());
},
} satisfies ProvableExtended<T, InferJson<A>> as any;
}

function construct<Raw, T extends Raw>(Class: Constructor<T>, value: Raw): T {
let instance = Object.create(Class.prototype);
return Object.assign(instance, value);
}
4 changes: 2 additions & 2 deletions src/lib/provable-types/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
provableTuple,
HashInput,
NonMethods,
} from '../../bindings/lib/provable-snarky.js';
} from './provable-derivers.js';
import type {
InferJson,
InferProvable,
InferredProvable,
IsPure,
} from '../../bindings/lib/provable-snarky.js';
} from './provable-derivers.js';
import { Provable } from '../provable.js';
import { assert } from '../errors.js';
import { inCheckedComputation } from '../provable-context.js';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/provable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
InferJson,
InferProvable,
InferredProvable,
} from '../bindings/lib/provable-snarky.js';
} from './provable-types/provable-derivers.js';
import {
inCheckedComputation,
inProver,
Expand Down

0 comments on commit b2f48f5

Please sign in to comment.