Skip to content

Commit

Permalink
feat: updated TFHEExecutor to support new types
Browse files Browse the repository at this point in the history
chore: eq-neq  bool support and bitwise scalar support

chore: overloads for fheEq and fheNe

fix: reverts onchain when rhs is 0 in fheDiv-fheRem

feat: added support for scalar and,or,xor

chore: cleanup codegen

feat: adds euint128 and euint256 types

chore: removes ebool fheRandBounded and adds manual tests

feat: adds new types for fheRand

feat: adds trivialEncrypt for ebytesXXX

feat: adds fheEq fheNe ifThenElse for new types

chore: rename asyncDecrypt() to initGateway()
  • Loading branch information
jatZama committed Oct 18, 2024
1 parent f51b435 commit 45e860d
Show file tree
Hide file tree
Showing 51 changed files with 60,523 additions and 18,003 deletions.
21 changes: 4 additions & 17 deletions codegen/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export enum ReturnType {
Ebool,
}

export const SUPPORTED_BITS: number[] = [4, 8, 16, 32, 64];
export const SUPPORTED_BITS: number[] = [4, 8, 16, 32, 64, 128, 256];

export const ALL_OPERATORS: Operator[] = [
{
Expand Down Expand Up @@ -91,7 +91,7 @@ export const ALL_OPERATORS: Operator[] = [
{
name: 'and',
precompileName: 'BitwiseAnd',
hasScalar: false,
hasScalar: true,
hasEncrypted: true,
arguments: OperatorArguments.Binary,
returnType: ReturnType.Uint,
Expand All @@ -101,7 +101,7 @@ export const ALL_OPERATORS: Operator[] = [
{
name: 'or',
precompileName: 'BitwiseOr',
hasScalar: false,
hasScalar: true,
hasEncrypted: true,
arguments: OperatorArguments.Binary,
returnType: ReturnType.Uint,
Expand All @@ -111,7 +111,7 @@ export const ALL_OPERATORS: Operator[] = [
{
name: 'xor',
precompileName: 'BitwiseXor',
hasScalar: false,
hasScalar: true,
hasEncrypted: true,
arguments: OperatorArguments.Binary,
returnType: ReturnType.Uint,
Expand Down Expand Up @@ -260,16 +260,3 @@ export function checks(operators: Operator[]): Operator[] {

return operators;
}

export function networkCodegenContext(network: Network): CodegenContext {
switch (network) {
case Network.Evmos:
return {
libFheAddress: '0x000000000000000000000000000000000000005d',
};
case Network.Network1:
return {
libFheAddress: '0x010000000000000000000000000000000000005D',
};
}
}
5 changes: 1 addition & 4 deletions codegen/generateOverloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type SupportedFunction = SupportedFunctionParams &
};

const SUPPORTED_UINT = [8, 16, 32, 64, 128, 256];
const SUPPORTED_BITS = [4, 8, 16, 32, 64];
const SUPPORTED_BITS = [4, 8, 16, 32, 64, 128, 256];

const bigIntMin = (...args: bigint[]) => {
return args.reduce((min, e) => (e < min ? e : min), args[0]);
Expand Down Expand Up @@ -183,17 +183,14 @@ export const SUPPORTED_FUNCTIONS: SupportedFunctions = {
},
or: {
supportedBits: SUPPORTED_BITS,
noScalar: true,
evalTest: (lhsNumber, rhsNumber) => lhsNumber | rhsNumber,
},
and: {
supportedBits: SUPPORTED_BITS,
noScalar: true,
evalTest: (lhsNumber, rhsNumber) => lhsNumber & rhsNumber,
},
xor: {
supportedBits: SUPPORTED_BITS,
noScalar: true,
evalTest: (lhsNumber, rhsNumber) => lhsNumber ^ rhsNumber,
},
not: {
Expand Down
9 changes: 3 additions & 6 deletions codegen/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mkdirSync, writeFileSync } from 'fs';

import { ALL_OPERATORS, Network, SUPPORTED_BITS, checks, networkCodegenContext } from './common';
import { ALL_OPERATORS, SUPPORTED_BITS, checks } from './common';
import operatorsPrices from './operatorsPrices.json';
import { generateFHEPayment } from './payments';
import * as t from './templates';
Expand All @@ -9,12 +9,9 @@ import * as testgen from './testgen';
function generateAllFiles() {
const numSplits = 12;
const operators = checks(ALL_OPERATORS);

const network = Network[(process.env.TARGET_NETWORK as keyof typeof Network) || 'Evmos'];
const context = networkCodegenContext(network);
const [tfheSolSource, overloads] = t.tfheSol(context, operators, SUPPORTED_BITS, false);
const [tfheSolSource, overloads] = t.tfheSol(operators, SUPPORTED_BITS, false);
const ovShards = testgen.splitOverloadsToShards(overloads);
writeFileSync('lib/Impl.sol', t.implSol(context, operators));
writeFileSync('lib/Impl.sol', t.implSol(operators));
writeFileSync('lib/TFHE.sol', tfheSolSource);
writeFileSync('lib/FHEPayment.sol', generateFHEPayment(operatorsPrices));
writeFileSync('payment/Payment.sol', t.paymentSol());
Expand Down
183 changes: 141 additions & 42 deletions codegen/operatorsPrices.json
Original file line number Diff line number Diff line change
@@ -1,119 +1,218 @@
{
"fheAdd": {
"binary": true,
"scalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000 },
"nonScalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000 }
"scalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000, "6": 218000, "8": 253000 },
"nonScalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000, "6": 218000, "8": 253000 }
},
"fheSub": {
"binary": true,
"scalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000 },
"nonScalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000 }
"scalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000, "6": 218000, "8": 253000 },
"nonScalar": { "1": 65000, "2": 94000, "3": 133000, "4": 162000, "5": 188000, "6": 218000, "8": 253000 }
},
"fheMul": {
"binary": true,
"scalar": { "1": 88000, "2": 159000, "3": 208000, "4": 264000, "5": 356000 },
"nonScalar": { "1": 150000, "2": 197000, "3": 262000, "4": 359000, "5": 641000 }
"scalar": { "1": 88000, "2": 159000, "3": 208000, "4": 264000, "5": 356000, "6": 480000, "8": 647000 },
"nonScalar": { "1": 150000, "2": 197000, "3": 262000, "4": 359000, "5": 641000, "6": 1145000, "8": 2045000 }
},
"fheDiv": {
"binary": true,
"scalar": { "1": 139000, "2": 238000, "3": 314000, "4": 398000, "5": 584000, "6": 857000, "8": 1258000 }
},
"fheRem": {
"binary": true,
"scalar": { "1": 286000, "2": 460000, "3": 622000, "4": 805000, "5": 1095000, "6": 1499000, "8": 2052000 }
},
"fheDiv": { "binary": true, "scalar": { "1": 139000, "2": 238000, "3": 314000, "4": 398000, "5": 584000 } },
"fheRem": { "binary": true, "scalar": { "1": 286000, "2": 460000, "3": 622000, "4": 805000, "5": 1095000 } },
"fheBitAnd": {
"binary": true,
"nonScalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000 }
"scalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 },
"nonScalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 }
},
"fheBitOr": {
"binary": true,
"nonScalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000 }
"scalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 },
"nonScalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 }
},
"fheBitXor": {
"binary": true,
"nonScalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000 }
"scalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 },
"nonScalar": { "0": 26000, "1": 32000, "2": 34000, "3": 34000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 }
},
"fheShl": {
"binary": true,
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000 }
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000, "6": 282000, "8": 350000 }
},
"fheShr": {
"binary": true,
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000 }
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000, "6": 282000, "8": 350000 }
},
"fheRotl": {
"binary": true,
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000 }
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000, "6": 282000, "8": 350000 }
},
"fheRotr": {
"binary": true,
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000 }
"scalar": { "1": 35000, "2": 35000, "3": 35000, "4": 35000, "5": 38000, "6": 41000, "8": 44000 },
"nonScalar": { "1": 116000, "2": 133000, "3": 153000, "4": 183000, "5": 227000, "6": 282000, "8": 350000 }
},
"fheEq": {
"binary": true,
"scalar": { "1": 51000, "2": 53000, "3": 54000, "4": 82000, "5": 86000, "7": 90000, "11": 300000 },
"nonScalar": { "1": 51000, "2": 53000, "3": 54000, "4": 82000, "5": 86000, "7": 90000, "11": 300000 }
"scalar": {
"0": 49000,
"1": 51000,
"2": 53000,
"3": 54000,
"4": 82000,
"5": 86000,
"6": 88000,
"7": 90000,
"8": 100000,
"9": 150000,
"10": 200000,
"11": 300000
},
"nonScalar": {
"0": 49000,
"1": 51000,
"2": 53000,
"3": 54000,
"4": 82000,
"5": 86000,
"6": 88000,
"7": 90000,
"8": 100000,
"9": 150000,
"10": 200000,
"11": 300000
}
},
"fheNe": {
"binary": true,
"scalar": { "1": 51000, "2": 53000, "3": 54000, "4": 82000, "5": 86000, "7": 90000, "11": 300000 },
"nonScalar": { "1": 51000, "2": 53000, "3": 54000, "4": 82000, "5": 86000, "7": 90000, "11": 300000 }
"scalar": {
"0": 49000,
"1": 51000,
"2": 53000,
"3": 54000,
"4": 82000,
"5": 86000,
"6": 88000,
"7": 90000,
"8": 100000,
"9": 150000,
"10": 200000,
"11": 300000
},
"nonScalar": {
"0": 49000,
"1": 51000,
"2": 53000,
"3": 54000,
"4": 82000,
"5": 86000,
"6": 88000,
"7": 90000,
"8": 100000,
"9": 150000,
"10": 200000,
"11": 300000
}
},
"fheGe": {
"binary": true,
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 }
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 }
},
"fheGt": {
"binary": true,
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 }
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 }
},
"fheLe": {
"binary": true,
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 }
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 }
},
"fheLt": {
"binary": true,
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000 }
"scalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 },
"nonScalar": { "1": 70000, "2": 82000, "3": 105000, "4": 128000, "5": 156000, "6": 190000, "8": 231000 }
},
"fheMin": {
"binary": true,
"scalar": { "1": 121000, "2": 128000, "3": 150000, "4": 164000, "5": 192000 },
"nonScalar": { "1": 121000, "2": 128000, "3": 153000, "4": 183000, "5": 210000 }
"scalar": { "1": 121000, "2": 128000, "3": 150000, "4": 164000, "5": 192000, "6": 225000, "8": 264000 },
"nonScalar": { "1": 121000, "2": 128000, "3": 153000, "4": 183000, "5": 210000, "6": 241000, "8": 277000 }
},
"fheMax": {
"binary": true,
"scalar": { "1": 121000, "2": 128000, "3": 150000, "4": 164000, "5": 192000 },
"nonScalar": { "1": 121000, "2": 128000, "3": 153000, "4": 183000, "5": 210000 }
"scalar": { "1": 121000, "2": 128000, "3": 150000, "4": 164000, "5": 192000, "6": 225000, "8": 264000 },
"nonScalar": { "1": 121000, "2": 128000, "3": 153000, "4": 183000, "5": 210000, "6": 241000, "8": 277000 }
},
"fheNeg": {
"binary": false,
"types": { "1": 60000, "2": 95000, "3": 131000, "4": 160000, "5": 199000 }
"types": { "1": 60000, "2": 95000, "3": 131000, "4": 160000, "5": 199000, "6": 248000, "8": 309000 }
},
"fheNot": {
"binary": false,
"types": { "0": 30000, "1": 33000, "2": 34000, "3": 35000, "4": 36000, "5": 37000 }
"types": { "0": 30000, "1": 33000, "2": 34000, "3": 35000, "4": 36000, "5": 37000, "6": 38000, "8": 39000 }
},
"cast": {
"binary": false,
"types": { "1": 200, "2": 200, "3": 200, "4": 200, "5": 200 }
"types": { "0": 200, "1": 200, "2": 200, "3": 200, "4": 200, "5": 200, "6": 200, "8": 200 }
},
"trivialEncrypt": {
"binary": false,
"types": { "0": 100, "1": 100, "2": 100, "3": 200, "4": 300, "5": 600, "7": 700 }
"types": {
"0": 100,
"1": 100,
"2": 100,
"3": 200,
"4": 300,
"5": 600,
"6": 650,
"7": 700,
"8": 800,
"9": 1600,
"10": 3200,
"11": 6400
}
},
"ifThenElse": {
"binary": false,
"types": { "1": 45000, "2": 47000, "3": 47000, "4": 50000, "5": 53000, "7": 80000 }
"types": {
"0": 43000,
"1": 45000,
"2": 47000,
"3": 47000,
"4": 50000,
"5": 53000,
"6": 70000,
"7": 80000,
"8": 90000,
"9": 150000,
"10": 200000,
"11": 300000
}
},
"fheRand": {
"binary": false,
"types": { "2": 100000, "3": 100000, "4": 100000, "5": 100000 }
"types": {
"0": 100000,
"1": 100000,
"2": 100000,
"3": 100000,
"4": 100000,
"5": 100000,
"6": 100000,
"8": 100000,
"9": 200000,
"10": 300000,
"11": 400000
}
},
"fheRandBounded": {
"binary": false,
"types": { "2": 100000, "3": 100000, "4": 100000, "5": 100000 }
"types": { "1": 100000, "2": 100000, "3": 100000, "4": 100000, "5": 100000, "6": 100000, "8": 100000 }
}
}
Loading

0 comments on commit 45e860d

Please sign in to comment.