Loading...
Loading...
Loading...
Loading...
FheLib
is a library implemented inside fhevm-go. It offers FHE-related functionalities such as homomorphic operations, decryption/reencryption requests and so on. FheLib is exposed as a single precompiled
contract (or a precompile
for short) that is integrated into the underlying blockchain.
FheLib functions can be called by calling the FheLib precompile with a respective EVM function selector.
This page describes the required inputs, behaviours and outputs of some of these functions.
The GetCiphertext
function returns a serialized TFHE ciphertext from protected storage given:
contract address where the ciphertext is stored at
the ebool/e(u)int value (also called a handle) for which the ciphertext is requested
GetCiphertext only works via the eth_call
RPC.
To call GetCiphertext via eth_call
, the following Python can serve as an example:
In order to use the library, you need to clone the repository and build it. This is required because the library depends on the tfhe-rs
library that needs to be built from source (for now), and Go doesn't support such a build.
You can now use it in your project by adding it to go.mod
, and adding a replace
to point to your local build. An example using fhevm-go
v1.0.0:
The replace is necessary for now as Go build system can't build the tfhe-rs
library that fhevm-go
needs. It's therefore necessary that we build it manually as mentioned above, then point to our ready-to-use directory in go.mod
📁 Github | 💛 Community support | 🟨 Zama Bounty Program
fhevm-go is an open-source library used to easily integrate the fhEVM into an EVM-compatible blockchain.
fhevm-go gives your EVM the ability to compute on encrypted data using fully homomorphic encryption by:
a collection of operations on encrypted data via precompiled contracts
various additional EVM components that support encrypted computation
The library helps EVM maintainers to extend their EVM with the power of FHE. If you are looking for a library to deploy and use smart contracts on an fhEVM, you should better look at fhevm
This software is distributed under the BSD-3-Clause-Clear license. If you have any questions, please contact us at hello@zama.ai.
This document is a guide listing detailed steps to integrate fhevm-go
into or any other implementations that follow the same architecture.
This document is based on go-ethereum v1.13.5
core/state_transition.go
replace the last return with:
which will impact tests as the returned gas won’t be the same.
core/vm/contracts.go
update the PrecompiledContract
interface to:
Add:
to all precompiled contract maps (e.g. PrecompiledContractsHomestead
)
We used 93 as the address of the precompile here, but you can choose any other address as far as client libraries know where to find it.
fheLib
precompileRunPrecompiledContract
core/vm/errors.go
fhevm-go
to be recognized at runtimecore/vm/evm.go
EVM
struct with new fieldsWhile implementing fhevmEnvironment
as:
In:
Initialize isGasEstimation
using config.IsGasEstimation
Initialize isEthCall
using config.IsEthCall
Initialize fhevmEnvironment
with FhevmImplementation{interpreter: nil, logger: &fhevm.DefaultLogger{}, data: fhevm.NewFhevmData(), params: fhevm.DefaultFhevmParams()}
After initializing evm.interpreter
make sure to point fhevmEnvironment
to it evm.fhevmEnvironment.interpreter = evm.interpreter
then initialize it fhevm.InitFhevm(&evm.fhevmEnvironment)
After changing precompiled contract interface in 2, we have to change usages of:
to:
Now implement the fhevm.EVMEnvironment
interface for FhevmImplementation
:
core/vm/instructions.go
opSstore
Rewrite opSstore
by a call to their fhevm implementation:
core/vm/interpreter.go
Config
struct with new fieldsGetMemory
, GetStack
and GetContract
methodsRun
methodIn:
core/vm/stack.go
core/vm/operations_acl.go
In func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
Just before original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32())
, add this block to increase SSTORE gas cost for storing a ciphertext:
internal/ethapi/api.go
Add isGasEstimation, isEthCall bool
arguments to func doCall
and pass them in vm.Config
during EVM creation:
Add isGasEstimation, isEthCall bool
arguments to func DoCall
and forward them in the call to doCall
Update usages of doCall
and DoCall
by simply setting IsEthCall
to true
when it’s a call, and IsGasEstimation
to true
when it’s estimating gas
graphql/graphql.go
Update usages of doCall
and DoCall
by simply setting IsEthCall
to true
when it’s a call, and IsGasEstimation
to true
when it’s estimating gas