Skip to content

Commit

Permalink
Fixed two parties random support
Browse files Browse the repository at this point in the history
  • Loading branch information
asimaranov committed Dec 13, 2024
1 parent 1f339e1 commit 5e2c311
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 87 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"deploy": "npm run build && node build/scripts/deploy.js",
"publish_ipfs_request": "npm run build && node build/scripts/publish_request.js",
"prepare_for_deploy": "npm run build && node rm_build.js && node build/scripts/publish_request.js && node build/scripts/prepare_verification_keys.js ** npm run build",
"deploy_factory": "npm run prepare_for_deploy && node build/scripts/deploy_factory.js"
"deploy_factory": "npm run prepare_for_deploy && node build/scripts/deploy_factory.js",
"prepare_for_deploy_two_parties": "node build/scripts/prepare_verification_keys.js && npm run build && node rm_build.js ** npm run build",
"deploy_factory_two_parties": "npm run prepare_for_deploy_two_parties && node build/scripts/deploy_factory.js"
},
"devDependencies": {
"@babel/preset-env": "^7.16.4",
Expand Down
10 changes: 8 additions & 2 deletions scripts/prepare_verification_keys.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import fs from 'fs';
import { Cache, Field } from 'o1js';
import { ZkonRequestCoordinator, ZkonZkProgram } from 'zkon-zkapp';
import { RandomManager } from '../src/Random/RandomManager.js';
import { RandomManagerTwoParties } from '../src/Random/RandomManagerTwoParties.js';
import { TicketReduceProgram } from '../src/Proofs/TicketReduceProof.js';
import { DistributionProgram } from '../src/Proofs/DistributionProof.js';
import { PLottery } from '../src/PLottery.js';

await ZkonZkProgram.compile({ cache: Cache.FileSystem('cache') });
await ZkonRequestCoordinator.compile({ cache: Cache.FileSystem('cache') });
const { verificationKey: randomManagerVK } = await RandomManager.compile();
const { verificationKey: randomManagerVK } = await RandomManagerTwoParties.compile();
const { verificationKey: randomManagerTwoPartiesVK } = await RandomManagerTwoParties.compile();

await TicketReduceProgram.compile({ cache: Cache.FileSystem('cache') });
await DistributionProgram.compile({ cache: Cache.FileSystem('cache') });
const { verificationKey: PLotteryVK } = await PLottery.compile({
Expand All @@ -20,6 +22,10 @@ const result = {
hash: randomManagerVK.hash.toString(),
data: randomManagerVK.data,
},
randomManagerTwoParties: {
hash: randomManagerTwoPartiesVK.hash.toString(),
data: randomManagerTwoPartiesVK.data,
},
PLotteryVK: {
hash: PLotteryVK.hash.toString(),
data: PLotteryVK.data,
Expand Down
21 changes: 18 additions & 3 deletions src/Tests/MockedContracts/MockedRandomManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { Field, method, PublicKey } from 'o1js';
import { Field, method } from 'o1js';
import { CommitValue, RandomManager } from '../../Random/RandomManager';

export class MockedRandomManager extends RandomManager {
public checkPermission(address: PublicKey) {}
}
@method async mockReceiveZkonResponse(randomValue: Field) {
this.curRandomValue.set(randomValue);
}

@method override async commitValue(commitValue: CommitValue) {
this.permissionCheck();

// this.checkRoundPass();

const currentCommit = this.commit.getAndRequireEquals();
currentCommit.assertEquals(Field(0), 'Already committed');

this.commit.set(commitValue.hash());

// await this.callZkon();
}
}
2 changes: 1 addition & 1 deletion src/Tests/MockedContracts/MockedRandomManagerTwoParties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import { CommitValue, RandomManagerTwoParties } from '../../Random/RandomManager

export class MockedRandomManager extends RandomManagerTwoParties {
public checkPermission(address: PublicKey) {}
}
}
46 changes: 17 additions & 29 deletions src/Tests/PLottery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { dummyBase64Proof } from 'o1js/dist/node/lib/proof-system/zkprogram';
import { Pickles } from 'o1js/dist/node/snarky';
import { PStateManager } from '../StateManager/PStateManager';
import { TicketReduceProgram } from '../Proofs/TicketReduceProof';
import { CommitValue, RandomManagerTwoParties } from '../Random/RandomManagerTwoParties';
import { MockedRandomManager } from './MockedContracts/MockedRandomManagerTwoParties';
import { CommitValue, RandomManager } from '../Random/RandomManager';
import { RandomManagerManager } from '../StateManager/RandomManagerManager';
import { MockedRandomManager } from './MockedContracts/MockedRandomManager';
import { FactoryManager } from '../StateManager/FactoryStateManager';
import { MerkleMap20 } from '../Structs/CustomMerkleMap';
import { MockedPlotteryFactory } from './MockedContracts/MockedFactory';
Expand Down Expand Up @@ -55,21 +56,15 @@ export async function mockProof<I, O, P>(
});
}

