Skip to content

Commit

Permalink
Encode Zero Knowledge Proof Presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptphitraining committed Jun 27, 2024
1 parent 94f3aa8 commit 274123e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
33 changes: 14 additions & 19 deletions src/ZKLottoGame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ describe('ZKLottoGame', () => {

const signature2 = Signature.create(
senderKey,
[gameWeek, PlayerEntryHash]);
[gameWeek, winningHash]);

const txn4 = await Mina.transaction(senderAccount, async () => {
await zkApp.ClaimWinning(senderAccount, signature2, gameWeek, PlayerEntry, senderWitness);
await zkApp.ClaimWinning(senderAccount, signature2, gameWeek, winningHash, senderWitness);
});
await txn4.prove();
await txn4.sign([senderKey]).send();
Expand All @@ -219,52 +219,47 @@ describe('ZKLottoGame', () => {
expect(gameWeek).toEqual(Field(1));

// Play Game
const chosenNumbers = [Field(2), Field(15), Field(19), Field(28), Field(35), Field(42)]
const PlayerEntry = LottoNumbers.from(gameWeek, chosenNumbers);
const PlayerEntryHash = PlayerEntry.hash();
const wrongNumbers = [Field(2), Field(15), Field(20), Field(28), Field(35), Field(43)]
const wrongEntry = LottoNumbers.from(gameWeek, wrongNumbers);
const PlayerWrongEntryHash = wrongEntry.hash();

const signature1 = Signature.create(
senderKey,
[gameWeek, PlayerEntryHash]);
[gameWeek, PlayerWrongEntryHash]);


const txn2 = await Mina.transaction(senderAccount, async () => {
await zkApp.play(senderAccount, signature1, gameWeek, PlayerEntryHash);
await zkApp.play(senderAccount, signature1, gameWeek, PlayerWrongEntryHash);
});
await txn2.prove();
await txn2.sign([senderKey]).send();


//End Game
const lottoEntryInfo = LottoNumbers.from(gameWeek, chosenNumbers);

const winningNumbers = [Field(2), Field(15), Field(19), Field(28), Field(35), Field(42)]
const WinningEntry = LottoNumbers.from(gameWeek, winningNumbers);

const txn3 = await Mina.transaction(senderAccount, async () => {
await zkApp.endLottoWeek(lottoEntryInfo);
await zkApp.endLottoWeek(WinningEntry);
});
await txn3.prove();
await txn3.sign([senderKey]).send();

const winningHash = lottoEntryInfo.hash();
// const updatedNum2 = zkApp.LottoWinHash.get();
const winningHash = WinningEntry.hash();
expect(winningHash).toEqual(zkApp.LottoWinHash.get());


// Claim Wins



const wrongNumbers = [Field(2), Field(15), Field(20), Field(28), Field(35), Field(43)]
const wrongEntry = LottoNumbers.from(gameWeek, wrongNumbers);
const wrongEntryHash = wrongEntry.hash();

const senderWitness = new MerkleWitness8(tree.getWitness(gameWeek.toBigInt()));
const signature2 = Signature.create(
senderKey,
[gameWeek, wrongEntryHash]);
[gameWeek, winningHash]);

try {
const txn4 = await Mina.transaction(senderAccount, async () => {
await zkApp.ClaimWinning(senderAccount, signature2, gameWeek, wrongEntry, senderWitness);
await zkApp.ClaimWinning(senderAccount, signature2, gameWeek, winningHash, senderWitness);
});
await txn4.prove();
await txn4.sign([senderKey]).send();
Expand Down
9 changes: 4 additions & 5 deletions src/ZKLottoGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class LottoNumbers extends Struct({

class ZKLottoGame extends SmartContract {
// The board is serialized as a single field element
@state(Field) lottoboard = State<GameBoard>();
// @state(Field) lottoboard = State<GameBoard>();

//lotto game states
@state(Bool) lottoGameDone = State<Bool>();
Expand Down Expand Up @@ -113,7 +113,6 @@ class ZKLottoGame extends SmartContract {
@method async endLottoWeek(winningNums: LottoNumbers) {
//start lotto game week by increasing by 1 week
//ensure current game week is at least 1 week past previous game week
const currentGameTimeEnd = this.currentGameTimeEnd.get();
this.currentGameTimeEnd.requireEquals(this.currentGameTimeEnd.get());
// this.network.timestamp.get().assertGreaterThanOrEqual(currentGameTimeEnd);

Expand Down Expand Up @@ -170,7 +169,7 @@ class ZKLottoGame extends SmartContract {
let tree = new MerkleTree(8);

let weektoBigInt: BigInt;
Provable.asProver(() => {
Provable.asProver(() => {
weektoBigInt = week_.toBigInt();
tree.setLeaf(week_.toBigInt() % BigInt(256), leaf);
});
Expand All @@ -184,13 +183,13 @@ class ZKLottoGame extends SmartContract {
pubkey: PublicKey,
signature: Signature,
week_: Field,
winningNums: LottoNumbers,
lottoEntryHash: Field,
witness: MerkleWitness8,
) {
const currentRoot = this.storageTreeRoot.get();
this.storageTreeRoot.requireEquals(currentRoot);

const leaf = winningNums.hash();
const leaf = lottoEntryHash;
const isValid = witness.calculateRoot(leaf).equals(currentRoot);
isValid.assertTrue("Invalid proof");

Expand Down

0 comments on commit 274123e

Please sign in to comment.