Skip to content

Commit d2bc223

Browse files
authored
fix(gateway-contracts): make getCiphertextMaterials backward compatible with fhevm 0.7 (#791)
* fix(gateway-contracts): make getCiphertextMaterials backward compatible with fhevm 0.7
1 parent c9d2319 commit d2bc223

File tree

5 files changed

+173
-83
lines changed

5 files changed

+173
-83
lines changed

.github/workflows/gateway-contracts-upgrade-tests.yml

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
with:
4646
# This version should be updated whenever we release new contract versions or
4747
# touch a contract upgrade path.
48-
ref: v0.7.4
48+
ref: v0.8.0-4
4949
path: previous-fhevm
5050
persist-credentials: 'false'
5151

@@ -123,33 +123,37 @@ jobs:
123123
npx hardhat task:deployEmptyUUPSProxies
124124
docker cp deploy-gateway-contracts:/app/addresses ./
125125
126-
- name: Upgrade GatewayConfig contract
127-
working-directory: current-fhevm/gateway-contracts
128-
env:
129-
DOTENV_CONFIG_PATH: .env
130-
HARDHAT_NETWORK: staging
131-
CHAIN_ID_GATEWAY: 54321
132-
RPC_URL: http://localhost:8546
133-
run: |
134-
npx hardhat task:upgradeGatewayConfig \
135-
--current-implementation previous-contracts/GatewayConfig.sol:GatewayConfig \
136-
--new-implementation contracts/GatewayConfig.sol:GatewayConfig \
137-
--use-internal-proxy-address true \
138-
--verify-contract false
139-
140-
- name: Upgrade Decryption contract
141-
working-directory: current-fhevm/gateway-contracts
142-
env:
143-
DOTENV_CONFIG_PATH: .env
144-
HARDHAT_NETWORK: staging
145-
CHAIN_ID_GATEWAY: 54321
146-
RPC_URL: http://localhost:8546
147-
run: |
148-
npx hardhat task:upgradeDecryption \
149-
--current-implementation previous-contracts/Decryption.sol:Decryption \
150-
--new-implementation contracts/Decryption.sol:Decryption \
151-
--use-internal-proxy-address true \
152-
--verify-contract false
126+
# TODO: We should instead automatically detect if the contract needs to be upgraded
127+
# See https://github.com/zama-ai/fhevm-internal/issues/379
128+
# - name: Upgrade GatewayConfig contract
129+
# working-directory: current-fhevm/gateway-contracts
130+
# env:
131+
# DOTENV_CONFIG_PATH: .env
132+
# HARDHAT_NETWORK: staging
133+
# CHAIN_ID_GATEWAY: 54321
134+
# RPC_URL: http://localhost:8546
135+
# run: |
136+
# npx hardhat task:upgradeGatewayConfig \
137+
# --current-implementation previous-contracts/GatewayConfig.sol:GatewayConfig \
138+
# --new-implementation contracts/GatewayConfig.sol:GatewayConfig \
139+
# --use-internal-proxy-address true \
140+
# --verify-contract false
141+
142+
# TODO: We should instead automatically detect if the contract needs to be upgraded
143+
# See https://github.com/zama-ai/fhevm-internal/issues/379
144+
# - name: Upgrade Decryption contract
145+
# working-directory: current-fhevm/gateway-contracts
146+
# env:
147+
# DOTENV_CONFIG_PATH: .env
148+
# HARDHAT_NETWORK: staging
149+
# CHAIN_ID_GATEWAY: 54321
150+
# RPC_URL: http://localhost:8546
151+
# run: |
152+
# npx hardhat task:upgradeDecryption \
153+
# --current-implementation previous-contracts/Decryption.sol:Decryption \
154+
# --new-implementation contracts/Decryption.sol:Decryption \
155+
# --use-internal-proxy-address true \
156+
# --verify-contract false
153157

154158
- name: Upgrade CiphertextCommits contract
155159
working-directory: current-fhevm/gateway-contracts
@@ -165,6 +169,54 @@ jobs:
165169
--use-internal-proxy-address true \
166170
--verify-contract false
167171
172+
# TODO: We should instead automatically detect if the contract needs to be upgraded
173+
# See https://github.com/zama-ai/fhevm-internal/issues/379
174+
# - name: Upgrade InputVerification contract
175+
# working-directory: current-fhevm/gateway-contracts
176+
# env:
177+
# DOTENV_CONFIG_PATH: .env
178+
# HARDHAT_NETWORK: staging
179+
# CHAIN_ID_GATEWAY: 54321
180+
# RPC_URL: http://localhost:8546
181+
# run: |
182+
# npx hardhat task:upgradeInputVerification \
183+
# --current-implementation previous-contracts/InputVerification.sol:InputVerification \
184+
# --new-implementation contracts/InputVerification.sol:InputVerification \
185+
# --use-internal-proxy-address true \
186+
# --verify-contract false
187+
188+
# TODO: We should instead automatically detect if the contract needs to be upgraded
189+
# See https://github.com/zama-ai/fhevm-internal/issues/379
190+
# - name: Upgrade MultichainAcl contract
191+
# working-directory: current-fhevm/gateway-contracts
192+
# env:
193+
# DOTENV_CONFIG_PATH: .env
194+
# HARDHAT_NETWORK: staging
195+
# CHAIN_ID_GATEWAY: 54321
196+
# RPC_URL: http://localhost:8546
197+
# run: |
198+
# npx hardhat task:upgradeMultichainAcl \
199+
# --current-implementation previous-contracts/MultichainAcl.sol:MultichainAcl \
200+
# --new-implementation contracts/MultichainAcl.sol:MultichainAcl \
201+
# --use-internal-proxy-address true \
202+
# --verify-contract false
203+
204+
# TODO: We should instead automatically detect if the contract needs to be upgraded
205+
# See https://github.com/zama-ai/fhevm-internal/issues/379
206+
# - name: Upgrade KmsManagement contract
207+
# working-directory: current-fhevm/gateway-contracts
208+
# env:
209+
# DOTENV_CONFIG_PATH: .env
210+
# HARDHAT_NETWORK: staging
211+
# CHAIN_ID_GATEWAY: 54321
212+
# RPC_URL: http://localhost:8546
213+
# run: |
214+
# npx hardhat task:upgradeKmsManagement \
215+
# --current-implementation previous-contracts/KmsManagement.sol:KmsManagement \
216+
# --new-implementation contracts/KmsManagement.sol:KmsManagement \
217+
# --use-internal-proxy-address true \
218+
# --verify-contract false
219+
168220
- name: Clean up
169221
working-directory: previous-fhevm/gateway-contracts
170222
if: always()

gateway-contracts/contracts/CiphertextCommits.sol

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ contract CiphertextCommits is
3333
/// @dev they can still define their own private constants with the same name.
3434
string private constant CONTRACT_NAME = "CiphertextCommits";
3535
uint256 private constant MAJOR_VERSION = 0;
36-
uint256 private constant MINOR_VERSION = 2;
36+
uint256 private constant MINOR_VERSION = 3;
3737
uint256 private constant PATCH_VERSION = 0;
3838

3939
/// Constant used for making sure the version number using in the `reinitializer` modifier is
4040
/// identical between `initializeFromEmptyProxy` and the reinitializeVX` method
41-
uint64 private constant REINITIALIZER_VERSION = 3;
41+
uint64 private constant REINITIALIZER_VERSION = 4;
4242

4343
/// @notice The contract's variable storage struct (@dev see ERC-7201)
4444
/// @custom:storage-location erc7201:fhevm_gateway.storage.CiphertextCommits
@@ -50,8 +50,8 @@ contract CiphertextCommits is
5050
/// @notice The key IDs used for generating the ciphertext.
5151
/// @dev It's necessary in case new keys are generated: we need to know what key to use for using a ciphertext.
5252
mapping(bytes32 ctHandle => uint256 keyId) _keyIds;
53-
/// @notice The chain IDs associated to the ciphertext handle.
54-
mapping(bytes32 ctHandle => uint256 chainId) _chainIds; // deprecated
53+
/// @notice DEPRECATED
54+
mapping(bytes32 ctHandle => uint256 chainId) _chainIds; // DEPRECATED
5555
/// @notice The mapping of already added ciphertexts tied to the given handle.
5656
mapping(bytes32 ctHandle => bool isAdded) _isCiphertextMaterialAdded;
5757
/// @notice The counter of confirmations received for a ciphertext to be added.
@@ -91,11 +91,11 @@ contract CiphertextCommits is
9191
}
9292

