Skip to content

Commit

Permalink
feat!: hide dkg public params
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 22, 2023
1 parent ccdeba0 commit 87e237f
Show file tree
Hide file tree
Showing 10 changed files with 689 additions and 705 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"prebuild": "yarn typechain"
},
"dependencies": {
"@nucypher/nucypher-core": "^0.9.0",
"@nucypher/nucypher-core": "../nucypher-core/nucypher-core-wasm/pkg",
"axios": "^0.21.1",
"deep-equal": "^2.2.1",
"ethers": "^5.4.1",
Expand Down
2 changes: 1 addition & 1 deletion src/agents/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const POLYGON: Contracts = {

const MUMBAI: Contracts = {
SUBSCRIPTION_MANAGER: '0xb9015d7b35ce7c81dde38ef7136baa3b1044f313',
COORDINATOR: undefined,
COORDINATOR: '0x0f019Ade1D34399D946CF2f161386362655Dd1A4',
};

const GOERLI: Contracts = {
Expand Down
5 changes: 2 additions & 3 deletions src/agents/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ export class DkgCoordinatorAgent {
): Promise<DkgParticipant[]> {
const Coordinator = await this.connectReadOnly(provider);
// TODO: Remove `as unknown` cast after regenerating the contract types: https://github.com/nucypher/nucypher-contracts/pull/77
return (await Coordinator.getParticipants(
ritualId
)) as unknown as DkgParticipant[];
const participants = await Coordinator.getParticipants(ritualId);
return participants as unknown as DkgParticipant[];
}

public static async getRitual(
Expand Down
24 changes: 3 additions & 21 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
DecryptionSharePrecomputed,
DecryptionShareSimple,
decryptWithSharedSecret,
DkgPublicParameters,
EncryptedThresholdDecryptionRequest,
EncryptedThresholdDecryptionResponse,
SessionSharedSecret,
Expand All @@ -28,7 +27,6 @@ import { Porter } from './porter';
export type CbdTDecDecrypterJSON = {
porterUri: string;
ritualId: number;
dkgPublicParams: Uint8Array;
threshold: number;
};

Expand All @@ -38,15 +36,13 @@ export class CbdTDecDecrypter {
private constructor(
private readonly porter: Porter,
private readonly ritualId: number,
private readonly dkgPublicParams: DkgPublicParameters,
private readonly threshold: number
) {}

public static create(porterUri: string, dkgRitual: DkgRitual) {
return new CbdTDecDecrypter(
new Porter(porterUri),
dkgRitual.id,
dkgRitual.dkgPublicParams,
dkgRitual.threshold
);
}
Expand All @@ -58,7 +54,7 @@ export class CbdTDecDecrypter {
variant: number,
ciphertext: Ciphertext,
aad: Uint8Array
): Promise<readonly Uint8Array[]> {
): Promise<Uint8Array> {
const decryptionShares = await this.retrieve(
provider,
conditionExpr,
Expand All @@ -69,14 +65,7 @@ export class CbdTDecDecrypter {
const combineDecryptionSharesFn =
getCombineDecryptionSharesFunction(variant);
const sharedSecret = combineDecryptionSharesFn(decryptionShares);

const plaintext = decryptWithSharedSecret(
ciphertext,
aad,
sharedSecret,
this.dkgPublicParams
);
return [plaintext];
return decryptWithSharedSecret(ciphertext, aad, sharedSecret);
}

// Retrieve decryption shares
Expand Down Expand Up @@ -205,7 +194,6 @@ export class CbdTDecDecrypter {
return {
porterUri: this.porter.porterUrl.toString(),
ritualId: this.ritualId,
dkgPublicParams: this.dkgPublicParams.toBytes(),
threshold: this.threshold,
};
}
Expand All @@ -217,15 +205,9 @@ export class CbdTDecDecrypter {
public static fromObj({
porterUri,
ritualId,
dkgPublicParams,
threshold,
}: CbdTDecDecrypterJSON) {
return new CbdTDecDecrypter(
new Porter(porterUri),
ritualId,
DkgPublicParameters.fromBytes(dkgPublicParams),
threshold
);
return new CbdTDecDecrypter(new Porter(porterUri), ritualId, threshold);
}

public static fromJSON(json: string) {
Expand Down
35 changes: 16 additions & 19 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import {
DecryptionSharePrecomputed,
DecryptionShareSimple,
DkgPublicKey,
DkgPublicParameters,
SharedSecret,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { bytesEquals } from './utils';
import { bytesEquals, fromHexString } from './utils';

// TODO: Expose from @nucypher/nucypher-core
export enum FerveoVariant {
Expand Down Expand Up @@ -48,51 +47,37 @@ export function getCombineDecryptionSharesFunction(
export interface DkgRitualJSON {
id: number;
dkgPublicKey: Uint8Array;
dkgPublicParams: Uint8Array;
threshold: number;
}

export class DkgRitual {
constructor(
public readonly id: number,
public readonly dkgPublicKey: DkgPublicKey,
public readonly dkgPublicParams: DkgPublicParameters,
public readonly threshold: number
) {}

public toObj(): DkgRitualJSON {
return {
id: this.id,
dkgPublicKey: this.dkgPublicKey.toBytes(),
dkgPublicParams: this.dkgPublicParams.toBytes(),
threshold: this.threshold,
};
}

public static fromObj({
id,
dkgPublicKey,
dkgPublicParams,
threshold,
}: DkgRitualJSON): DkgRitual {
return new DkgRitual(
id,
DkgPublicKey.fromBytes(dkgPublicKey),
DkgPublicParameters.fromBytes(dkgPublicParams),
threshold
);
return new DkgRitual(id, DkgPublicKey.fromBytes(dkgPublicKey), threshold);
}

public equals(other: DkgRitual): boolean {
return (
this.id === other.id &&
// TODO: Replace with `equals` after https://github.com/nucypher/nucypher-core/issues/56 is fixed
bytesEquals(this.dkgPublicKey.toBytes(), other.dkgPublicKey.toBytes()) &&
// TODO: Replace with `equals` after https://github.com/nucypher/nucypher-core/issues/56 is fixed
bytesEquals(
this.dkgPublicParams.toBytes(),
other.dkgPublicParams.toBytes()
)
bytesEquals(this.dkgPublicKey.toBytes(), other.dkgPublicKey.toBytes())
);
}
}
Expand All @@ -112,7 +97,19 @@ export class DkgClient {
throw new Error('Invalid provider');
}
// TODO: Create a new DKG ritual here
throw new Error('Not implemented');
const pkWord1 = fromHexString(
'9045795411ed251bf2eecc9415552c41863502a207104ef7ab482bc2364729d9'
);
console.assert(pkWord1.length === 32);
const pkWord2 = fromHexString('b99e2949cee8d888663b2995fc647fcf');
// We need to concat two words returned by the DKG contract
const dkgPkBytes = new Uint8Array([...pkWord1, ...pkWord2]);
console.assert(dkgPkBytes.length === 48);

return {
id: 0,
dkgPublicKey: DkgPublicKey.fromBytes(dkgPkBytes),
} as DkgRitual;
}

// TODO: Without Validator public key in Coordinator, we cannot verify the
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ import * as conditions from './conditions';
// TODO: Not sure how to re-export this type from the conditions module
export { conditions, CustomContextParam };

// DKG
export { FerveoVariant } from './dkg';

// SDK
export { Cohort } from './sdk/cohort';
export { DeployedPreStrategy, PreStrategy } from './sdk/strategy/pre-strategy';
export { DeployedCbdStrategy, CbdStrategy } from './sdk/strategy/cbd-strategy';

// Re-exports
export {
Expand All @@ -41,4 +45,5 @@ export {
Signer,
TreasureMap,
MessageKit,
Ciphertext,
} from '@nucypher/nucypher-core';
2 changes: 1 addition & 1 deletion test/unit/cbd-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('CbdDeployedStrategy', () => {
expect(getParticipantsSpy).toHaveBeenCalled();
expect(sessionKeySpy).toHaveBeenCalled();
expect(decryptSpy).toHaveBeenCalled();
expect(decryptedMessage[0]).toEqual(toBytes(message));
expect(decryptedMessage).toEqual(toBytes(message));
});

describe('serialization', () => {
Expand Down
19 changes: 19 additions & 0 deletions test/unit/ritual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DkgPublicKey } from '@nucypher/nucypher-core';

import { fromHexString } from '../../src/utils';

describe('Ritual', () => {
it('deserializes premade dkg ritual', async () => {
const pkWord1 = fromHexString(
'9045795411ed251bf2eecc9415552c41863502a207104ef7ab482bc2364729d9'
);
const pkWord2 = fromHexString('b99e2949cee8d888663b2995fc647fcf');

// We need to concat two words returned by the DKG contract
const dkgPkBytes = new Uint8Array([...pkWord1, ...pkWord2]);
expect(dkgPkBytes.length).toEqual(48);

const dkgPk = DkgPublicKey.fromBytes(dkgPkBytes);
expect(dkgPk.toBytes()).toEqual(dkgPkBytes);
});
});
17 changes: 3 additions & 14 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ export const fakeTDecFlow = ({
variant,
ciphertext,
aad,
dkg,
message,
}: FakeDkgRitualFlow) => {
// Having aggregated the transcripts, the validators can now create decryption shares
Expand Down Expand Up @@ -348,12 +347,7 @@ export const fakeTDecFlow = ({
}

// The client should have access to the public parameters of the DKG
const plaintext = decryptWithSharedSecret(
ciphertext,
aad,
sharedSecret,
dkg.publicParams()
);
const plaintext = decryptWithSharedSecret(ciphertext, aad, sharedSecret);
if (!bytesEqual(plaintext, message)) {
throw new Error('Decryption failed');
}
Expand Down Expand Up @@ -500,13 +494,8 @@ export const mockRandomSessionStaticSecret = (secret: SessionStaticSecret) => {

export const fakeRitualId = 0;

export const fakeDkgRitual = (ritual: { dkg: Dkg }, thresold: number) => {
return new DkgRitual(
fakeRitualId,
ritual.dkg.publicKey(),
ritual.dkg.publicParams(),
thresold
);
export const fakeDkgRitual = (ritual: { dkg: Dkg }, threshold: number) => {
return new DkgRitual(fakeRitualId, ritual.dkg.publicKey(), threshold);
};

export const mockInitializeRitual = (fakeRitual: unknown) => {
Expand Down
Loading

0 comments on commit 87e237f

Please sign in to comment.