Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 16, 2023
1 parent 03eb03e commit 8231c49
Show file tree
Hide file tree
Showing 8 changed files with 680 additions and 682 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
7 changes: 1 addition & 6 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ export class CbdTDecDecrypter {
getCombineDecryptionSharesFunction(variant);
const sharedSecret = combineDecryptionSharesFn(decryptionShares);

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

Expand Down
34 changes: 17 additions & 17 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 @@ -47,40 +46,29 @@ export function getCombineDecryptionSharesFunction(
export interface DkgRitualJSON {
id: number;
dkgPublicKey: Uint8Array;
dkgPublicParams: Uint8Array;
}

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

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

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

public equals(other: DkgRitual): boolean {
return (
this.id === other.id &&
bytesEquals(this.dkgPublicKey.toBytes(), other.dkgPublicKey.toBytes()) &&
bytesEquals(
this.dkgPublicParams.toBytes(),
other.dkgPublicParams.toBytes()
)
bytesEquals(this.dkgPublicKey.toBytes(), other.dkgPublicKey.toBytes())
);
}
}
Expand All @@ -100,7 +88,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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export { conditions, CustomContextParam };
// 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 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);
});
});
14 changes: 2 additions & 12 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 @@ -501,11 +495,7 @@ export const mockRandomSessionStaticSecret = (secret: SessionStaticSecret) => {
export const fakeRitualId = 0;

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

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

0 comments on commit 8231c49

Please sign in to comment.