Skip to content

Commit cc3a955

Browse files
committed
feat(library-solidity): update fromExternal to facilitate smart account integration
chore(library-solidity): fix typo in comments chore(library-solidity): reformat comments
1 parent e3b2c34 commit cc3a955

File tree

4 files changed

+196
-17
lines changed

4 files changed

+196
-17
lines changed

host-contracts/lib/FHE.sol

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ library FHE {
2727
/// @notice Returned if the returned KMS signatures are not valid.
2828
error InvalidKMSSignatures();
2929

30+
/// @notice Returned if the sender is not allowed to use the handle.
31+
error SenderNotAllowedToUseHandle(bytes32 handle, address sender);
32+
3033
/// @notice This event is emitted when public decryption has been successfully verified.
3134
event PublicDecryptionVerified(bytes32[] handlesList, bytes abiEncodedCleartexts);
3235

@@ -8441,9 +8444,19 @@ library FHE {
84418444

84428445
/**
84438446
* @dev Convert an inputHandle with corresponding inputProof to an encrypted ebool integer.
8447+
* @dev If inputProof is empty, the externalEbool inputHandle can be used as a regular ebool handle if it
8448+
* has already been verified and allowed to the sender.
8449+
* This could facilitate integrating smart contract accounts with fhevm.
84448450
*/
84458451
function fromExternal(externalEbool inputHandle, bytes memory inputProof) internal returns (ebool) {
8446-
return ebool.wrap(Impl.verify(externalEbool.unwrap(inputHandle), inputProof, FheType.Bool));
8452+
if (inputProof.length != 0) {
8453+
return ebool.wrap(Impl.verify(externalEbool.unwrap(inputHandle), inputProof, FheType.Bool));
8454+
} else {
8455+
bytes32 inputBytes32 = externalEbool.unwrap(inputHandle);
8456+
ebool inputEbool = ebool.wrap(inputBytes32);
8457+
if (!isSenderAllowed(inputEbool)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8458+
return inputEbool;
8459+
}
84478460
}
84488461

84498462
/**
@@ -8455,9 +8468,19 @@ library FHE {
84558468

84568469
/**
84578470
* @dev Convert an inputHandle with corresponding inputProof to an encrypted euint8 integer.
8471+
* @dev If inputProof is empty, the externalEuint8 inputHandle can be used as a regular euint8 handle if it
8472+
* has already been verified and allowed to the sender.
8473+
* This could facilitate integrating smart contract accounts with fhevm.
84588474
*/
84598475
function fromExternal(externalEuint8 inputHandle, bytes memory inputProof) internal returns (euint8) {
8460-
return euint8.wrap(Impl.verify(externalEuint8.unwrap(inputHandle), inputProof, FheType.Uint8));
8476+
if (inputProof.length != 0) {
8477+
return euint8.wrap(Impl.verify(externalEuint8.unwrap(inputHandle), inputProof, FheType.Uint8));
8478+
} else {
8479+
bytes32 inputBytes32 = externalEuint8.unwrap(inputHandle);
8480+
euint8 inputEuint8 = euint8.wrap(inputBytes32);
8481+
if (!isSenderAllowed(inputEuint8)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8482+
return inputEuint8;
8483+
}
84618484
}
84628485

84638486
/**
@@ -8469,9 +8492,19 @@ library FHE {
84698492

84708493
/**
84718494
* @dev Convert an inputHandle with corresponding inputProof to an encrypted euint16 integer.
8495+
* @dev If inputProof is empty, the externalEuint16 inputHandle can be used as a regular euint16 handle if it
8496+
* has already been verified and allowed to the sender.
8497+
* This could facilitate integrating smart contract accounts with fhevm.
84728498
*/
84738499
function fromExternal(externalEuint16 inputHandle, bytes memory inputProof) internal returns (euint16) {
8474-
return euint16.wrap(Impl.verify(externalEuint16.unwrap(inputHandle), inputProof, FheType.Uint16));
8500+
if (inputProof.length != 0) {
8501+
return euint16.wrap(Impl.verify(externalEuint16.unwrap(inputHandle), inputProof, FheType.Uint16));
8502+
} else {
8503+
bytes32 inputBytes32 = externalEuint16.unwrap(inputHandle);
8504+
euint16 inputEuint16 = euint16.wrap(inputBytes32);
8505+
if (!isSenderAllowed(inputEuint16)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8506+
return inputEuint16;
8507+
}
84758508
}
84768509

84778510
/**
@@ -8483,9 +8516,19 @@ library FHE {
84838516

84848517
/**
84858518
* @dev Convert an inputHandle with corresponding inputProof to an encrypted euint32 integer.
8519+
* @dev If inputProof is empty, the externalEuint32 inputHandle can be used as a regular euint32 handle if it
8520+
* has already been verified and allowed to the sender.
8521+
* This could facilitate integrating smart contract accounts with fhevm.
84868522
*/
84878523
function fromExternal(externalEuint32 inputHandle, bytes memory inputProof) internal returns (euint32) {
8488-
return euint32.wrap(Impl.verify(externalEuint32.unwrap(inputHandle), inputProof, FheType.Uint32));
8524+
if (inputProof.length != 0) {
8525+
return euint32.wrap(Impl.verify(externalEuint32.unwrap(inputHandle), inputProof, FheType.Uint32));
8526+
} else {
8527+
bytes32 inputBytes32 = externalEuint32.unwrap(inputHandle);
8528+
euint32 inputEuint32 = euint32.wrap(inputBytes32);
8529+
if (!isSenderAllowed(inputEuint32)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8530+
return inputEuint32;
8531+
}
84898532
}
84908533

84918534
/**
@@ -8497,9 +8540,19 @@ library FHE {
84978540

84988541
/**
84998542
* @dev Convert an inputHandle with corresponding inputProof to an encrypted euint64 integer.
8543+
* @dev If inputProof is empty, the externalEuint64 inputHandle can be used as a regular euint64 handle if it
8544+
* has already been verified and allowed to the sender.
8545+
* This could facilitate integrating smart contract accounts with fhevm.
85008546
*/
85018547
function fromExternal(externalEuint64 inputHandle, bytes memory inputProof) internal returns (euint64) {
8502-
return euint64.wrap(Impl.verify(externalEuint64.unwrap(inputHandle), inputProof, FheType.Uint64));
8548+
if (inputProof.length != 0) {
8549+
return euint64.wrap(Impl.verify(externalEuint64.unwrap(inputHandle), inputProof, FheType.Uint64));
8550+
} else {
8551+
bytes32 inputBytes32 = externalEuint64.unwrap(inputHandle);
8552+
euint64 inputEuint64 = euint64.wrap(inputBytes32);
8553+
if (!isSenderAllowed(inputEuint64)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8554+
return inputEuint64;
8555+
}
85038556
}
85048557

85058558
/**
@@ -8511,9 +8564,19 @@ library FHE {
85118564

85128565
/**
85138566
* @dev Convert an inputHandle with corresponding inputProof to an encrypted euint128 integer.
8567+
* @dev If inputProof is empty, the externalEuint128 inputHandle can be used as a regular euint128 handle if it
8568+
* has already been verified and allowed to the sender.
8569+
* This could facilitate integrating smart contract accounts with fhevm.
85148570
*/
85158571
function fromExternal(externalEuint128 inputHandle, bytes memory inputProof) internal returns (euint128) {
8516-
return euint128.wrap(Impl.verify(externalEuint128.unwrap(inputHandle), inputProof, FheType.Uint128));
8572+
if (inputProof.length != 0) {
8573+
return euint128.wrap(Impl.verify(externalEuint128.unwrap(inputHandle), inputProof, FheType.Uint128));
8574+
} else {
8575+
bytes32 inputBytes32 = externalEuint128.unwrap(inputHandle);
8576+
euint128 inputEuint128 = euint128.wrap(inputBytes32);
8577+
if (!isSenderAllowed(inputEuint128)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8578+
return inputEuint128;
8579+
}
85178580
}
85188581

85198582
/**
@@ -8525,9 +8588,19 @@ library FHE {
85258588

85268589
/**
85278590
* @dev Convert an inputHandle with corresponding inputProof to an encrypted eaddress integer.
8591+
* @dev If inputProof is empty, the externalEaddress inputHandle can be used as a regular eaddress handle if it
8592+
* has already been verified and allowed to the sender.
8593+
* This could facilitate integrating smart contract accounts with fhevm.
85288594
*/
85298595
function fromExternal(externalEaddress inputHandle, bytes memory inputProof) internal returns (eaddress) {
8530-
return eaddress.wrap(Impl.verify(externalEaddress.unwrap(inputHandle), inputProof, FheType.Uint160));
8596+
if (inputProof.length != 0) {
8597+
return eaddress.wrap(Impl.verify(externalEaddress.unwrap(inputHandle), inputProof, FheType.Uint160));
8598+
} else {
8599+
bytes32 inputBytes32 = externalEaddress.unwrap(inputHandle);
8600+
eaddress inputEaddress = eaddress.wrap(inputBytes32);
8601+
if (!isSenderAllowed(inputEaddress)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8602+
return inputEaddress;
8603+
}
85318604
}
85328605

85338606
/**
@@ -8539,9 +8612,19 @@ library FHE {
85398612

85408613
/**
85418614
* @dev Convert an inputHandle with corresponding inputProof to an encrypted euint256 integer.
8615+
* @dev If inputProof is empty, the externalEuint256 inputHandle can be used as a regular euint256 handle if it
8616+
* has already been verified and allowed to the sender.
8617+
* This could facilitate integrating smart contract accounts with fhevm.
85428618
*/
85438619
function fromExternal(externalEuint256 inputHandle, bytes memory inputProof) internal returns (euint256) {
8544-
return euint256.wrap(Impl.verify(externalEuint256.unwrap(inputHandle), inputProof, FheType.Uint256));
8620+
if (inputProof.length != 0) {
8621+
return euint256.wrap(Impl.verify(externalEuint256.unwrap(inputHandle), inputProof, FheType.Uint256));
8622+
} else {
8623+
bytes32 inputBytes32 = externalEuint256.unwrap(inputHandle);
8624+
euint256 inputEuint256 = euint256.wrap(inputBytes32);
8625+
if (!isSenderAllowed(inputEuint256)) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
8626+
return inputEuint256;
8627+
}
85458628
}
85468629

85478630
/**

library-solidity/codegen/src/templateFHEDotSol.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,19 @@ function handleSolidityTFHEConvertPlaintextAndEinputToRespectiveType(fheType: Ad
537537
let result = `
538538
/**
539539
* @dev Convert an inputHandle with corresponding inputProof to an encrypted e${fheType.type.toLowerCase()} integer.
540+
* @dev If inputProof is empty, the externalE${fheType.type.toLowerCase()} inputHandle can be used as a regular e${fheType.type.toLowerCase()} handle if it
541+
* has already been verified and allowed to the sender.
542+
* This could facilitate integrating smart contract accounts with fhevm.
540543
*/
541544
function fromExternal(externalE${fheType.type.toLowerCase()} inputHandle, bytes memory inputProof) internal returns (e${fheType.type.toLowerCase()}) {
542-
return e${fheType.type.toLowerCase()}.wrap(Impl.verify(externalE${fheType.type.toLowerCase()}.unwrap(inputHandle), inputProof, FheType.${fheType.isAlias ? fheType.aliasType : fheType.type}));
545+
if (inputProof.length!=0) {
546+
return e${fheType.type.toLowerCase()}.wrap(Impl.verify(externalE${fheType.type.toLowerCase()}.unwrap(inputHandle), inputProof, FheType.${fheType.isAlias ? fheType.aliasType : fheType.type}));
547+
} else {
548+
bytes32 inputBytes32 = externalE${fheType.type.toLowerCase()}.unwrap(inputHandle);
549+
e${fheType.type.toLowerCase()} inputE${fheType.type.toLowerCase()} = e${fheType.type.toLowerCase()}.wrap(inputBytes32);
550+
if(!isSenderAllowed(inputE${fheType.type.toLowerCase()})) revert SenderNotAllowedToUseHandle(inputBytes32, msg.sender);
551+
return inputE${fheType.type.toLowerCase()};
552+
}
543553
}
544554
545555
`;

library-solidity/codegen/src/templates/FHE.sol-template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ library FHE {
3333
/// @notice Returned if the returned KMS signatures are not valid.
3434
error InvalidKMSSignatures();
3535

36+
/// @notice Returned if the sender is not allowed to use the handle.
37+
error SenderNotAllowedToUseHandle(bytes32 handle, address sender);
38+
3639
/// @notice This event is emitted when public decryption has been successfully verified.
3740
event PublicDecryptionVerified(bytes32[] handlesList, bytes abiEncodedCleartexts);
3841

0 commit comments

Comments
 (0)