Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 922dcdf

Browse files
committed
refactor: codegen
1 parent 666a4fc commit 922dcdf

File tree

10 files changed

+3858
-6825
lines changed

10 files changed

+3858
-6825
lines changed

contracts/codegen/common.ts

Lines changed: 118 additions & 261 deletions
Large diffs are not rendered by default.

contracts/codegen/generateOverloads.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
14
import { SUPPORTED_BITS, SUPPORTED_UINT } from './common';
25

36
type Test = {
@@ -301,7 +304,4 @@ export const generateTests = () => {
301304

302305
const tests = generateTests();
303306

304-
const fs = require('fs');
305-
const path = require('path');
306-
307307
fs.writeFileSync(`${path.resolve(__dirname)}/overloads.json`, JSON.stringify(tests));

contracts/codegen/main.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
import { mkdirSync, writeFileSync } from 'fs';
22

3-
import { ALL_OPERATORS, SUPPORTED_BITS, checks } from './common';
3+
import { validateFHETypes, validateOperators } from './common';
4+
import { ALL_OPERATORS } from './operators';
45
import operatorsPrices from './operatorsPrices.json';
5-
import { generateFHEGasLimit } from './payments';
6-
import * as t from './templates';
7-
import * as testgen from './testgen';
6+
import { generateSolidityFHEGasLimit } from './payments';
7+
import { generateSolidityImplLib, generateSolidityTFHELibAndOverloads } from './templates';
8+
import { generateSolidityUnitTestContracts, generateTypeScriptTestCode, splitOverloadsToShards } from './testgen';
9+
import { ALL_FHE_TYPES } from './types';
810

11+
/**
12+
* Generates all necessary files including Solidity contracts and TypeScript test files.
13+
*
14+
* This function performs the following steps:
15+
* 1. Generates FHE types from a JSON file.
16+
* 2. Validates and processes the list of operators.
17+
* 3. Generates Solidity source code for TFHE and implementation contracts.
18+
* 4. Splits the generated overloads into multiple shards to avoid exceeding Solidity's contract size limit.
19+
* 5. Writes the generated Solidity contracts and test files to the appropriate directories.
20+
* 6. Generates TypeScript test code for the split overloads and writes them to the test directory.
21+
*
22+
*/
923
function generateAllFiles() {
10-
const numberOfTestSplits = 12;
11-
if (!ALL_OPERATORS || !Array.isArray(ALL_OPERATORS) || ALL_OPERATORS.length === 0) {
12-
throw new Error('ALL_OPERATORS is not defined or invalid');
13-
}
14-
const operators = checks(ALL_OPERATORS);
15-
const [tfheSolSource, overloads] = t.tfheSol(operators, SUPPORTED_BITS, false);
16-
const overloadShards = testgen.splitOverloadsToShards(overloads);
17-
writeFileSync('lib/Impl.sol', t.implSol(operators));
24+
// const numberOfTestSplits = 12;
25+
26+
// Validate the FHE types
27+
validateFHETypes(ALL_FHE_TYPES);
28+
// Validate the operators
29+
validateOperators(ALL_OPERATORS);
30+
31+
const [tfheSolSource] = generateSolidityTFHELibAndOverloads(ALL_OPERATORS, ALL_FHE_TYPES);
32+
33+
/// Generate core Solidity contract files.
34+
writeFileSync('lib/Impl.sol', generateSolidityImplLib(ALL_OPERATORS));
1835
writeFileSync('lib/TFHE.sol', tfheSolSource);
19-
writeFileSync('contracts/FHEGasLimit.sol', generateFHEGasLimit(operatorsPrices));
36+
writeFileSync('contracts/FHEGasLimit.sol', generateSolidityFHEGasLimit(operatorsPrices));
37+
38+
/// Generate unit test files.
39+
/**
40+
const overloadShards = splitOverloadsToShards(overloads);
2041
mkdirSync('contracts/tests', { recursive: true });
2142
overloadShards.forEach((os) => {
22-
writeFileSync(`examples/tests/TFHETestSuite${os.shardNumber}.sol`, testgen.generateSmartContract(os));
43+
writeFileSync(`examples/tests/TFHETestSuite${os.shardNumber}.sol`, generateSolidityUnitTestContracts(os));
2344
});
24-
const tsSplits: string[] = testgen.generateTestCode(overloadShards, numberOfTestSplits);
45+
46+
const tsSplits: string[] = generateTypeScriptTestCode(overloadShards, numberOfTestSplits);
2547
tsSplits.forEach((split, splitIdx) => writeFileSync(`test/tfheOperations/tfheOperations${splitIdx + 1}.ts`, split));
48+
*/
2649
}
2750

2851
generateAllFiles();

contracts/codegen/operators.ts

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import { Operator, OperatorArguments, ReturnType } from './common';
2+
3+
/**
4+
* A list of all supported operators with their respective properties.
5+
*
6+
* Each operator object contains the following properties:
7+
* - `name`: The name of the operator.
8+
* - `hasScalar`: A boolean indicating if the operator supports scalar values.
9+
* - `hasEncrypted`: A boolean indicating if the operator supports encrypted values.
10+
* - `arguments`: The type of arguments the operator accepts (binary or unary).
11+
* - `returnType`: The return type of the operator.
12+
* - `fheLibName`: The corresponding function name in the FHE library.
13+
* - `leftScalarEncrypt` (optional): A boolean indicating if the left scalar should be encrypted.
14+
* - `leftScalarDisable` (optional): A boolean indicating if the left scalar is disabled.
15+
* - `shiftOperator` (optional): A boolean indicating if the operator is a shift operator.
16+
* - `rotateOperator` (optional): A boolean indicating if the operator is a rotate operator.
17+
* - `leftScalarInvertOp` (optional): The name of the inverted operator for the left scalar.
18+
*/
19+
export const ALL_OPERATORS: Operator[] = [
20+
{
21+
name: 'add',
22+
hasScalar: true,
23+
hasEncrypted: true,
24+
arguments: OperatorArguments.Binary,
25+
returnType: ReturnType.Uint,
26+
fheLibName: 'fheAdd',
27+
},
28+
{
29+
name: 'sub',
30+
hasScalar: true,
31+
hasEncrypted: true,
32+
arguments: OperatorArguments.Binary,
33+
returnType: ReturnType.Uint,
34+
leftScalarEncrypt: true,
35+
fheLibName: 'fheSub',
36+
},
37+
{
38+
name: 'mul',
39+
hasScalar: true,
40+
hasEncrypted: true,
41+
arguments: OperatorArguments.Binary,
42+
returnType: ReturnType.Uint,
43+
fheLibName: 'fheMul',
44+
},
45+
{
46+
name: 'div',
47+
hasScalar: true,
48+
hasEncrypted: false,
49+
arguments: OperatorArguments.Binary,
50+
returnType: ReturnType.Uint,
51+
leftScalarDisable: true,
52+
fheLibName: 'fheDiv',
53+
},
54+
{
55+
name: 'rem',
56+
hasScalar: true,
57+
hasEncrypted: false,
58+
arguments: OperatorArguments.Binary,
59+
returnType: ReturnType.Uint,
60+
leftScalarDisable: true,
61+
fheLibName: 'fheRem',
62+
},
63+
{
64+
name: 'and',
65+
hasScalar: true,
66+
hasEncrypted: true,
67+
arguments: OperatorArguments.Binary,
68+
returnType: ReturnType.Uint,
69+
fheLibName: 'fheBitAnd',
70+
},
71+
{
72+
name: 'or',
73+
hasScalar: true,
74+
hasEncrypted: true,
75+
arguments: OperatorArguments.Binary,
76+
returnType: ReturnType.Uint,
77+
fheLibName: 'fheBitOr',
78+
},
79+
{
80+
name: 'xor',
81+
hasScalar: true,
82+
hasEncrypted: true,
83+
arguments: OperatorArguments.Binary,
84+
returnType: ReturnType.Uint,
85+
fheLibName: 'fheBitXor',
86+
},
87+
{
88+
name: 'shl',
89+
hasScalar: true,
90+
hasEncrypted: true,
91+
arguments: OperatorArguments.Binary,
92+
returnType: ReturnType.Uint,
93+
leftScalarEncrypt: true,
94+
shiftOperator: true,
95+
fheLibName: 'fheShl',
96+
},
97+
{
98+
name: 'shr',
99+
hasScalar: true,
100+
hasEncrypted: true,
101+
arguments: OperatorArguments.Binary,
102+
returnType: ReturnType.Uint,
103+
leftScalarEncrypt: true,
104+
shiftOperator: true,
105+
fheLibName: 'fheShr',
106+
},
107+
{
108+
name: 'rotl',
109+
hasScalar: true,
110+
hasEncrypted: true,
111+
arguments: OperatorArguments.Binary,
112+
returnType: ReturnType.Uint,
113+
leftScalarEncrypt: true,
114+
rotateOperator: true,
115+
fheLibName: 'fheRotl',
116+
},
117+
{
118+
name: 'rotr',
119+
hasScalar: true,
120+
hasEncrypted: true,
121+
arguments: OperatorArguments.Binary,
122+
returnType: ReturnType.Uint,
123+
leftScalarEncrypt: true,
124+
rotateOperator: true,
125+
fheLibName: 'fheRotr',
126+
},
127+
{
128+
name: 'eq',
129+
hasScalar: true,
130+
hasEncrypted: true,
131+
arguments: OperatorArguments.Binary,
132+
returnType: ReturnType.Ebool,
133+
fheLibName: 'fheEq',
134+
},
135+
{
136+
name: 'ne',
137+
hasScalar: true,
138+
hasEncrypted: true,
139+
arguments: OperatorArguments.Binary,
140+
returnType: ReturnType.Ebool,
141+
fheLibName: 'fheNe',
142+
},
143+
{
144+
name: 'ge',
145+
leftScalarInvertOp: 'le',
146+
hasScalar: true,
147+
hasEncrypted: true,
148+
arguments: OperatorArguments.Binary,
149+
returnType: ReturnType.Ebool,
150+
fheLibName: 'fheGe',
151+
},
152+
{
153+
name: 'gt',
154+
leftScalarInvertOp: 'lt',
155+
hasScalar: true,
156+
hasEncrypted: true,
157+
arguments: OperatorArguments.Binary,
158+
returnType: ReturnType.Ebool,
159+
fheLibName: 'fheGt',
160+
},
161+
{
162+
name: 'le',
163+
leftScalarInvertOp: 'ge',
164+
hasScalar: true,
165+
hasEncrypted: true,
166+
arguments: OperatorArguments.Binary,
167+
returnType: ReturnType.Ebool,
168+
fheLibName: 'fheLe',
169+
},
170+
{
171+
name: 'lt',
172+
leftScalarInvertOp: 'gt',
173+
hasScalar: true,
174+
hasEncrypted: true,
175+
arguments: OperatorArguments.Binary,
176+
returnType: ReturnType.Ebool,
177+
fheLibName: 'fheLt',
178+
},
179+
{
180+
name: 'min',
181+
hasScalar: true,
182+
hasEncrypted: true,
183+
arguments: OperatorArguments.Binary,
184+
returnType: ReturnType.Uint,
185+
fheLibName: 'fheMin',
186+
},
187+
{
188+
name: 'max',
189+
hasScalar: true,
190+
hasEncrypted: true,
191+
arguments: OperatorArguments.Binary,
192+
returnType: ReturnType.Uint,
193+
fheLibName: 'fheMax',
194+
},
195+
{
196+
name: 'neg',
197+
hasScalar: true,
198+
hasEncrypted: true,
199+
arguments: OperatorArguments.Unary,
200+
returnType: ReturnType.Uint,
201+
fheLibName: 'fheNeg',
202+
},
203+
{
204+
name: 'not',
205+
hasScalar: true,
206+
hasEncrypted: true,
207+
arguments: OperatorArguments.Unary,
208+
returnType: ReturnType.Uint,
209+
fheLibName: 'fheNot',
210+
},
211+
];

contracts/codegen/payments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface PriceData {
77
};
88
}
99

10-
export function generateFHEGasLimit(priceData: PriceData): string {
10+
export function generateSolidityFHEGasLimit(priceData: PriceData): string {
1111
let output = `// SPDX-License-Identifier: BSD-3-Clause-Clear
1212
pragma solidity ^0.8.24;
1313

0 commit comments

Comments
 (0)