Skip to content

Commit 7dd9c02

Browse files
authored
chore(gateway-contracts): add checks and comments for code quality (#1255)
1 parent e6068a9 commit 7dd9c02

14 files changed

Lines changed: 2185 additions & 152 deletions

gateway-contracts/contracts/CiphertextCommits.sol

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,13 @@ contract CiphertextCommits is ICiphertextCommits, UUPSUpgradeableEmptyProxy, Gat
202202
function getCiphertextMaterials(
203203
bytes32[] calldata ctHandles
204204
) external view virtual returns (CiphertextMaterial[] memory ctMaterials) {
205+
// Check that the list of handles is not empty
206+
if (ctHandles.length == 0) {
207+
revert EmptyCtHandles();
208+
}
209+
205210
CiphertextCommitsStorage storage $ = _getCiphertextCommitsStorage();
211+
206212
ctMaterials = new CiphertextMaterial[](ctHandles.length);
207213

208214
for (uint256 i = 0; i < ctHandles.length; i++) {
@@ -233,6 +239,11 @@ contract CiphertextCommits is ICiphertextCommits, UUPSUpgradeableEmptyProxy, Gat
233239
function getSnsCiphertextMaterials(
234240
bytes32[] calldata ctHandles
235241
) external view virtual returns (SnsCiphertextMaterial[] memory snsCtMaterials) {
242+
// Check that the list of handles is not empty
243+
if (ctHandles.length == 0) {
244+
revert EmptyCtHandles();
245+
}
246+
236247
CiphertextCommitsStorage storage $ = _getCiphertextCommitsStorage();
237248
snsCtMaterials = new SnsCiphertextMaterial[](ctHandles.length);
238249

gateway-contracts/contracts/Decryption.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,11 @@ contract Decryption is
547547
bytes32[] calldata ctHandles,
548548
bytes calldata /* extraData */
549549
) external view virtual returns (bool) {
550+
// Return false if the list of handles is empty
551+
if (ctHandles.length == 0) {
552+
return false;
553+
}
554+
550555
// For each handle, check that it is allowed for public decryption and that the ciphertext
551556
// material represented by it has been added.
552557
for (uint256 i = 0; i < ctHandles.length; i++) {
@@ -568,6 +573,11 @@ contract Decryption is
568573
CtHandleContractPair[] calldata ctHandleContractPairs,
569574
bytes calldata /* extraData */
570575
) external view virtual returns (bool) {
576+
// Return false if the list of handles is empty
577+
if (ctHandleContractPairs.length == 0) {
578+
return false;
579+
}
580+
571581
// For each handle, check that the user and contracts accounts have access to it and that the
572582
// ciphertext material represented by it has been added.
573583
for (uint256 i = 0; i < ctHandleContractPairs.length; i++) {

gateway-contracts/contracts/GatewayConfig.sol

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,27 @@ contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeab
610610

611611
// Register the new KMS nodes
612612
for (uint256 i = 0; i < newKmsNodes.length; i++) {
613-
$.isKmsTxSender[newKmsNodes[i].txSenderAddress] = true;
614-
$.kmsNodes[newKmsNodes[i].txSenderAddress] = newKmsNodes[i];
615-
$.kmsTxSenderAddresses.push(newKmsNodes[i].txSenderAddress);
616-
$.isKmsSigner[newKmsNodes[i].signerAddress] = true;
617-
$.kmsSignerAddresses.push(newKmsNodes[i].signerAddress);
613+
address newKmsTxSenderAddress = newKmsNodes[i].txSenderAddress;
614+
address newKmsSignerAddress = newKmsNodes[i].signerAddress;
615+
616+
// Check for KMS transaction sender and signer duplicates
617+
if ($.isKmsTxSender[newKmsTxSenderAddress]) {
618+
revert KmsTxSenderAlreadyRegistered(newKmsTxSenderAddress);
619+
}
620+
if ($.isKmsSigner[newKmsSignerAddress]) {
621+
revert KmsSignerAlreadyRegistered(newKmsSignerAddress);
622+
}
623+
624+
// Register transaction sender
625+
$.isKmsTxSender[newKmsTxSenderAddress] = true;
626+
$.kmsTxSenderAddresses.push(newKmsTxSenderAddress);
627+
628+
// Register KMS node
629+
$.kmsNodes[newKmsTxSenderAddress] = newKmsNodes[i];
630+
631+
// Register signer
632+
$.isKmsSigner[newKmsSignerAddress] = true;
633+
$.kmsSignerAddresses.push(newKmsSignerAddress);
618634
}
619635

620636
// Setting the thresholds should be done after the KMS nodes have been registered as the functions
@@ -642,11 +658,27 @@ contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeab
642658

643659
// Register the new coprocessors
644660
for (uint256 i = 0; i < newCoprocessors.length; i++) {
645-
$.isCoprocessorTxSender[newCoprocessors[i].txSenderAddress] = true;
646-
$.coprocessors[newCoprocessors[i].txSenderAddress] = newCoprocessors[i];
647-
$.coprocessorTxSenderAddresses.push(newCoprocessors[i].txSenderAddress);
648-
$.isCoprocessorSigner[newCoprocessors[i].signerAddress] = true;
649-
$.coprocessorSignerAddresses.push(newCoprocessors[i].signerAddress);
661+
address newCoprocessorTxSenderAddress = newCoprocessors[i].txSenderAddress;
662+
address newCoprocessorSignerAddress = newCoprocessors[i].signerAddress;
663+
664+
// Check for coprocessor transaction sender and signer duplicates
665+
if ($.isCoprocessorTxSender[newCoprocessorTxSenderAddress]) {
666+
revert CoprocessorTxSenderAlreadyRegistered(newCoprocessorTxSenderAddress);
667+
}
668+
if ($.isCoprocessorSigner[newCoprocessorSignerAddress]) {
669+
revert CoprocessorSignerAlreadyRegistered(newCoprocessorSignerAddress);
670+
}
671+
672+
// Register coprocessor transaction sender
673+
$.isCoprocessorTxSender[newCoprocessorTxSenderAddress] = true;
674+
$.coprocessorTxSenderAddresses.push(newCoprocessorTxSenderAddress);
675+
676+
// Register coprocessor
677+
$.coprocessors[newCoprocessorTxSenderAddress] = newCoprocessors[i];
678+
679+
// Register coprocessor signer
680+
$.isCoprocessorSigner[newCoprocessorSignerAddress] = true;
681+
$.coprocessorSignerAddresses.push(newCoprocessorSignerAddress);
650682
}
651683

652684
// Setting the coprocessor threshold should be done after the coprocessors have been
@@ -667,11 +699,27 @@ contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeab
667699

668700
// Register the new custodians
669701
for (uint256 i = 0; i < newCustodians.length; i++) {
670-
$.custodians[newCustodians[i].txSenderAddress] = newCustodians[i];
671-
$.custodianTxSenderAddresses.push(newCustodians[i].txSenderAddress);
672-
$.isCustodianTxSender[newCustodians[i].txSenderAddress] = true;
673-
$.custodianSignerAddresses.push(newCustodians[i].signerAddress);
674-
$.isCustodianSigner[newCustodians[i].signerAddress] = true;
702+
address newCustodianTxSenderAddress = newCustodians[i].txSenderAddress;
703+
address newCustodianSignerAddress = newCustodians[i].signerAddress;
704+
705+
// Check for custodian transaction sender and signer duplicates
706+
if ($.isCustodianTxSender[newCustodianTxSenderAddress]) {
707+
revert CustodianTxSenderAlreadyRegistered(newCustodianTxSenderAddress);
708+
}
709+
if ($.isCustodianSigner[newCustodianSignerAddress]) {
710+
revert CustodianSignerAlreadyRegistered(newCustodianSignerAddress);
711+
}
712+
713+
// Register custodian transaction sender
714+
$.isCustodianTxSender[newCustodianTxSenderAddress] = true;
715+
$.custodianTxSenderAddresses.push(newCustodianTxSenderAddress);
716+
717+
// Register custodian
718+
$.custodians[newCustodianTxSenderAddress] = newCustodians[i];
719+
720+
// Register custodian signer
721+
$.isCustodianSigner[newCustodianSignerAddress] = true;
722+
$.custodianSignerAddresses.push(newCustodianSignerAddress);
675723
}
676724
}
677725

@@ -703,7 +751,7 @@ contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeab
703751

704752
// Check that the public decryption threshold `t` is valid. It must verify:
705753
// - `t >= 1` : the public decryption consensus should require at least one vote
706-
// - `t <= n` : it should be less than the number of registered KMS nodes
754+
// - `t <= n` : it should be less than or equal to the number of registered KMS nodes
707755
if (newPublicDecryptionThreshold == 0) {
708756
revert InvalidNullPublicDecryptionThreshold();
709757
}
@@ -724,7 +772,7 @@ contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeab
724772

725773
// Check that the user decryption threshold `t` is valid. It must verify:
726774
// - `t >= 1` : the user decryption consensus should require at least one vote
727-
// - `t <= n` : it should be less than the number of registered KMS nodes
775+
// - `t <= n` : it should be less than or equal to the number of registered KMS nodes
728776
if (newUserDecryptionThreshold == 0) {
729777
revert InvalidNullUserDecryptionThreshold();
730778
}
@@ -745,7 +793,7 @@ contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeab
745793

746794
// Check that the coprocessor threshold `t` is valid. It must verify:
747795
// - `t >= 1` : the coprocessor consensus should require at least one vote
748-
// - `t <= n` : it should be less than the number of registered coprocessors
796+
// - `t <= n` : it should be less than or equal to the number of registered coprocessors
749797
if (newCoprocessorThreshold == 0) {
750798
revert InvalidNullCoprocessorThreshold();
751799
}
@@ -766,7 +814,7 @@ contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeab
766814

767815
// Check that the key and CRS generation threshold `t` is valid. It must verify:
768816
// - `t >= 1` : the key and CRS generation consensus should require at least one vote
769-
// - `t <= n` : it should be less than the number of registered KMS nodes
817+
// - `t <= n` : it should be less than or equal to the number of registered KMS nodes
770818
if (newKmsGenThreshold == 0) {
771819
revert InvalidNullKmsGenThreshold();
772820
}

gateway-contracts/contracts/InputVerification.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ contract InputVerification is
206206
* @dev See {IInputVerification-verifyProofResponse}.
207207
* We restrict this call to coprocessor transaction senders because, in case of reorgs, we need to
208208
* prevent anyone else from copying the signature and sending it to trigger a consensus.
209+
* Also, a user is currently allowed to send an empty list of packed ciphertexts for the input
210+
* proof request, meaning the coprocessors will respond with an empty list of handles. We thus
211+
* don't revert if the `ctHandles` list is empty on purpose.
209212
*/
210213
function verifyProofResponse(
211214
uint256 zkProofId,

gateway-contracts/contracts/interfaces/ICiphertextCommits.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ interface ICiphertextCommits {
4848
*/
4949
error CoprocessorAlreadyAdded(bytes32 ctHandle, address txSender);
5050

51+
/**
52+
* @notice Error indicating that the list of handles is empty.
53+
*/
54+
error EmptyCtHandles();
55+
5156
/**
5257
* @notice Error indicating that the given ciphertext material represented by the given handle has not
5358
* been added in the contract.

gateway-contracts/contracts/interfaces/IGatewayConfig.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,52 @@ interface IGatewayConfig {
127127
*/
128128
error EmptyKmsNodes();
129129

130+
/**
131+
* @notice Error emitted when the KMS transaction sender is already registered.
132+
* @param kmsTxSenderAddress The KMS transaction sender address.
133+
*/
134+
error KmsTxSenderAlreadyRegistered(address kmsTxSenderAddress);
135+
136+
/**
137+
* @notice Error emitted when the KMS signer is already registered.
138+
* @param kmsSignerAddress The KMS signer address.
139+
*/
140+
error KmsSignerAlreadyRegistered(address kmsSignerAddress);
141+
130142
/**
131143
* @notice Error emitted when the coprocessors list is empty.
132144
*/
133145
error EmptyCoprocessors();
134146

147+
/**
148+
* @notice Error emitted when the coprocessor transaction sender is already registered.
149+
* @param coprocessorTxSenderAddress The coprocessor transaction sender address.
150+
*/
151+
error CoprocessorTxSenderAlreadyRegistered(address coprocessorTxSenderAddress);
152+
153+
/**
154+
* @notice Error emitted when the coprocessor signer is already registered.
155+
* @param coprocessorSignerAddress The coprocessor signer address.
156+
*/
157+
error CoprocessorSignerAlreadyRegistered(address coprocessorSignerAddress);
158+
135159
/**
136160
* @notice Error emitted when the custodians list is empty.
137161
*/
138162
error EmptyCustodians();
139163

164+
/**
165+
* @notice Error emitted when the custodian transaction sender is already registered.
166+
* @param custodianTxSenderAddress The custodian transaction sender address.
167+
*/
168+
error CustodianTxSenderAlreadyRegistered(address custodianTxSenderAddress);
169+
170+
/**
171+
* @notice Error emitted when the custodian signer is already registered.
172+
* @param custodianSignerAddress The custodian signer address.
173+
*/
174+
error CustodianSignerAlreadyRegistered(address custodianSignerAddress);
175+
140176
/**
141177
* @notice Error emitted when the MPC threshold is greater or equal to the number of KMS nodes.
142178
* @param mpcThreshold The MPC threshold.

gateway-contracts/rust_bindings/src/ciphertext_commits.rs

Lines changed: 112 additions & 5 deletions
Large diffs are not rendered by default.

gateway-contracts/rust_bindings/src/decryption.rs

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

gateway-contracts/rust_bindings/src/gateway_config.rs

Lines changed: 836 additions & 64 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)