From a325b5935171ee7c029de18b638b2b319fb25f47 Mon Sep 17 00:00:00 2001 From: Coby Date: Sun, 14 Apr 2024 12:39:58 -0400 Subject: [PATCH] fix provable tests and asyncing code examples --- examples/smart_contract/election/contract.ts | 16 ++--- examples/smart_contract/tokenState/README.md | 7 -- .../smart_contract/tokenState/contract.ts | 62 ---------------- examples/smart_contract/tokenState/run.ts | 71 ------------------- examples/zk_program/age_gate/circuit.ts | 13 ++-- src/lib/PackingPlant.ts | 10 +-- tests/provable/end_to_end.test.ts | 2 +- .../provable/example_packed_string_circuit.ts | 13 +--- tests/provable/example_packed_uint_circuit.ts | 8 +-- 9 files changed, 23 insertions(+), 179 deletions(-) delete mode 100644 examples/smart_contract/tokenState/README.md delete mode 100644 examples/smart_contract/tokenState/contract.ts delete mode 100644 examples/smart_contract/tokenState/run.ts diff --git a/examples/smart_contract/election/contract.ts b/examples/smart_contract/election/contract.ts index 635b7df..40f2a4e 100644 --- a/examples/smart_contract/election/contract.ts +++ b/examples/smart_contract/election/contract.ts @@ -9,7 +9,7 @@ export class Election extends SmartContract { @state(Ballot) ballot3 = State(); @state(Ballot) ballot4 = State(); - init() { + async init() { super.init(); this.ballot1.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n])); this.ballot2.set(Ballot.fromBigInts([0n, 0n, 0n, 0n, 0n, 0n, 0n])); @@ -18,9 +18,9 @@ export class Election extends SmartContract { } @method - castBallot1(vote: Ballot) { + async castBallot1(vote: Ballot) { const unpackedVote = Ballot.unpack(vote.packed); - const ballot1 = this.ballot1.getAndAssertEquals(); + const ballot1 = this.ballot1.getAndRequireEquals(); const unpackedBallot1 = Ballot.unpack(ballot1.packed); let voteSum = UInt32.from(0); @@ -33,9 +33,9 @@ export class Election extends SmartContract { } @method - castBallot2(vote: Ballot) { + async castBallot2(vote: Ballot) { const unpackedVote = Ballot.unpack(vote.packed); - const ballot2 = this.ballot2.getAndAssertEquals(); + const ballot2 = this.ballot2.getAndRequireEquals(); const unpackedBallot2 = Ballot.unpack(ballot2.packed); let voteSum = UInt32.from(0); @@ -50,7 +50,7 @@ export class Election extends SmartContract { @method castBallot3(vote: Ballot) { const unpackedVote = Ballot.unpack(vote.packed); - const ballot3 = this.ballot3.getAndAssertEquals(); + const ballot3 = this.ballot3.getAndRequireEquals(); const unpackedBallot3 = Ballot.unpack(ballot3.packed); let voteSum = UInt32.from(0); @@ -63,9 +63,9 @@ export class Election extends SmartContract { } @method - castBallot4(vote: Ballot) { + async castBallot4(vote: Ballot) { const unpackedVote = Ballot.unpack(vote.packed); - const ballot4 = this.ballot4.getAndAssertEquals(); + const ballot4 = this.ballot4.getAndRequireEquals(); const unpackedBallot4 = Ballot.unpack(ballot4.packed); let voteSum = UInt32.from(0); diff --git a/examples/smart_contract/tokenState/README.md b/examples/smart_contract/tokenState/README.md deleted file mode 100644 index 8d045de..0000000 --- a/examples/smart_contract/tokenState/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Using Tokens as State Smart Contract - -Using o1js-pack, a token can actually be a packed value. Token balances are natively a `UInt64`, so there is not as much room as a full `Field`, but we can still pack 2 `UInt32`, or we could pack 8 characters, or 64 booleans. - -The `mint` and `burn` mechanisms are used like an unspent transaction output system. The state going in is always burnt, and the state coming out is unrelated numerically. In the example in `run.ts`, the token balance is set to 5 when the state is (5, 0). But when the state is (5, 10), the token balance becomes 42949672965. - -This can be useful for per-user state in a zkapp. Booleans could be used to track feature flags or whether or not a user account is active, in good standing, etc... Characters could be used to store shore strings on a per-user basis, perhaps a country code? UInts can be used like in this example to track the first and last time a user has used an app. Or you could use half of the state to track an actual user balance, and the other half to track another number like number of token transactions. diff --git a/examples/smart_contract/tokenState/contract.ts b/examples/smart_contract/tokenState/contract.ts deleted file mode 100644 index 55a0d94..0000000 --- a/examples/smart_contract/tokenState/contract.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { - SmartContract, - state, - State, - method, - UInt32, - UInt64, - Account, - Provable, -} from 'o1js'; -import { PackedUInt32Factory } from '../../../src/index.js'; - -/** - * 2 UInt32s representing the first-joined block and the last-updated block - * Could be used in conjunction with some other app logic to ensure only active users can access some resource, or similar - */ -export class UserBlockStamps extends PackedUInt32Factory(2) {} - -export class UserBlockStampsToken extends SmartContract { - /** - * Initializes the user state by setting the first-joined blockstamp to the current blockheight - */ - @method - joinSystem() { - const account = Account(this.sender, this.token.id); - const currentTokenBalance = account.balance.getAndAssertEquals(); - currentTokenBalance.assertEquals(UInt64.from(0)); - - const userBlockStamps = UserBlockStamps.fromUInt32s([ - this.network.blockchainLength.getAndAssertEquals(), - UInt32.from(0), - ]); - this.token.mint({ - address: this.sender, - amount: UInt64.from(userBlockStamps.packed), - }); - } - - /** - * Updates the user state by setting the last-updated blockstamp to the current blockheight - */ - @method - interactWithSystem() { - const account = Account(this.sender, this.token.id); - const currentTokenBalance = account.balance.getAndAssertEquals(); - const existingUserBlockstamps = UserBlockStamps.unpack( - currentTokenBalance.value - ); - - this.token.burn({ - address: this.sender, - amount: currentTokenBalance, - }); - - existingUserBlockstamps[1] = - this.network.blockchainLength.getAndAssertEquals(); - this.token.mint({ - address: this.sender, - amount: UInt64.from(UserBlockStamps.pack(existingUserBlockstamps)), - }); - } -} diff --git a/examples/smart_contract/tokenState/run.ts b/examples/smart_contract/tokenState/run.ts deleted file mode 100644 index 549509b..0000000 --- a/examples/smart_contract/tokenState/run.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { AccountUpdate, Mina, PrivateKey, UInt32 } from 'o1js'; -import { UserBlockStamps, UserBlockStampsToken } from './contract.js'; - -let Local = Mina.LocalBlockchain({ proofsEnabled: true }); -Mina.setActiveInstance(Local); - -// a test account that pays all the fees, and puts additional funds into the zkapp -let { privateKey: senderKey, publicKey: sender } = Local.testAccounts[0]; - -// the zkapp account -let zkappKey = PrivateKey.random(); -let zkappAddress = zkappKey.toPublicKey(); - -// a special account that is allowed to pull out half of the zkapp balance, once -let privilegedKey = PrivateKey.random(); -let privilegedAddress = privilegedKey.toPublicKey(); - -let initialBalance = 10_000_000_000; -let zkapp = new UserBlockStampsToken(zkappAddress); - -console.time('compile'); -await UserBlockStampsToken.compile(); -console.timeEnd('compile'); - -console.time('deploy'); -let tx = await Mina.transaction(sender, () => { - let senderUpdate = AccountUpdate.fundNewAccount(sender); - senderUpdate.send({ to: zkappAddress, amount: initialBalance }); - zkapp.deploy({ zkappKey }); -}); -await tx.prove(); -await tx.sign([senderKey]).send(); -console.timeEnd('deploy'); - -Local.setBlockchainLength(UInt32.from(5)); - -console.time('join system'); -tx = await Mina.transaction(sender, () => { - AccountUpdate.fundNewAccount(sender); - zkapp.joinSystem(); -}); -await tx.prove(); -await tx.sign([senderKey]).send(); -console.timeEnd('join system'); - -let tokenBalance = Mina.getBalance(sender, zkapp.token.id); -console.log(tokenBalance.toString()); -console.log(UserBlockStamps.unpack(tokenBalance.value).toString()); - -Local.setBlockchainLength(UInt32.from(10)); - -console.time('interact with system'); -tx = await Mina.transaction(sender, () => { - zkapp.interactWithSystem(); -}); -await tx.prove(); -await tx.sign([senderKey]).send(); -console.timeEnd('interact with system'); - -tokenBalance = Mina.getBalance(sender, zkapp.token.id); -console.log(tokenBalance.toString()); -console.log(UserBlockStamps.unpack(tokenBalance.value).toString()); - -// compile: 1.818s -// deploy: 615.559ms -// join system: 23.085s -// 5 -// 5,0 -// interact with system: 15.263s -// 42949672965 -// 5,10 diff --git a/examples/zk_program/age_gate/circuit.ts b/examples/zk_program/age_gate/circuit.ts index 311af7a..d832684 100644 --- a/examples/zk_program/age_gate/circuit.ts +++ b/examples/zk_program/age_gate/circuit.ts @@ -1,11 +1,9 @@ import { Field, - Experimental, + ZkProgram, UInt32, Provable, - Poseidon, SelfProof, - Character, Bool, Empty, } from 'o1js'; @@ -34,19 +32,20 @@ const isOlderThan = (reference: PackedString, toVerify: PackedString): Bool => { return ver.greaterThan(ref); }; -export const AgeGateCircuit = Experimental.ZkProgram({ +export const AgeGateCircuit = ZkProgram({ + name: 'AgeGateCircuit', publicOutput: PackedCounters, methods: { init: { privateInputs: [], - method() { + async method() { return PackedCounters.fromBigInts([0n, 0n]); }, }, verifyAge: { privateInputs: [SelfProof, PackedString], - method( + async method( oldProof: SelfProof, dateToVerify: PackedString ) { @@ -72,4 +71,4 @@ export const AgeGateCircuit = Experimental.ZkProgram({ }, }); -export const AgeGateCircuitProof = Experimental.ZkProgram.Proof(AgeGateCircuit); +export const AgeGateCircuitProof = ZkProgram.Proof(AgeGateCircuit); diff --git a/src/lib/PackingPlant.ts b/src/lib/PackingPlant.ts index 5b8d98a..9625bdb 100644 --- a/src/lib/PackingPlant.ts +++ b/src/lib/PackingPlant.ts @@ -1,12 +1,4 @@ -import { - Field, - Struct, - Poseidon, - provable, - InferProvable, - Provable, - ProvableExtended, -} from 'o1js'; +import { Field, Struct, provable, InferProvable, Provable } from 'o1js'; const MAX_BITS_PER_FIELD = 254n; diff --git a/tests/provable/end_to_end.test.ts b/tests/provable/end_to_end.test.ts index 29f8dd9..0952a2c 100644 --- a/tests/provable/end_to_end.test.ts +++ b/tests/provable/end_to_end.test.ts @@ -4,7 +4,7 @@ import { TextInputProgram, TextInputProof, } from './example_packed_string_circuit'; -import { Character, Poseidon } from 'o1js'; +import { Character } from 'o1js'; describe('End to End Votes Test', () => { const init = [0n, 0n]; diff --git a/tests/provable/example_packed_string_circuit.ts b/tests/provable/example_packed_string_circuit.ts index b6e63dd..1818440 100644 --- a/tests/provable/example_packed_string_circuit.ts +++ b/tests/provable/example_packed_string_circuit.ts @@ -1,11 +1,4 @@ -import { - ZkProgram, - SelfProof, - Character, - Poseidon, - Provable, - Field, -} from 'o1js'; +import { ZkProgram, SelfProof, Character, Provable } from 'o1js'; import { PackedStringFactory } from '../../src/lib/packed-types/PackedString'; export class TextInput extends PackedStringFactory() {} @@ -17,14 +10,14 @@ export const TextInputProgram = ZkProgram({ methods: { init: { privateInputs: [], - method(state: TextInput) { + async method(state: TextInput) { const initState = TextInput.fromString('Mina Protocol'); state.assertEquals(initState); }, }, changeFirstLetter: { privateInputs: [SelfProof, Provable.Array(Character, 31), Character], - method( + async method( newState: TextInput, oldProof: SelfProof, oldCharacters: Array, diff --git a/tests/provable/example_packed_uint_circuit.ts b/tests/provable/example_packed_uint_circuit.ts index 4355929..ef10393 100644 --- a/tests/provable/example_packed_uint_circuit.ts +++ b/tests/provable/example_packed_uint_circuit.ts @@ -10,14 +10,14 @@ export const VotesProgram = ZkProgram({ methods: { init: { privateInputs: [], - method(state: Votes) { + async method(publicInput: Votes) { const initState = Votes.fromBigInts([0n, 0n]); - state.assertEquals(initState); + publicInput.assertEquals(initState); }, }, incrementIndex0: { privateInputs: [SelfProof], - method(newState: Votes, oldProof: SelfProof) { + async method(newState: Votes, oldProof: SelfProof) { oldProof.verify(); const unpacked = Votes.unpack(oldProof.publicInput.packed); unpacked[0] = unpacked[0].add(1); @@ -26,7 +26,7 @@ export const VotesProgram = ZkProgram({ }, incrementIndex1: { privateInputs: [SelfProof], - method(newState: Votes, oldProof: SelfProof) { + async method(newState: Votes, oldProof: SelfProof) { oldProof.verify(); const unpacked = Votes.unpack(oldProof.publicInput.packed); unpacked[1] = unpacked[1].add(1);