-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48ce85d
commit b2f48f5
Showing
11 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters