Skip to content

Commit

Permalink
Merge pull request #1507 from o1-labs/feature/consolidate-exists
Browse files Browse the repository at this point in the history
Refactor provable core
  • Loading branch information
mitschabaude authored Mar 26, 2024
2 parents 95e5d41 + 6e62425 commit a55491c
Show file tree
Hide file tree
Showing 94 changed files with 1,246 additions and 1,066 deletions.
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

0 comments on commit a55491c

Please sign in to comment.