Skip to content

Commit

Permalink
some refactoring and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Nov 24, 2023
1 parent 2c62e38 commit 8589608
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
12 changes: 11 additions & 1 deletion lib/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
GenericProvable,
GenericProvablePure,
GenericProvableExtended,
GenericProvableExtendedPure,
GenericField,
GenericBool,
GenericHashInput,
Expand All @@ -29,13 +30,22 @@ interface GenericProvablePure<T, Field> extends GenericProvable<T, Field> {
check: (x: T) => void;
}

type GenericProvableExtended<T, TJson, Field> = GenericProvable<T, Field> & {
type ProvableExtension<T, TJson, Field> = {
toInput: (x: T) => { fields?: Field[]; packed?: [Field, number][] };
toJSON: (x: T) => TJson;
fromJSON: (x: TJson) => T;
emptyValue?: () => T;
};

type GenericProvableExtended<T, TJson, Field> = GenericProvable<T, Field> &
ProvableExtension<T, TJson, Field>;

type GenericProvableExtendedPure<T, TJson, Field> = GenericProvablePure<
T,
Field
> &
ProvableExtension<T, TJson, Field>;

type GenericField<Field> = ((value: number | string | bigint) => Field) &
GenericProvableExtended<Field, string, Field> &
Binable<Field> & { sizeInBytes(): number };
Expand Down
9 changes: 2 additions & 7 deletions lib/provable-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
GenericProvable,
GenericProvablePure,
GenericProvableExtended,
GenericProvableExtendedPure,
} from './generic.js';

export {
Expand Down Expand Up @@ -180,7 +181,7 @@ function createProvable<Field>(): ProvableConstructor<Field> {
let keys = isToplevel ? objectKeys : Object.keys(typeObj);
let values = fromJSON(
keys.map((k) => typeObj[k]),
json
keys.map((k) => json[k])
);
return Object.fromEntries(keys.map((k, i) => [k, values[i]]));
}
Expand Down Expand Up @@ -353,12 +354,6 @@ type IsPureBase<A, Field> = A extends GenericProvablePure<any, Field>
}[keyof A]
: false;

type GenericProvableExtendedPure<T, TJson, Field> = GenericProvablePure<
T,
Field
> &
GenericProvableExtended<T, TJson, Field>;

type InferredProvable<A, Field> = IsPure<A, Field> extends true
? GenericProvableExtendedPure<InferProvable<A, Field>, InferJson<A>, Field>
: GenericProvableExtended<InferProvable<A, Field>, InferJson<A>, Field>;
19 changes: 5 additions & 14 deletions lib/provable-snarky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
InferJson,
InferredProvable as GenericInferredProvable,
IsPure as GenericIsPure,
createHashInput,
} from './provable-generic.js';
import { Tuple } from '../../lib/util/types.js';

// TODO make this whole file reuse ./provable-generic.ts
import { GenericHashInput } from './generic.js';

// external API
export { ProvableExtended, provable, provablePure, provableTuple };
Expand All @@ -29,6 +29,7 @@ type ProvableExtension<T, TJson = any> = {
toInput: (x: T) => { fields?: Field[]; packed?: [Field, number][] };
toJSON: (x: T) => TJson;
fromJSON: (x: TJson) => T;
emptyValue?: () => T;
};
type ProvableExtended<T, TJson = any> = Provable<T> &
ProvableExtension<T, TJson>;
Expand All @@ -39,18 +40,8 @@ type InferProvable<T> = GenericInferProvable<T, Field>;
type InferredProvable<T> = GenericInferredProvable<T, Field>;
type IsPure<T> = GenericIsPure<T, Field>;

type HashInput = { fields?: Field[]; packed?: [Field, number][] };
const HashInput = {
get empty() {
return {};
},
append(input1: HashInput, input2: HashInput): HashInput {
return {
fields: (input1.fields ?? []).concat(input2.fields ?? []),
packed: (input1.packed ?? []).concat(input2.packed ?? []),
};
},
};
type HashInput = GenericHashInput<Field>;
const HashInput = createHashInput<Field>();

const provable = createProvable<Field>();

Expand Down

0 comments on commit 8589608

Please sign in to comment.