Skip to content

Commit

Permalink
Merge pull request #645 from zama-ai/coreContracts061-0
Browse files Browse the repository at this point in the history
Core contracts061 0
  • Loading branch information
jatZama authored Dec 9, 2024
2 parents 77f3545 + b0c9133 commit 8d8cd43
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
41 changes: 40 additions & 1 deletion gateway/lib/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ library Gateway {
assembly {
calldatacopy(add(decryptedResult, 0x20), start, length) // Copy the relevant part of calldata to decryptedResult memory
}
decryptedResult = shiftOffsets(decryptedResult, handlesList);
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
return
IKMSVerifier($.KMSVerifierAddress).verifyDecryptionEIP712KMSSignatures(
Expand Down Expand Up @@ -154,7 +155,45 @@ library Gateway {
revert("Unsupported handle type");
}
}
signedDataLength += 32; // for the signatures offset
signedDataLength += 32; // add offset of signatures
return signedDataLength;
}

function shiftOffsets(bytes memory input, uint256[] memory handlesList) private pure returns (bytes memory) {
uint256 numArgs = handlesList.length;
for (uint256 i = 0; i < numArgs; i++) {
uint8 typeCt = uint8(handlesList[i] >> 8);
if (typeCt >= 9) {
input = subToBytes32Slice(input, 32 * i); // because we append the signatures, all bytes offsets are shifted by 0x20
}
}
input = remove32Slice(input, 32 * numArgs);
return input;
}

function subToBytes32Slice(bytes memory data, uint256 offset) private pure returns (bytes memory) {
// @note: data is assumed to be more than 32+offset bytes long
assembly {
let ptr := add(add(data, 0x20), offset)
let val := mload(ptr)
val := sub(val, 0x20)
mstore(ptr, val)
}
return data;
}

function remove32Slice(bytes memory input, uint256 start) public pure returns (bytes memory) {
// @note we assume start+32 is less than input.length
bytes memory result = new bytes(input.length - 32);

for (uint256 i = 0; i < start; i++) {
result[i] = input[i];
}

for (uint256 i = start + 32; i < input.length; i++) {
result[i - 32] = input[i];
}

return result;
}
}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fhevm",
"description": "A Solidity library for interacting with the Zama Blockchain",
"version": "0.6.0",
"version": "0.6.1-0",
"engines": {
"node": ">=20.0.0"
},
Expand Down Expand Up @@ -73,7 +73,7 @@
"eslint-config-prettier": "^8.5.0",
"ethers": "^6.8.0",
"fhevmjs": "^0.6.0-8",
"fhevm-core-contracts": "0.6.0-5",
"fhevm-core-contracts": "0.6.1-0",
"hardhat": "^2.22.10",
"hardhat-deploy": "^0.11.29",
"hardhat-gas-reporter": "^1.0.2",
Expand Down
12 changes: 4 additions & 8 deletions test/asyncDecrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const fulfillAllPastRequestsIds = async (mocked: boolean) => {
const handles = event.args[1];
const typesList = handles.map((handle) => parseInt(handle.toString(16).slice(-4, -2), 16));
const msgValue = event.args[4];
const passSignaturesToCaller = event.args[6];

if (!results.includes(requestID)) {
// if request is not already fulfilled
if (mocked) {
Expand Down Expand Up @@ -154,13 +154,9 @@ const fulfillAllPastRequestsIds = async (mocked: boolean) => {
const abiCoder = new ethers.AbiCoder();
let encodedData;
let calldata;
if (!passSignaturesToCaller) {
encodedData = abiCoder.encode(['uint256', ...types], [31, ...valuesFormatted4]); // 31 is just a dummy uint256 requestID to get correct abi encoding for the remaining arguments (i.e everything except the requestID)
calldata = '0x' + encodedData.slice(66); // we just pop the dummy requestID to get the correct value to pass for `decryptedCts`
} else {
encodedData = abiCoder.encode(['uint256', ...types, 'bytes[]'], [31, ...valuesFormatted4, []]); // adding also a dummy empty array of bytes for correct abi-encoding when used with signatures
calldata = '0x' + encodedData.slice(66).slice(0, -64); // we also pop the last 32 bytes (empty bytes[])
}

encodedData = abiCoder.encode(['uint256', ...types], [31, ...valuesFormatted4]); // 31 is just a dummy uint256 requestID to get correct abi encoding for the remaining arguments (i.e everything except the requestID)
calldata = '0x' + encodedData.slice(66); // we just pop the dummy requestID to get the correct value to pass for `decryptedCts`

const numSigners = +process.env.NUM_KMS_SIGNERS!;
const decryptResultsEIP712signatures = await computeDecryptSignatures(handles, calldata, numSigners);
Expand Down

0 comments on commit 8d8cd43

Please sign in to comment.