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

Refactor provable core #1507

Merged
merged 29 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5850367
solution for the field import cycle
mitschabaude Mar 14, 2024
1015cf8
define the one and only internal version of exists, use for field, mo…
mitschabaude Mar 14, 2024
9f57813
add missing boolean check
mitschabaude Mar 14, 2024
9093963
remove duplicate exists
mitschabaude Mar 14, 2024
1ebd505
remove unused snarky apis
mitschabaude Mar 14, 2024
506fadc
straighten some error messages
mitschabaude Mar 22, 2024
cfe0bfc
remove fieldvar reexports
mitschabaude Mar 22, 2024
ae59026
bindings
mitschabaude Mar 22, 2024
774bbd0
Merge branch 'main' into feature/consolidate-exists
mitschabaude Mar 22, 2024
254b616
move provable intf
mitschabaude Mar 22, 2024
48b897c
simplify and unduplicate provable intf docs / methods
mitschabaude Mar 22, 2024
4495d14
update doccomments we need elsewhere
mitschabaude Mar 22, 2024
82fe43b
don't use interfaces
mitschabaude Mar 22, 2024
89a6cf2
move circuit-value.test.ts
mitschabaude Mar 22, 2024
146421f
move circuit-value.ts
mitschabaude Mar 22, 2024
96b4b4f
remove circuit value
mitschabaude Mar 22, 2024
d02927a
fixup examples
mitschabaude Mar 22, 2024
88deee4
tweak
mitschabaude Mar 22, 2024
ef8fa79
remove matrixProp
mitschabaude Mar 22, 2024
c20bced
rename circuit-value.ts to struct.ts
mitschabaude Mar 22, 2024
82da166
bindings
mitschabaude Mar 22, 2024
96357b5
move circuit value logic to its own file
mitschabaude Mar 22, 2024
c4a7850
move string, merkle map, merkle tree to provable-types
mitschabaude Mar 22, 2024
4b1cd6d
move contents of weirdly-named provable folder into mina-signer
mitschabaude Mar 22, 2024
a5f8e6c
remove inapproporiate dependencies on wrappers created for mina-signer
mitschabaude Mar 22, 2024
48ce85d
move bigint derivers into mina-signer
mitschabaude Mar 22, 2024
b2f48f5
move provable derivers into o1js
mitschabaude Mar 22, 2024
0f23f82
separate unconstrained from struct
mitschabaude Mar 25, 2024
6e62425
remove duplicate toConstant method
mitschabaude Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Change `{SmartContract,ZkProgram}.analyzeMethods()` to be async https://github.com/o1-labs/o1js/pull/1450
- `Provable.runAndCheck()`, `Provable.constraintSystem()` and `{SmartContract,ZkProgram}.digest()` are also async now
- `Provable.runAndCheckSync()` added and immediately deprecated for a smoother upgrade path for tests
- **Remove deprecated APIs**
- Remove `CircuitValue`, `prop`, `arrayProp` and `matrixProp` https://github.com/o1-labs/o1js/pull/1507
- Remove `this.sender` which unintuitively did not prove that its value was the actual sender of the transaction https://github.com/o1-labs/o1js/pull/1464 [@julio4](https://github.com/julio4)
Replaced by more explicit APIs:
- `this.sender.getUnconstrained()` which has the old behavior of `this.sender`, and returns an unconstrained value (which means that the prover can set it to any value they want)
Expand Down
16 changes: 5 additions & 11 deletions src/examples/zkapps/voting/election-preconditions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { CircuitValue, prop, UInt32 } from 'o1js';
import { Struct, UInt32 } from 'o1js';

export default class ElectionPreconditions extends CircuitValue {
@prop startElection: UInt32;
@prop endElection: UInt32;

constructor(startElection: UInt32, endElection: UInt32) {
super();
this.startElection = startElection;
this.endElection = endElection;
}
}
export default class ElectionPreconditions extends Struct({
startElection: UInt32,
endElection: UInt32,
}) {}
6 changes: 4 additions & 2 deletions src/examples/zkapps/voting/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import {
} from './preconditions.js';
import { Voting, Voting_ } from './voting.js';

