Skip to content

Commit 2d912cd

Browse files
eudelins-zamamelanciani
authored andcommitted
chore(gateway-contracts): user decrypt shares in events only
feat(gateway-contracts): make Decryption upgradeable for devnet stress test feat(gateway-contracts): add counterShares in UserDecryptionResponse event chore(gateway-contracts): update bindings chore(gateway-contracts): rename consensus event chore(gateway-contracts): improve counterShare implem chore(gateway-contracts): rename counterShares to indexShare and make it start at 0
1 parent 1842f6d commit 2d912cd

8 files changed

Lines changed: 500 additions & 139 deletions

File tree

gateway-contracts/contracts/Decryption.sol

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,8 @@ contract Decryption is
208208
// ----------------------------------------------------------------------------------------------
209209
// User decryption state variables:
210210
// ----------------------------------------------------------------------------------------------
211-
/// @notice Verified signatures for a user decryption.
212-
mapping(uint256 decryptionId => bytes[] verifiedSignatures) verifiedUserDecryptSignatures;
213211
/// @notice The decryption payloads stored during user decryption requests.
214212
mapping(uint256 decryptionId => UserDecryptionPayload payload) userDecryptionPayloads;
215-
/// @notice The user decrypted shares received from user decryption responses.
216-
mapping(uint256 decryptionId => bytes[] shares) userDecryptedShares;
217213
}
218214

219215
/// @dev Storage location has been computed using the following command:
@@ -546,37 +542,34 @@ contract Decryption is
546542
/// @dev KMS node that has not already signed.
547543
_validateDecryptionResponseEIP712Signature(decryptionId, digest, signature);
548544

549-
/// @dev Store the signature for the user decryption response.
550-
/// @dev This list is then used to check the consensus. Important: the mapping should not
551-
/// @dev consider the digest (contrary to the public decryption case) as shares are expected
552-
/// @dev to be different for each KMS node.
553-
bytes[] storage verifiedSignatures = $.verifiedUserDecryptSignatures[decryptionId];
554-
verifiedSignatures.push(signature);
555-
556-
/// @dev Store the user decrypted share for the user decryption response.
557-
$.userDecryptedShares[decryptionId].push(userDecryptedShare);
558-
559545
// Store the KMS transaction sender address for the public decryption response
560546
// It is important to consider the same mapping fields used for the consensus
561547
// A "late" valid KMS transaction sender address will still be added in the list.
562548
// We thus use a zero digest (default value for `bytes32`) to still be able to retrieve the
563549
// list later independently of the decryption response type (public or user).
564-
$.consensusTxSenderAddresses[decryptionId][0].push(msg.sender);
550+
address[] storage txSenderAddresses = $.consensusTxSenderAddresses[decryptionId][0];
551+
txSenderAddresses.push(msg.sender);
552+
553+
// Store the user decrypted share for the user decryption response.
554+
// The index of the share is the length of the txSenderAddresses - 1 so that the first response
555+
// associated to this decryptionId has an index of 0.
556+
emit UserDecryptionResponse(
557+
decryptionId,
558+
txSenderAddresses.length - 1,
559+
userDecryptedShare,
560+
signature,
561+
extraData
562+
);
565563

566564
// Send the event if and only if the consensus is reached in the current response call.
567565
// This means a "late" response will not be reverted, just ignored and no event will be emitted
568-
if (!$.decryptionDone[decryptionId] && _isConsensusReachedUser(verifiedSignatures.length)) {
566+
if (!$.decryptionDone[decryptionId] && _isThresholdReachedUser(txSenderAddresses.length)) {
569567
$.decryptionDone[decryptionId] = true;
570568

571569
// Since we use the default value for `bytes32`, this means we do not need to store the
572570
// digest in `decryptionConsensusDigest` here like we do for the public decryption case.
573571

574-
emit UserDecryptionResponse(
575-
decryptionId,
576-
$.userDecryptedShares[decryptionId],
577-
verifiedSignatures,
578-
extraData
579-
);
572+
emit UserDecryptionResponseThresholdReached(decryptionId);
580573
}
581574
}
582575

@@ -840,20 +833,20 @@ contract Decryption is
840833
);
841834
}
842835

