Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to nucypher-core@0.11.0 #250

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "file:../nucypher-core/nucypher-core-wasm/pkg",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be updated after nucypher-core@0.11.0 release

"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 @@ -21,7 +22,6 @@ import { ConditionExpression } from '../conditions';
import {
DkgClient,
DkgRitual,
FerveoVariant,
getCombineDecryptionSharesFunction,
getVariantClass,
} from '../dkg';
Expand Down Expand Up @@ -82,7 +82,7 @@ export class CbdTDecDecrypter {
public async retrieve(
web3Provider: ethers.providers.Web3Provider,
conditionExpr: ConditionExpression,
variant: number,
variant: FerveoVariant,
ciphertext: Ciphertext,
verifyRitual = true
): Promise<DecryptionSharePrecomputed[] | DecryptionShareSimple[]> {
Expand Down Expand Up @@ -145,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 @@ -171,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)) {
Copy link
Member

@manumonti manumonti Jul 13, 2023

Choose a reason for hiding this comment

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

Just out of curiosity: why has this been changed from switch conditionals to If/ else if?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implementing comparison for WASM objects is tricky (==, ===). If we were to simply run a1 === a2, it's going to fail even though a1 and a2 are identical. This is because JS will attempt to compare WASM objects which are just pointers to some memory on the heap (and not the values of the objects). This is why we expose equals method - to compare the underlying values and not the pointers.

So previously we sort of got away without doing all of that because FerveoVariant was just a number. And you can compare primitive types just fine, so === and == work. switch statement is relying on equality, so we could use it with number. Again, all was good.

Now that the FerveoVariant is a WASM object instead of a number primitive type we can't use switch anymore, we have to manually compare objects using equals.

Perhaps there is a better way to do that, like somehow implementing a native === overloading in wasm-bindgen, but I didn't figure out it yet.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, got it. Thanks for the elaborate explanation, Piotr 🙌

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 @@ -40,7 +40,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