9393
/**
94-
* @notice Re-initializes the contract from V1.
94+
* @notice Re-initializes the contract from V2.
9595
*/
9696
/// @custom:oz-upgrades-unsafe-allow missing-initializer-call
9797
/// @custom:oz-upgrades-validate-as-initializer
98-
function reinitializeV2() public virtual reinitializer(REINITIALIZER_VERSION) {}
98+
function reinitializeV3() public virtual reinitializer(REINITIALIZER_VERSION) {}
9999

100100
/// @notice See {ICiphertextCommits-addCiphertextMaterial}.
101101
/// @dev This function calls the GatewayConfig contract to check that the sender address is a Coprocessor.
@@ -179,17 +179,31 @@ contract CiphertextCommits is
179179
ctMaterials = new CiphertextMaterial[](ctHandles.length);
180180

181181
for (uint256 i = 0; i < ctHandles.length; i++) {
182+
// Check that the consensus has been reached
182183
checkCiphertextMaterial(ctHandles[i]);
183184

184185
// Get the unique hash associated to the handle in order to retrieve the list of coprocessor
185186
// transaction sender address that were involved in the consensus
186187
bytes32 addCiphertextHash = $._ctHandleConsensusHash[ctHandles[i]];
187188

189+
// If the consensus has been reached but the hash is 0x0, it means that the handle has been
190+
// added in V1: the handle was used to retrieve the list of transaction sender addresses
191+
// instead of the hash
192+
// We therefore consider this in order to be backward compatible.
193+
// To be deprecated
194+
// See https://github.com/zama-ai/fhevm-internal/issues/381
195+
address[] memory coprocessorTxSenderAddresses;
196+
if (addCiphertextHash == bytes32(0)) {
197+
coprocessorTxSenderAddresses = $._coprocessorTxSenderAddresses[ctHandles[i]];
198+
} else {
199+
coprocessorTxSenderAddresses = $._coprocessorTxSenderAddresses[addCiphertextHash];
200+
}
201+
188202
ctMaterials[i] = CiphertextMaterial(
189203
ctHandles[i],
190204
$._keyIds[ctHandles[i]],
191205
$._ciphertextDigests[ctHandles[i]],
192-
$._coprocessorTxSenderAddresses[addCiphertextHash]
206+
coprocessorTxSenderAddresses
193207
);
194208
}
195209

