Skip to content

Commit

Permalink
Merge pull request ethereum#15 from zama-ai/feature/add_fhe_operation…
Browse files Browse the repository at this point in the history
…s_add_encrypt_decrypt

Feature/add fhe operations add encrypt decrypt
  • Loading branch information
leventdem authored Dec 15, 2022
2 parents c4d0e16 + 0765df3 commit 01e32db
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 5 deletions.
5 changes: 5 additions & 0 deletions clean_tfhe_rs_api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

rm -rf tfhe-rs
rm core/vm/lib/libtfhe.*
rm core/vm/tfhe.h
251 changes: 247 additions & 4 deletions core/vm/contracts.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions install_thfe_rs_api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

git clone git@github.com:zama-ai/tfhe-rs.git
mkdir -p core/vm/lib
cd tfhe-rs
make build_c_api
cp target/release/libtfhe.* ../core/vm/lib
cp target/release/tfhe.h ../core/vm
58 changes: 57 additions & 1 deletion tests/solidity/zama/handles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@
pragma solidity >=0.7.0 <0.9.0;

contract Precompiles {

function precompile_add(uint256 handle1, uint256 handle2) internal view returns (uint256 out_handle) {
bytes32[2] memory input;
input[0] = bytes32(handle1);
input[1] = bytes32(handle2);
bytes32[1] memory output;
assembly {
if iszero(staticcall(gas(), 65, input, 64, output, 32)) {
revert(0, 0)
}
}
out_handle = uint256(output[0]);
}

function precompile_decrypt(uint256 handle1) internal view returns (uint256 out_handle) {
bytes32[1] memory input;
input[0] = bytes32(handle1);
bytes32[1] memory output;
assembly {
if iszero(staticcall(gas(), 69, input, 32, output, 32)) {
revert(0, 0)
}
}
out_handle = uint256(output[0]);

}

function precompile_encrypt(uint256 to_be_encrypted) internal view returns (uint256 out_handle) {
bytes32[1] memory input;
input[0] = bytes32(to_be_encrypted);
bytes32[1] memory output;
assembly {
if iszero(staticcall(gas(), 70, input, 32, output, 32)) {
revert(0, 0)
}
}
out_handle = uint256(output[0]);
}

function precompile_reencrypt(uint256 in_handle) internal view returns (uint256 out_handle) {
bytes32[1] memory input;
input[0] = bytes32(in_handle);
Expand Down Expand Up @@ -41,6 +80,7 @@ contract Precompiles {

contract HandleOwner is Precompiles {
uint256 public handle;
uint256 public handle2;
uint256 public bogus_handle = 42;
Callee callee;
address payable owner;
Expand All @@ -54,6 +94,22 @@ contract HandleOwner is Precompiles {
handle = precompile_verify(ciphertext);
}

function store2(bytes memory ciphertext) public {
handle2 = precompile_verify(ciphertext);
}

function add() public view returns (uint256) {
return precompile_add(handle, handle2);
}

function decrypt() public view returns (uint256) {
return precompile_decrypt(handle);
}

function encrypt(uint256 input) public view returns (uint256) {
return precompile_encrypt(input);
}

// If called before `ovewrite_handle()`, `reencrypt()` must suceed.
function reencrypt() public view returns (uint256) {
return precompile_reencrypt(handle);
Expand Down Expand Up @@ -126,4 +182,4 @@ contract Caller is Precompiles {
owner.load_handle_without_returning_it();
return precompile_reencrypt(handle);
}
}
}

0 comments on commit 01e32db

Please sign in to comment.