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

Commit cdd8b1c

Browse files
committed
refactor: testgen and tests
1 parent 583f805 commit cdd8b1c

30 files changed

+12385
-15054
lines changed

contracts/codegen/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export enum OperatorArguments {
178178
* Enum representing the possible return types.
179179
*/
180180
export enum ReturnType {
181-
Uint,
181+
Euint,
182182
Ebool,
183183
}
184184

contracts/codegen/generateOverloads.ts

Lines changed: 214 additions & 130 deletions
Large diffs are not rendered by default.

contracts/codegen/main.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { mkdirSync, writeFileSync } from 'fs';
2+
import path from 'path';
23

34
import { validateFHETypes, validateOperators } from './common';
5+
import { generateOverloads } from './generateOverloads';
46
import { ALL_OPERATORS } from './operators';
57
import operatorsPrices from './operatorsPrices.json';
68
import { generateSolidityFHEGasLimit } from './payments';
79
import { generateSolidityImplLib, generateSolidityTFHELibAndOverloads } from './templates';
8-
import { generateSolidityOverloads } from './templates-testgen';
9-
import { generateSolidityUnitTestContracts, generateTypeScriptTestCode, splitOverloadsToShards } from './testgen';
10+
import {
11+
generateSolidityOverloadTestFiles,
12+
generateSolidityUnitTestContracts,
13+
generateTypeScriptTestCode,
14+
splitOverloadsToShards,
15+
} from './testgen';
1016
import { ALL_FHE_TYPES } from './types';
1117

1218
/**
@@ -36,9 +42,15 @@ function generateAllFiles() {
3642
writeFileSync('lib/TFHE.sol', tfheSolSource);
3743
writeFileSync('contracts/FHEGasLimit.sol', generateSolidityFHEGasLimit(operatorsPrices));
3844

39-
/// Generate unit test files.
40-
const overloads = generateSolidityOverloads(ALL_OPERATORS, ALL_FHE_TYPES);
41-
const overloadShards = splitOverloadsToShards(overloads);
45+
// TODO: For now, the testgen only supports automatically generated tests for euintXX.
46+
/// Generate overloads, split them into shards, and generate Solidity contracts to be used for TypeScript unit test files.
47+
writeFileSync(
48+
`${path.resolve(__dirname)}/overloads.json`,
49+
JSON.stringify(generateOverloads(ALL_FHE_TYPES), (_key, value) =>
50+
typeof value === 'bigint' ? value.toString() : value,
51+
),
52+
);
53+
const overloadShards = splitOverloadsToShards(generateSolidityOverloadTestFiles(ALL_OPERATORS, ALL_FHE_TYPES));
4254
mkdirSync('contracts/tests', { recursive: true });
4355
overloadShards.forEach((os) => {
4456
writeFileSync(`examples/tests/TFHETestSuite${os.shardNumber}.sol`, generateSolidityUnitTestContracts(os));

contracts/codegen/operators.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ export const ALL_OPERATORS: Operator[] = [
2222
hasScalar: true,
2323
hasEncrypted: true,
2424
arguments: OperatorArguments.Binary,
25-
returnType: ReturnType.Uint,
25+
returnType: ReturnType.Euint,
2626
fheLibName: 'fheAdd',
2727
},
2828
{
2929
name: 'sub',
3030
hasScalar: true,
3131
hasEncrypted: true,
3232
arguments: OperatorArguments.Binary,
33-
returnType: ReturnType.Uint,
33+
returnType: ReturnType.Euint,
3434
leftScalarEncrypt: true,
3535
fheLibName: 'fheSub',
3636
},
@@ -39,15 +39,15 @@ export const ALL_OPERATORS: Operator[] = [
3939
hasScalar: true,
4040
hasEncrypted: true,
4141
arguments: OperatorArguments.Binary,
42-
returnType: ReturnType.Uint,
42+
returnType: ReturnType.Euint,
4343
fheLibName: 'fheMul',
4444
},
4545
{
4646
name: 'div',
4747
hasScalar: true,
4848
hasEncrypted: false,
4949
arguments: OperatorArguments.Binary,
50-
returnType: ReturnType.Uint,
50+
returnType: ReturnType.Euint,
5151
leftScalarDisable: true,
5252
fheLibName: 'fheDiv',
5353
},
@@ -56,7 +56,7 @@ export const ALL_OPERATORS: Operator[] = [
5656
hasScalar: true,
5757
hasEncrypted: false,
5858
arguments: OperatorArguments.Binary,
59-
returnType: ReturnType.Uint,
59+
returnType: ReturnType.Euint,
6060
leftScalarDisable: true,
6161
fheLibName: 'fheRem',
6262
},
@@ -65,31 +65,31 @@ export const ALL_OPERATORS: Operator[] = [
6565
hasScalar: true,
6666
hasEncrypted: true,
6767
arguments: OperatorArguments.Binary,
68-
returnType: ReturnType.Uint,
68+
returnType: ReturnType.Euint,
6969
fheLibName: 'fheBitAnd',
7070
},
7171
{
7272
name: 'or',
7373
hasScalar: true,
7474
hasEncrypted: true,
7575
arguments: OperatorArguments.Binary,
76-
returnType: ReturnType.Uint,
76+
returnType: ReturnType.Euint,
7777
fheLibName: 'fheBitOr',
7878
},
7979
{
8080
name: 'xor',
8181
hasScalar: true,
8282
hasEncrypted: true,
8383
arguments: OperatorArguments.Binary,
84-
returnType: ReturnType.Uint,
84+
returnType: ReturnType.Euint,
8585
fheLibName: 'fheBitXor',
8686
},
8787
{
8888
name: 'shl',
8989
hasScalar: true,
9090
hasEncrypted: true,
9191
arguments: OperatorArguments.Binary,
92-
returnType: ReturnType.Uint,
92+
returnType: ReturnType.Euint,
9393
leftScalarEncrypt: true,
9494
shiftOperator: true,
9595
fheLibName: 'fheShl',
@@ -99,7 +99,7 @@ export const ALL_OPERATORS: Operator[] = [
9999
hasScalar: true,
100100
hasEncrypted: true,
101101
arguments: OperatorArguments.Binary,
102-
returnType: ReturnType.Uint,
102+
returnType: ReturnType.Euint,
103103
leftScalarEncrypt: true,
104104
shiftOperator: true,
105105
fheLibName: 'fheShr',
@@ -109,7 +109,7 @@ export const ALL_OPERATORS: Operator[] = [
109109
hasScalar: true,
110110
hasEncrypted: true,
111111
arguments: OperatorArguments.Binary,
112-
returnType: ReturnType.Uint,
112+
returnType: ReturnType.Euint,
113113
leftScalarEncrypt: true,
114114
rotateOperator: true,
115115
fheLibName: 'fheRotl',
@@ -119,7 +119,7 @@ export const ALL_OPERATORS: Operator[] = [
119119
hasScalar: true,
120120
hasEncrypted: true,
121121
arguments: OperatorArguments.Binary,
122-
returnType: ReturnType.Uint,
122+
returnType: ReturnType.Euint,
123123
leftScalarEncrypt: true,
124124
rotateOperator: true,
125125
fheLibName: 'fheRotr',
@@ -181,31 +181,31 @@ export const ALL_OPERATORS: Operator[] = [
181181
hasScalar: true,
182182
hasEncrypted: true,
183183
arguments: OperatorArguments.Binary,
184-
returnType: ReturnType.Uint,
184+
returnType: ReturnType.Euint,
185185
fheLibName: 'fheMin',
186186
},
187187
{
188188
name: 'max',
189189
hasScalar: true,
190190
hasEncrypted: true,
191191
arguments: OperatorArguments.Binary,
192-
returnType: ReturnType.Uint,
192+
returnType: ReturnType.Euint,
193193
fheLibName: 'fheMax',
194194
},
195195
{
196196
name: 'neg',
197197
hasScalar: true,
198198
hasEncrypted: true,
199199
arguments: OperatorArguments.Unary,
200-
returnType: ReturnType.Uint,
200+
returnType: ReturnType.Euint,
201201
fheLibName: 'fheNeg',
202202
},
203203
{
204204
name: 'not',
205205
hasScalar: true,
206206
hasEncrypted: true,
207207
arguments: OperatorArguments.Unary,
208-
returnType: ReturnType.Uint,
208+
returnType: ReturnType.Euint,
209209
fheLibName: 'fheNot',
210210
},
211211
];

0 commit comments

Comments
 (0)