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

Implement feedback from integrating tDec flow #227

Merged
merged 7 commits into from
Jun 27, 2023
Merged

Conversation

piotr-roslaniec
Copy link
Contributor

@piotr-roslaniec piotr-roslaniec commented Jun 16, 2023

Type of PR:

  • Feature

Required reviews:

  • 2

What this does:

Issues fixed/closed:

  • Fixes #...

Why it's needed:

  • Brings nucypher-ts up to speed with dkg-dev-7

Notes for reviewers:

  • This is just a PoC of the new tDec version. The necessary API changes will be addressed in a different PR.
  • Significant changes are in src/characters/cbd-recipient.ts, src/agents/coordinator.ts, and in src/characters/porter.ts

@github-actions
Copy link

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

@piotr-roslaniec piotr-roslaniec marked this pull request as ready for review June 27, 2023 13:37
@codecov-commenter
Copy link

codecov-commenter commented Jun 27, 2023

Codecov Report

Merging #227 (c2acc61) into tdec-epic (ccdeba0) will decrease coverage by 0.44%.
The diff coverage is 61.90%.

@@              Coverage Diff              @@
##           tdec-epic     #227      +/-   ##
=============================================
- Coverage      83.89%   83.45%   -0.44%     
=============================================
  Files             37       37              
  Lines            956      973      +17     
  Branches         121      122       +1     
=============================================
+ Hits             802      812      +10     
- Misses           147      155       +8     
+ Partials           7        6       -1     
Impacted Files Coverage Δ
src/agents/contracts.ts 47.05% <ø> (ø)
src/characters/porter.ts 31.42% <0.00%> (-0.93%) ⬇️
src/conditions/context/context.ts 97.82% <ø> (ø)
src/dkg.ts 50.00% <22.22%> (-1.17%) ⬇️
src/agents/coordinator.ts 26.31% <33.33%> (+6.31%) ⬆️
src/utils.ts 97.87% <50.00%> (-2.13%) ⬇️
src/characters/enrico.ts 82.75% <66.66%> (-2.43%) ⬇️
src/characters/cbd-recipient.ts 92.18% <91.66%> (-1.37%) ⬇️
src/conditions/compound-condition.ts 100.00% <100.00%> (ø)
src/conditions/condition-expr.ts 100.00% <100.00%> (ø)
... and 2 more

Comment on lines +40 to +43
provider: participant.provider,
aggregated: participant.aggregated,
decryptionRequestStaticKey: SessionStaticKey.fromBytes(
fromHexString(participant.decryptionRequestStaticKey)
Copy link
Member

Choose a reason for hiding this comment

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

This data is currently relayed from porter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's fetched from Coordinator contract

ciphertext: Ciphertext,
aad: Uint8Array
): Promise<readonly Uint8Array[]> {
variant: FerveoVariant,
Copy link
Member

Choose a reason for hiding this comment

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

👍🏻

@@ -90,21 +82,25 @@ export class CbdTDecDecrypter {
provider,
this.ritualId
);
// We only need the `threshold` participants
const sufficientDkgParticipants = dkgParticipants.slice(0, this.threshold);
Copy link
Member

Choose a reason for hiding this comment

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

This can be hardcoded as shares // 2 + 1 to match nucypher/nucypher (can be in another PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we don't want to support tunable threshold any time soon? Taking notes for API design.

Comment on lines +175 to +176
const sharedSecret = ephemeralSessionKey.deriveSharedSecret(
decryptionRequestStaticKey
Copy link
Member

Choose a reason for hiding this comment

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

very nice

DkgPublicParameters.fromBytes(dkgPublicParams),
threshold
);
return new CbdTDecDecrypter(new Porter(porterUri), ritualId, threshold);
Copy link
Member

Choose a reason for hiding this comment

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

(in another PR) CbdTDecDecrypter -> ThresholdDecrypter || Bob || something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, suggestions for naming are welcome

Comment on lines +60 to +62
if (!withConditions) {
throw new Error('Conditions are required for CBD encryption.');
}
Copy link
Member

Choose a reason for hiding this comment

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

Agree! This is a major differentiating factor between the PRE-adapted-Tdec and the real one.

}
// TODO: Create a new DKG ritual here
throw new Error('Not implemented');
// TODO: Update API: Replace with getExistingRitual and support ritualId in Strategy
Copy link
Member

Choose a reason for hiding this comment

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

Let's track this is an issue

