Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Aug 7, 2023
2 parents 1ad1ad9 + a638ce5 commit 771df6c
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 360 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.10.0",
"@nucypher/nucypher-core": "^0.11.0",
"axios": "^0.21.1",
"deep-equal": "^2.2.1",
"ethers": "^5.4.1",
Expand Down
52 changes: 41 additions & 11 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import {
decryptWithSharedSecret,
EncryptedThresholdDecryptionRequest,
EncryptedThresholdDecryptionResponse,
FerveoVariant,
SessionSharedSecret,
SessionStaticSecret,
ThresholdDecryptionRequest,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { DkgCoordinatorAgent, DkgParticipant } from '../agents/coordinator';
import {
DkgCoordinatorAgent,
DkgParticipant,
DkgRitualState,
} from '../agents/coordinator';
import { ConditionExpression } from '../conditions';
import {
DkgClient,
DkgRitual,
FerveoVariant,
getCombineDecryptionSharesFunction,
getVariantClass,
} from '../dkg';
Expand Down Expand Up @@ -52,13 +57,15 @@ export class CbdTDecDecrypter {
provider: ethers.providers.Web3Provider,
conditionExpr: ConditionExpression,
variant: FerveoVariant,
ciphertext: Ciphertext
ciphertext: Ciphertext,
verifyRitual = true
): Promise<Uint8Array> {
const decryptionShares = await this.retrieve(
provider,
conditionExpr,
variant,
ciphertext
ciphertext,
verifyRitual
);

const combineDecryptionSharesFn =
Expand All @@ -73,16 +80,39 @@ export class CbdTDecDecrypter {

// Retrieve decryption shares
public async retrieve(
provider: ethers.providers.Web3Provider,
web3Provider: ethers.providers.Web3Provider,
conditionExpr: ConditionExpression,
variant: number,
ciphertext: Ciphertext
variant: FerveoVariant,
ciphertext: Ciphertext,
verifyRitual = true
): Promise<DecryptionSharePrecomputed[] | DecryptionShareSimple[]> {
const ritualState = await DkgCoordinatorAgent.getRitualState(
web3Provider,
this.ritualId
);
if (ritualState !== DkgRitualState.FINALIZED) {
throw new Error(
`Ritual with id ${this.ritualId} is not finalized. Ritual state is ${ritualState}.`
);
}

if (verifyRitual) {
const isLocallyVerified = await DkgClient.verifyRitual(
web3Provider,
this.ritualId
);
if (!isLocallyVerified) {
throw new Error(
`Ritual with id ${this.ritualId} has failed local verification.`
);
}
}

const dkgParticipants = await DkgCoordinatorAgent.getParticipants(
provider,
web3Provider,
this.ritualId
);
const contextStr = await conditionExpr.buildContext(provider).toJson();
const contextStr = await conditionExpr.buildContext(web3Provider).toJson();
const { sharedSecrets, encryptedRequests } = this.makeDecryptionRequests(
this.ritualId,
variant,
Expand Down Expand Up @@ -115,7 +145,7 @@ export class CbdTDecDecrypter {
private makeDecryptionShares(
encryptedResponses: Record<string, EncryptedThresholdDecryptionResponse>,
sessionSharedSecret: Record<string, SessionSharedSecret>,
variant: number,
variant: FerveoVariant,
expectedRitualId: number
) {
const decryptedResponses = Object.entries(encryptedResponses).map(
Expand All @@ -141,7 +171,7 @@ export class CbdTDecDecrypter {

private makeDecryptionRequests(
ritualId: number,
variant: number,
variant: FerveoVariant,
ciphertext: Ciphertext,
conditionExpr: ConditionExpression,
contextStr: string,
Expand Down
37 changes: 15 additions & 22 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DkgPublicKey,
EthereumAddress,
FerveoPublicKey,
FerveoVariant,
SharedSecret,
Validator,
ValidatorMessage,
Expand All @@ -17,22 +18,15 @@ import { DkgCoordinatorAgent, DkgRitualState } from './agents/coordinator';
import { ChecksumAddress } from './types';
import { bytesEquals, fromHexString, objectEquals } from './utils';

// TODO: Expose from @nucypher/nucypher-core
export enum FerveoVariant {
Simple = 0,
Precomputed = 1,
}

export function getVariantClass(
variant: FerveoVariant
): typeof DecryptionShareSimple | typeof DecryptionSharePrecomputed {
switch (variant) {
case FerveoVariant.Simple:
return DecryptionShareSimple;
case FerveoVariant.Precomputed:
return DecryptionSharePrecomputed;
default:
throw new Error(`Invalid FerveoVariant: ${variant}`);
if (variant.equals(FerveoVariant.simple)) {
return DecryptionShareSimple;
} else if (variant.equals(FerveoVariant.precomputed)) {
return DecryptionSharePrecomputed;
} else {
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}

Expand All @@ -41,13 +35,12 @@ export function getCombineDecryptionSharesFunction(
): (
shares: DecryptionShareSimple[] | DecryptionSharePrecomputed[]
) => SharedSecret {
switch (variant) {
case FerveoVariant.Simple:
return combineDecryptionSharesSimple;
case FerveoVariant.Precomputed:
return combineDecryptionSharesPrecomputed;
default:
throw new Error(`Invalid FerveoVariant: ${variant}`);
if (variant.equals(FerveoVariant.simple)) {
return combineDecryptionSharesSimple;
} else if (variant.equals(FerveoVariant.precomputed)) {
return combineDecryptionSharesPrecomputed;
} else {
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}

Expand Down Expand Up @@ -209,12 +202,12 @@ export class DkgClient {
const participantPublicKeys: Record<string, FerveoPublicKey> = {
'0x210eeAC07542F815ebB6FD6689637D8cA2689392': FerveoPublicKey.fromBytes(
fromHexString(
'6000000000000000ace9d7567b26dafc512b2303cfdaa872850c62b100078ddeaabf8408c7308b3a43dfeb88375c21ef63230fb4008ce7e908764463c6765e556f9b03009eb1757d179eaa26bf875332807cc070d62a385ed2e66e09f4f4766451da12779a09036e'
'ace9d7567b26dafc512b2303cfdaa872850c62b100078ddeaabf8408c7308b3a43dfeb88375c21ef63230fb4008ce7e908764463c6765e556f9b03009eb1757d179eaa26bf875332807cc070d62a385ed2e66e09f4f4766451da12779a09036e'
)
),
'0xb15d5A4e2be34f4bE154A1b08a94Ab920FfD8A41': FerveoPublicKey.fromBytes(
fromHexString(
'60000000000000008b373fdb6b43e9dca028bd603c2bf90f0e008ec83ff217a8d7bc006b585570e6ab1ce761bad0e21c1aed1363286145f61134ed0ab53f4ebaa05036396c57f6e587f33d49667c1003cd03b71ad651b09dd4791bc631eaef93f1b313bbee7bd63a'
'8b373fdb6b43e9dca028bd603c2bf90f0e008ec83ff217a8d7bc006b585570e6ab1ce761bad0e21c1aed1363286145f61134ed0ab53f4ebaa05036396c57f6e587f33d49667c1003cd03b71ad651b09dd4791bc631eaef93f1b313bbee7bd63a'
)
),
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as conditions from './conditions';
export { conditions, CustomContextParam };

// DKG
export { FerveoVariant } from './dkg';
export { FerveoVariant } from '@nucypher/nucypher-core';

// SDK
export { Cohort } from './sdk/cohort';
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cbd-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const ownsNFT = new ERC721Ownership({
});
const conditionExpr = new ConditionExpression(ownsNFT);
const ursulas = fakeUrsulas();
const variant = FerveoVariant.Precomputed;
const variant = FerveoVariant.precomputed;
const ritualId = 0;

const makeCbdStrategy = async () => {
Expand Down
25 changes: 15 additions & 10 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
EthereumAddress,
ferveoEncrypt,
FerveoPublicKey,
FerveoVariant,
Keypair,
PublicKey,
reencrypt,
Expand Down Expand Up @@ -51,7 +52,7 @@ import {
RetrieveCFragsResult,
Ursula,
} from '../src/characters/porter';
import { DkgClient, DkgRitual, FerveoVariant } from '../src/dkg';
import { DkgClient, DkgRitual } from '../src/dkg';
import { BlockchainPolicy, PreEnactedPolicy } from '../src/policies/policy';
import { ChecksumAddress } from '../src/types';
import { toBytes, toHexString, zip } from '../src/utils';
Expand Down Expand Up @@ -219,14 +220,14 @@ export const mockDetectEthereumProvider = () => {
};

export const fakeDkgFlow = (
variant: FerveoVariant | FerveoVariant.Precomputed,
variant: FerveoVariant,
ritualId: number,
sharesNum: number,
threshold: number
) => {
if (
variant !== FerveoVariant.Simple &&
variant !== FerveoVariant.Precomputed
!variant.equals(FerveoVariant.simple) &&
!variant.equals(FerveoVariant.precomputed)
) {
throw new Error(`Invalid variant: ${variant}`);
}
Expand Down Expand Up @@ -323,20 +324,22 @@ export const fakeTDecFlow = ({
}

let decryptionShare;
if (variant === FerveoVariant.Precomputed) {
if (variant.equals(FerveoVariant.precomputed)) {
decryptionShare = aggregate.createDecryptionSharePrecomputed(
dkg,
ciphertext,
aad,
keypair
);
} else {
} else if (variant.equals(FerveoVariant.simple)) {
decryptionShare = aggregate.createDecryptionShareSimple(
dkg,
ciphertext,
aad,
keypair
);
} else {
throw new Error(`Invalid variant: ${variant}`);
}
decryptionShares.push(decryptionShare);
});
Expand All @@ -345,10 +348,12 @@ export const fakeTDecFlow = ({
// This part is in the client API

let sharedSecret;
if (variant === FerveoVariant.Precomputed) {
if (variant.equals(FerveoVariant.precomputed)) {
sharedSecret = combineDecryptionSharesPrecomputed(decryptionShares);
} else {
} else if (variant.equals(FerveoVariant.simple)) {
sharedSecret = combineDecryptionSharesSimple(decryptionShares);
} else {
throw new Error(`Invalid variant: ${variant}`);
}

// The client should have access to the public parameters of the DKG
Expand Down Expand Up @@ -403,7 +408,7 @@ export const fakeCoordinatorRitual = (
publicKeyHash: string;
totalAggregations: number;
} => {
const ritual = fakeDkgTDecFlowE2e(FerveoVariant.Precomputed);
const ritual = fakeDkgTDecFlowE2e(FerveoVariant.precomputed);
const dkgPkBytes = ritual.dkg.publicKey().toBytes();
return {
id: ritualId,
Expand All @@ -425,7 +430,7 @@ export const fakeCoordinatorRitual = (

export const fakeDkgParticipants = (
ritualId: number,
variant = FerveoVariant.Precomputed
variant = FerveoVariant.precomputed
): {
participants: DkgParticipant[];
participantSecrets: Record<string, SessionStaticSecret>;
Expand Down
Loading

0 comments on commit 771df6c

Please sign in to comment.