@@ -204,17 +218,31 @@ contract CiphertextCommits is
204218
snsCtMaterials = new SnsCiphertextMaterial[](ctHandles.length);
205219

206220
for (uint256 i = 0; i < ctHandles.length; i++) {
221+
// Check that the consensus has been reached
207222
checkCiphertextMaterial(ctHandles[i]);
208223

209224
// Get the unique hash associated to the handle in order to retrieve the list of transaction
210225
// sender address that participated in the consensus
211226
bytes32 addCiphertextHash = $._ctHandleConsensusHash[ctHandles[i]];
212227

228+
// If the consensus has been reached but the hash is 0x0, it means that the handle has been
229+
// added in V1: the handle was used to retrieve the list of transaction sender addresses
230+
// instead of the hash
231+
// We therefore consider this in order to be backward compatible.
232+
// To be deprecated
233+
// See https://github.com/zama-ai/fhevm-internal/issues/381
234+
address[] memory coprocessorTxSenderAddresses;
235+
if (addCiphertextHash == bytes32(0)) {
236+
coprocessorTxSenderAddresses = $._coprocessorTxSenderAddresses[ctHandles[i]];
237+
} else {
238+
coprocessorTxSenderAddresses = $._coprocessorTxSenderAddresses[addCiphertextHash];
239+
}
240+
213241
snsCtMaterials[i] = SnsCiphertextMaterial(
214242
ctHandles[i],
215243
$._keyIds[ctHandles[i]],
216244
$._snsCiphertextDigests[ctHandles[i]],
217-
$._coprocessorTxSenderAddresses[addCiphertextHash]
245+
coprocessorTxSenderAddresses
218246
);
219247
}
220248

