3. Turn it into FHEVM
Initiate the contract
1
Create the FHECounter.sol file
FHECounter.sol filecd <your-project-root-directory>/contracts// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
/// @title A simple counter contract
contract Counter {
uint32 private _count;
/// @notice Returns the current count
function getCount() external view returns (uint32) {
return _count;
}
/// @notice Increments the counter by a specific value
function increment(uint32 value) external {
_count += value;
}
/// @notice Decrements the counter by a specific value
function decrement(uint32 value) external {
require(_count >= value, "Counter: cannot decrement below zero");
_count -= value;
}
}2
Turn Counter into FHECounter
Counter into FHECounter// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
import { FHE, euint32, externalEuint32 } from "@fhevm/solidity/lib/FHE.sol";
import { ZamaEthereumConfig } from "@fhevm/solidity/config/ZamaConfig.sol";/// @title A simple counter contract
contract Counter {/// @title A simple FHE counter contract
contract FHECounter is ZamaEthereumConfig {npx hardhat compileApply FHE functions and types
1
Comment out the increment() and decrement() Functions
increment() and decrement() Functions /// @notice Increments the counter by a specific value
// function increment(uint32 value) external {
// _count += value;
// }
/// @notice Decrements the counter by a specific value
// function decrement(uint32 value) external {
// require(_count >= value, "Counter: cannot decrement below zero");
// _count -= value;
// }2
3
Replace increment(uint32 value) with the FHEVM version increment(externalEuint32 value)
increment(uint32 value) with the FHEVM version increment(externalEuint32 value) /// @notice Increments the counter by a specific value
// function increment(uint32 value) external {
// _count += value;
// }/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
// _count += value;
}4
Convert externalEuint32 to euint32
externalEuint32 to euint32FHE.fromExternal(inputEuint32, inputProof);/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
// _count += value;
}/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
euint32 evalue = FHE.fromExternal(inputEuint32, inputProof);
// _count += value;
}5
Convert _count += value into its FHEVM equivalent
_count += value into its FHEVM equivalent/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
euint32 evalue = FHE.fromExternal(inputEuint32, inputProof);
// _count += value;
}/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
euint32 evalue = FHE.fromExternal(inputEuint32, inputProof);
_count = FHE.add(_count, evalue);
}Grant FHE Permissions
Replace
With :
Convert decrement() to its FHEVM equivalent
decrement() to its FHEVM equivalentCompile FHECounter.sol
FHECounter.solLast updated