const testCommitValue = {
v1: new CommitValue({
value: Field(123),
salt: Field(456),
}),
v2: new CommitValue({
value: Field(654),
salt: Field(117),
}),
};
const testCommitValue = new CommitValue({
value: Field(123),
salt: Field(456),
});

const testVRFValue = Field(789);

const testWinningCombination = generateNumbersSeed(
Poseidon.hash([testCommitValue.v1.value, testCommitValue.v2.value])
Poseidon.hash([testCommitValue.value, testVRFValue])
);

const ROUNDS = 10;
Expand Down Expand Up @@ -147,32 +142,25 @@ describe('Add', () => {
let randomManager = randomManagers[round];
let rmStateManager = factoryManager.randomManagers[round];
let tx = await Mina.transaction(deployerAccount, async () => {
await randomManager.firstPartyCommit(testCommitValue.v1);
await randomManager.commitValue(testCommitValue);
});
await tx.prove();
await tx.sign([deployerKey]).send();

let tx2 = await Mina.transaction(deployerAccount, async () => {
await randomManager.secondPartyCommit(testCommitValue.v2);
});
await tx2.prove();
await tx2.sign([deployerKey]).send();
// rmStateManager.addCommit(testCommitValue);
rmStateManager.addCommit(testCommitValue);
};
produceResultInRM = async (round: number) => {
let randomManager = randomManagers[round];

let tx = await Mina.transaction(deployerAccount, async () => {
await randomManager.mockReceiveZkonResponse(testVRFValue);
});
await tx.prove();
await tx.sign([deployerKey]).send();
let tx2 = await Mina.transaction(deployerAccount, async () => {
await randomManager.revealFirstCommit(testCommitValue.v1);
await randomManager.reveal(testCommitValue);
});
await tx2.prove();
await tx2.sign([deployerKey]).send();

let tx3 = await Mina.transaction(deployerAccount, async () => {
await randomManager.revealSecondCommit(testCommitValue.v2);
});
await tx3.prove();
await tx3.sign([deployerKey]).send();
};

deployRound = async (round: number) => {
Expand Down Expand Up @@ -494,4 +482,4 @@ describe('Add', () => {
}
}
});
});
});
72 changes: 25 additions & 47 deletions src/Tests/Random.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ import { DistributionProgram } from '../Proofs/DistributionProof';
import { MockedRandomManager } from './MockedContracts/MockedRandomManager';
import { MockedPlotteryFactory } from './MockedContracts/MockedFactory';

const testCommitValues = [...Array(10)].map((_, i) => {
return {
v1: new CommitValue({ value: Field(i + 1), salt: Field.random() }),
v2: new CommitValue({ value: Field(2 * i + 1), salt: Field.random() }),
};
});
const testCommitValues = [...Array(10)].map(
(_, i) => new CommitValue({ value: Field(i), salt: Field.random() })
);

