Skip to content

Commit

Permalink
Merge pull request #535 from zama-ai/removePayment
Browse files Browse the repository at this point in the history
feat: set null FHE gas prices +  remove Payment lib
  • Loading branch information
jatZama authored Sep 27, 2024
2 parents a91ab1c + 7b24a8c commit 76275f9
Show file tree
Hide file tree
Showing 36 changed files with 331 additions and 495 deletions.
1 change: 0 additions & 1 deletion codegen/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function generateAllFiles() {
const ovShards = testgen.splitOverloadsToShards(overloads);
writeFileSync('lib/Impl.sol', t.implSol(context, operators));
writeFileSync('lib/TFHE.sol', tfheSolSource);
writeFileSync('lib/FhevmLib.sol', t.fhevmLibSol(operators));
writeFileSync('lib/FHEPayment.sol', generateFHEPayment(operatorsPrices));
writeFileSync('payment/Payment.sol', t.paymentSol());
mkdirSync('examples/tests', { recursive: true });
Expand Down
4 changes: 2 additions & 2 deletions codegen/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function generateFHEPayment(priceData: PriceData): string {
address public constant tfheExecutorAddress = tfheExecutorAdd;
uint256 private constant FHE_GAS_BLOCKLIMIT = 10_000_000;
uint256 private constant MIN_FHE_GASPRICE = 10_000_000; // minimum of 0.01 Gwei
uint256 private constant FHE_GASPRICE_NATIVE_RATIO = 1000; // fhe gas price is set to 0.1% of native gas price (if above minimum)
uint256 private constant MIN_FHE_GASPRICE = 0; // eg: 10_000_000 means a minimum of 0.01 Gwei
uint256 private constant FHE_GASPRICE_NATIVE_RATIO = 0; // eg: 1000 means fhe gas price is set to 0.1% of native gas price (if above minimum)
/// @custom:storage-location erc7201:fhevm.storage.FHEPayment
struct FHEPaymentStorage {
Expand Down
8 changes: 2 additions & 6 deletions codegen/testgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ async function deployTfheTestFixture${os.shardNumber}(): Promise<TFHETestSuite${
const admin = signers.alice;
const contractFactory = await ethers.getContractFactory('TFHETestSuite${os.shardNumber}');
const contract = await contractFactory.connect(admin).deploy({
value: ethers.parseEther('0.001'),
});
const contract = await contractFactory.connect(admin).deploy();
await contract.waitForDeployment();
return contract;
Expand Down Expand Up @@ -231,7 +229,6 @@ export function generateSmartContract(os: OverloadShard): string {
pragma solidity ^0.8.24;
import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";
contract TFHETestSuite${os.shardNumber} {
ebool public resb;
Expand All @@ -241,9 +238,8 @@ export function generateSmartContract(os: OverloadShard): string {
euint32 public res32;
euint64 public res64;
constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}
`);
Expand Down
4 changes: 1 addition & 3 deletions examples/BlindAuction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "../lib/TFHE.sol";
import "./EncryptedERC20.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";
import "../gateway/GatewayCaller.sol";
import "../payment/Payment.sol";

contract BlindAuction is Ownable2Step, GatewayCaller {
uint256 public endTime;
Expand Down Expand Up @@ -56,10 +55,9 @@ contract BlindAuction is Ownable2Step, GatewayCaller {
EncryptedERC20 _tokenContract,
uint256 biddingTime,
bool isStoppable
) payable Ownable(msg.sender) {
) Ownable(msg.sender) {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Gateway.setGateway(Gateway.defaultGatewayAddress());
Payment.depositForThis(msg.value);
beneficiary = _beneficiary;
tokenContract = _tokenContract;
endTime = block.timestamp + biddingTime;
Expand Down
4 changes: 1 addition & 3 deletions examples/EncryptedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pragma solidity ^0.8.24;

import "../lib/TFHE.sol";
import "../payment/Payment.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";

contract EncryptedERC20 is Ownable2Step {
Expand All @@ -22,9 +21,8 @@ contract EncryptedERC20 is Ownable2Step {
// A mapping of the form mapping(owner => mapping(spender => allowance)).
mapping(address => mapping(address => euint64)) internal allowances;

constructor(string memory name_, string memory symbol_) payable Ownable(msg.sender) {
constructor(string memory name_, string memory symbol_) Ownable(msg.sender) {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
_name = name_;
_symbol = symbol_;
}
Expand Down
4 changes: 1 addition & 3 deletions examples/Rand.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pragma solidity ^0.8.24;

import "../lib/TFHE.sol";
import "../payment/Payment.sol";

contract Rand {
euint8 public value8;
Expand All @@ -12,9 +11,8 @@ contract Rand {
euint64 public value64;
euint64 public value64Bounded;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function generate8() public {
Expand Down
5 changes: 1 addition & 4 deletions examples/Reencrypt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pragma solidity ^0.8.24;

import "../lib/TFHE.sol";
import "../payment/Payment.sol";

contract Reencrypt {
ebool public xBool;
Expand All @@ -15,11 +14,9 @@ contract Reencrypt {
eaddress public xAddress;
ebytes256 public yBytes256;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());

Payment.depositForThis(msg.value);

xBool = TFHE.asEbool(true);
TFHE.allowThis(xBool);
TFHE.allow(xBool, msg.sender);
Expand Down
4 changes: 1 addition & 3 deletions examples/TestAsyncDecrypt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.24;

import "../lib/TFHE.sol";
import "../gateway/GatewayCaller.sol";
import "../payment/Payment.sol";

contract TestAsyncDecrypt is GatewayCaller {
ebool xBool;
Expand Down Expand Up @@ -32,10 +31,9 @@ contract TestAsyncDecrypt is GatewayCaller {

uint256 public latestRequestID;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Gateway.setGateway(Gateway.defaultGatewayAddress());
Payment.depositForThis(msg.value);
xBool = TFHE.asEbool(true);
TFHE.allowThis(xBool);
xUint4 = TFHE.asEuint4(4);
Expand Down
4 changes: 1 addition & 3 deletions examples/tests/TFHEManualTestSuite.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.24;

import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";

contract TFHEManualTestSuite {
ebool public resb;
Expand All @@ -13,9 +12,8 @@ contract TFHEManualTestSuite {
euint64 public res64;
eaddress public resAdd;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function eqEbytes256(einput inp1, bytes calldata inputProof1, einput inp2, bytes calldata inputProof2) external {
Expand Down
4 changes: 1 addition & 3 deletions examples/tests/TFHETestSuite1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.24;

import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";

contract TFHETestSuite1 {
ebool public resb;
Expand All @@ -12,9 +11,8 @@ contract TFHETestSuite1 {
euint32 public res32;
euint64 public res64;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function add_euint4_euint4(einput a, einput b, bytes calldata inputProof) public {
Expand Down
4 changes: 1 addition & 3 deletions examples/tests/TFHETestSuite2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.24;

import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";

contract TFHETestSuite2 {
ebool public resb;
Expand All @@ -12,9 +11,8 @@ contract TFHETestSuite2 {
euint32 public res32;
euint64 public res64;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function eq_euint8_euint4(einput a, einput b, bytes calldata inputProof) public {
Expand Down
4 changes: 1 addition & 3 deletions examples/tests/TFHETestSuite3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.24;

import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";

contract TFHETestSuite3 {
ebool public resb;
Expand All @@ -12,9 +11,8 @@ contract TFHETestSuite3 {
euint32 public res32;
euint64 public res64;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function min_euint16_euint4(einput a, einput b, bytes calldata inputProof) public {
Expand Down
4 changes: 1 addition & 3 deletions examples/tests/TFHETestSuite4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.24;

import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";

contract TFHETestSuite4 {
ebool public resb;
Expand All @@ -12,9 +11,8 @@ contract TFHETestSuite4 {
euint32 public res32;
euint64 public res64;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function or_euint32_euint8(einput a, einput b, bytes calldata inputProof) public {
Expand Down
4 changes: 1 addition & 3 deletions examples/tests/TFHETestSuite5.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.24;

import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";

contract TFHETestSuite5 {
ebool public resb;
Expand All @@ -12,9 +11,8 @@ contract TFHETestSuite5 {
euint32 public res32;
euint64 public res64;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function le_euint64_euint8(einput a, einput b, bytes calldata inputProof) public {
Expand Down
4 changes: 1 addition & 3 deletions examples/tests/TFHETestSuite6.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.24;

import "../../lib/TFHE.sol";
import "../../payment/Payment.sol";

contract TFHETestSuite6 {
ebool public resb;
Expand All @@ -12,9 +11,8 @@ contract TFHETestSuite6 {
euint32 public res32;
euint64 public res64;

constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

function shr_euint64_euint8(einput a, einput b, bytes calldata inputProof) public {
Expand Down
4 changes: 2 additions & 2 deletions lib/FHEPayment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ contract FHEPayment is UUPSUpgradeable, Ownable2StepUpgradeable {
address public constant tfheExecutorAddress = tfheExecutorAdd;

uint256 private constant FHE_GAS_BLOCKLIMIT = 10_000_000;
uint256 private constant MIN_FHE_GASPRICE = 10_000_000; // minimum of 0.01 Gwei
uint256 private constant FHE_GASPRICE_NATIVE_RATIO = 1000; // fhe gas price is set to 0.1% of native gas price (if above minimum)
uint256 private constant MIN_FHE_GASPRICE = 0; // eg: 10_000_000 means a minimum of 0.01 Gwei
uint256 private constant FHE_GASPRICE_NATIVE_RATIO = 0; // eg: 1000 means fhe gas price is set to 0.1% of native gas price (if above minimum)

/// @custom:storage-location erc7201:fhevm.storage.FHEPayment
struct FHEPaymentStorage {
Expand Down
4 changes: 1 addition & 3 deletions test/encryptedERC20/EncryptedERC20.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export async function deployEncryptedERC20Fixture(): Promise<EncryptedERC20> {
const signers = await getSigners();

const contractFactory = await ethers.getContractFactory('EncryptedERC20');
const contract = await contractFactory.connect(signers.alice).deploy('Naraggara', 'NARA', {
value: ethers.parseEther('0.001'),
}); // City of Zama's battle
const contract = await contractFactory.connect(signers.alice).deploy('Naraggara', 'NARA'); // City of Zama's battle
await contract.waitForDeployment();

return contract;
Expand Down
18 changes: 5 additions & 13 deletions test/gatewayDecrypt/testAsyncDecrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ describe('TestAsyncDecrypt', function () {
// very first request of decryption always fail at the moment due to a gateway bug
// TODO: remove following 8 lines when the gateway bug will be fixed
const contractFactory = await ethers.getContractFactory('TestAsyncDecrypt');
this.contract = await contractFactory.connect(this.signers.alice).deploy({
value: ethers.parseEther('0.001'),
});
this.contract = await contractFactory.connect(this.signers.alice).deploy();
await this.contract.waitForDeployment();
this.contractAddress = await this.contract.getAddress();
this.instances = await createInstances(this.signers);
Expand All @@ -30,7 +28,7 @@ describe('TestAsyncDecrypt', function () {

beforeEach(async function () {
const contractFactory = await ethers.getContractFactory('TestAsyncDecrypt');
this.contract = await contractFactory.connect(this.signers.alice).deploy({ value: ethers.parseEther('0.001') });
this.contract = await contractFactory.connect(this.signers.alice).deploy();
this.contractAddress = await this.contract.getAddress();
this.instances = await createInstances(this.signers);
});
Expand Down Expand Up @@ -89,9 +87,7 @@ describe('TestAsyncDecrypt', function () {

it('test async decrypt bool trustless', async function () {
const contractFactory = await ethers.getContractFactory('TestAsyncDecrypt');
const contract2 = await contractFactory.connect(this.signers.alice).deploy({
value: ethers.parseEther('0.001'),
});
const contract2 = await contractFactory.connect(this.signers.alice).deploy();
const tx2 = await contract2.requestBoolTrustless({ gasLimit: 5_000_000 });
await tx2.wait();
await awaitAllDecryptionResults();
Expand Down Expand Up @@ -390,9 +386,7 @@ describe('TestAsyncDecrypt', function () {

it('test async decrypt ebytes256 non-trivial trustless', async function () {
const contractFactory = await ethers.getContractFactory('TestAsyncDecrypt');
const contract2 = await contractFactory.connect(this.signers.alice).deploy({
value: ethers.parseEther('0.001'),
});
const contract2 = await contractFactory.connect(this.signers.alice).deploy();
const inputAlice = this.instances.alice.createEncryptedInput(
await contract2.getAddress(),
this.signers.alice.address,
Expand All @@ -412,9 +406,7 @@ describe('TestAsyncDecrypt', function () {

it('test async decrypt mixed with ebytes256 trustless', async function () {
const contractFactory = await ethers.getContractFactory('TestAsyncDecrypt');
const contract2 = await contractFactory.connect(this.signers.alice).deploy({
value: ethers.parseEther('0.001'),
});
const contract2 = await contractFactory.connect(this.signers.alice).deploy();
const inputAlice = this.instances.alice.createEncryptedInput(
await contract2.getAddress(),
this.signers.alice.address,
Expand Down
8 changes: 2 additions & 6 deletions test/kmsVerifier/kmsVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ describe('KMSVerifier', function () {
expect((await kmsVerifier.getSigners()).length).to.equal(2); // one signer has been added

const contractFactory = await ethers.getContractFactory('TestAsyncDecrypt');
const contract = await contractFactory.connect(this.signers.alice).deploy({
value: ethers.parseEther('0.001'),
});
const contract = await contractFactory.connect(this.signers.alice).deploy();
const tx2 = await contract.requestBool({ gasLimit: 5_000_000 });
await tx2.wait();
await awaitAllDecryptionResults();
Expand Down Expand Up @@ -79,9 +77,7 @@ describe('KMSVerifier', function () {
const y4 = await contract.yUint8();
expect(y4).to.equal(42); // even with more than 2 signatures decryption should still succeed

const contract2 = await contractFactory.connect(this.signers.alice).deploy({
value: ethers.parseEther('0.001'),
});
const contract2 = await contractFactory.connect(this.signers.alice).deploy();
const inputAlice = this.instances.alice.createEncryptedInput(
await contract2.getAddress(),
this.signers.alice.address,
Expand Down
4 changes: 2 additions & 2 deletions test/paymentUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export async function initializeFHEPayment() {
return fhePayment;
}

export const FHE_GASPRICE_NATIVE_RATIO = 1000n;
export const MIN_FHE_GASPRICE = 10_000_000n;
export const FHE_GASPRICE_NATIVE_RATIO = 0n; // 1000n;
export const MIN_FHE_GASPRICE = 0n; // 10_000_000n;
Loading

0 comments on commit 76275f9

Please sign in to comment.