@@ -6,15 +6,30 @@ import { OperatorArguments, ReturnType } from './common';
66import { resolveTemplatePath } from './paths' ;
77import { getUint , removeTemplateComments } from './utils' ;
88
9- export function generateSolidityFHELib (
10- operators : Operator [ ] ,
11- fheTypes : FheTypeInfo [ ] ,
12- fheTypeDotSol : string ,
13- implDotSol : string ,
14- ) : string {
9+ export function generateFhevmECDSALib ( ) {
10+ const file = resolveTemplatePath ( 'FhevmECDSA.sol-template' ) ;
11+ const template = readFileSync ( file , 'utf8' ) ;
12+ let code = removeTemplateComments ( template ) ;
13+ return code ;
14+ }
15+
16+ export function generateSolidityFHELib ( {
17+ operators,
18+ fheTypes,
19+ fheTypeDotSol,
20+ implDotSol,
21+ ecdsaDotSol,
22+ } : {
23+ operators : Operator [ ] ;
24+ fheTypes : FheTypeInfo [ ] ;
25+ fheTypeDotSol : string ;
26+ implDotSol : string ;
27+ ecdsaDotSol : string ;
28+ } ) : string {
1529 // Placeholders:
1630 // =============
1731 // $${ImplDotSol}$$
32+ // $${EcdsaDotSol}$$
1833 // $${FheTypeDotSol}$$
1934 // $${FHEOperators}$$
2035 // $${ACLFunctions}$$
@@ -25,6 +40,7 @@ export function generateSolidityFHELib(
2540 let code = removeTemplateComments ( template ) ;
2641
2742 code = code . replace ( '$${ImplDotSol}$$' , implDotSol ) ;
43+ code = code . replace ( '$${EcdsaDotSol}$$' , ecdsaDotSol ) ;
2844 code = code . replace ( '$${FheTypeDotSol}$$' , fheTypeDotSol ) ;
2945
3046 // Exclude types that do not support any operators.
@@ -191,9 +207,13 @@ function handleSolidityTFHEEncryptedOperatorForTwoEncryptedTypes(
191207
192208 res . push ( `
193209 /**
194- * @dev Evaluates ${ operator . name } (e${ lhsFheType . type . toLowerCase ( ) } a, e${ rhsFheType . type . toLowerCase ( ) } b) and returns the result.
210+ * @dev Evaluates ${
211+ operator . name
212+ } (e${ lhsFheType . type . toLowerCase ( ) } a, e${ rhsFheType . type . toLowerCase ( ) } b) and returns the result.
195213 */
196- function ${ operator . name } (e${ lhsFheType . type . toLowerCase ( ) } a, e${ rhsFheType . type . toLowerCase ( ) } b) internal returns (${ returnType } ) {
214+ function ${
215+ operator . name
216+ } (e${ lhsFheType . type . toLowerCase ( ) } a, e${ rhsFheType . type . toLowerCase ( ) } b) internal returns (${ returnType } ) {
197217 if (!isInitialized(a)) {
198218 a = asE${ lhsFheType . type . toLowerCase ( ) } (0);
199219 }
@@ -272,7 +292,9 @@ function generateSolidityTFHEScalarOperator(fheType: AdjustedFheType, operator:
272292 let implExpressionA ;
273293
274294 if ( fheType . type == 'Bool' ) {
275- implExpressionA = `Impl.${ operator . name } (e${ fheType . type . toLowerCase ( ) } .unwrap(a), bytes32(uint256(b?1:0))${ scalarFlag } )` ;
295+ implExpressionA = `Impl.${
296+ operator . name
297+ } (e${ fheType . type . toLowerCase ( ) } .unwrap(a), bytes32(uint256(b?1:0))${ scalarFlag } )`;
276298 } else if ( fheType . type . startsWith ( 'Int' ) ) {
277299 throw new Error ( 'Int types are not supported!' ) ;
278300 } else {
@@ -318,9 +340,13 @@ function generateSolidityTFHEScalarOperator(fheType: AdjustedFheType, operator:
318340 res . push ( `
319341
320342 /**
321- * @dev Evaluates ${ operator . name } (e${ fheType . type . toLowerCase ( ) } a, ${ clearMatchingType . toLowerCase ( ) } b) and returns the result.
343+ * @dev Evaluates ${
344+ operator . name
345+ } (e${ fheType . type . toLowerCase ( ) } a, ${ clearMatchingType . toLowerCase ( ) } b) and returns the result.
322346 */
323- function ${ operator . name } (e${ fheType . type . toLowerCase ( ) } a, ${ clearMatchingType . toLowerCase ( ) } b) internal returns (${ returnType } ) {
347+ function ${
348+ operator . name
349+ } (e${ fheType . type . toLowerCase ( ) } a, ${ clearMatchingType . toLowerCase ( ) } b) internal returns (${ returnType } ) {
324350 if (!isInitialized(a)) {
325351 a = asE${ fheType . type . toLowerCase ( ) } (${
326352 fheType . type == 'Bool' ? 'false' : fheType . type == 'Address' ? `${ clearMatchingType . toLowerCase ( ) } (0)` : 0
@@ -335,9 +361,13 @@ function generateSolidityTFHEScalarOperator(fheType: AdjustedFheType, operator:
335361 res . push ( `
336362
337363 /**
338- * @dev Evaluates ${ operator . name } (${ clearMatchingType . toLowerCase ( ) } a, e${ fheType . type . toLowerCase ( ) } b) and returns the result.
364+ * @dev Evaluates ${
365+ operator . name
366+ } (${ clearMatchingType . toLowerCase ( ) } a, e${ fheType . type . toLowerCase ( ) } b) and returns the result.
339367 */
340- function ${ operator . name } (${ clearMatchingType . toLowerCase ( ) } a, e${ fheType . type . toLowerCase ( ) } b) internal returns (${ returnType } ) {
368+ function ${
369+ operator . name
370+ } (${ clearMatchingType . toLowerCase ( ) } a, e${ fheType . type . toLowerCase ( ) } b) internal returns (${ returnType } ) {
341371 ${ maybeEncryptLeft }
342372 if (!isInitialized(b)) {
343373 b = asE${ fheType . type . toLowerCase ( ) } (${
@@ -379,7 +409,9 @@ function handleSolidityTFHEShiftOperator(fheType: AdjustedFheType, operator: Ope
379409
380410 const leftExpr = 'a' ;
381411 const rightExpr = castRightToLeft ? `asE${ fheType . type . toLowerCase ( ) } (b)` : 'b' ;
382- let implExpression : string = `Impl.${ operator . name } (e${ fheType . type . toLowerCase ( ) } .unwrap(${ leftExpr } ), e${ fheType . type . toLowerCase ( ) } .unwrap(${ rightExpr } )${ scalarFlag } )` ;
412+ let implExpression : string = `Impl.${
413+ operator . name
414+ } (e${ fheType . type . toLowerCase ( ) } .unwrap(${ leftExpr } ), e${ fheType . type . toLowerCase ( ) } .unwrap(${ rightExpr } )${ scalarFlag } )`;
383415
384416 res . push ( `
385417 /**
@@ -398,13 +430,17 @@ function handleSolidityTFHEShiftOperator(fheType: AdjustedFheType, operator: Ope
398430
399431 // Code and test for shift(euint{inputBits},uint8 }
400432 scalarFlag = ', true' ;
401- implExpression = `Impl.${ operator . name } (e${ fheType . type . toLowerCase ( ) } .unwrap(a), bytes32(uint256(b))${ scalarFlag } )` ;
433+ implExpression = `Impl.${
434+ operator . name
435+ } (e${ fheType . type . toLowerCase ( ) } .unwrap(a), bytes32(uint256(b))${ scalarFlag } )`;
402436
403437 res . push ( `
404438 /**
405439 * @dev Evaluates ${ operator . name } (e${ fheType . type . toLowerCase ( ) } a, ${ getUint ( rhsBits ) } ) and returns the result.
406440 */
407- function ${ operator . name } (e${ fheType . type . toLowerCase ( ) } a, ${ getUint ( rhsBits ) } b) internal returns (e${ fheType . type . toLowerCase ( ) } ) {
441+ function ${ operator . name } (e${ fheType . type . toLowerCase ( ) } a, ${ getUint (
442+ rhsBits ,
443+ ) } b) internal returns (e${ fheType . type . toLowerCase ( ) } ) {
408444 if (!isInitialized(a)) {
409445 a = asE${ fheType . type . toLowerCase ( ) } (0);
410446 }
@@ -466,7 +502,9 @@ function handleSolidityTFHECustomCastBetweenTwoEuint(
466502 */
467503 function asE${ outputFheType . type . toLowerCase ( ) } (e${ inputFheType . type . toLowerCase ( ) } value) internal returns (e${ outputFheType . type . toLowerCase ( ) } ) {
468504 ${ checkInitialized ( 'value' , inputFheType . type ) }
469- return e${ outputFheType . type . toLowerCase ( ) } .wrap(Impl.cast(e${ inputFheType . type . toLowerCase ( ) } .unwrap(value), FheType.${ outputFheType . type } ));
505+ return e${ outputFheType . type . toLowerCase ( ) } .wrap(Impl.cast(e${ inputFheType . type . toLowerCase ( ) } .unwrap(value), FheType.${
506+ outputFheType . type
507+ } ));
470508 }
471509 ` ;
472510}
@@ -543,7 +581,9 @@ function handleSolidityTFHEConvertPlaintextAndEinputToRespectiveType(fheType: Ad
543581 */
544582 function fromExternal(externalE${ fheType . type . toLowerCase ( ) } inputHandle, bytes memory inputProof) internal returns (e${ fheType . type . toLowerCase ( ) } ) {
545583 if (inputProof.length!=0) {
546- return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.verify(externalE${ fheType . type . toLowerCase ( ) } .unwrap(inputHandle), inputProof, FheType.${ fheType . isAlias ? fheType . aliasType : fheType . type } ));
584+ return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.verify(externalE${ fheType . type . toLowerCase ( ) } .unwrap(inputHandle), inputProof, FheType.${
585+ fheType . isAlias ? fheType . aliasType : fheType . type
586+ } ));
547587 } else {
548588 bytes32 inputBytes32 = externalE${ fheType . type . toLowerCase ( ) } .unwrap(inputHandle);
549589 if(inputBytes32 == 0){
@@ -583,8 +623,12 @@ function handleSolidityTFHEConvertPlaintextAndEinputToRespectiveType(fheType: Ad
583623 /**
584624 * @dev Convert a plaintext value to an encrypted e${ fheType . type . toLowerCase ( ) } value.
585625 */
586- function asE${ fheType . type . toLowerCase ( ) } (${ fheType . clearMatchingType } value) internal returns (e${ fheType . type . toLowerCase ( ) } ) {
587- return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.trivialEncrypt(uint256(${ value } ), FheType.${ fheType . isAlias ? fheType . aliasType : fheType . type } ));
626+ function asE${ fheType . type . toLowerCase ( ) } (${
627+ fheType . clearMatchingType
628+ } value) internal returns (e${ fheType . type . toLowerCase ( ) } ) {
629+ return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.trivialEncrypt(uint256(${ value } ), FheType.${
630+ fheType . isAlias ? fheType . aliasType : fheType . type
631+ } ));
588632 }
589633
590634 ` ;
@@ -690,7 +734,9 @@ function handleSolidityTFHERand(fheType: AdjustedFheType): string {
690734 * @dev Generates a random encrypted value.
691735 */
692736 function randE${ fheType . type . toLowerCase ( ) } () internal returns (e${ fheType . type . toLowerCase ( ) } ) {
693- return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.rand(FheType.${ fheType . isAlias ? fheType . aliasType : fheType . type } ));
737+ return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.rand(FheType.${
738+ fheType . isAlias ? fheType . aliasType : fheType . type
739+ } ));
694740 }
695741
696742 ` ;
@@ -702,8 +748,12 @@ function handleSolidityTFHERand(fheType: AdjustedFheType): string {
702748 * @dev Generates a random encrypted ${ fheType . bitLength } -bit unsigned integer in the [0, upperBound) range.
703749 * The upperBound must be a power of 2.
704750 */
705- function randE${ fheType . type . toLowerCase ( ) } (uint${ fheType . bitLength } upperBound) internal returns (e${ fheType . type . toLowerCase ( ) } ) {
706- return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.randBounded(upperBound, FheType.${ fheType . isAlias ? fheType . aliasType : fheType . type } ));
751+ function randE${ fheType . type . toLowerCase ( ) } (uint${
752+ fheType . bitLength
753+ } upperBound) internal returns (e${ fheType . type . toLowerCase ( ) } ) {
754+ return e${ fheType . type . toLowerCase ( ) } .wrap(Impl.randBounded(upperBound, FheType.${
755+ fheType . isAlias ? fheType . aliasType : fheType . type
756+ } ));
707757 }
708758
709759 ` ;
0 commit comments