Skip to content

Commit 63b959a

Browse files
chore(gateway-contracts): user decrypt shares in events only (#845)
* chore(gateway-contracts): user decrypt shares in events only
1 parent 9a92155 commit 63b959a

File tree

11 files changed

+506
-147
lines changed

11 files changed

+506
-147
lines changed

gateway-contracts/contracts/Decryption.sol

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,13 @@ contract Decryption is
207207
verifiedPublicDecryptSignatures;
208208
/// @notice Handles of the ciphertexts requested for a public decryption
209209
mapping(uint256 decryptionId => bytes32[] ctHandles) publicCtHandles;
210+
/// @notice The number of public decryption requests, used to generate request IDs (`decryptionId`).
211+
uint256 publicDecryptionCounter;
210212
// ----------------------------------------------------------------------------------------------
211213
// User decryption state variables:
212214
// ----------------------------------------------------------------------------------------------
213-
/// @notice Verified signatures for a user decryption.
214-
mapping(uint256 decryptionId => bytes[] verifiedSignatures) verifiedUserDecryptSignatures;
215215
/// @notice The decryption payloads stored during user decryption requests.
216216
mapping(uint256 decryptionId => UserDecryptionPayload payload) userDecryptionPayloads;
217-
/// @notice The user decrypted shares received from user decryption responses.
218-
mapping(uint256 decryptionId => bytes[] shares) userDecryptedShares;
219-
// ----------------------------------------------------------------------------------------------
220-
// Decryption counters:
221-
// ----------------------------------------------------------------------------------------------
222-
/// @notice The number of public decryption requests, used to generate request IDs (`decryptionId`).
223-
uint256 publicDecryptionCounter;
224217
/// @notice The number of user decryption requests, used to generate request IDs (`decryptionId`)
225218
/// @notice (including delegated user decryption requests).
226219
uint256 userDecryptionCounter;
@@ -572,37 +565,34 @@ contract Decryption is
572565
/// @dev KMS node that has not already signed.
573566
_validateDecryptionResponseEIP712Signature(decryptionId, digest, signature);
574567

575-
/// @dev Store the signature for the user decryption response.
576-
/// @dev This list is then used to check the consensus. Important: the mapping should not
577-
/// @dev consider the digest (contrary to the public decryption case) as shares are expected
578-
/// @dev to be different for each KMS node.
579-
bytes[] storage verifiedSignatures = $.verifiedUserDecryptSignatures[decryptionId];
580-
verifiedSignatures.push(signature);
581-
582-
/// @dev Store the user decrypted share for the user decryption response.
583-
$.userDecryptedShares[decryptionId].push(userDecryptedShare);
584-
585568
// Store the KMS transaction sender address for the public decryption response
586569
// It is important to consider the same mapping fields used for the consensus
587570
// A "late" valid KMS transaction sender address will still be added in the list.
588571
// We thus use a zero digest (default value for `bytes32`) to still be able to retrieve the
589572
// list later independently of the decryption response type (public or user).
590-
$.consensusTxSenderAddresses[decryptionId][0].push(msg.sender);
573+
address[] storage txSenderAddresses = $.consensusTxSenderAddresses[decryptionId][0];
574+
txSenderAddresses.push(msg.sender);
575+
576+
// Store the user decrypted share for the user decryption response.
577+
// The index of the share is the length of the txSenderAddresses - 1 so that the first response
578+
// associated to this decryptionId has an index of 0.
579+
emit UserDecryptionResponse(
580+
decryptionId,
581+
txSenderAddresses.length - 1,
582+
userDecryptedShare,
583+
signature,
584+
extraData
585+
);
591586

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

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

600-
emit UserDecryptionResponse(
601-
decryptionId,
602-
$.userDecryptedShares[decryptionId],
603-
verifiedSignatures,
604-
extraData
605-
);
595+
emit UserDecryptionResponseThresholdReached(decryptionId);
606596
}
607597
}
608598

@@ -867,20 +857,20 @@ contract Decryption is
867857
);
868858
}
869859

870-
/// @notice Checks if the consensus is reached among the KMS nodes.
871-
/// @param kmsCounter The number of KMS nodes that agreed
872-
/// @return Whether the consensus is reached
873-
function _isConsensusReachedPublic(uint256 kmsCounter) internal view virtual returns (bool) {
874-
uint256 consensusThreshold = GATEWAY_CONFIG.getPublicDecryptionThreshold();
875-
return kmsCounter >= consensusThreshold;
860+
/// @notice Indicates if the consensus is reached for public decryption.
861+
/// @param numVerifiedResponses The number of public decryption responses that have been verified.
862+
/// @return Whether the consensus has been reached
863+
function _isConsensusReachedPublic(uint256 numVerifiedResponses) internal view virtual returns (bool) {
864+
uint256 publicDecryptionThreshold = GATEWAY_CONFIG.getPublicDecryptionThreshold();
865+
return numVerifiedResponses >= publicDecryptionThreshold;
876866
}
877867

878-
/// @notice Checks if the consensus for user decryption is reached among the KMS signers.
879-
/// @param verifiedSignaturesCount The number of signatures that have been verified for a user decryption.
880-
/// @return Whether the consensus is reached.
881-
function _isConsensusReachedUser(uint256 verifiedSignaturesCount) internal view virtual returns (bool) {
882-
uint256 consensusThreshold = GATEWAY_CONFIG.getUserDecryptionThreshold();
883-
return verifiedSignaturesCount >= consensusThreshold;
868+
/// @notice Indicates if the number of verified user decryption responses has reached the threshold.
869+
/// @param numVerifiedResponses The number of user decryption responses that have been verified.
870+
/// @return Whether the threshold has been reached.
871+
function _isThresholdReachedUser(uint256 numVerifiedResponses) internal view virtual returns (bool) {
872+
uint256 userDecryptionThreshold = GATEWAY_CONFIG.getUserDecryptionThreshold();
873+
return numVerifiedResponses >= userDecryptionThreshold;
884874
}
885875

886876
/// @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
@@ -71,11 +71,14 @@ contract DecryptionMock {
7171

7272
event UserDecryptionResponse(
7373
uint256 indexed decryptionId,
74-
bytes[] userDecryptedShares,
75-
bytes[] signatures,
74+
uint256 indexShare,
75+
bytes userDecryptedShare,
76+
bytes signature,
7677
bytes extraData
7778
);
7879

80+
event UserDecryptionResponseThresholdReached(uint256 indexed decryptionId);
81+
7982
uint256 publicDecryptionCounter = 1 << 248;
8083
uint256 userDecryptionCounter = 2 << 248;
8184

@@ -137,9 +140,10 @@ contract DecryptionMock {
137140
bytes calldata signature,
138141
bytes calldata extraData
139142
) external {
140-
bytes[] memory userDecryptedShares = new bytes[](1);
141-
bytes[] memory signatures = new bytes[](1);
143+
uint256 indexShare;
144+
145+
emit UserDecryptionResponse(decryptionId, indexShare, userDecryptedShare, signature, extraData);
142146

143-
emit UserDecryptionResponse(decryptionId, userDecryptedShares, signatures, extraData);
147+
emit UserDecryptionResponseThresholdReached(decryptionId);
144148
}
145149
}

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)