Hello,
I’m testing homomorphic operations and I’m having an issue while communicating the contract.
I’ve observed that there is a delay getting the updated value from the contract. For example I have script that subtracts two numbers 5 and 3. I have to run the script twice to get the correct answer which is 2.
If I change the values to 8 and 2, I get the answer 2 and I have to run it again to get the correct answer which is 6.
Can anyone guide me on how to fix this, if possible. I’ve attached portions of my code for reference. Thank you
JS:
const interact = async () => {
// Initialize contract with ethers
const contract = new Contract(CONTRACT_ADDRESS, abi, signer);// Get instance to encrypt amount parameter
const instance = await getInstance();
await instance.setSignature(CONTRACT_ADDRESS, ethers.Signature)
const generatedToken = instance.generatePublicKey({
verifyingContract: CONTRACT_ADDRESS,
});await contract.setNumbers(instance.encrypt32(10), instance.encrypt32(5)); await contract.subtractNumbers(); const result = await contract.reencryptSubtraction(generatedToken.publicKey); const decResult = instance.decrypt(CONTRACT_ADDRESS, result); console.log(decResult);
};
interact()
.then(successful => {console.log(“success”); process.exit(0); })
.catch(wrong => {console.log(“something went wrong”, wrong); process.exit(0); });
Contract:
function setNumbers(bytes calldata first, bytes calldata second) public { firstNum = TFHE.asEuint32(first); secondNum = TFHE.asEuint32(second); } function subtractNumbers() public { result = TFHE.sub(firstNum, secondNum); } function reencryptSubtraction(bytes32 publicKey) public view returns (bytes memory) { return TFHE.reencrypt(result, publicKey, 0); }