const testVRFValues = [...Array(10)].map((_, i) =>
Field(Poseidon.hash([Field(i)]))
Expand All @@ -57,15 +54,11 @@ describe('Add', () => {
randomManager: MockedRandomManager,
factoryManager: FactoryManager,
mineNBlocks: (n: number) => void,
commitValue: (
round: number,
commitValue1: CommitValue,
commitValue2: CommitValue
) => Promise<void>,
commitValue: (round: number, commitValue: CommitValue) => Promise<void>,
produceResultInRM: (
round: number,
commitValue1: CommitValue,
commitValue2: CommitValue
vrfValue: Field,
commitValue: CommitValue
) => Promise<void>;
beforeAll(async () => {
if (proofsEnabled) {
Expand Down Expand Up @@ -95,41 +88,29 @@ describe('Add', () => {
let curAmount = Local.getNetworkState().globalSlotSinceGenesis;
Local.setGlobalSlot(curAmount.add(n));
};
commitValue = async (
round: number,
commitValue1: CommitValue,
commitValue2: CommitValue
) => {
commitValue = async (round: number, commitValue: CommitValue) => {
let tx = Mina.transaction(deployerAccount, async () => {
randomManager.firstPartyCommit(commitValue1);
randomManager.commitValue(commitValue);
});
await tx.prove();
await tx.sign([deployerKey]).send();

let tx2 = Mina.transaction(deployerAccount, async () => {
randomManager.secondPartyCommit(commitValue2);
});
await tx2.prove();
await tx2.sign([deployerKey]).send();

// factoryManager.randomManagers[round].addCommit(commitValue);
factoryManager.randomManagers[round].addCommit(commitValue);
};
produceResultInRM = async (
round: number,
commitValue1: CommitValue,
commitValue2: CommitValue
vrfValue: Field,
commitValue: CommitValue
) => {
let tx = Mina.transaction(deployerAccount, async () => {
randomManager.mockReceiveZkonResponse(vrfValue);
});
await tx.prove();
await tx.sign([deployerKey]).send();
let tx2 = Mina.transaction(deployerAccount, async () => {
randomManager.revealFirstCommit(commitValue1);
randomManager.reveal(commitValue);
});
await tx2.prove();
await tx2.sign([deployerKey]).send();

let tx3 = Mina.transaction(deployerAccount, async () => {
randomManager.revealSecondCommit(commitValue2);
});
await tx3.prove();
await tx3.sign([deployerKey]).send();
};
});
async function localDeploy() {
Expand Down Expand Up @@ -167,10 +148,10 @@ describe('Add', () => {
factoryManager.roundsMap.getRoot()
);

// expect(randomManager.startSlot.get()).toEqual(UInt32.from(0));
// expect(randomManager.commit.get()).toEqual(Field(0));
// expect(randomManager.result.get()).toEqual(Field(0));
// expect(randomManager.curRandomValue.get()).toEqual(Field(0));
expect(randomManager.startSlot.get()).toEqual(UInt32.from(0));
expect(randomManager.commit.get()).toEqual(Field(0));
expect(randomManager.result.get()).toEqual(Field(0));
expect(randomManager.curRandomValue.get()).toEqual(Field(0));
});

it('Should produce random value', async () => {
Expand All @@ -179,16 +160,13 @@ describe('Add', () => {
// for (let i = 0; i < 10; i++) {
let i = 0;
// console.log(i);
await commitValue(i, testCommitValues[i].v1, testCommitValues[i].v2);
await commitValue(i, testCommitValues[i]);

mineNBlocks(BLOCK_PER_ROUND + 1);

await produceResultInRM(i, testCommitValues[i].v1, testCommitValues[i].v2);
await produceResultInRM(i, testVRFValues[i], testCommitValues[i]);

const seed = Poseidon.hash([
testCommitValues[i].v1.value,
testCommitValues[i].v2.value,
]);
const seed = Poseidon.hash([testCommitValues[i].value, testVRFValues[i]]);
expect(randomManager.result.get()).toEqual(seed);
// }
});
Expand All @@ -202,4 +180,4 @@ describe('Add', () => {

// expect(rmStateManager.commit).toEqual(copy.commit);
// });
});
});
Loading

0 comments on commit 5e2c311

Please sign in to comment.