Skip to content

Commit f64ec93

Browse files
committed
feat(host-contracts): add previous block hash to the hashes used to generate computed handles
1 parent ad169ea commit f64ec93

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

host-contracts/contracts/FHEVMExecutor.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ contract FHEVMExecutor is UUPSUpgradeableEmptyProxy, FHEEvents, ACLOwnable {
674674

675675
/// @dev It must not cast to same type.
676676
if (typeCt == toType) revert InvalidType();
677-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, Operators.cast, ct, toType, acl, block.chainid));
677+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, Operators.cast, ct, toType, acl, block.chainid, blockhash(block.number - 1)));
678678
result = _appendMetadataToPrehandle(result, toType);
679679
hcuLimit.checkHCUForCast(toType, ct, result, msg.sender);
680680
acl.allowTransient(result, msg.sender);
@@ -698,7 +698,7 @@ contract FHEVMExecutor is UUPSUpgradeableEmptyProxy, FHEEvents, ACLOwnable {
698698
(1 << uint8(FheType.Uint256));
699699

700700
if ((1 << uint8(toType)) & supportedTypes == 0) revert UnsupportedType();
701-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, Operators.trivialEncrypt, pt, toType, acl, block.chainid));
701+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, Operators.trivialEncrypt, pt, toType, acl, block.chainid, blockhash(block.number - 1)));
702702
result = _appendMetadataToPrehandle(result, toType);
703703
hcuLimit.checkHCUForTrivialEncrypt(toType, result, msg.sender);
704704
acl.allowTransient(result, msg.sender);
@@ -815,7 +815,7 @@ contract FHEVMExecutor is UUPSUpgradeableEmptyProxy, FHEEvents, ACLOwnable {
815815

816816
function _unaryOp(Operators op, bytes32 ct) internal virtual returns (bytes32 result) {
817817
if (!acl.isAllowed(ct, msg.sender)) revert ACLNotAllowed(ct, msg.sender);
818-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, ct, acl, block.chainid));
818+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, ct, acl, block.chainid, blockhash(block.number - 1)));
819819
FheType typeCt = _typeOf(ct);
820820
result = _appendMetadataToPrehandle(result, typeCt);
821821
acl.allowTransient(result, msg.sender);
@@ -839,7 +839,7 @@ contract FHEVMExecutor is UUPSUpgradeableEmptyProxy, FHEEvents, ACLOwnable {
839839
FheType lhsType = _typeOf(lhs);
840840
if (lhsType != rhsType) revert IncompatibleTypes();
841841
}
842-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, rhs, scalar, acl, block.chainid));
842+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, rhs, scalar, acl, block.chainid, blockhash(block.number - 1)));
843843
result = _appendMetadataToPrehandle(result, resultType);
844844
acl.allowTransient(result, msg.sender);
845845
}
@@ -862,7 +862,7 @@ contract FHEVMExecutor is UUPSUpgradeableEmptyProxy, FHEEvents, ACLOwnable {
862862
if (lhsType != FheType.Bool) revert UnsupportedType();
863863
if (middleType != rhsType) revert IncompatibleTypes();
864864

865-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, middle, rhs, acl, block.chainid));
865+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, middle, rhs, acl, block.chainid, blockhash(block.number - 1)));
866866
result = _appendMetadataToPrehandle(result, middleType);
867867
acl.allowTransient(result, msg.sender);
868868
}

host-contracts/rust_bindings/src/fhevm_executor.rs

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

host-contracts/test/fhevmExecutor/fhevmExecutor.t.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ contract FHEVMExecutorTest is SupportedTypesConstants, Test {
313313
bytes32 handle,
314314
FheType resultType
315315
) internal view returns (bytes32 result) {
316-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, handle, acl, block.chainid));
316+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, handle, acl, block.chainid, blockhash(block.number - 1)));
317317
result = _appendMetadataToPrehandle(resultType, result, block.chainid, HANDLE_VERSION);
318318
}
319319

@@ -325,7 +325,7 @@ contract FHEVMExecutorTest is SupportedTypesConstants, Test {
325325
FheType resultType
326326
) internal view returns (bytes32 result) {
327327
scalar = scalar & 0x01;
328-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, rhs, scalar, acl, block.chainid));
328+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, rhs, scalar, acl, block.chainid, blockhash(block.number - 1)));
329329
result = _appendMetadataToPrehandle(resultType, result, block.chainid, HANDLE_VERSION);
330330
}
331331

@@ -337,7 +337,7 @@ contract FHEVMExecutorTest is SupportedTypesConstants, Test {
337337
FheType resultType
338338
) internal view returns (bytes32 result) {
339339
scalar = scalar & 0x01;
340-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, rhs, scalar, acl, block.chainid));
340+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, rhs, scalar, acl, block.chainid, blockhash(block.number - 1)));
341341
result = _appendMetadataToPrehandle(resultType, result, block.chainid, HANDLE_VERSION);
342342
}
343343

@@ -348,7 +348,7 @@ contract FHEVMExecutorTest is SupportedTypesConstants, Test {
348348
bytes32 rhs,
349349
FheType middleFheType
350350
) internal view returns (bytes32 result) {
351-
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, middle, rhs, acl, block.chainid));
351+
result = keccak256(abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, op, lhs, middle, rhs, acl, block.chainid, blockhash(block.number - 1)));
352352
result = _appendMetadataToPrehandle(middleFheType, result, block.chainid, HANDLE_VERSION);
353353
}
354354

@@ -1117,7 +1117,7 @@ contract FHEVMExecutorTest is SupportedTypesConstants, Test {
11171117
address sender = address(123);
11181118

11191119
bytes32 expectedResult = keccak256(
1120-
abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, FHEVMExecutor.Operators.trivialEncrypt, pt, FheType(fheType), acl, block.chainid)
1120+
abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, FHEVMExecutor.Operators.trivialEncrypt, pt, FheType(fheType), acl, block.chainid, blockhash(block.number - 1))
11211121
);
11221122
expectedResult = _appendMetadataToPrehandle(FheType(fheType), expectedResult, block.chainid, HANDLE_VERSION);
11231123

@@ -1143,7 +1143,7 @@ contract FHEVMExecutorTest is SupportedTypesConstants, Test {
11431143
_approveHandleInACL(handle, sender);
11441144

11451145
bytes32 expectedResult = keccak256(
1146-
abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, FHEVMExecutor.Operators.cast, handle, FheType(fheOutputType), acl, block.chainid)
1146+
abi.encodePacked(COMPUTATION_DOMAIN_SEPARATOR, FHEVMExecutor.Operators.cast, handle, FheType(fheOutputType), acl, block.chainid, blockhash(block.number - 1))
11471147
);
11481148

11491149
expectedResult = _appendMetadataToPrehandle(

0 commit comments

Comments
 (0)