@@ -235,6 +263,16 @@ contract CiphertextCommits is
235263
// This digest remains the default value (0x0) until the consensus is reached.
236264
bytes32 addCiphertextHash = $._ctHandleConsensusHash[ctHandle];
237265

266+
// If the consensus has been reached but the hash is 0x0, it means that the handle has been
267+
// added in V1: the handle was used to retrieve the list of transaction sender addresses
268+
// instead of the hash
269+
// We therefore consider this in order to be backward compatible.
270+
// DEPRECATED: to remove before mainnet
271+
// See https://github.com/zama-ai/fhevm-internal/issues/381
272+
if (addCiphertextHash == bytes32(0) && $._isCiphertextMaterialAdded[ctHandle]) {
273+
return $._coprocessorTxSenderAddresses[ctHandle];
274+
}
275+
238276
return $._coprocessorTxSenderAddresses[addCiphertextHash];
239277
}
240278

gateway-contracts/rust_bindings/src/ciphertext_commits.rs

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

gateway-contracts/selectors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CiphertextCommits
3131
|----------+----------------------------------------------------------+--------------------------------------------------------------------|
3232
| Function | proxiableUUID() | 0x52d1902d |
3333
|----------+----------------------------------------------------------+--------------------------------------------------------------------|
34-
| Function | reinitializeV2() | 0xc4115874 |
34+
| Function | reinitializeV3() | 0xbac22bb8 |
3535
|----------+----------------------------------------------------------+--------------------------------------------------------------------|
3636
| Function | renounceOwnership() | 0x715018a6 |
3737
|----------+----------------------------------------------------------+--------------------------------------------------------------------|
@@ -960,7 +960,7 @@ CiphertextCommitsV2Example
960960
|----------+----------------------------------------------------------+--------------------------------------------------------------------|
961961
| Function | proxiableUUID() | 0x52d1902d |
962962
|----------+----------------------------------------------------------+--------------------------------------------------------------------|
963-
| Function | reinitializeV2() | 0xc4115874 |
963+
| Function | reinitializeV3() | 0xbac22bb8 |
964964
|----------+----------------------------------------------------------+--------------------------------------------------------------------|
965965
| Function | renounceOwnership() | 0x715018a6 |
966966
|----------+----------------------------------------------------------+--------------------------------------------------------------------|

gateway-contracts/test/upgrades/upgrades.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe("Upgrades", function () {
9191
});
9292
const ciphertextCommits = await upgrades.upgradeProxy(emptyUUPS, ciphertextCommitsFactoryV1);
9393
await ciphertextCommits.waitForDeployment();
94-
expect(await ciphertextCommits.getVersion()).to.equal("CiphertextCommits v0.2.0");
94+
expect(await ciphertextCommits.getVersion()).to.equal("CiphertextCommits v0.3.0");
9595
const ciphertextCommitsV2 = await upgrades.upgradeProxy(ciphertextCommits, ciphertextCommitsFactoryV2);
9696
await ciphertextCommitsV2.waitForDeployment();
9797
expect(await ciphertextCommitsV2.getVersion()).to.equal("CiphertextCommits v1000.0.0");

0 commit comments

Comments
 (0)