Skip to content

Commit

Permalink
test local verification
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 29, 2023
1 parent c982702 commit c6b1070
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 27 deletions.
40 changes: 24 additions & 16 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,6 @@ export class DkgRitual {
}
}

// TODO: Without Validator public key in Coordinator, we cannot verify the
// transcript. We need to add it to the Coordinator (nucypher-contracts #77).
const participantPublicKeys: Record<string, FerveoPublicKey> = {
'0x210eeAC07542F815ebB6FD6689637D8cA2689392': FerveoPublicKey.fromBytes(
fromHexString(
'6000000000000000ace9d7567b26dafc512b2303cfdaa872850c62b100078ddeaabf8408c7308b3a43dfeb88375c21ef63230fb4008ce7e908764463c6765e556f9b03009eb1757d179eaa26bf875332807cc070d62a385ed2e66e09f4f4766451da12779a09036e'
)
),
'0xb15d5A4e2be34f4bE154A1b08a94Ab920FfD8A41': FerveoPublicKey.fromBytes(
fromHexString(
'60000000000000008b373fdb6b43e9dca028bd603c2bf90f0e008ec83ff217a8d7bc006b585570e6ab1ce761bad0e21c1aed1363286145f61134ed0ab53f4ebaa05036396c57f6e587f33d49667c1003cd03b71ad651b09dd4791bc631eaef93f1b313bbee7bd63a'
)
),
};

export class DkgClient {
// TODO: Update API: Replace with getExistingRitual and support ritualId in Strategy
public static async initializeRitual(
Expand Down Expand Up @@ -149,12 +134,35 @@ export class DkgClient {
const validatorAddress = EthereumAddress.fromString(p.provider);
// TODO: Replace with real keys
// const publicKey = FerveoPublicKey.fromBytes(fromHexString(p.???));
const publicKey = participantPublicKeys[p.provider];
const publicKey = DkgClient.getParticipantPublicKey(p.provider);
const validator = new Validator(validatorAddress, publicKey);
return new ValidatorMessage(validator, p.transcript);
});
const aggregate = new AggregatedTranscript(validatorMessages);

return aggregate.verify(ritual.dkgSize, validatorMessages);
}

public static getParticipantPublicKey = (address: string) => {
// TODO: Without Validator public key in Coordinator, we cannot verify the
// transcript. We need to add it to the Coordinator (nucypher-contracts #77).
const participantPublicKeys: Record<string, FerveoPublicKey> = {
'0x210eeAC07542F815ebB6FD6689637D8cA2689392': FerveoPublicKey.fromBytes(
fromHexString(
'6000000000000000ace9d7567b26dafc512b2303cfdaa872850c62b100078ddeaabf8408c7308b3a43dfeb88375c21ef63230fb4008ce7e908764463c6765e556f9b03009eb1757d179eaa26bf875332807cc070d62a385ed2e66e09f4f4766451da12779a09036e'
)
),
'0xb15d5A4e2be34f4bE154A1b08a94Ab920FfD8A41': FerveoPublicKey.fromBytes(
fromHexString(
'60000000000000008b373fdb6b43e9dca028bd603c2bf90f0e008ec83ff217a8d7bc006b585570e6ab1ce761bad0e21c1aed1363286145f61134ed0ab53f4ebaa05036396c57f6e587f33d49667c1003cd03b71ad651b09dd4791bc631eaef93f1b313bbee7bd63a'
)
),
};

const publicKey = participantPublicKeys[address];
if (!publicKey) {
throw new Error(`No public key for participant: ${address}`);
}
return publicKey;
};
}
52 changes: 41 additions & 11 deletions test/integration/dkg-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { SecretKey } from '@nucypher/nucypher-core';

import { DkgCoordinatorAgent } from '../../src/agents/coordinator';
import { DkgClient } from '../../src/dkg';
import {
fakeCoordinatorRitual,
fakeDkgParticipants,
fakeRitualId,
fakeWeb3Provider,
mockGetParticipantPublicKey,
mockGetParticipants,
mockVerifyRitual,
} from '../utils';

jest.mock('../../src/agents/coordinator', () => ({
DkgCoordinatorAgent: {
getRitual: () => Promise.resolve(fakeCoordinatorRitual(fakeRitualId)),
getParticipants: () => Promise.resolve(fakeDkgParticipants(fakeRitualId)),
getParticipants: () =>
Promise.resolve(fakeDkgParticipants(fakeRitualId).participants),
},
}));

Expand Down Expand Up @@ -42,13 +46,39 @@ describe('DkgCoordinatorAgent', () => {
});
});

// TODO: Fix this test after the DkgClient.verifyRitual() method is implemented
// describe('DkgClient', () => {
// it('verifies the dkg ritual', async () => {
// const provider = fakeWeb3Provider(SecretKey.random().toBEBytes());
//
// const dkgClient = new DkgClient(provider);
// const isValid = await dkgClient.verifyRitual(fakeRitualId);
// expect(isValid).toBeTruthy();
// });
// });
describe('DkgClient', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('verifies the dkg ritual', async () => {
const provider = fakeWeb3Provider(SecretKey.random().toBEBytes());
const verifyRitualSpy = mockVerifyRitual();

const isValid = await DkgClient.verifyRitual(provider, fakeRitualId);
expect(isValid).toBeTruthy();
expect(verifyRitualSpy).toHaveBeenCalled();
});

it('rejects on missing participant pk', async () => {
const provider = fakeWeb3Provider(SecretKey.random().toBEBytes());

await expect(async () =>
DkgClient.verifyRitual(provider, fakeRitualId)
).rejects.toThrow(
'No public key for participant: 0x0000000000000000000000000000000000000000'
);
});

it('rejects on bad participant pk', async () => {
const provider = fakeWeb3Provider(SecretKey.random().toBEBytes());
const getParticipantPublicKeysSpy = mockGetParticipantPublicKey();

await expect(async () =>
DkgClient.verifyRitual(provider, fakeRitualId)
).rejects.toThrow(
"Transcript aggregate doesn't match the received PVSS instances"
);
expect(getParticipantPublicKeysSpy).toHaveBeenCalled();
});
});
12 changes: 12 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EncryptedThresholdDecryptionResponse,
EncryptedTreasureMap,
ferveoEncrypt,
FerveoPublicKey,
PublicKey,
reencrypt,
SecretKey,
Expand Down Expand Up @@ -540,3 +541,14 @@ export const mockVerifyRitual = (isValid = true) => {
(_provider, _ritualId) => Promise.resolve(isValid)
);
};

export const mockGetParticipantPublicKey = (pk = fakeFerveoPublicKey()) => {
return jest.spyOn(DkgClient, 'getParticipantPublicKey').mockImplementation(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(_address) => pk
);
};

export const fakeFerveoPublicKey = (): FerveoPublicKey => {
return Keypair.random().publicKey;
};

1 comment on commit c6b1070

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/kits: 19.53 KB
build/module/src/agents: 35.16 KB
build/module/src/characters: 89.84 KB
build/module/src/policies: 19.53 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src: 437.50 KB
build/module/test: 46.88 KB
build/module: 695.31 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/kits: 19.53 KB
build/main/src/agents: 35.16 KB
build/main/src/characters: 89.84 KB
build/main/src/policies: 19.53 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src: 441.41 KB
build/main/test: 46.88 KB
build/main: 699.22 KB
build: 1.37 MB

Please sign in to comment.