From 676db11495381d22b0b9af323bde251e5fb3a7e3 Mon Sep 17 00:00:00 2001 From: jatZama Date: Wed, 22 Jan 2025 21:51:08 +0100 Subject: [PATCH] chore: removed obsolete utils functions --- contracts/test/coprocessorUtils.ts | 700 +---------------------------- 1 file changed, 1 insertion(+), 699 deletions(-) diff --git a/contracts/test/coprocessorUtils.ts b/contracts/test/coprocessorUtils.ts index 0476342d..41ee537c 100644 --- a/contracts/test/coprocessorUtils.ts +++ b/contracts/test/coprocessorUtils.ts @@ -9,32 +9,12 @@ import operatorsPrices from '../codegen/operatorsPrices.json'; const parsedEnvCoprocessor = dotenv.parse(fs.readFileSync('addresses/.env.exec')); const coprocAddress = parsedEnvCoprocessor.TFHE_EXECUTOR_CONTRACT_ADDRESS; -const coprocAdd = parsedEnvCoprocessor.TFHE_EXECUTOR_CONTRACT_ADDRESS.replace(/^0x/, '') - .replace(/^0+/, '') - .toLowerCase(); - -const parsedEnvACL = dotenv.parse(fs.readFileSync('addresses/.env.acl')); -const aclAddress = parsedEnvACL.ACL_CONTRACT_ADDRESS; -let chainId: number; let firstBlockListening = 0; let lastBlockSnapshot = 0; let lastCounterRand = 0; let counterRand = 0; - -const contractABI = JSON.parse( - fs.readFileSync('artifacts/contracts/TFHEExecutor.sol/TFHEExecutor.json').toString(), -).abi; - -const iface = new ethers.Interface(contractABI); - -const functions = iface.fragments.filter((fragment) => fragment.type === 'function'); - -const selectors = functions.reduce((acc, func) => { - const signature = `${func.name}(${func.inputs.map((input) => input.type).join(',')})`; - acc[func.selector] = signature; - return acc; -}, {}); +let chainId: number; //const db = new Database('./sql.db'); // on-disk db for debugging const db = new Database(':memory:'); @@ -78,60 +58,11 @@ export const getClearText = async (handle: BigInt): Promise => { db.serialize(() => db.run('CREATE TABLE IF NOT EXISTS ciphertexts (handle BINARY PRIMARY KEY,clearText TEXT)')); -enum Operators { - fheAdd = 0, - fheSub, - fheMul, - fheDiv, - fheRem, - fheBitAnd, - fheBitOr, - fheBitXor, - fheShl, - fheShr, - fheRotl, - fheRotr, - fheEq, - fheNe, - fheGe, - fheGt, - fheLe, - fheLt, - fheMin, - fheMax, - fheNeg, - fheNot, - verifyCiphertext, - cast, - trivialEncrypt, - fheIfThenElse, - fheRand, - fheRandBounded, -} - -interface EvmState { - stack: string[]; - memory: string[]; -} - interface FHEVMEvent { eventName: string; args: object; } -function extractCalldata(memory: string[], offset: number, size: number): string { - const startIndex = Math.floor(offset / 32); - const endIndex = Math.ceil((offset + size) / 32); - const memorySegments = memory.slice(startIndex, endIndex); - let calldata = ''; - for (let i = 0; i < memorySegments.length; i++) { - calldata += memorySegments[i]; - } - const calldataStart = (offset % 32) * 2; - const calldataEnd = calldataStart + size * 2; - return calldata.slice(calldataStart, calldataEnd); -} - const NumBits = { 0: 1n, //ebool 1: 4n, //euint4 @@ -147,8 +78,6 @@ const NumBits = { 11: 2048n, //ebytes256 }; -const HANDLE_VERSION = 0; - export function numberToEvenHexString(num: number) { if (typeof num !== 'number' || num < 0) { throw new Error('Input should be a non-negative number.'); @@ -160,10 +89,6 @@ export function numberToEvenHexString(num: number) { return hexString; } -function appendType(handle: string, type: number): string { - return handle.slice(0, -4) + numberToEvenHexString(type) + numberToEvenHexString(HANDLE_VERSION); -} - function getRandomBigInt(numBits: number): bigint { if (numBits <= 0) { throw new Error('Number of bits must be greater than 0'); @@ -180,623 +105,6 @@ function getRandomBigInt(numBits: number): bigint { return randomBigInt; } -async function insertHandle( - obj2: EvmState, - validIdxes: [number], - blockStatus: { - blockhash: string; - timestamp: number; - }, -) { - const obj = obj2.value; - if (isCoprocAdd(obj!.stack.at(-2))) { - const argsOffset = Number(`0x${obj!.stack.at(-4)}`); - const argsSize = Number(`0x${obj!.stack.at(-5)}`); - const calldata = extractCalldata(obj.memory, argsOffset, argsSize); - const currentSelector = '0x' + calldata.slice(0, 8); - const decodedData = iface.decodeFunctionData(currentSelector, '0x' + calldata); - - let handle; - let clearText; - let clearLHS; - let clearRHS; - let lhsType; - let resultType; - let shift; - - switch (selectors[currentSelector]) { - case 'trivialEncrypt(uint256,bytes1)': - resultType = Number(decodedData[1]); - clearText = decodedData[0]; - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.trivialEncrypt, decodedData[0], decodedData[1], aclAddress, chainId], - ), - ); - handle = appendType(handle, resultType); - insertSQL(handle, clearText); - break; - - case 'trivialEncrypt(bytes,bytes1)': - resultType = Number(decodedData[1]); - clearText = decodedData[0]; - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'bytes', 'bytes1', 'address', 'uint256'], - [Operators.trivialEncrypt, decodedData[0], decodedData[1], aclAddress, chainId], - ), - ); - handle = appendType(handle, resultType); - insertSQL(handle, clearText); - break; - - case 'fheAdd(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheAdd, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) + decodedData[1]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) + BigInt(clearRHS); - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheSub(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheSub, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) - decodedData[1]; - if (clearText < 0n) clearText = clearText + 2n ** NumBits[resultType]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) - BigInt(clearRHS); - if (clearText < 0n) clearText = clearText + 2n ** NumBits[resultType]; - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheMul(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheMul, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) * decodedData[1]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) * BigInt(clearRHS); - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheDiv(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheDiv, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) / decodedData[1]; - } else { - throw new Error('Non-scalar div not implemented yet'); - } - insertSQL(handle, clearText); - break; - - case 'fheRem(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheRem, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) % decodedData[1]; - } else { - throw new Error('Non-scalar rem not implemented yet'); - } - insertSQL(handle, clearText); - break; - - case 'fheBitAnd(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheBitAnd, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) & decodedData[1]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) & BigInt(clearRHS); - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheBitOr(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheBitOr, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) | decodedData[1]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) | BigInt(clearRHS); - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheBitXor(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheBitXor, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) ^ decodedData[1]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) ^ BigInt(clearRHS); - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheShl(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheShl, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) << decodedData[1] % NumBits[resultType]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) << BigInt(clearRHS) % NumBits[resultType]; - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheShr(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheShr, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) >> decodedData[1] % NumBits[resultType]; - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) >> BigInt(clearRHS) % NumBits[resultType]; - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheRotl(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheRotl, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - - if (decodedData[2] === '0x01') { - shift = decodedData[1] % NumBits[resultType]; - clearText = (BigInt(clearLHS) << shift) | (BigInt(clearLHS) >> (NumBits[resultType] - shift)); - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - shift = BigInt(clearRHS) % NumBits[resultType]; - clearText = (BigInt(clearLHS) << shift) | (BigInt(clearLHS) >> (NumBits[resultType] - shift)); - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheRotr(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheRotr, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - - if (decodedData[2] === '0x01') { - shift = decodedData[1] % NumBits[resultType]; - clearText = (BigInt(clearLHS) >> shift) | (BigInt(clearLHS) << (NumBits[resultType] - shift)); - clearText = clearText % 2n ** NumBits[resultType]; - } else { - clearRHS = await getClearText(decodedData[1]); - shift = BigInt(clearRHS) % NumBits[resultType]; - clearText = (BigInt(clearLHS) >> shift) | (BigInt(clearLHS) << (NumBits[resultType] - shift)); - clearText = clearText % 2n ** NumBits[resultType]; - } - insertSQL(handle, clearText); - break; - - case 'fheEq(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheEq, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) === decodedData[1] ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) === BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheEq(uint256,bytes,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'bytes', 'bytes1', 'address', 'uint256'], - [Operators.fheEq, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) === BigInt(decodedData[1]) ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) === BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheNe(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheNe, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) !== decodedData[1] ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) !== BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheNe(uint256,bytes,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'bytes', 'bytes1', 'address', 'uint256'], - [Operators.fheNe, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) !== BigInt(decodedData[1]) ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) !== BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheGe(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheGe, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) >= decodedData[1] ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) >= BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheGt(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheGt, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) > decodedData[1] ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) > BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheLe(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheLe, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) <= decodedData[1] ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) <= BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheLt(uint256,uint256,bytes1)': - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheLt, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, 0); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) < decodedData[1] ? 1n : 0n; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) < BigInt(clearRHS) ? 1n : 0n; - } - insertSQL(handle, clearText); - break; - - case 'fheMax(uint256,uint256,bytes1)': - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheMax, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) > decodedData[1] ? clearLHS : decodedData[1]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) > BigInt(clearRHS) ? clearLHS : clearRHS; - } - insertSQL(handle, clearText); - break; - - case 'fheMin(uint256,uint256,bytes1)': - lhsType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - resultType = lhsType; - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.fheMin, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, resultType); - clearLHS = await getClearText(decodedData[0]); - if (decodedData[2] === '0x01') { - clearText = BigInt(clearLHS) < decodedData[1] ? clearLHS : decodedData[1]; - } else { - clearRHS = await getClearText(decodedData[1]); - clearText = BigInt(clearLHS) < BigInt(clearRHS) ? clearLHS : clearRHS; - } - insertSQL(handle, clearText); - break; - - case 'cast(uint256,bytes1)': - resultType = parseInt(decodedData[1]); - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'bytes1', 'address', 'uint256'], - [Operators.cast, decodedData[0], decodedData[1], aclAddress, chainId], - ), - ); - clearText = BigInt(await getClearText(decodedData[0])) % 2n ** NumBits[resultType]; - handle = appendType(handle, resultType); - insertSQL(handle, clearText); - break; - - case 'fheNot(uint256)': - resultType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'address', 'uint256'], - [Operators.fheNot, decodedData[0], aclAddress, chainId], - ), - ); - handle = appendType(handle, resultType); - clearText = BigInt(await getClearText(decodedData[0])); - clearText = bitwiseNotUintBits(clearText, Number(NumBits[resultType])); - insertSQL(handle, clearText); - break; - - case 'fheNeg(uint256)': - resultType = parseInt(decodedData[0].toString(16).slice(-4, -2), 16); - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'address', 'uint256'], - [Operators.fheNeg, decodedData[0], aclAddress, chainId], - ), - ); - handle = appendType(handle, resultType); - clearText = BigInt(await getClearText(decodedData[0])); - clearText = bitwiseNotUintBits(clearText, Number(NumBits[resultType])); - clearText = (clearText + 1n) % 2n ** NumBits[resultType]; - insertSQL(handle, clearText); - break; - - case 'verifyCiphertext(bytes32,address,bytes,bytes1)': - try { - await getClearText(BigInt(decodedData[0])); - } catch { - throw Error('User input was not found in DB'); - } - break; - - case 'fheIfThenElse(uint256,uint256,uint256)': - resultType = parseInt(decodedData[1].toString(16).slice(-4, -2), 16); - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'uint256', 'uint256', 'address', 'uint256'], - [Operators.fheIfThenElse, decodedData[0], decodedData[1], decodedData[2], aclAddress, chainId], - ), - ); - handle = appendType(handle, resultType); - const clearControl = BigInt(await getClearText(decodedData[0])); - const clearIfTrue = BigInt(await getClearText(decodedData[1])); - const clearIfFalse = BigInt(await getClearText(decodedData[2])); - if (clearControl === 1n) { - clearText = clearIfTrue; - } else { - clearText = clearIfFalse; - } - insertSQL(handle, clearText); - break; - - case 'fheRand(bytes1)': - if (validIdxes.includes(obj2.index)) { - resultType = parseInt(decodedData[0], 16); - const preseed = ethers.keccak256( - ethers.solidityPacked( - ['uint256', 'address', 'uint256', 'uint256', 'uint256'], - [counterRand, aclAddress, chainId, blockStatus.blockhash, blockStatus.timestamp], - ), - ); - const seed = preseed.slice(0, 34); - handle = ethers.keccak256( - ethers.solidityPacked(['uint8', 'bytes1', 'bytes16'], [Operators.fheRand, decodedData[0], seed]), - ); - handle = appendType(handle, resultType); - clearText = getRandomBigInt(Number(NumBits[resultType])); - insertSQL(handle, clearText, true); - counterRand++; - } - break; - - case 'fheRandBounded(uint256,bytes1)': - if (validIdxes.includes(obj2.index)) { - resultType = parseInt(decodedData[1], 16); - const preseed = ethers.keccak256( - ethers.solidityPacked( - ['uint256', 'address', 'uint256', 'uint256', 'uint256'], - [counterRand, aclAddress, chainId, blockStatus.blockhash, blockStatus.timestamp], - ), - ); - const seed = preseed.slice(0, 34); - handle = ethers.keccak256( - ethers.solidityPacked( - ['uint8', 'uint256', 'bytes1', 'bytes16'], - [Operators.fheRandBounded, decodedData[0], decodedData[1], seed], - ), - ); - handle = appendType(handle, resultType); - clearText = getRandomBigInt(Number(log2(BigInt(decodedData[0])))); - insertSQL(handle, clearText, true); - counterRand++; - } - break; - } - } -} - function bitwiseNotUintBits(value: BigInt, numBits: number) { if (typeof value !== 'bigint') { throw new TypeError('The input value must be a BigInt.'); @@ -809,12 +117,6 @@ function bitwiseNotUintBits(value: BigInt, numBits: number) { return ~value & BIT_MASK; } -function isCoprocAdd(longString: string): boolean { - const strippedLongString = longString.replace(/^0+/, ''); - const normalizedLongString = strippedLongString.toLowerCase(); - return normalizedLongString === coprocAdd; -} - export const awaitCoprocessor = async (): Promise => { chainId = (await ethers.provider.getNetwork()).chainId; await processAllPastTFHEExecutorEvents();