843-
/// @notice Checks if the consensus is reached among the KMS nodes.
844-
/// @param kmsCounter The number of KMS nodes that agreed
845-
/// @return Whether the consensus is reached
846-
function _isConsensusReachedPublic(uint256 kmsCounter) internal view virtual returns (bool) {
847-
uint256 consensusThreshold = GATEWAY_CONFIG.getPublicDecryptionThreshold();
848-
return kmsCounter >= consensusThreshold;
836+
/// @notice Indicates if the consensus is reached for public decryption.
837+
/// @param numVerifiedResponses The number of public decryption responses that have been verified.
838+
/// @return Whether the consensus has been reached
839+
function _isConsensusReachedPublic(uint256 numVerifiedResponses) internal view virtual returns (bool) {
840+
uint256 publicDecryptionThreshold = GATEWAY_CONFIG.getPublicDecryptionThreshold();
841+
return numVerifiedResponses >= publicDecryptionThreshold;
849842
}
850843

851-
/// @notice Checks if the consensus for user decryption is reached among the KMS signers.
852-
/// @param verifiedSignaturesCount The number of signatures that have been verified for a user decryption.
853-
/// @return Whether the consensus is reached.
854-
function _isConsensusReachedUser(uint256 verifiedSignaturesCount) internal view virtual returns (bool) {
855-
uint256 consensusThreshold = GATEWAY_CONFIG.getUserDecryptionThreshold();
856-
return verifiedSignaturesCount >= consensusThreshold;
844+
/// @notice Indicates if the number of verified user decryption responses has reached the threshold.
845+
/// @param numVerifiedResponses The number of user decryption responses that have been verified.
846+
/// @return Whether the threshold has been reached.
847+
function _isThresholdReachedUser(uint256 numVerifiedResponses) internal view virtual returns (bool) {
848+
uint256 userDecryptionThreshold = GATEWAY_CONFIG.getUserDecryptionThreshold();
849+
return numVerifiedResponses >= userDecryptionThreshold;
857850
}
858851

859852
/// @notice Check the handles' conformance for public decryption requests.

gateway-contracts/contracts/interfaces/IDecryption.sol

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,25 @@ interface IDecryption {
7878
/**
7979
* @notice Emitted when an public decryption response is made.
8080
* @param decryptionId The decryption request ID associated with the response.
81-
* @param userDecryptedShares The list of decryption shares reencrypted with the user's public key.
82-
* @param signatures The signatures of all the KMS connectors that responded.
81+
* @param indexShare The index of the share associated with the decryption.
82+
* @param userDecryptedShare The decryption share reencrypted with the user's public key.
83+
* @param signature The signature of the KMS connector that responded.
8384
* @param extraData Generic bytes metadata for versioned payloads. First byte is for the version.
8485
*/
8586
event UserDecryptionResponse(
8687
uint256 indexed decryptionId,
87-
bytes[] userDecryptedShares,
88-
bytes[] signatures,
88+
uint256 indexShare,
89+
bytes userDecryptedShare,
90+
bytes signature,
8991
bytes extraData
9092
);
9193

94+
/**
95+
* @notice Emitted when the number of user decryption response received reaches the threshold.
96+
* @param decryptionId The decryption request ID.
97+
*/
98+
event UserDecryptionResponseThresholdReached(uint256 indexed decryptionId);
99+
92100
/// @notice Error indicating that the input list of handles is empty.
93101
error EmptyCtHandles();
94102

gateway-contracts/contracts/mocks/DecryptionMock.sol

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ contract DecryptionMock {
3636

3737
event UserDecryptionResponse(
3838
uint256 indexed decryptionId,
39-
bytes[] userDecryptedShares,
40-
bytes[] signatures,
39+
uint256 indexShare,
40+
bytes userDecryptedShare,
41+
bytes signature,
4142
bytes extraData
4243
);
4344

45+
event UserDecryptionResponseThresholdReached(uint256 indexed decryptionId);
46+
4447
uint256 decryptionRequestCounter;
4548

4649
function publicDecryptionRequest(bytes32[] calldata ctHandles, bytes calldata extraData) external {
@@ -101,9 +104,10 @@ contract DecryptionMock {
101104
bytes calldata signature,
102105
bytes calldata extraData
103106
) external {
104-
bytes[] memory userDecryptedShares = new bytes[](1);
105-
bytes[] memory signatures = new bytes[](1);
107+
uint256 indexShare;
108+
109+
emit UserDecryptionResponse(decryptionId, indexShare, userDecryptedShare, signature, extraData);
106110

107-
emit UserDecryptionResponse(decryptionId, userDecryptedShares, signatures, extraData);
111+
emit UserDecryptionResponseThresholdReached(decryptionId);
108112
}
109113
}

gateway-contracts/rust_bindings/src/decryption.rs

Lines changed: 202 additions & 38 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)