Skip to content

Commit

Permalink
Update to nucypher-core@0.11.0 (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec authored and derekpierre committed Aug 9, 2023
2 parents 47b174a + 6b75a24 commit b861631
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 351 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
8 changes: 4 additions & 4 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
decryptWithSharedSecret,
EncryptedThresholdDecryptionRequest,
EncryptedThresholdDecryptionResponse,
FerveoVariant,
SessionSharedSecret,
SessionStaticSecret,
ThresholdDecryptionRequest,
Expand All @@ -16,7 +17,6 @@ import { DkgCoordinatorAgent, DkgParticipant } from '../agents/coordinator';
import { ConditionExpression } from '../conditions';
import {
DkgRitual,
FerveoVariant,
getCombineDecryptionSharesFunction,
getVariantClass,
} from '../dkg';
Expand Down Expand Up @@ -75,7 +75,7 @@ export class CbdTDecDecrypter {
public async retrieve(
provider: ethers.providers.Web3Provider,
conditionExpr: ConditionExpression,
variant: number,
variant: FerveoVariant,
ciphertext: Ciphertext
): Promise<DecryptionSharePrecomputed[] | DecryptionShareSimple[]> {
const dkgParticipants = await DkgCoordinatorAgent.getParticipants(
Expand Down Expand Up @@ -115,7 +115,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 +141,7 @@ export class CbdTDecDecrypter {

private makeDecryptionRequests(
ritualId: number,
variant: number,
variant: FerveoVariant,
ciphertext: Ciphertext,
conditionExpr: ConditionExpression,
contextStr: string,
Expand Down
33 changes: 13 additions & 20 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DecryptionSharePrecomputed,
DecryptionShareSimple,
DkgPublicKey,
FerveoVariant,
SharedSecret,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';
Expand All @@ -12,22 +13,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 @@ -36,13 +30,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
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 @@ -18,6 +18,7 @@ import {
EncryptedTreasureMap,
EthereumAddress,
ferveoEncrypt,
FerveoVariant,
Keypair,
PublicKey,
reencrypt,
Expand Down Expand Up @@ -50,7 +51,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 @@ -218,14 +219,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 @@ -322,20 +323,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 @@ -344,10 +347,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 @@ -402,7 +407,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 @@ -424,7 +429,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 b861631

Please sign in to comment.