-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTestAsyncDecrypt.sol
More file actions
444 lines (397 loc) · 17.3 KB
/
TestAsyncDecrypt.sol
File metadata and controls
444 lines (397 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
import "@fhevm/solidity/lib/FHE.sol";
import {E2EFHEVMConfig} from "./E2EFHEVMConfigLocal.sol";
/// @notice Contract for testing asynchronous decryption using the Gateway
contract TestAsyncDecrypt is E2EFHEVMConfig {
/// @dev Encrypted state variables
ebool xBool;
euint8 xUint8;
euint16 xUint16;
euint32 xUint32;
euint32 xUint32_2;
euint32 xUint32_3;
euint64 xUint64;
euint64 xUint64_2;
euint64 xUint64_3;
euint128 xUint128;
euint128 xUint128_2;
euint128 xUint128_3;
eaddress xAddress;
eaddress xAddress2;
euint256 xUint256;
/// @dev Decrypted state variables
bool public yBool;
uint8 public yUint8;
uint16 public yUint16;
uint32 public yUint32;
uint32 public yUint32_2;
uint32 public yUint32_3;
uint64 public yUint64;
uint64 public yUint64_2;
uint64 public yUint64_3;
uint128 public yUint128;
uint128 public yUint128_2;
uint128 public yUint128_3;
address public yAddress;
address public yAddress2;
uint256 public yUint256;
bytes public yBytes64;
bytes public yBytes128;
bytes public yBytes256;
/// @dev private mapping that can be used to link a requestID to an array of uint256
mapping(uint256 => uint256[]) private paramsUint256;
/// @notice Constructor to initialize the contract and set up encrypted values
constructor() {
/// @dev Initialize encrypted variables with sample values
xBool = FHE.asEbool(true);
FHE.allowThis(xBool);
xUint8 = FHE.asEuint8(42);
FHE.allowThis(xUint8);
xUint16 = FHE.asEuint16(16);
FHE.allowThis(xUint16);
xUint32 = FHE.asEuint32(32);
FHE.allowThis(xUint32);
xUint32_2 = FHE.asEuint32(1000);
FHE.allowThis(xUint32_2);
xUint32_3 = FHE.asEuint32(2000);
FHE.allowThis(xUint32_3);
xUint64 = FHE.asEuint64(18446744073709551600);
FHE.allowThis(xUint64);
xUint64_2 = FHE.asEuint64(76575465786);
FHE.allowThis(xUint64_2);
xUint64_3 = FHE.asEuint64(6400);
FHE.allowThis(xUint64_3);
xUint128 = FHE.asEuint128(1267650600228229401496703205443);
FHE.allowThis(xUint128);
xUint128_2 = FHE.asEuint128(10000);
FHE.allowThis(xUint128_2);
xUint128_3 = FHE.asEuint128(20000);
FHE.allowThis(xUint128_3);
xUint256 = FHE.asEuint256(27606985387162255149739023449108101809804435888681546220650096895197251);
FHE.allowThis(xUint256);
xAddress = FHE.asEaddress(0x8ba1f109551bD432803012645Ac136ddd64DBA72);
FHE.allowThis(xAddress);
xAddress2 = FHE.asEaddress(0xf48b8840387ba3809DAE990c930F3b4766A86ca3);
FHE.allowThis(xAddress2);
}
/// @notice Function to request decryption of a boolean value with an infinite loop in the callback
function requestBoolInfinite() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xBool);
FHE.requestDecryption(cts, this.callbackBoolInfinite.selector);
}
/// @notice Callback function for the infinite loop decryption request (WARNING: This function will never complete)
function callbackBoolInfinite(
uint256 requestID,
bool decryptedInput,
bytes[] memory signatures
) public returns (bool) {
FHE.checkSignatures(requestID, signatures);
uint256 i = 0;
while (true) {
i++;
}
yBool = decryptedInput;
return yBool;
}
/// @notice Request decryption of a boolean value
function requestBool() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xBool);
FHE.requestDecryption(cts, this.callbackBool.selector);
}
/// @notice Attempt to request decryption of a fake boolean value (should revert)
function requestFakeBool() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = 0x4200000000000000000000000000000000000000000000000000000000000000;
/// @dev This should revert because the previous ebool is not honestly obtained
FHE.requestDecryption(cts, this.callbackBool.selector);
}
/// @notice Callback function for boolean decryption
function callbackBool(uint256 requestID, bool decryptedInput, bytes[] memory signatures) public returns (bool) {
FHE.checkSignatures(requestID, signatures);
yBool = decryptedInput;
return yBool;
}
/// @notice Request decryption of an 8-bit unsigned integer
function requestUint8() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint8);
FHE.requestDecryption(cts, this.callbackUint8.selector);
}
/// @notice Attempt to request decryption of a fake 8-bit unsigned integer (should revert)
function requestFakeUint8() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = 0x4200000000000000000000000000000000000000000000000000000000000200;
/// @dev This should revert because the previous handle is not honestly obtained
FHE.requestDecryption(cts, this.callbackUint8.selector);
}
/// @notice Callback function for 8-bit unsigned integer decryption
/// @param decryptedInput The decrypted 8-bit unsigned integer
/// @return The decrypted value
function callbackUint8(uint256 requestID, uint8 decryptedInput, bytes[] memory signatures) public returns (uint8) {
FHE.checkSignatures(requestID, signatures);
yUint8 = decryptedInput;
return decryptedInput;
}
/// @notice Request decryption of a 16-bit unsigned integer
function requestUint16() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint16);
FHE.requestDecryption(cts, this.callbackUint16.selector);
}
/// @notice Attempt to request decryption of a fake 16-bit unsigned integer (should revert)
function requestFakeUint16() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = 0x4200000000000000000000000000000000000000000000000000000000000300;
/// @dev This should revert because the previous handle is not honestly obtained
FHE.requestDecryption(cts, this.callbackUint16.selector);
}
/// @notice Callback function for 16-bit unsigned integer decryption
/// @param decryptedInput The decrypted 16-bit unsigned integer
/// @return The decrypted value
function callbackUint16(
uint256 requestID,
uint16 decryptedInput,
bytes[] memory signatures
) public returns (uint16) {
FHE.checkSignatures(requestID, signatures);
yUint16 = decryptedInput;
return decryptedInput;
}
/// @notice Request decryption of a 32-bit unsigned integer with additional inputs
/// @param input1 First additional input
/// @param input2 Second additional input
function requestUint32(uint32 input1, uint32 input2) public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint32);
uint256 requestID = FHE.requestDecryption(cts, this.callbackUint32.selector);
addParamsUint256(requestID, input1);
addParamsUint256(requestID, input2);
}
/// @notice Attempt to request decryption of a fake 32-bit unsigned integer (should revert)
function requestFakeUint32() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = 0x4200000000000000000000000000000000000000000000000000000000000400;
/// @dev This should revert because the previous handle is not honestly obtained
FHE.requestDecryption(cts, this.callbackUint32.selector);
}
/// @notice Callback function for 32-bit unsigned integer decryption
/// @param requestID The ID of the decryption request
/// @param decryptedInput The decrypted 32-bit unsigned integer
/// @return The result of the computation
function callbackUint32(
uint256 requestID,
uint32 decryptedInput,
bytes[] memory signatures
) public returns (uint32) {
FHE.checkSignatures(requestID, signatures);
uint256[] memory params = getParamsUint256(requestID);
unchecked {
uint32 result = uint32(uint256(params[0])) + uint32(uint256(params[1])) + decryptedInput;
yUint32 = result;
return result;
}
}
/// @notice Request decryption of a 64-bit unsigned integer
function requestUint64() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint64);
FHE.requestDecryption(cts, this.callbackUint64.selector);
}
/// @notice Attempt to request decryption of a fake 64-bit unsigned integer (should revert)
function requestFakeUint64() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = 0x4200000000000000000000000000000000000000000000000000000000000500;
/// @dev This should revert because the previous handle is not honestly obtained
FHE.requestDecryption(cts, this.callbackUint64.selector);
}
/// @notice Request decryption of a non-trivial 64-bit unsigned integer
/// @param inputHandle The input handle for the encrypted value
/// @param inputProof The input proof for the encrypted value
function requestUint64NonTrivial(externalEuint64 inputHandle, bytes calldata inputProof) public {
euint64 inputNonTrivial = FHE.fromExternal(inputHandle, inputProof);
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(inputNonTrivial);
FHE.requestDecryption(cts, this.callbackUint64.selector);
}
/// @notice Callback function for 64-bit unsigned integer decryption
/// @param decryptedInput The decrypted 64-bit unsigned integer
/// @return The decrypted value
function callbackUint64(
uint256 requestID,
uint64 decryptedInput,
bytes[] memory signatures
) public returns (uint64) {
FHE.checkSignatures(requestID, signatures);
yUint64 = decryptedInput;
return decryptedInput;
}
function requestUint128() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint128);
FHE.requestDecryption(cts, this.callbackUint128.selector);
}
function requestUint128NonTrivial(externalEuint128 inputHandle, bytes calldata inputProof) public {
euint128 inputNonTrivial = FHE.fromExternal(inputHandle, inputProof);
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(inputNonTrivial);
FHE.requestDecryption(cts, this.callbackUint128.selector);
}
function callbackUint128(
uint256 requestID,
uint128 decryptedInput,
bytes[] memory signatures
) public returns (uint128) {
FHE.checkSignatures(requestID, signatures);
yUint128 = decryptedInput;
return decryptedInput;
}
function requestUint256() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint256);
FHE.requestDecryption(cts, this.callbackUint256.selector);
}
function requestUint256NonTrivial(externalEuint256 inputHandle, bytes calldata inputProof) public {
euint256 inputNonTrivial = FHE.fromExternal(inputHandle, inputProof);
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(inputNonTrivial);
FHE.requestDecryption(cts, this.callbackUint256.selector);
}
function callbackUint256(
uint256 requestID,
uint256 decryptedInput,
bytes[] memory signatures
) public returns (uint256) {
FHE.checkSignatures(requestID, signatures);
yUint256 = decryptedInput;
return decryptedInput;
}
/// @notice Request decryption of an encrypted address
function requestAddress() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xAddress);
FHE.requestDecryption(cts, this.callbackAddress.selector);
}
/// @notice Request decryption of multiple encrypted addresses
function requestSeveralAddresses() public {
bytes32[] memory cts = new bytes32[](2);
cts[0] = FHE.toBytes32(xAddress);
cts[1] = FHE.toBytes32(xAddress2);
FHE.requestDecryption(cts, this.callbackAddresses.selector);
}
/// @notice Callback function for multiple address decryption
/// @param decryptedInput1 The first decrypted address
/// @param decryptedInput2 The second decrypted address
/// @return The first decrypted address
function callbackAddresses(
uint256 requestID,
address decryptedInput1,
address decryptedInput2,
bytes[] memory signatures
) public returns (address) {
FHE.checkSignatures(requestID, signatures);
yAddress = decryptedInput1;
yAddress2 = decryptedInput2;
return decryptedInput1;
}
/// @notice Attempt to request decryption of a fake address (should revert)
function requestFakeAddress() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = 0x4200000000000000000000000000000000000000000000000000000000000700;
/// @dev This should revert because the previous handle is not honestly obtained
FHE.requestDecryption(cts, this.callbackAddress.selector);
}
/// @notice Callback function for address decryption
/// @param decryptedInput The decrypted address
/// @return The decrypted address
function callbackAddress(
uint256 requestID,
address decryptedInput,
bytes[] memory signatures
) public returns (address) {
FHE.checkSignatures(requestID, signatures);
yAddress = decryptedInput;
return decryptedInput;
}
/// @notice Request decryption of mixed data types
/// @dev Demonstrates how to do a mixed decryption request
/// @param inputHandle The encrypted input handle for euint256
/// @param inputProof The proof for the encrypted euint256
function requestMixed(externalEuint256 inputHandle, bytes calldata inputProof) public {
bytes32[] memory cts = new bytes32[](4);
cts[0] = FHE.toBytes32(xBool);
cts[1] = FHE.toBytes32(xAddress);
cts[2] = FHE.toBytes32(xUint32);
euint256 inputEuint256 = FHE.fromExternal(inputHandle, inputProof);
cts[3] = FHE.toBytes32(inputEuint256);
FHE.requestDecryption(cts, this.callbackMixed.selector);
}
/// @notice Callback function for mixed data type decryption including 256-bit encrypted bytes
/// @dev Processes and stores the decrypted values
/// @param decBool Decrypted boolean
/// @param decAddress Decrypted address
/// @param decEuint32 Decrypted 32-bit unsigned integer
/// @param decEuint256 Decrypted 256-bit unsigned integer
/// @param signatures Signatures to verify the authenticity of the decryption
function callbackMixed(
uint256 requestID,
bool decBool,
address decAddress,
uint32 decEuint32,
uint256 decEuint256,
bytes[] memory signatures
) public {
FHE.checkSignatures(requestID, signatures);
yBool = decBool;
yAddress = decAddress;
yUint32 = decEuint32;
yUint256 = decEuint256;
}
/// @dev internal setter to link a decryption requestID to a uint256 value
/// @dev if used multiple times with same requestID, it will map the requestID to the list of all added inputs
function addParamsUint256(uint256 requestID, uint256 value) internal {
paramsUint256[requestID].push(value);
}
/// @dev internal getter to recover all uint256 values linked to a specific requestID
function getParamsUint256(uint256 requestID) internal view returns (uint256[] memory) {
return paramsUint256[requestID];
}
/// @notice Request decryption of a 32-bit unsigned integer
function requestUint32_2() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint32_2);
FHE.requestDecryption(cts, this.callbackUint32_2.selector);
}
function callbackUint32_2(uint256 requestID, uint32 decryptedInput, bytes[] memory signatures) public {
FHE.checkSignatures(requestID, signatures);
yUint32_2 = decryptedInput;
}
/// @notice Request decryption of a 32-bit unsigned integer
function requestUint32_3() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint32_3);
FHE.requestDecryption(cts, this.callbackUint32_3.selector);
}
function callbackUint32_3(uint256 requestID, uint32 decryptedInput, bytes[] memory signatures) public {
FHE.checkSignatures(requestID, signatures);
yUint32_3 = decryptedInput;
}
function requestUint128_Many() public {
bytes32[] memory cts = new bytes32[](1);
cts[0] = FHE.toBytes32(xUint128_2);
FHE.requestDecryption(cts, this.callbackUint128_2.selector);
bytes32[] memory cts_2 = new bytes32[](1);
cts_2[0] = FHE.toBytes32(xUint128_3);
FHE.requestDecryption(cts_2, this.callbackUint128_3.selector);
}
function callbackUint128_2(uint256 requestID, uint128 decryptedInput, bytes[] memory signatures) public {
FHE.checkSignatures(requestID, signatures);
yUint128_2 = decryptedInput;
}
function callbackUint128_3(uint256 requestID, uint128 decryptedInput, bytes[] memory signatures) public {
FHE.checkSignatures(requestID, signatures);
yUint128_3 = decryptedInput;
}
}