From 12be568d58f657d8e8e3f73d61454538ff6211a4 Mon Sep 17 00:00:00 2001 From: Brandon Kase Date: Tue, 1 Nov 2022 12:38:37 -0400 Subject: [PATCH 1/2] Removes static one/zero/-1 from Field Part of #511 --- src/examples/api_exploration.ts | 2 +- src/examples/constraint_system.ts | 8 +- src/examples/ex00_preimage.ts | 2 +- src/examples/matrix_mul.ts | 2 +- src/examples/program.ts | 6 +- src/examples/rollup/data_store.ts | 2 +- src/examples/rollup/merkle_stack.ts | 2 +- src/examples/zkapps/dex/erc20.ts | 2 +- .../zkapps/merkle_tree/merkle_zkapp.ts | 2 +- src/examples/zkapps/reducer/reducer.ts | 4 +- .../zkapps/reducer/reducer_composite.ts | 4 +- .../zkapps/simple_and_counter_zkapp.ts | 2 +- .../zkapps/simple_zkapp_with_proof.ts | 2 +- src/examples/zkapps/voting/demo.ts | 10 +- src/examples/zkapps/voting/dummyContract.ts | 2 +- src/examples/zkapps/voting/member.ts | 6 +- src/examples/zkapps/voting/test.ts | 46 +-- src/lib/account_update.test.ts | 4 +- src/lib/account_update.ts | 26 +- src/lib/circuit_value.ts | 4 +- src/lib/circuit_value.unit-test.ts | 12 +- src/lib/encoding.ts | 2 +- src/lib/field.test.ts | 14 +- src/lib/hash-input.unit-test.ts | 8 +- src/lib/hash.ts | 6 +- src/lib/int.test.ts | 314 +++++++++--------- src/lib/int.ts | 16 +- src/lib/mina.ts | 14 +- src/lib/proof_system.ts | 2 +- src/lib/scalar.test.ts | 4 +- src/lib/signature.ts | 2 +- src/lib/state.ts | 4 +- src/lib/string.ts | 4 +- src/snarky.d.ts | 28 +- 34 files changed, 278 insertions(+), 290 deletions(-) diff --git a/src/examples/api_exploration.ts b/src/examples/api_exploration.ts index 755f3c735..8e035d3e2 100644 --- a/src/examples/api_exploration.ts +++ b/src/examples/api_exploration.ts @@ -31,7 +31,7 @@ console.assert(x0.equals(x1).toBoolean()); // When initializing with booleans, true corresponds to the field element 1, and false corresponds to 0 const b = new Field(true); -console.assert(b.equals(Field.one).toBoolean()); +console.assert(b.equals(Field(1)).toBoolean()); /* You can perform arithmetic operations on field elements. The arithmetic methods can take any "fieldy" values as inputs: diff --git a/src/examples/constraint_system.ts b/src/examples/constraint_system.ts index f4a58f89d..200eb30ff 100644 --- a/src/examples/constraint_system.ts +++ b/src/examples/constraint_system.ts @@ -1,11 +1,11 @@ import { Field, Circuit, Poseidon } from 'snarkyjs'; -let hash = Poseidon.hash([Field.one, Field.minusOne]); +let hash = Poseidon.hash([Field(1), Field(-1)]); let { rows, digest } = Circuit.constraintSystem(() => { - let x = Circuit.witness(Field, () => Field.one); - let y = Circuit.witness(Field, () => Field.minusOne); - x.add(y).assertEquals(Field.zero); + let x = Circuit.witness(Field, () => Field(1)); + let y = Circuit.witness(Field, () => Field(-1)); + x.add(y).assertEquals(Field(0)); let z = Poseidon.hash([x, y]); z.assertEquals(hash); }); diff --git a/src/examples/ex00_preimage.ts b/src/examples/ex00_preimage.ts index 26fde6bb8..41b2167f1 100644 --- a/src/examples/ex00_preimage.ts +++ b/src/examples/ex00_preimage.ts @@ -26,7 +26,7 @@ await isReady; console.log('generating keypair...'); const kp = Main.generateKeypair(); -const preimage = Field.one; +const preimage = Field(1); const hash = Poseidon.hash([preimage]); console.log('prove...'); diff --git a/src/examples/matrix_mul.ts b/src/examples/matrix_mul.ts index 4e5409a7d..4e823db86 100644 --- a/src/examples/matrix_mul.ts +++ b/src/examples/matrix_mul.ts @@ -27,7 +27,7 @@ function matrixMul(x: Field[][], y: Field[][]): Field[][] { for (let i = 0; i < n; i++) { result[i] = []; for (let j = 0; j < o; j++) { - result[i][j] = Field.zero; + result[i][j] = Field(0); for (let k = 0; k < m; k++) { result[i][j] = result[i][j].add(x[i][k].mul(y[k][j])); } diff --git a/src/examples/program.ts b/src/examples/program.ts index fd38a96a6..c536966b4 100644 --- a/src/examples/program.ts +++ b/src/examples/program.ts @@ -8,7 +8,7 @@ let MyProgram = Experimental.ZkProgram({ privateInputs: [], method(publicInput: Field) { - publicInput.assertEquals(Field.zero); + publicInput.assertEquals(Field(0)); }, }, @@ -32,7 +32,7 @@ let { verificationKey } = await MyProgram.compile(); console.log('verification key', verificationKey.slice(0, 10) + '..'); console.log('proving base case...'); -let proof = await MyProgram.baseCase(Field.zero); +let proof = await MyProgram.baseCase(Field(0)); proof = testJsonRoundtrip(proof); console.log('verify...'); @@ -40,7 +40,7 @@ let ok = await verify(proof.toJSON(), verificationKey); console.log('ok?', ok); console.log('proving step 1...'); -proof = await MyProgram.inductiveCase(Field.one, proof); +proof = await MyProgram.inductiveCase(Field(1), proof); proof = testJsonRoundtrip(proof); console.log('verify alternative...'); diff --git a/src/examples/rollup/data_store.ts b/src/examples/rollup/data_store.ts index 752250854..beb241d4e 100644 --- a/src/examples/rollup/data_store.ts +++ b/src/examples/rollup/data_store.ts @@ -64,7 +64,7 @@ export class Keyed { const n = eltTyp.sizeInFields(); const xs = []; for (var i = 0; i < n; ++i) { - xs.push(Field.zero); + xs.push(Field(0)); } return eltTyp.fromFields(xs); })(); diff --git a/src/examples/rollup/merkle_stack.ts b/src/examples/rollup/merkle_stack.ts index 116119a66..462ff6191 100644 --- a/src/examples/rollup/merkle_stack.ts +++ b/src/examples/rollup/merkle_stack.ts @@ -16,7 +16,7 @@ export class MerkleStack { this.values = { computed: false, f }; this.eltTyp = eltTyp; // TODO - this.commitment = Field.zero; + this.commitment = Field(0); } getValues(): Array<[A, Field]> { diff --git a/src/examples/zkapps/dex/erc20.ts b/src/examples/zkapps/dex/erc20.ts index cfa2ec03e..b5179f399 100644 --- a/src/examples/zkapps/dex/erc20.ts +++ b/src/examples/zkapps/dex/erc20.ts @@ -99,7 +99,7 @@ class TrivialCoin extends SmartContract implements Erc20 { for (let i = 0; i < 8; i++) { let state = this.self.update.appState[i]; state.isSome = Bool(true); - state.value = Field.zero; + state.value = Field(0); } // since this is the only method of this zkApp that resets the entire state, provedState: true implies // that this function was run. Since it can be run only once, this implies it was run exactly once diff --git a/src/examples/zkapps/merkle_tree/merkle_zkapp.ts b/src/examples/zkapps/merkle_tree/merkle_zkapp.ts index 035b4e3f9..8a81319f4 100644 --- a/src/examples/zkapps/merkle_tree/merkle_zkapp.ts +++ b/src/examples/zkapps/merkle_tree/merkle_zkapp.ts @@ -58,7 +58,7 @@ class Account extends CircuitValue { } } // we need the initiate tree root in order to tell the contract about our off-chain storage -let initialCommitment: Field = Field.zero; +let initialCommitment: Field = Field(0); /* We want to write a smart contract that serves as a leaderboard, but only has the commitment of the off-chain storage stored in an on-chain variable. diff --git a/src/examples/zkapps/reducer/reducer.ts b/src/examples/zkapps/reducer/reducer.ts index d8cf5f236..25f7525f0 100644 --- a/src/examples/zkapps/reducer/reducer.ts +++ b/src/examples/zkapps/reducer/reducer.ts @@ -14,7 +14,7 @@ import { await isReady; -const INCREMENT = Field.one; +const INCREMENT = Field(1); class CounterZkapp extends SmartContract { // the "reducer" field describes a type of action that we can dispatch, and reduce later @@ -61,7 +61,7 @@ class CounterZkapp extends SmartContract { } const doProofs = true; -const initialCounter = Field.zero; +const initialCounter = Field(0); let Local = Mina.LocalBlockchain(); Mina.setActiveInstance(Local); diff --git a/src/examples/zkapps/reducer/reducer_composite.ts b/src/examples/zkapps/reducer/reducer_composite.ts index d189a0b15..6e00c95d5 100644 --- a/src/examples/zkapps/reducer/reducer_composite.ts +++ b/src/examples/zkapps/reducer/reducer_composite.ts @@ -22,7 +22,7 @@ class MaybeIncrement extends Struct({ isIncrement: Bool, otherData: Field, }) {} -const INCREMENT = { isIncrement: Bool(true), otherData: Field.zero }; +const INCREMENT = { isIncrement: Bool(true), otherData: Field(0) }; class CounterZkapp extends SmartContract { // the "reducer" field describes a type of action that we can dispatch, and reduce later @@ -72,7 +72,7 @@ class CounterZkapp extends SmartContract { } const doProofs = true; -const initialCounter = Field.zero; +const initialCounter = Field(0); let Local = Mina.LocalBlockchain(); Mina.setActiveInstance(Local); diff --git a/src/examples/zkapps/simple_and_counter_zkapp.ts b/src/examples/zkapps/simple_and_counter_zkapp.ts index 3906403ad..f954f187a 100644 --- a/src/examples/zkapps/simple_and_counter_zkapp.ts +++ b/src/examples/zkapps/simple_and_counter_zkapp.ts @@ -29,7 +29,7 @@ const doProofs = true; await isReady; -const INCREMENT = Field.one; +const INCREMENT = Field(1); let offchainStorage = { pendingActions: [] as Field[][], diff --git a/src/examples/zkapps/simple_zkapp_with_proof.ts b/src/examples/zkapps/simple_zkapp_with_proof.ts index 71e6ed739..f8ac5c068 100644 --- a/src/examples/zkapps/simple_zkapp_with_proof.ts +++ b/src/examples/zkapps/simple_zkapp_with_proof.ts @@ -27,7 +27,7 @@ class NotSoSimpleZkapp extends SmartContract { @method init(proof: TrivialProof) { proof.verify(); - this.x.set(Field.one); + this.x.set(Field(1)); } @method update( diff --git a/src/examples/zkapps/voting/demo.ts b/src/examples/zkapps/voting/demo.ts index 921b7f827..fa137c5a2 100644 --- a/src/examples/zkapps/voting/demo.ts +++ b/src/examples/zkapps/voting/demo.ts @@ -103,7 +103,7 @@ try { 0n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(150) ), voterStore @@ -127,7 +127,7 @@ try { 1n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(160) ), voterStore @@ -152,7 +152,7 @@ try { 2n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(170) ), voterStore @@ -191,7 +191,7 @@ try { 0n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(250) ), candidateStore @@ -215,7 +215,7 @@ try { 1n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(400) ), candidateStore diff --git a/src/examples/zkapps/voting/dummyContract.ts b/src/examples/zkapps/voting/dummyContract.ts index 4172439ea..0b4a8f080 100644 --- a/src/examples/zkapps/voting/dummyContract.ts +++ b/src/examples/zkapps/voting/dummyContract.ts @@ -21,7 +21,7 @@ export class DummyContract extends SmartContract { setVerificationKey: Permissions.proofOrSignature(), incrementNonce: Permissions.proofOrSignature(), }); - this.sum.set(Field.zero); + this.sum.set(Field(0)); } /** diff --git a/src/examples/zkapps/voting/member.ts b/src/examples/zkapps/voting/member.ts index 3f9d4d6b5..b878c2cd6 100644 --- a/src/examples/zkapps/voting/member.ts +++ b/src/examples/zkapps/voting/member.ts @@ -13,7 +13,7 @@ import { export class MerkleWitness extends Experimental.MerkleWitness(8) {} let w = { isLeft: false, - sibling: Field.zero, + sibling: Field(0), }; let dummyWitness = Array.from(Array(MerkleWitness.height - 1).keys()).map( () => w @@ -50,7 +50,7 @@ export class Member extends CircuitValue { this.hashVoted = Bool(false); this.accountId = accountId; this.isCandidate = Bool(false); - this.votes = Field.zero; + this.votes = Field(0); this.witness = new MerkleWitness(dummyWitness); this.votesWitness = new MerkleWitness(dummyWitness); @@ -75,7 +75,7 @@ export class Member extends CircuitValue { } static empty() { - return new Member(PublicKey.empty(), Field.zero, UInt64.zero, Field.zero); + return new Member(PublicKey.empty(), Field(0), UInt64.zero, Field(0)); } static from(publicKey: PublicKey, tokenId: Field, balance: UInt64) { diff --git a/src/examples/zkapps/voting/test.ts b/src/examples/zkapps/voting/test.ts index 10421af2f..c06f2594e 100644 --- a/src/examples/zkapps/voting/test.ts +++ b/src/examples/zkapps/voting/test.ts @@ -72,9 +72,9 @@ export async function testSet( let verificationKeySet = await deployContracts( contracts, params, - Field.zero, - Field.zero, - Field.zero, + Field(0), + Field(0), + Field(0), true ); console.log('checking that the tx is valid using default verification key'); @@ -84,7 +84,7 @@ export async function testSet( () => { let m = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(15) ); verificationKeySet.Local.addAccount(m.publicKey, m.balance.toString()); @@ -115,7 +115,7 @@ export async function testSet( () => { let m = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(15) ); verificationKeySet.Local.addAccount(m.publicKey, m.balance.toString()); @@ -152,9 +152,9 @@ export async function testSet( let permissionedSet = await deployContracts( contracts, params, - Field.zero, - Field.zero, - Field.zero + Field(0), + Field(0), + Field(0) ); console.log('checking that the tx is valid using default permissions'); @@ -163,7 +163,7 @@ export async function testSet( () => { let m = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(15) ); permissionedSet.Local.addAccount(m.publicKey, m.balance.toString()); @@ -196,7 +196,7 @@ export async function testSet( () => { let m = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(15) ); permissionedSet.Local.addAccount(m.publicKey, m.balance.toString()); @@ -241,7 +241,7 @@ export async function testSet( let tx = await Mina.transaction(invalidSet.feePayer, () => { let m = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(15) ); invalidSet.Local.addAccount(m.publicKey, m.balance.toString()); @@ -296,7 +296,7 @@ export async function testSet( let tx = await Mina.transaction(sequenceOverflowSet.feePayer, () => { let m = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(15) ); sequenceOverflowSet.Local.addAccount(m.publicKey, m.balance.toString()); @@ -385,7 +385,7 @@ export async function testSet( 0n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(15) ), votersStore, @@ -441,7 +441,7 @@ export async function testSet( () => { let v = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), params.voterPreconditions.minMina.sub(1) ); @@ -457,7 +457,7 @@ export async function testSet( () => { let v = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), params.voterPreconditions.maxMina.add(1) ); @@ -512,7 +512,7 @@ export async function testSet( 0n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), params.candidatePreconditions.minMina.add(1) ), candidatesStore, @@ -534,7 +534,7 @@ export async function testSet( 1n, Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), params.candidatePreconditions.minMina.add(1) ), candidatesStore, @@ -651,7 +651,7 @@ export async function testSet( () => { let lateCandidate = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(200) ); // register late candidate @@ -668,7 +668,7 @@ export async function testSet( () => { let lateVoter = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(50) ); @@ -818,7 +818,7 @@ export async function testSet( // attempting to vote for the registered candidate let fakeCandidate = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), params.candidatePreconditions.minMina.add(1) ); voting.vote(fakeCandidate, votersStore.get(0n)!); @@ -834,7 +834,7 @@ export async function testSet( () => { let fakeVoter = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), UInt64.from(50) ); voting.vote(fakeVoter, votersStore.get(0n)!); @@ -926,7 +926,7 @@ export async function testSet( () => { let voter = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), params.voterPreconditions.minMina.add(1) ); voting.voterRegistration(voter); @@ -942,7 +942,7 @@ export async function testSet( () => { let candidate = Member.from( PrivateKey.random().toPublicKey(), - Field.zero, + Field(0), params.candidatePreconditions.minMina.add(1) ); voting.candidateRegistration(candidate); diff --git a/src/lib/account_update.test.ts b/src/lib/account_update.test.ts index c53789964..b87b9dca8 100644 --- a/src/lib/account_update.test.ts +++ b/src/lib/account_update.test.ts @@ -63,7 +63,7 @@ describe('accountUpdate', () => { expect(accountUpdate2.hash()).toEqual(hash); // if we change something on the cloned accountUpdate, the hash should become different - AccountUpdate.setValue(accountUpdate2.update.appState[0], Field.one); + AccountUpdate.setValue(accountUpdate2.update.appState[0], Field(1)); expect(accountUpdate2.hash()).not.toEqual(hash); }); }); @@ -109,5 +109,5 @@ describe('accountUpdate', () => { // to check that we got something that looks like a Field // note: `instanceof Field` doesn't work function isField(x: any) { - return x?.constructor === Field.one.constructor; + return x?.constructor === Field(1).constructor; } diff --git a/src/lib/account_update.ts b/src/lib/account_update.ts index 8ae190a3c..99a70905c 100644 --- a/src/lib/account_update.ts +++ b/src/lib/account_update.ts @@ -408,10 +408,10 @@ const Body = { return { appState: Array(ZkappStateLength) .fill(0) - .map(() => keep(Field.zero)), + .map(() => keep(Field(0))), delegate: keep(PublicKey.empty()), // TODO - verificationKey: keep({ data: '', hash: Field.zero }), + verificationKey: keep({ data: '', hash: Field(0) }), permissions: keep(Permissions.initial()), // TODO don't hard code zkappUri: keep({ @@ -429,7 +429,7 @@ const Body = { vestingIncrement: UInt64.zero, vestingPeriod: UInt32.zero, }), - votingFor: keep(Field.zero), + votingFor: keep(Field(0)), }; }, @@ -445,7 +445,7 @@ const Body = { events: Events.empty(), sequenceEvents: SequenceEvents.empty(), caller: TokenId.default, - callData: Field.zero, + callData: Field(0), callDepth: 0, preconditions: Preconditions.ignoreAll(), // the default assumption is that snarkyjs transactions don't include the fee payer @@ -498,15 +498,15 @@ type NetworkPrecondition = Preconditions['network']; let NetworkPrecondition = { ignoreAll(): NetworkPrecondition { let stakingEpochData = { - ledger: { hash: ignore(Field.zero), totalCurrency: ignore(uint64()) }, - seed: ignore(Field.zero), - startCheckpoint: ignore(Field.zero), - lockCheckpoint: ignore(Field.zero), + ledger: { hash: ignore(Field(0)), totalCurrency: ignore(uint64()) }, + seed: ignore(Field(0)), + startCheckpoint: ignore(Field(0)), + lockCheckpoint: ignore(Field(0)), epochLength: ignore(uint32()), }; let nextEpochData = cloneCircuitValue(stakingEpochData); return { - snarkedLedgerHash: ignore(Field.zero), + snarkedLedgerHash: ignore(Field(0)), timestamp: ignore(uint64()), blockchainLength: ignore(uint32()), minWindowDensity: ignore(uint32()), @@ -544,12 +544,12 @@ const AccountPrecondition = { ignoreAll(): AccountPrecondition { let appState: Array> = []; for (let i = 0; i < ZkappStateLength; ++i) { - appState.push(ignore(Field.zero)); + appState.push(ignore(Field(0))); } return { balance: ignore(uint64()), nonce: ignore(uint32()), - receiptChainHash: ignore(Field.zero), + receiptChainHash: ignore(Field(0)), delegate: ignore(PublicKey.empty()), state: appState, sequenceState: ignore(SequenceEvents.emptySequenceState()), @@ -590,7 +590,7 @@ const TokenId = { ...Types.TokenId, ...Encoding.TokenId, get default() { - return Field.one; + return Field(1); }, }; @@ -1355,7 +1355,7 @@ const CallForest = { // Mina_base.Zkapp_command.Digest.Forest.empty emptyHash() { - return Field.zero; + return Field(0); }, // similar to Mina_base.Zkapp_command.Call_forest.accumulate_hashes diff --git a/src/lib/circuit_value.ts b/src/lib/circuit_value.ts index 6f599512b..f2ddd6ab8 100644 --- a/src/lib/circuit_value.ts +++ b/src/lib/circuit_value.ts @@ -815,7 +815,7 @@ function opaque({ } // FIXME: the logic in here to check for obj.constructor.name actually doesn't work -// something that works is Field.one.constructor === obj.constructor etc +// something that works is Field(1).constructor === obj.constructor etc function cloneCircuitValue(obj: T): T { // primitive JS types and functions aren't cloned if (typeof obj !== 'object' || obj === null) return obj; @@ -978,7 +978,7 @@ Circuit.switch = function >( if (mask.every((b) => b.toField().isConstant())) checkMask(); else Circuit.asProver(checkMask); let size = type.sizeInFields(); - let fields = Array(size).fill(Field.zero); + let fields = Array(size).fill(Field(0)); for (let i = 0; i < nValues; i++) { let valueFields = type.toFields(values[i]); let maskField = mask[i].toField(); diff --git a/src/lib/circuit_value.unit-test.ts b/src/lib/circuit_value.unit-test.ts index 6f9484406..cc46166fd 100644 --- a/src/lib/circuit_value.unit-test.ts +++ b/src/lib/circuit_value.unit-test.ts @@ -31,7 +31,7 @@ expect(type.sizeInFields()).toEqual(4); // toFields // note that alphabetical order of keys determines ordering here and elsewhere let fields = type.toFields(value); -expect(fields).toEqual([Field.zero, Field.zero, Field.one, Field(2)]); +expect(fields).toEqual([Field(0), Field(0), Field(1), Field(2)]); // toAuxiliary let aux = type.toAuxiliary(value); @@ -40,10 +40,10 @@ expect(aux).toEqual([[[1], [true]], ['arbitrary data!!!'], [], [[], []]]); // toInput let input = type.toInput(value); expect(input).toEqual({ - fields: [Field.zero], + fields: [Field(0)], packed: [ - [Field.zero, 1], - [Field.one, 32], + [Field(0), 1], + [Field(1), 32], [Field(2), 32], ], }); @@ -73,11 +73,11 @@ expect(() => uint: [ UInt32.zero, // invalid Uint32 - new UInt32(Field.minusOne), + new UInt32(Field(-1)), ], })); }) -).toThrow(`Expected ${Field.minusOne} to fit in 32 bits`); +).toThrow(`Expected ${Field(-1)} to fit in 32 bits`); // class version of `provable` class MyStruct extends Struct({ diff --git a/src/lib/encoding.ts b/src/lib/encoding.ts index 4a1013e2d..8a390176e 100644 --- a/src/lib/encoding.ts +++ b/src/lib/encoding.ts @@ -284,7 +284,7 @@ function fieldFromBase58( if (versionNumber !== undefined) bytes.shift(); let uint8array = new Uint8Array(32); uint8array.set(bytes); - return Object.assign(Object.create(Field.one.constructor.prototype), { + return Object.assign(Object.create(Field(1).constructor.prototype), { value: [0, uint8array], }); } diff --git a/src/lib/field.test.ts b/src/lib/field.test.ts index 6fc5dc679..f1b3904ee 100644 --- a/src/lib/field.test.ts +++ b/src/lib/field.test.ts @@ -21,9 +21,9 @@ describe('Field constructor', () => { }); it('handles negative numbers', () => { - expect(Field(-1)).toEqual(Field.one.neg()); + expect(Field(-1)).toEqual(Field(1).neg()); expect(Field(-(2 ** 31))).toEqual(Field(2 ** 31).neg()); - expect(Field.fromNumber(-1)).toEqual(Field.one.neg()); + expect(Field.fromNumber(-1)).toEqual(Field(1).neg()); }); it('throws on fractional numbers', () => { @@ -35,9 +35,9 @@ describe('Field constructor', () => { // Field(bigint), Field.fromBigInt, toBigInt it('handles bigints', () => { - expect(Field(-1n)).toEqual(Field.one.neg()); + expect(Field(-1n)).toEqual(Field(1).neg()); expect(Field.fromBigInt(-1n)).toEqual(Field.fromNumber(-1)); - expect(Field(Field.ORDER - 1n)).toEqual(Field.one.neg()); + expect(Field(Field.ORDER - 1n)).toEqual(Field(1).neg()); expect(Field(1n << 64n).toString()).toEqual('18446744073709551616'); expect(Field.fromBigInt(1n << 64n)).toEqual(Field('18446744073709551616')); }); @@ -47,9 +47,9 @@ describe('Field constructor', () => { describe('Field serialization and static props', () => { it('toBigInt works on static props', () => { - expect(Field.one.toBigInt()).toEqual(1n); - expect(Field.zero.toBigInt()).toEqual(0n); - expect(Field.minusOne.toBigInt()).toEqual(Field.ORDER - 1n); + expect(Field(1).toBigInt()).toEqual(1n); + expect(Field(0).toBigInt()).toEqual(0n); + expect(Field(-1).toBigInt()).toEqual(Field.ORDER - 1n); expect(Field(0xff).toBigInt()).toEqual(0xffn); }); }); diff --git a/src/lib/hash-input.unit-test.ts b/src/lib/hash-input.unit-test.ts index 1b01e740f..07df2fa8c 100644 --- a/src/lib/hash-input.unit-test.ts +++ b/src/lib/hash-input.unit-test.ts @@ -118,12 +118,12 @@ let tokenOwner = PrivateKey.random().toPublicKey(); body.tokenId = new Token({ tokenOwner }).id; body.caller = body.tokenId; let events = Events.empty(); -events = Events.pushEvent(events, [Field.one]); -events = Events.pushEvent(events, [Field.zero]); +events = Events.pushEvent(events, [Field(1)]); +events = Events.pushEvent(events, [Field(0)]); body.events = events; let sequenceEvents = SequenceEvents.empty(); -sequenceEvents = SequenceEvents.pushEvent(sequenceEvents, [Field.one]); -sequenceEvents = SequenceEvents.pushEvent(sequenceEvents, [Field.zero]); +sequenceEvents = SequenceEvents.pushEvent(sequenceEvents, [Field(1)]); +sequenceEvents = SequenceEvents.pushEvent(sequenceEvents, [Field(0)]); body.sequenceEvents = sequenceEvents; testInput(Body, Ledger.hashInputFromJson.body, body); diff --git a/src/lib/hash.ts b/src/lib/hash.ts index 8f21440b6..1caa3a748 100644 --- a/src/lib/hash.ts +++ b/src/lib/hash.ts @@ -47,7 +47,7 @@ const Poseidon = { }, get initialState(): [Field, Field, Field] { - return [Field.zero, Field.zero, Field.zero]; + return [Field(0), Field(0), Field(0)]; }, Sponge, @@ -103,7 +103,7 @@ function prefixToField(prefix: string) { function packToFields({ fields = [], packed = [] }: HashInput) { if (packed.length === 0) return fields; let packedBits = []; - let currentPackedField = Field.zero; + let currentPackedField = Field(0); let currentSize = 0; for (let [field, size] of packed) { currentSize += size; @@ -150,7 +150,7 @@ const TokenSymbolPure: ProvableExtended< }; class TokenSymbol extends Struct(TokenSymbolPure) { static get empty() { - return { symbol: '', field: Field.zero }; + return { symbol: '', field: Field(0) }; } static from(symbol: string): TokenSymbol { diff --git a/src/lib/int.test.ts b/src/lib/int.test.ts index cce4ecb5b..d105493ff 100644 --- a/src/lib/int.test.ts +++ b/src/lib/int.test.ts @@ -27,9 +27,9 @@ describe('int', () => { describe('Int64', () => { describe('toString', () => { - it('should be the same as Field.zero', async () => { + it('should be the same as Field(0)', async () => { const int = new Int64(UInt64.zero, Sign.one); - const field = Field.zero; + const field = Field(0); expect(int.toString()).toEqual(field.toString()); }); @@ -46,8 +46,8 @@ describe('int', () => { }); describe('zero', () => { - it('should be the same as Field zero', async () => { - expect(Int64.zero.magnitude.value).toEqual(Field.zero); + it('should be the same as Field(0)', async () => { + expect(Int64.zero.magnitude.value).toEqual(Field(0)); }); }); @@ -68,7 +68,7 @@ describe('int', () => { describe('neg', () => { it('neg(1)=-1', () => { const int = Int64.one; - expect(int.neg().toField()).toEqual(Field.minusOne); + expect(int.neg().toField()).toEqual(Field(-1)); }); it('neg(2^53-1)=-2^53-1', () => { const int = Int64.fromNumber(NUMBERMAX); @@ -175,24 +175,24 @@ describe('int', () => { }); describe('toFields', () => { - it('toFields(1) should be the same as [Field.one, Field.one]', () => { - expect(Int64.toFields(Int64.one)).toEqual([Field.one, Field.one]); + it('toFields(1) should be the same as [Field(1), Field(1)]', () => { + expect(Int64.toFields(Int64.one)).toEqual([Field(1), Field(1)]); }); it('toFields(2^53-1) should be the same as Field(2^53-1)', () => { expect(Int64.toFields(Int64.fromNumber(NUMBERMAX))).toEqual([ Field(String(NUMBERMAX)), - Field.one, + Field(1), ]); }); }); describe('fromFields', () => { it('fromFields([1, 1]) should be the same as Int64.one', () => { - expect(Int64.fromFields([Field.one, Field.one])).toEqual(Int64.one); + expect(Int64.fromFields([Field(1), Field(1)])).toEqual(Int64.one); }); it('fromFields(2^53-1) should be the same as Field(2^53-1)', () => { - expect(Int64.fromFields([Field(String(NUMBERMAX)), Field.one])).toEqual( + expect(Int64.fromFields([Field(String(NUMBERMAX)), Field(1)])).toEqual( Int64.fromNumber(NUMBERMAX) ); }); @@ -230,8 +230,8 @@ describe('int', () => { it('1+1=2', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.add(y).assertEquals(new UInt64(Field(2))); }); }).not.toThrow(); @@ -262,7 +262,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.MAXINT()); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.add(y); }); }).toThrow(); @@ -273,9 +273,9 @@ describe('int', () => { it('1-1=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); - x.sub(y).assertEquals(new UInt64(Field.zero)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); + x.sub(y).assertEquals(new UInt64(Field(0))); }); }).not.toThrow(); }); @@ -293,8 +293,8 @@ describe('int', () => { it('should throw on sub if results in negative number', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.zero)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(0))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.sub(y); }); }).toThrow(); @@ -305,7 +305,7 @@ describe('int', () => { it('1x2=2', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); const y = Circuit.witness(UInt64, () => new UInt64(Field(2))); x.mul(y).assertEquals(new UInt64(Field(2))); }); @@ -315,9 +315,9 @@ describe('int', () => { it('1x0=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.zero)); - x.mul(y).assertEquals(new UInt64(Field.zero)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(0))); + x.mul(y).assertEquals(new UInt64(Field(0))); }); }).not.toThrow(); }); @@ -336,7 +336,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.MAXINT()); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.mul(y).assertEquals(UInt64.MAXINT()); }); }).not.toThrow(); @@ -358,7 +358,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => new UInt64(Field(2))); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.div(y).assertEquals(new UInt64(Field(2))); }); }).not.toThrow(); @@ -367,9 +367,9 @@ describe('int', () => { it('0/1=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.zero)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); - x.div(y).assertEquals(new UInt64(Field.zero)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(0))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); + x.div(y).assertEquals(new UInt64(Field(0))); }); }).not.toThrow(); }); @@ -388,7 +388,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.MAXINT()); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.div(y).assertEquals(UInt64.MAXINT()); }); }).not.toThrow(); @@ -398,7 +398,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.MAXINT()); - const y = Circuit.witness(UInt64, () => new UInt64(Field.zero)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(0))); x.div(y); }); }).toThrow(); @@ -409,9 +409,9 @@ describe('int', () => { it('1%1=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); - x.mod(y).assertEquals(new UInt64(Field.zero)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); + x.mod(y).assertEquals(new UInt64(Field(0))); }); }).not.toThrow(); }); @@ -431,7 +431,7 @@ describe('int', () => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.MAXINT()); const y = Circuit.witness(UInt64, () => new UInt64(Field(7))); - x.mod(y).assertEquals(new UInt64(Field.one)); + x.mod(y).assertEquals(new UInt64(Field(1))); }); }).not.toThrow(); }); @@ -440,8 +440,8 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.MAXINT()); - const y = Circuit.witness(UInt64, () => new UInt64(Field.zero)); - x.mod(y).assertEquals(new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(0))); + x.mod(y).assertEquals(new UInt64(Field(1))); }); }).toThrow(); }); @@ -451,7 +451,7 @@ describe('int', () => { it('1<2=true', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); const y = Circuit.witness(UInt64, () => new UInt64(Field(2))); x.assertLt(y); }); @@ -461,8 +461,8 @@ describe('int', () => { it('1<1=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertLt(y); }); }).toThrow(); @@ -472,7 +472,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => new UInt64(Field(2))); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertLt(y); }); }).toThrow(); @@ -519,8 +519,8 @@ describe('int', () => { it('1<=1=true', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertLte(y); }); }).not.toThrow(); @@ -530,7 +530,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => new UInt64(Field(2))); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertLte(y); }); }).toThrow(); @@ -578,7 +578,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => new UInt64(Field(2))); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertGt(y); }); }).not.toThrow(); @@ -587,8 +587,8 @@ describe('int', () => { it('1>1=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertGt(y); }); }).toThrow(); @@ -597,7 +597,7 @@ describe('int', () => { it('1>2=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); const y = Circuit.witness(UInt64, () => new UInt64(Field(2))); x.assertGt(y); }); @@ -645,8 +645,8 @@ describe('int', () => { it('1<=1=true', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertGte(y); }); }).not.toThrow(); @@ -655,7 +655,7 @@ describe('int', () => { it('1>=2=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const x = Circuit.witness(UInt64, () => new UInt64(Field(1))); const y = Circuit.witness(UInt64, () => new UInt64(Field(2))); x.assertGte(y); }); @@ -701,11 +701,11 @@ describe('int', () => { describe('from() ', () => { describe('fromNumber()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.fromNumber(1)); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertEquals(y); }); }).not.toThrow(); @@ -727,11 +727,11 @@ describe('int', () => { }); }); describe('fromString()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt64, () => UInt64.fromString('1')); - const y = Circuit.witness(UInt64, () => new UInt64(Field.one)); + const y = Circuit.witness(UInt64, () => new UInt64(Field(1))); x.assertEquals(y); }); }).not.toThrow(); @@ -758,7 +758,7 @@ describe('int', () => { describe('Outside of circuit', () => { describe('add', () => { it('1+1=2', () => { - expect(new UInt64(Field.one).add(1).toString()).toEqual('2'); + expect(new UInt64(Field(1)).add(1).toString()).toEqual('2'); }); it('5000+5000=10000', () => { @@ -770,7 +770,7 @@ describe('int', () => { expect( new UInt64(value) .add(new UInt64(value)) - .add(new UInt64(Field.one)) + .add(new UInt64(Field(1))) .toString() ).toEqual(UInt64.MAXINT().toString()); }); @@ -784,7 +784,7 @@ describe('int', () => { describe('sub', () => { it('1-1=0', () => { - expect(new UInt64(Field.one).sub(1).toString()).toEqual('0'); + expect(new UInt64(Field(1)).sub(1).toString()).toEqual('0'); }); it('10000-5000=5000', () => { @@ -800,11 +800,11 @@ describe('int', () => { describe('mul', () => { it('1x2=2', () => { - expect(new UInt64(Field.one).mul(2).toString()).toEqual('2'); + expect(new UInt64(Field(1)).mul(2).toString()).toEqual('2'); }); it('1x0=0', () => { - expect(new UInt64(Field.one).mul(0).toString()).toEqual('0'); + expect(new UInt64(Field(1)).mul(0).toString()).toEqual('0'); }); it('1000x1000=1000000', () => { @@ -832,7 +832,7 @@ describe('int', () => { }); it('0/1=0', () => { - expect(new UInt64(Field.zero).div(1).toString()).toEqual('0'); + expect(new UInt64(Field(0)).div(1).toString()).toEqual('0'); }); it('2000/1000=2', () => { @@ -854,7 +854,7 @@ describe('int', () => { describe('mod', () => { it('1%1=0', () => { - expect(new UInt64(Field.one).mod(1).toString()).toEqual('0'); + expect(new UInt64(Field(1)).mod(1).toString()).toEqual('0'); }); it('500%32=20', () => { @@ -874,19 +874,19 @@ describe('int', () => { describe('lt', () => { it('1<2=true', () => { - expect(new UInt64(Field.one).lt(new UInt64(Field(2)))).toEqual( + expect(new UInt64(Field(1)).lt(new UInt64(Field(2)))).toEqual( Bool(true) ); }); it('1<1=false', () => { - expect(new UInt64(Field.one).lt(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(1)).lt(new UInt64(Field(1)))).toEqual( Bool(false) ); }); it('2<1=false', () => { - expect(new UInt64(Field(2)).lt(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(2)).lt(new UInt64(Field(1)))).toEqual( Bool(false) ); }); @@ -910,13 +910,13 @@ describe('int', () => { describe('lte', () => { it('1<=1=true', () => { - expect(new UInt64(Field.one).lte(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(1)).lte(new UInt64(Field(1)))).toEqual( Bool(true) ); }); it('2<=1=false', () => { - expect(new UInt64(Field(2)).lte(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(2)).lte(new UInt64(Field(1)))).toEqual( Bool(false) ); }); @@ -941,13 +941,13 @@ describe('int', () => { describe('assertLte', () => { it('1<=1=true', () => { expect(() => { - new UInt64(Field.one).assertLte(new UInt64(Field.one)); + new UInt64(Field(1)).assertLte(new UInt64(Field(1))); }).not.toThrow(); }); it('2<=1=false', () => { expect(() => { - new UInt64(Field(2)).assertLte(new UInt64(Field.one)); + new UInt64(Field(2)).assertLte(new UInt64(Field(1))); }).toThrow(); }); @@ -972,19 +972,19 @@ describe('int', () => { describe('gt', () => { it('2>1=true', () => { - expect(new UInt64(Field(2)).gt(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(2)).gt(new UInt64(Field(1)))).toEqual( Bool(true) ); }); it('1>1=false', () => { - expect(new UInt64(Field.one).gt(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(1)).gt(new UInt64(Field(1)))).toEqual( Bool(false) ); }); it('1>2=false', () => { - expect(new UInt64(Field.one).gt(new UInt64(Field(2)))).toEqual( + expect(new UInt64(Field(1)).gt(new UInt64(Field(2)))).toEqual( Bool(false) ); }); @@ -1008,19 +1008,19 @@ describe('int', () => { describe('gte', () => { it('2>=1=true', () => { - expect(new UInt64(Field(2)).gte(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(2)).gte(new UInt64(Field(1)))).toEqual( Bool(true) ); }); it('1>=1=true', () => { - expect(new UInt64(Field.one).gte(new UInt64(Field.one))).toEqual( + expect(new UInt64(Field(1)).gte(new UInt64(Field(1)))).toEqual( Bool(true) ); }); it('1>=2=false', () => { - expect(new UInt64(Field.one).gte(new UInt64(Field(2)))).toEqual( + expect(new UInt64(Field(1)).gte(new UInt64(Field(2)))).toEqual( Bool(false) ); }); @@ -1045,13 +1045,13 @@ describe('int', () => { describe('assertGt', () => { it('1>1=false', () => { expect(() => { - new UInt64(Field.one).assertGt(new UInt64(Field.one)); + new UInt64(Field(1)).assertGt(new UInt64(Field(1))); }).toThrow(); }); it('2>1=true', () => { expect(() => { - new UInt64(Field(2)).assertGt(new UInt64(Field.one)); + new UInt64(Field(2)).assertGt(new UInt64(Field(1))); }).not.toThrow(); }); @@ -1077,13 +1077,13 @@ describe('int', () => { describe('assertGte', () => { it('1>=1=true', () => { expect(() => { - new UInt64(Field.one).assertGte(new UInt64(Field.one)); + new UInt64(Field(1)).assertGte(new UInt64(Field(1))); }).not.toThrow(); }); it('2>=1=true', () => { expect(() => { - new UInt64(Field(2)).assertGte(new UInt64(Field.one)); + new UInt64(Field(2)).assertGte(new UInt64(Field(1))); }).not.toThrow(); }); @@ -1107,9 +1107,9 @@ describe('int', () => { }); describe('toString()', () => { - it('should be the same as Field.zero', async () => { - const uint64 = new UInt64(Field.zero); - const field = Field.zero; + it('should be the same as Field(0)', async () => { + const uint64 = new UInt64(Field(0)); + const field = Field(0); expect(uint64.toString()).toEqual(field.toString()); }); it('should be the same as 2^53-1', async () => { @@ -1136,9 +1136,9 @@ describe('int', () => { describe('from() ', () => { describe('fromNumber()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { const uint = UInt64.fromNumber(1); - expect(uint.value).toEqual(new UInt64(Field.one).value); + expect(uint.value).toEqual(new UInt64(Field(1)).value); }); it('should be the same as 2^53-1', () => { @@ -1147,9 +1147,9 @@ describe('int', () => { }); }); describe('fromString()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { const uint = UInt64.fromString('1'); - expect(uint.value).toEqual(new UInt64(Field.one).value); + expect(uint.value).toEqual(new UInt64(Field(1)).value); }); it('should be the same as 2^53-1', () => { @@ -1169,8 +1169,8 @@ describe('int', () => { it('1+1=2', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.add(y).assertEquals(new UInt32(Field(2))); }); }).not.toThrow(); @@ -1201,7 +1201,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => UInt32.MAXINT()); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.add(y); }); }).toThrow(); @@ -1212,9 +1212,9 @@ describe('int', () => { it('1-1=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); - x.sub(y).assertEquals(new UInt32(Field.zero)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); + x.sub(y).assertEquals(new UInt32(Field(0))); }); }).not.toThrow(); }); @@ -1232,8 +1232,8 @@ describe('int', () => { it('should throw on sub if results in negative number', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.zero)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(0))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.sub(y); }); }).toThrow(); @@ -1244,7 +1244,7 @@ describe('int', () => { it('1x2=2', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); const y = Circuit.witness(UInt32, () => new UInt32(Field(2))); x.mul(y).assertEquals(new UInt32(Field(2))); }); @@ -1254,9 +1254,9 @@ describe('int', () => { it('1x0=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.zero)); - x.mul(y).assertEquals(new UInt32(Field.zero)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(0))); + x.mul(y).assertEquals(new UInt32(Field(0))); }); }).not.toThrow(); }); @@ -1275,7 +1275,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => UInt32.MAXINT()); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.mul(y).assertEquals(UInt32.MAXINT()); }); }).not.toThrow(); @@ -1297,7 +1297,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => new UInt32(Field(2))); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.div(y).assertEquals(new UInt32(Field(2))); }); }).not.toThrow(); @@ -1306,9 +1306,9 @@ describe('int', () => { it('0/1=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.zero)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); - x.div(y).assertEquals(new UInt32(Field.zero)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(0))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); + x.div(y).assertEquals(new UInt32(Field(0))); }); }).not.toThrow(); }); @@ -1327,7 +1327,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => UInt32.MAXINT()); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.div(y).assertEquals(UInt32.MAXINT()); }); }).not.toThrow(); @@ -1337,7 +1337,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => UInt32.MAXINT()); - const y = Circuit.witness(UInt32, () => new UInt32(Field.zero)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(0))); x.div(y); }); }).toThrow(); @@ -1348,9 +1348,9 @@ describe('int', () => { it('1%1=0', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); - x.mod(y).assertEquals(new UInt32(Field.zero)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); + x.mod(y).assertEquals(new UInt32(Field(0))); }); }).not.toThrow(); }); @@ -1379,8 +1379,8 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => UInt32.MAXINT()); - const y = Circuit.witness(UInt32, () => new UInt32(Field.zero)); - x.mod(y).assertEquals(new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(0))); + x.mod(y).assertEquals(new UInt32(Field(1))); }); }).toThrow(); }); @@ -1390,7 +1390,7 @@ describe('int', () => { it('1<2=true', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); const y = Circuit.witness(UInt32, () => new UInt32(Field(2))); x.assertLt(y); }); @@ -1400,8 +1400,8 @@ describe('int', () => { it('1<1=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertLt(y); }); }).toThrow(); @@ -1411,7 +1411,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => new UInt32(Field(2))); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertLt(y); }); }).toThrow(); @@ -1458,8 +1458,8 @@ describe('int', () => { it('1<=1=true', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertLte(y); }); }).not.toThrow(); @@ -1469,7 +1469,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => new UInt32(Field(2))); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertLte(y); }); }).toThrow(); @@ -1517,7 +1517,7 @@ describe('int', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => new UInt32(Field(2))); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertGt(y); }); }).not.toThrow(); @@ -1526,8 +1526,8 @@ describe('int', () => { it('1>1=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertGt(y); }); }).toThrow(); @@ -1536,7 +1536,7 @@ describe('int', () => { it('1>2=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); const y = Circuit.witness(UInt32, () => new UInt32(Field(2))); x.assertGt(y); }); @@ -1584,8 +1584,8 @@ describe('int', () => { it('1<=1=true', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertGte(y); }); }).not.toThrow(); @@ -1594,7 +1594,7 @@ describe('int', () => { it('1>=2=false', () => { expect(() => { Circuit.runAndCheck(() => { - const x = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const x = Circuit.witness(UInt32, () => new UInt32(Field(1))); const y = Circuit.witness(UInt32, () => new UInt32(Field(2))); x.assertGte(y); }); @@ -1640,11 +1640,11 @@ describe('int', () => { describe('from() ', () => { describe('fromNumber()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => UInt32.fromNumber(1)); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertEquals(y); }); }).not.toThrow(); @@ -1666,11 +1666,11 @@ describe('int', () => { }); }); describe('fromString()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { expect(() => { Circuit.runAndCheck(() => { const x = Circuit.witness(UInt32, () => UInt32.fromString('1')); - const y = Circuit.witness(UInt32, () => new UInt32(Field.one)); + const y = Circuit.witness(UInt32, () => new UInt32(Field(1))); x.assertEquals(y); }); }).not.toThrow(); @@ -1697,7 +1697,7 @@ describe('int', () => { describe('Outside of circuit', () => { describe('add', () => { it('1+1=2', () => { - expect(new UInt32(Field.one).add(1).toString()).toEqual('2'); + expect(new UInt32(Field(1)).add(1).toString()).toEqual('2'); }); it('5000+5000=10000', () => { @@ -1709,7 +1709,7 @@ describe('int', () => { expect( new UInt32(value) .add(new UInt32(value)) - .add(new UInt32(Field.one)) + .add(new UInt32(Field(1))) .toString() ).toEqual(UInt32.MAXINT().toString()); }); @@ -1723,7 +1723,7 @@ describe('int', () => { describe('sub', () => { it('1-1=0', () => { - expect(new UInt32(Field.one).sub(1).toString()).toEqual('0'); + expect(new UInt32(Field(1)).sub(1).toString()).toEqual('0'); }); it('10000-5000=5000', () => { @@ -1739,11 +1739,11 @@ describe('int', () => { describe('mul', () => { it('1x2=2', () => { - expect(new UInt32(Field.one).mul(2).toString()).toEqual('2'); + expect(new UInt32(Field(1)).mul(2).toString()).toEqual('2'); }); it('1x0=0', () => { - expect(new UInt32(Field.one).mul(0).toString()).toEqual('0'); + expect(new UInt32(Field(1)).mul(0).toString()).toEqual('0'); }); it('1000x1000=1000000', () => { @@ -1771,7 +1771,7 @@ describe('int', () => { }); it('0/1=0', () => { - expect(new UInt32(Field.zero).div(1).toString()).toEqual('0'); + expect(new UInt32(Field(0)).div(1).toString()).toEqual('0'); }); it('2000/1000=2', () => { @@ -1793,7 +1793,7 @@ describe('int', () => { describe('mod', () => { it('1%1=0', () => { - expect(new UInt32(Field.one).mod(1).toString()).toEqual('0'); + expect(new UInt32(Field(1)).mod(1).toString()).toEqual('0'); }); it('500%32=20', () => { @@ -1813,19 +1813,19 @@ describe('int', () => { describe('lt', () => { it('1<2=true', () => { - expect(new UInt32(Field.one).lt(new UInt32(Field(2)))).toEqual( + expect(new UInt32(Field(1)).lt(new UInt32(Field(2)))).toEqual( Bool(true) ); }); it('1<1=false', () => { - expect(new UInt32(Field.one).lt(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(1)).lt(new UInt32(Field(1)))).toEqual( Bool(false) ); }); it('2<1=false', () => { - expect(new UInt32(Field(2)).lt(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(2)).lt(new UInt32(Field(1)))).toEqual( Bool(false) ); }); @@ -1849,13 +1849,13 @@ describe('int', () => { describe('lte', () => { it('1<=1=true', () => { - expect(new UInt32(Field.one).lte(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(1)).lte(new UInt32(Field(1)))).toEqual( Bool(true) ); }); it('2<=1=false', () => { - expect(new UInt32(Field(2)).lte(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(2)).lte(new UInt32(Field(1)))).toEqual( Bool(false) ); }); @@ -1880,13 +1880,13 @@ describe('int', () => { describe('assertLte', () => { it('1<=1=true', () => { expect(() => { - new UInt32(Field.one).assertLte(new UInt32(Field.one)); + new UInt32(Field(1)).assertLte(new UInt32(Field(1))); }).not.toThrow(); }); it('2<=1=false', () => { expect(() => { - new UInt32(Field(2)).assertLte(new UInt32(Field.one)); + new UInt32(Field(2)).assertLte(new UInt32(Field(1))); }).toThrow(); }); @@ -1911,19 +1911,19 @@ describe('int', () => { describe('gt', () => { it('2>1=true', () => { - expect(new UInt32(Field(2)).gt(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(2)).gt(new UInt32(Field(1)))).toEqual( Bool(true) ); }); it('1>1=false', () => { - expect(new UInt32(Field.one).gt(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(1)).gt(new UInt32(Field(1)))).toEqual( Bool(false) ); }); it('1>2=false', () => { - expect(new UInt32(Field.one).gt(new UInt32(Field(2)))).toEqual( + expect(new UInt32(Field(1)).gt(new UInt32(Field(2)))).toEqual( Bool(false) ); }); @@ -1948,13 +1948,13 @@ describe('int', () => { describe('assertGt', () => { it('1>1=false', () => { expect(() => { - new UInt32(Field.one).assertGt(new UInt32(Field.one)); + new UInt32(Field(1)).assertGt(new UInt32(Field(1))); }).toThrow(); }); it('2>1=true', () => { expect(() => { - new UInt32(Field(2)).assertGt(new UInt32(Field.one)); + new UInt32(Field(2)).assertGt(new UInt32(Field(1))); }).not.toThrow(); }); @@ -1979,19 +1979,19 @@ describe('int', () => { describe('gte', () => { it('2>=1=true', () => { - expect(new UInt32(Field(2)).gte(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(2)).gte(new UInt32(Field(1)))).toEqual( Bool(true) ); }); it('1>=1=true', () => { - expect(new UInt32(Field.one).gte(new UInt32(Field.one))).toEqual( + expect(new UInt32(Field(1)).gte(new UInt32(Field(1)))).toEqual( Bool(true) ); }); it('1>=2=false', () => { - expect(new UInt32(Field.one).gte(new UInt32(Field(2)))).toEqual( + expect(new UInt32(Field(1)).gte(new UInt32(Field(2)))).toEqual( Bool(false) ); }); @@ -2016,13 +2016,13 @@ describe('int', () => { describe('assertGte', () => { it('1>=1=true', () => { expect(() => { - new UInt32(Field.one).assertGte(new UInt32(Field.one)); + new UInt32(Field(1)).assertGte(new UInt32(Field(1))); }).not.toThrow(); }); it('2>=1=true', () => { expect(() => { - new UInt32(Field(2)).assertGte(new UInt32(Field.one)); + new UInt32(Field(2)).assertGte(new UInt32(Field(1))); }).not.toThrow(); }); @@ -2046,9 +2046,9 @@ describe('int', () => { }); describe('toString()', () => { - it('should be the same as Field.zero', async () => { - const x = new UInt32(Field.zero); - const y = Field.zero; + it('should be the same as Field(0)', async () => { + const x = new UInt32(Field(0)); + const y = Field(0); expect(x.toString()).toEqual(y.toString()); }); it('should be the same as 2^32-1', async () => { @@ -2075,9 +2075,9 @@ describe('int', () => { describe('from() ', () => { describe('fromNumber()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { const x = UInt32.fromNumber(1); - expect(x.value).toEqual(new UInt32(Field.one).value); + expect(x.value).toEqual(new UInt32(Field(1)).value); }); it('should be the same as 2^53-1', () => { @@ -2086,9 +2086,9 @@ describe('int', () => { }); }); describe('fromString()', () => { - it('should be the same as Field.one', () => { + it('should be the same as Field(1)', () => { const x = UInt32.fromString('1'); - expect(x.value).toEqual(new UInt32(Field.one).value); + expect(x.value).toEqual(new UInt32(Field(1)).value); }); it('should be the same as 2^53-1', () => { diff --git a/src/lib/int.ts b/src/lib/int.ts index 6b27fc18d..88ccc88c9 100644 --- a/src/lib/int.ts +++ b/src/lib/int.ts @@ -11,11 +11,11 @@ class UInt64 extends CircuitValue { static NUM_BITS = 64; static get zero() { - return new UInt64(Field.zero); + return new UInt64(Field(0)); } static get one() { - return new UInt64(Field.one); + return new UInt64(Field(1)); } toString() { @@ -203,11 +203,11 @@ class UInt32 extends CircuitValue { static NUM_BITS = 32; static get zero(): UInt32 { - return new UInt32(Field.zero); + return new UInt32(Field(0)); } static get one(): UInt32 { - return new UInt32(Field.one); + return new UInt32(Field(1)); } toString(): string { @@ -377,14 +377,14 @@ class Sign extends CircuitValue { @prop value: Field; // +/- 1 static get one() { - return new Sign(Field.one); + return new Sign(Field(1)); } static get minusOne() { - return new Sign(Field.minusOne); + return new Sign(Field(-1)); } static check(x: Sign) { // x^2 == 1 <=> x == 1 or x == -1 - x.value.square().assertEquals(Field.one); + x.value.square().assertEquals(Field(1)); } static toInput(x: Sign): HashInput { return { packed: [[x.isPositive().toField(), 1]] }; @@ -401,7 +401,7 @@ class Sign extends CircuitValue { return new Sign(this.value.mul(y.value)); } isPositive() { - return this.value.equals(Field.one); + return this.value.equals(Field(1)); } toString() { return this.value.toString(); diff --git a/src/lib/mina.ts b/src/lib/mina.ts index ba13d1dbf..725758982 100644 --- a/src/lib/mina.ts +++ b/src/lib/mina.ts @@ -292,7 +292,7 @@ function LocalBlockchain({ nonce: new UInt32(ledgerAccount.nonce.value), appState: ledgerAccount.zkapp?.appState ?? - Array(ZkappStateLength).fill(Field.zero), + Array(ZkappStateLength).fill(Field(0)), tokenSymbol: ledgerAccount.tokenSymbol, receiptChainHash: ledgerAccount.receiptChainHash, provedState: Bool(ledgerAccount.zkapp?.provedState ?? false), @@ -777,7 +777,7 @@ function dummyAccount(pubkey?: PublicKey): Account { nonce: UInt32.zero, publicKey: pubkey ?? PublicKey.empty(), tokenId: TokenId.default, - appState: Array(ZkappStateLength).fill(Field.zero), + appState: Array(ZkappStateLength).fill(Field(0)), tokenSymbol: '', provedState: Bool(false), receiptChainHash: emptyReceiptChainHash(), @@ -788,14 +788,14 @@ function dummyAccount(pubkey?: PublicKey): Account { function defaultNetworkState(): NetworkValue { let epochData: NetworkValue['stakingEpochData'] = { - ledger: { hash: Field.zero, totalCurrency: UInt64.zero }, - seed: Field.zero, - startCheckpoint: Field.zero, - lockCheckpoint: Field.zero, + ledger: { hash: Field(0), totalCurrency: UInt64.zero }, + seed: Field(0), + startCheckpoint: Field(0), + lockCheckpoint: Field(0), epochLength: UInt32.zero, }; return { - snarkedLedgerHash: Field.zero, + snarkedLedgerHash: Field(0), timestamp: UInt64.zero, blockchainLength: UInt32.zero, minWindowDensity: UInt32.zero, diff --git a/src/lib/proof_system.ts b/src/lib/proof_system.ts index 86eff38c8..88444626f 100644 --- a/src/lib/proof_system.ts +++ b/src/lib/proof_system.ts @@ -562,7 +562,7 @@ function methodArgumentTypesAndValues( function emptyValue(type: Provable) { return type.fromFields( - Array(type.sizeInFields()).fill(Field.zero), + Array(type.sizeInFields()).fill(Field(0)), type.toAuxiliary() ); } diff --git a/src/lib/scalar.test.ts b/src/lib/scalar.test.ts index 506a9cbfb..e84365c79 100644 --- a/src/lib/scalar.test.ts +++ b/src/lib/scalar.test.ts @@ -29,7 +29,7 @@ describe('scalar', () => { it('should return a Scalar', () => { expect(() => { Circuit.runAndCheck(() => { - Circuit.witness(Scalar, () => Scalar.fromFields([Field.one])); + Circuit.witness(Scalar, () => Scalar.fromFields([Field(1)])); }); }).not.toThrow(); }); @@ -128,7 +128,7 @@ describe('scalar', () => { describe('fromFields', () => { it('should return a Scalar', () => { expect(() => { - Scalar.fromFields([Field.one]); + Scalar.fromFields([Field(1)]); }).not.toThrow(); }); }); diff --git a/src/lib/signature.ts b/src/lib/signature.ts index 8cc58f268..acabe2180 100644 --- a/src/lib/signature.ts +++ b/src/lib/signature.ts @@ -88,7 +88,7 @@ class PublicKey extends CircuitValue { } static empty() { - return PublicKey.from({ x: Field.zero, isOdd: Bool(false) }); + return PublicKey.from({ x: Field(0), isOdd: Bool(false) }); } isEmpty() { diff --git a/src/lib/state.ts b/src/lib/state.ts index 97be6f945..1655e0cd0 100644 --- a/src/lib/state.ts +++ b/src/lib/state.ts @@ -223,7 +223,7 @@ function createState(): InternalStateType { } if (account.appState === undefined) { // if the account is not a zkapp account, let the default state be all zeroes - return Array(layout.length).fill(Field.zero); + return Array(layout.length).fill(Field(0)); } else { let stateAsFields: Field[] = []; for (let i = 0; i < layout.length; ++i) { @@ -258,7 +258,7 @@ function createState(): InternalStateType { if (account === undefined) return undefined; let stateAsFields: Field[]; if (account.appState === undefined) { - stateAsFields = Array(layout.length).fill(Field.zero); + stateAsFields = Array(layout.length).fill(Field(0)); } else { stateAsFields = []; for (let i = 0; i < layout.length; i++) { diff --git a/src/lib/string.ts b/src/lib/string.ts index 7c13abce5..9da9eb762 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -57,7 +57,7 @@ class CircuitString extends CircuitValue { private computeLengthAndMask() { let n = this.values.length; // length is the actual, dynamic length - let length = Field.zero; + let length = Field(0); // mask is an array that is true where `this` has its first null character, false elsewhere let mask = []; let wasntNullAlready = Bool(true); @@ -152,7 +152,7 @@ class CircuitString extends CircuitValue { // note: this used to be a custom class, which doesn't work // NullCharacter must use the same circuits as normal Characters -let NullCharacter = () => new Character(Field.zero); +let NullCharacter = () => new Character(Field(0)); function fillWithNull([...values]: Character[], length: number) { let nullChar = NullCharacter(); diff --git a/src/snarky.d.ts b/src/snarky.d.ts index 03cf9087e..0f11543ff 100644 --- a/src/snarky.d.ts +++ b/src/snarky.d.ts @@ -58,7 +58,7 @@ declare class Field { * by -1. * * ```typescript - * const negOne = Field.one.neg(); + * const negOne = Field(1).neg(); * negOne.assertEquals(-1); * ``` */ @@ -69,7 +69,7 @@ declare class Field { * * ```typescript * const invX = x.inv(); - * invX.assertEquals(Field.one.div(x)); + * invX.assertEquals(Field(1).div(x)); * ``` * * @return A field element that is equivalent to one divided by this element. @@ -177,7 +177,7 @@ declare class Field { * Assert that this [[`Field`]] is lower than another Field-like value. * * ```ts - * Field.one.assertLt(2); + * Field(1).assertLt(2); * ``` * * This function can only be called inside a checked computation, like a @@ -188,7 +188,7 @@ declare class Field { * Assert that this [[`Field`]] is lower than or equal to another Field-like value. * * ```ts - * Field.one.assertLte(2); + * Field(1).assertLte(2); * ``` * * This function can only be called inside a checked computation, like a @@ -199,7 +199,7 @@ declare class Field { * Assert that this [[`Field`]] is greater than another Field-like value. * * ```ts - * Field.one.assertGt(0); + * Field(1).assertGt(0); * ``` * * This function can only be called inside a checked computation, like a @@ -210,7 +210,7 @@ declare class Field { * Assert that this [[`Field`]] is greater than or equal to another Field-like value. * * ```ts - * Field.one.assertGte(0); + * Field(1).assertGte(0); * ``` * * This function can only be called inside a checked computation, like a @@ -223,7 +223,7 @@ declare class Field { * Throws an error if the assertion fails. * * ```ts - * Field.one.assertEquals(1); + * Field(1).assertEquals(1); * ``` */ assertEquals(y: Field | number | string | boolean, message?: string): void; @@ -231,7 +231,7 @@ declare class Field { * Assert that this [[`Field`]] is either 0 or 1. * * ```ts - * Field.zero.assertBoolean(); + * Field(0).assertBoolean(); * ``` * * This function can only be called inside a checked computation, like a @@ -272,18 +272,6 @@ declare class Field { // value(this: Field | number | string | boolean): Field; /* Self members */ - /** - * The number 1 as a [[`Field`]]. - */ - static one: Field; - /** - * The number 0 as a [[`Field`]]. - */ - static zero: Field; - /** - * The number -1 as a [[`Field`]]. - */ - static minusOne: Field; /** * The field order as a `bigint`. */ From d45c720c7a217db166af3aa999300f3fc0d457dd Mon Sep 17 00:00:00 2001 From: Brandon Kase Date: Tue, 1 Nov 2022 14:31:41 -0400 Subject: [PATCH 2/2] Deprecates instead of removes Field members --- src/snarky.d.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/snarky.d.ts b/src/snarky.d.ts index 0f11543ff..c40ce4be7 100644 --- a/src/snarky.d.ts +++ b/src/snarky.d.ts @@ -272,6 +272,24 @@ declare class Field { // value(this: Field | number | string | boolean): Field; /* Self members */ + /** + * @deprecated Static constant values on Field are deprecated in favor of using the constructor `Field(1)`. + * + * The number 1 as a [[`Field`]]. + */ + static one: Field; + /** + * @deprecated Static constant values on Field are deprecated in favor of using the constructor `Field(0)`. + * + * The number 0 as a [[`Field`]]. + */ + static zero: Field; + /** + * @deprecated Static constant values on Field are deprecated in favor of using the constructor `Field(-1)`. + * + * The number -1 as a [[`Field`]]. + */ + static minusOne: Field; /** * The field order as a `bigint`. */