export interface VotingAppParams {
export { VotingAppParams };

type VotingAppParams = {
candidatePreconditions: ParticipantPreconditions;
voterPreconditions: ParticipantPreconditions;
electionPreconditions: ElectionPreconditions;
voterKey: PrivateKey;
candidateKey: PrivateKey;
votingKey: PrivateKey;
doProofs: boolean;
}
};

function defaultParams(): VotingAppParams {
return {
Expand Down
33 changes: 16 additions & 17 deletions src/examples/zkapps/voting/member.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
CircuitValue,
Field,
prop,
PublicKey,
UInt64,
Poseidon,
MerkleWitness,
Struct,
} from 'o1js';

export class MyMerkleWitness extends MerkleWitness(3) {}
Expand All @@ -17,24 +16,24 @@ let dummyWitness = Array.from(Array(MyMerkleWitness.height - 1).keys()).map(
() => w
);

export class Member extends CircuitValue {
@prop publicKey: PublicKey;
@prop balance: UInt64;
export class Member extends Struct({
publicKey: PublicKey,
balance: UInt64,

// will need this to keep track of votes for candidates
@prop votes: Field;

@prop witness: MyMerkleWitness;
@prop votesWitness: MyMerkleWitness;
// need this to keep track of votes for candidates
votes: Field,

witness: MyMerkleWitness,
votesWitness: MyMerkleWitness,
}) {
constructor(publicKey: PublicKey, balance: UInt64) {
super();
this.publicKey = publicKey;
this.balance = balance;
this.votes = Field(0);

this.witness = new MyMerkleWitness(dummyWitness);
this.votesWitness = new MyMerkleWitness(dummyWitness);
super({
publicKey,
balance,
votes: Field(0),
witness: new MyMerkleWitness(dummyWitness),
votesWitness: new MyMerkleWitness(dummyWitness),
});
}

getHash(): Field {
Expand Down
8 changes: 5 additions & 3 deletions src/examples/zkapps/voting/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import { ParticipantPreconditions } from './preconditions.js';

let participantPreconditions = ParticipantPreconditions.default;

interface MembershipParams {
Provable;

type MembershipParams = {
participantPreconditions: ParticipantPreconditions;
contractAddress: PublicKey;
doProofs: boolean;
}
};

/**
* Returns a new contract instance that based on a set of preconditions.
Expand Down Expand Up @@ -124,7 +126,7 @@ export class Membership_ extends SmartContract {
}),
Bool,
(state: Bool, action: Member) => {
return action.equals(member).or(state);
return Provable.equal(Member, action, member).or(state);
},
// initial state
{ state: Bool(false), actionState: accumulatedMembers }
Expand Down
18 changes: 0 additions & 18 deletions src/examples/zkapps/voting/participant-preconditions.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/examples/zkapps/voting/voting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ let voterPreconditions = ParticipantPreconditions.default;
*/
let electionPreconditions = ElectionPreconditions.default;

interface VotingParams {
type VotingParams = {
electionPreconditions: ElectionPreconditions;
voterPreconditions: ParticipantPreconditions;
candidatePreconditions: ParticipantPreconditions;
candidateAddress: PublicKey;
voterAddress: PublicKey;
contractAddress: PublicKey;
doProofs: boolean;
}
};

/**
* Returns a new contract instance that based on a set of preconditions.
Expand Down
25 changes: 10 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { ProvablePure } from './snarky.js';
export type { ProvablePure } from './lib/provable-types/provable-intf.js';
export { Ledger } from './snarky.js';
export { Field, Bool, Group, Scalar } from './lib/core.js';
export {
Expand All @@ -21,17 +21,9 @@ export type {
FlexibleProvable,
FlexibleProvablePure,
InferProvable,
} from './lib/circuit-value.js';
export {
CircuitValue,
prop,
arrayProp,
matrixProp,
provable,
provablePure,
Struct,
Unconstrained,
} from './lib/circuit-value.js';
} from './lib/provable-types/struct.js';
export { provable, provablePure, Struct } from './lib/provable-types/struct.js';
export { Unconstrained } from './lib/provable-types/unconstrained.js';
export { Provable } from './lib/provable.js';
export { Circuit, Keypair, public_, circuitMain } from './lib/circuit.js';
export { UInt32, UInt64, Int64, Sign, UInt8 } from './lib/int.js';
Expand Down Expand Up @@ -104,9 +96,12 @@ export {
} from './lib/fetch.js';
export * as Encryption from './lib/encryption.js';
export * as Encoding from './bindings/lib/encoding.js';
export { Character, CircuitString } from './lib/string.js';
export { MerkleTree, MerkleWitness } from './lib/merkle-tree.js';
export { MerkleMap, MerkleMapWitness } from './lib/merkle-map.js';
export { Character, CircuitString } from './lib/provable-types/string.js';
export { MerkleTree, MerkleWitness } from './lib/provable-types/merkle-tree.js';
export {
MerkleMap,
MerkleMapWitness,
} from './lib/provable-types/merkle-map.js';

export { Nullifier } from './lib/nullifier.js';

Expand Down
2 changes: 1 addition & 1 deletion src/lib/account-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
provable,
provablePure,
StructNoJson,
} from './circuit-value.js';
} from './provable-types/struct.js';
import { memoizationContext, memoizeWitness, Provable } from './provable.js';
import { Field, Bool } from './core.js';
import { Pickles, Test } from '../snarky.js';
Expand Down
21 changes: 8 additions & 13 deletions src/lib/bool.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { Snarky } from '../snarky.js';
import {
Field,
FieldConst,
FieldType,
FieldVar,
readVarMessage,
withMessage,
} from './field.js';
import { Bool as B } from '../provable/field-bigint.js';
import { Field, readVarMessage, withMessage } from './field.js';
import { FieldVar, FieldConst, FieldType } from './provable-core/fieldvar.js';
import { defineBinable } from '../bindings/lib/binable.js';
import { NonNegativeInteger } from '../bindings/crypto/non-negative.js';
import { asProver } from './provable-context.js';
import { existsOne } from './gadgets/common.js';
import { existsOne } from './provable-core/exists.js';
import { assertMul } from './gadgets/compatible.js';
import { setBoolConstructor } from './provable-core/field-constructor.js';

export { BoolVar, Bool };

Expand Down Expand Up @@ -45,7 +39,7 @@ class Bool {
this.value = x;
return;
}
this.value = FieldVar.constant(B(x));
this.value = FieldVar.constant(BigInt(x));
}

isConstant(): this is { value: ConstantBoolVar } {
Expand Down Expand Up @@ -200,7 +194,7 @@ class Bool {
}

/**
* This converts the {@link Bool} to a javascript [[boolean]].
* This converts the {@link Bool} to a JS `boolean`.
* This can only be called on non-witness values.
*/
toBoolean(): boolean {
Expand Down Expand Up @@ -365,6 +359,7 @@ class Bool {
},
};
}
setBoolConstructor(Bool);

const BoolBinable = defineBinable({
toBytes(b: Bool) {
Expand Down Expand Up @@ -394,5 +389,5 @@ function toBoolean(x: boolean | Bool): boolean {

function toFieldVar(x: boolean | Bool): BoolVar {
if (x instanceof Bool) return x.value;
return FieldVar.constant(B(x));
return FieldVar.constant(BigInt(x));
}
3 changes: 2 additions & 1 deletion src/lib/circuit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'reflect-metadata';
import { ProvablePure, Snarky } from '../snarky.js';
import { Snarky } from '../snarky.js';
import { MlFieldArray, MlFieldConstArray } from './ml/fields.js';
import { withThreadPool } from '../snarky.js';
import { Provable } from './provable.js';
import { snarkContext, gatesFromJson } from './provable-context.js';
import { prettifyStacktrace, prettifyStacktracePromise } from './errors.js';
import { ProvablePure } from './provable-types/provable-intf.js';

// external API
export { public_, circuitMain, Circuit, Keypair, Proof, VerificationKey };
Expand Down
Loading
Loading