Comment on lines +85 to +89
decryption_results: {
encrypted_decryption_responses: Record<
ChecksumAddress,
Base64EncodedBytes
>;
Copy link
Member

Choose a reason for hiding this comment

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

@derekpierre - (wrt porter) what's the reason behind the nesting here? What other data can be inside results other than the values of decryption_results?

Copy link
Member

Choose a reason for hiding this comment

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

To provide some consistency for Porter responses. All Porter requests PRE / CBD return as follows:

{
     "result": ...,
     "version": "x.y.z"
}

Where result could be anything. For example:

We can definitely rethink it if it feels too redundant.

@KPrasch
Copy link
Member

KPrasch commented Jun 27, 2023

👨🏻‍🚀

Approved with non-blocking comments. Well done!

src/agents/coordinator.ts Show resolved Hide resolved
Comment on lines 101 to 104

// TODO: How many errors are acceptable? Less than (threshold - shares)?
// TODO: If Porter accepts only `threshold` decryption requests, then we may not have any errors
if (Object.keys(errors).length > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Porter can return both successes and errors in the response from /cbd_decrypt, so

  • success is threshold number of responses returned (ignore any errors)
  • If less than threshold number of responses then fail with any errors provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I only need to check for presence of any errors then

Copy link
Member

Choose a reason for hiding this comment

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

No. Errors can be reported even if the overall operation was successful i.e. some Ursula's may fail while waiting for sufficient responses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we'd only want to throw here if we didn't receive enough responses:

    if (Object.keys(encryptedResponses).length < this.threshold) {
      throw new Error(
        `CBD decryption failed with errors: ${JSON.stringify(errors)}`
      );
    }

Copy link
Member

Choose a reason for hiding this comment

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

👍 - Correct.

With a message something like..."Threshold of responses not met; CBD decryption failed with errors..."

Comment on lines 125 to 128
const encryptedResponse = encryptedResponses[ursula];
if (!encryptedResponse) {
throw new Error(`Missing encrypted response from ${ursula}`);
}
Copy link
Member

Choose a reason for hiding this comment

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

Would this fail if any ursula did not return an encrypted response...? i.e.assuming a threshold number of responses have been returned, we only care about the ursulas that responded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I agree that we only need threshold responses and then we can call it a day, but I wanted to throw this error here to highlight some possible but difficult-to-debug edge-case. It's possible that encryptedResponses[ursula] is undefined and may result in some weirdness.

Copy link
Member

Choose a reason for hiding this comment

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

The issue here is that Porter only waits for a threshold number of responses, so some Ursulas may not have responded by the time Porter returns because the threhold was already met. So you likely will have Ursulas without responses, but that not be a problem.

Comment on lines +85 to +89
decryption_results: {
encrypted_decryption_responses: Record<
ChecksumAddress,
Base64EncodedBytes
>;
Copy link
Member

Choose a reason for hiding this comment

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

To provide some consistency for Porter responses. All Porter requests PRE / CBD return as follows:

{
     "result": ...,
     "version": "x.y.z"
}

Where result could be anything. For example:

We can definitely rethink it if it feels too redundant.

const contextStr = await conditionExpr.buildContext(provider).toJson();
const { sharedSecrets, encryptedRequests } = this.makeDecryptionRequests(
this.ritualId,
variant,
ciphertext,
conditionExpr,
contextStr,
dkgParticipants
sufficientDkgParticipants
Copy link
Member

@derekpierre derekpierre Jun 27, 2023

Choose a reason for hiding this comment

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

Is this limiting the number of Ursulas that we send the decryption request to, to be only a threshold number?If so, we need a threshold to respond, but we should still send requests to more than the threshold, or else we are expecting no non-responsive Ursulas. Porter already takes care of ensuring a threshold number of responses.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed - let's call that a robustness enhancement and track it in an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, I think I was thrown off by how Porter handles this. I can remove this reduction of participants in this PR.

@piotr-roslaniec
Copy link
Contributor Author

Documented comments on API design here: #166 (comment)

@github-actions
Copy link

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

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

🎸 - nice work!

@github-actions
Copy link

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

@piotr-roslaniec piotr-roslaniec merged commit 50dd01c into tdec-epic Jun 27, 2023
12 checks passed
@piotr-roslaniec piotr-roslaniec deleted the tdec-poc branch June 27, 2023 20:28
@piotr-roslaniec piotr-roslaniec restored the tdec-poc branch July 10, 2023 10:31
@piotr-roslaniec piotr-roslaniec deleted the tdec-poc branch July 10, 2023 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

4 participants