|
| 1 | +import 'package:fhel/afhe.dart'; |
1 | 2 | import 'package:fhel/seal.dart' show Seal;
|
| 3 | +import 'package:fhel_calculator/page/settings.dart'; |
2 | 4 |
|
3 |
| -List<int> addVector(Map<String,int> ctx, List<int> x, List<int> add) { |
4 |
| - final fhe = Seal('bfv'); |
5 |
| - fhe.genContext(ctx); |
6 |
| - fhe.genKeys(); |
| 5 | +/// Conditionally add two [Plaintext] objects |
| 6 | +/// |
| 7 | +/// At least one of the two integers must be encrypted. |
| 8 | +Ciphertext addCondition(SessionChanges s, Plaintext x, Plaintext add, bool xEncrypted, bool addEncrypted) { |
| 9 | + Seal fhe = s.fhe; |
| 10 | + Ciphertext cipherResult; |
| 11 | + |
| 12 | + if (xEncrypted && addEncrypted) { |
| 13 | + s.log('Adding Ciphertext + Ciphertext'); |
| 14 | + cipherResult = fhe.add(fhe.encrypt(x), fhe.encrypt(add)); |
| 15 | + } else if (xEncrypted) { |
| 16 | + s.log('Adding Ciphertext + Plaintext'); |
| 17 | + cipherResult = fhe.addPlain(fhe.encrypt(x), add); |
| 18 | + } else if (addEncrypted) { |
| 19 | + s.log('Adding Plaintext + Ciphertext'); |
| 20 | + cipherResult = fhe.addPlain(fhe.encrypt(add), x); |
| 21 | + } else { |
| 22 | + throw 'Both x and add cannot be plain'; // cannot return a Ciphertext |
| 23 | + } |
| 24 | + return cipherResult; |
| 25 | +} |
| 26 | + |
| 27 | +/// Add two integers |
| 28 | +/// |
| 29 | +/// Encrypts, adds, and decrypts the result, |
| 30 | +String addAsHex(SessionChanges s, int x, int add, bool xEncrypted, bool addEncrypted) { |
| 31 | + Seal fhe = s.fhe; |
7 | 32 |
|
8 |
| - final pt_x = fhe.encodeVecInt(x); |
9 |
| - final ct_x = fhe.encrypt(pt_x); |
| 33 | + // Convert to Hexidecimal |
| 34 | + try { |
| 35 | + s.logSession(); |
| 36 | + final start = DateTime.now(); |
| 37 | + final xRadix = x.toRadixString(16); |
| 38 | + s.log('$x.toRadixString(16) => $xRadix'); |
| 39 | + final plainX = fhe.plain(xRadix); |
10 | 40 |
|
11 |
| - final pt_add = fhe.encodeVecInt(add); |
12 |
| - final ct_add = fhe.encrypt(pt_add); |
| 41 | + final addRadix = add.toRadixString(16); |
| 42 | + s.log('$add.toRadixString(16) => $addRadix'); |
| 43 | + final plainAdd = fhe.plain(addRadix); |
13 | 44 |
|
14 |
| - final ct_res = fhe.add(ct_x, ct_add); |
15 |
| - final pt_res = fhe.decrypt(ct_res); |
| 45 | + if (!xEncrypted && !addEncrypted) { |
| 46 | + s.log('Adding Plaintext + Plaintext'); |
| 47 | + final result = (x + add).toString(); |
| 48 | + s.log('Result: $result'); |
| 49 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 50 | + return result; |
| 51 | + } |
| 52 | + Ciphertext cipherResult = addCondition(s, plainX, plainAdd, xEncrypted, addEncrypted); |
| 53 | + s.log('Ciphertext result size: ${cipherResult.size}'); |
16 | 54 |
|
17 |
| - return fhe.decodeVecInt(pt_res, x.length); |
| 55 | + final plainResult = fhe.decrypt(cipherResult); |
| 56 | + final result = int.parse(plainResult.text, radix: 16).toString(); |
| 57 | + s.log('Decrypted result: $result'); |
| 58 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 59 | + return result.toString(); |
| 60 | + } catch (e) { |
| 61 | + s.log(e.toString()); |
| 62 | + return e.toString(); |
| 63 | + } |
18 | 64 | }
|
19 | 65 |
|
20 |
| -int addInt(Map<String, int> ctx, int x, int add) { |
21 |
| - final fhe = Seal('bfv'); |
22 |
| - fhe.genContext(ctx); |
23 |
| - fhe.genKeys(); |
| 66 | +/// Add two doubles |
| 67 | +String addDouble(SessionChanges s, double x, double add, bool xEncrypted, bool addEncrypted) { |
| 68 | + Seal fhe = s.fhe; |
24 | 69 |
|
25 |
| - // Convert to Hexidecimal |
26 |
| - final pt_x = fhe.plain(x.toRadixString(16)); |
27 |
| - final ct_x = fhe.encrypt(pt_x); |
28 |
| - final pt_add = fhe.plain(add.toRadixString(16)); |
29 |
| - final ct_add = fhe.encrypt(pt_add); |
30 |
| - final ct_res = fhe.add(ct_x, ct_add); |
31 |
| - final pt_res = fhe.decrypt(ct_res); |
32 |
| - |
33 |
| - return int.parse(pt_res.text, radix: 16); |
| 70 | + if (fhe.scheme.name != 'ckks') { |
| 71 | + return '${fhe.scheme.name.toUpperCase()} does not support double addition'; |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + s.logSession(); |
| 76 | + final start = DateTime.now(); |
| 77 | + final plainX = fhe.encodeDouble(x); |
| 78 | + final plainAdd = fhe.encodeDouble(add); |
| 79 | + |
| 80 | + if (!xEncrypted && !addEncrypted) { |
| 81 | + s.log('Adding Plaintext + Plaintext'); |
| 82 | + final result = (x + add).toString(); |
| 83 | + s.log('Result: $result'); |
| 84 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 85 | + return result; |
| 86 | + } |
| 87 | + Ciphertext cipherResult = addCondition(s, plainX, plainAdd, xEncrypted, addEncrypted); |
| 88 | + s.log('Ciphertext result size: ${cipherResult.size}'); |
| 89 | + |
| 90 | + final plainResult = fhe.decrypt(cipherResult); |
| 91 | + // Generates an array of doubles filled of size (slot count) |
| 92 | + final result = fhe.decodeVecDouble(plainResult, 1).first; |
| 93 | + s.log('Decrypted result: $result'); |
| 94 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 95 | + return result.toString(); |
| 96 | + } catch (e) { |
| 97 | + s.log(e.toString()); |
| 98 | + return e.toString(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Add two vectors<int> |
| 103 | +String addVectorInt(SessionChanges s, List<int> x, List<int> add, bool xEncrypted, bool addEncrypted) { |
| 104 | + Seal fhe = s.fhe; |
| 105 | + |
| 106 | + if (fhe.scheme.name == 'ckks') { |
| 107 | + return 'CKKS does not support integer addition'; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + s.logSession(); |
| 112 | + final start = DateTime.now(); |
| 113 | + final plainX = fhe.encodeVecInt(x); |
| 114 | + final plainAdd = fhe.encodeVecInt(add); |
| 115 | + |
| 116 | + if (!xEncrypted && !addEncrypted) { |
| 117 | + final result = []; |
| 118 | + s.log('Adding Plaintext + Plaintext'); |
| 119 | + for (int i = 0; i < x.length; i++) { |
| 120 | + result.add(x[i] + add[i]); |
| 121 | + } |
| 122 | + s.log('Result: $result'); |
| 123 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 124 | + return result.join(','); |
| 125 | + } |
| 126 | + Ciphertext cipherResult = addCondition(s, plainX, plainAdd, xEncrypted, addEncrypted); |
| 127 | + s.log('Ciphertext result size: ${cipherResult.size}'); |
| 128 | + |
| 129 | + final plainResult = fhe.decrypt(cipherResult); |
| 130 | + final result = fhe.decodeVecInt(plainResult, x.length); |
| 131 | + s.log('Decrypted result: $result'); |
| 132 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 133 | + return result.join(','); |
| 134 | + } catch (e) { |
| 135 | + s.log(e.toString()); |
| 136 | + return e.toString(); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// Add two vectors<double> |
| 141 | +String addVectorDouble(SessionChanges s, List<double> x, List<double> add, bool xEncrypted, bool addEncrypted) { |
| 142 | + Seal fhe = s.fhe; |
| 143 | + |
| 144 | + if (fhe.scheme.name != 'ckks') { |
| 145 | + return '${fhe.scheme.name.toUpperCase()} does not support double addition'; |
| 146 | + } |
| 147 | + |
| 148 | + try { |
| 149 | + s.logSession(); |
| 150 | + final start = DateTime.now(); |
| 151 | + final plainX = fhe.encodeVecDouble(x); |
| 152 | + final plainAdd = fhe.encodeVecDouble(add); |
| 153 | + |
| 154 | + if (!xEncrypted && !addEncrypted) { |
| 155 | + s.log('Adding Plaintext + Plaintext'); |
| 156 | + final result = []; |
| 157 | + for (int i = 0; i < x.length; i++) { |
| 158 | + result.add(x[i] + add[i]); |
| 159 | + } |
| 160 | + s.log('Result: $result'); |
| 161 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 162 | + return result.join(','); |
| 163 | + } |
| 164 | + Ciphertext cipherResult = addCondition(s, plainX, plainAdd, xEncrypted, addEncrypted); |
| 165 | + s.log('Ciphertext result size: ${cipherResult.size}'); |
| 166 | + |
| 167 | + final plainResult = fhe.decrypt(cipherResult); |
| 168 | + final result = fhe.decodeVecDouble(plainResult, x.length); |
| 169 | + s.log('Decrypted result: $result'); |
| 170 | + s.log('Elapsed: ${DateTime.now().difference(start).inMilliseconds} ms'); |
| 171 | + return result.join(','); |
| 172 | + } catch (e) { |
| 173 | + s.log(e.toString()); |
| 174 | + return e.toString(); |
| 175 | + } |
34 | 176 | }
|
0 commit comments