@inco/lightning 0.8.0-devnet-2 → 0.8.0-devnet-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,794 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {IncoTest} from "./IncoTest.sol";
5
+ import {e, euint256, ebool, eaddress, inco} from "../Lib.sol";
6
+ import {ETypes} from "../Types.sol";
7
+
8
+ /// @notice Tests for Lib.sol library functions to achieve 100% coverage
9
+ /// @dev This file tests all the scalar variants and uncovered branches in the library
10
+ contract TestLib is IncoTest {
11
+
12
+ using e for euint256;
13
+ using e for ebool;
14
+ using e for uint256;
15
+ using e for bool;
16
+ using e for address;
17
+ using e for eaddress;
18
+ using e for bytes;
19
+
20
+ // ============ SANITIZE BRANCH TESTS ============
21
+
22
+ function testSanitizeEuint256Zero() public {
23
+ // Test sanitize with zero handle - should return asEuint256(0)
24
+ euint256 zero = euint256.wrap(bytes32(0));
25
+ euint256 sanitized = e.s(zero);
26
+ processAllOperations();
27
+ // After sanitize, a zero handle should become a valid encrypted 0
28
+ assertEq(getUint256Value(sanitized), 0);
29
+ }
30
+
31
+ function testSanitizeEboolZero() public {
32
+ // Test sanitize with zero handle - should return asEbool(false)
33
+ ebool zero = ebool.wrap(bytes32(0));
34
+ ebool sanitized = e.s(zero);
35
+ processAllOperations();
36
+ // After sanitize, a zero handle should become a valid encrypted false
37
+ assertEq(getBoolValue(sanitized), false);
38
+ }
39
+
40
+ function testSanitizeEaddress() public {
41
+ // Test sanitize(eaddress) with non-zero - just returns input
42
+ eaddress addr = e.asEaddress(address(0xdeadbeef));
43
+ processAllOperations();
44
+ eaddress sanitized = e.s(addr);
45
+ processAllOperations();
46
+ assertEq(getAddressValue(sanitized), address(0xdeadbeef));
47
+ }
48
+
49
+ function testSanitizeEaddressZero() public {
50
+ // Test sanitize with zero handle - should return asEaddress(0)
51
+ eaddress zero = eaddress.wrap(bytes32(0));
52
+ eaddress sanitized = e.s(zero);
53
+ processAllOperations();
54
+ // After sanitize, a zero handle should become a valid encrypted address(0)
55
+ assertEq(getAddressValue(sanitized), address(0));
56
+ }
57
+
58
+ // ============ SUB SCALAR VARIANTS ============
59
+
60
+ function testSubEuint256Uint256() public {
61
+ euint256 a = e.asEuint256(10);
62
+ euint256 c = a.sub(uint256(4));
63
+ processAllOperations();
64
+ assertEq(getUint256Value(c), 6);
65
+ }
66
+
67
+ function testSubUint256Euint256() public {
68
+ euint256 b = e.asEuint256(4);
69
+ euint256 c = uint256(10).sub(b);
70
+ processAllOperations();
71
+ assertEq(getUint256Value(c), 6);
72
+ }
73
+
74
+ // ============ MUL SCALAR VARIANTS ============
75
+
76
+ function testMulEuint256Uint256() public {
77
+ euint256 a = e.asEuint256(5);
78
+ euint256 c = a.mul(uint256(3));
79
+ processAllOperations();
80
+ assertEq(getUint256Value(c), 15);
81
+ }
82
+
83
+ function testMulUint256Euint256() public {
84
+ euint256 b = e.asEuint256(3);
85
+ euint256 c = uint256(5).mul(b);
86
+ processAllOperations();
87
+ assertEq(getUint256Value(c), 15);
88
+ }
89
+
90
+ // ============ DIV SCALAR VARIANTS ============
91
+
92
+ function testDivEuint256Uint256() public {
93
+ euint256 a = e.asEuint256(20);
94
+ euint256 c = a.div(uint256(4));
95
+ processAllOperations();
96
+ assertEq(getUint256Value(c), 5);
97
+ }
98
+
99
+ function testDivUint256Euint256() public {
100
+ euint256 b = e.asEuint256(4);
101
+ euint256 c = uint256(20).div(b);
102
+ processAllOperations();
103
+ assertEq(getUint256Value(c), 5);
104
+ }
105
+
106
+ // ============ REM SCALAR VARIANTS ============
107
+
108
+ function testRemEuint256Uint256() public {
109
+ euint256 a = e.asEuint256(10);
110
+ euint256 c = a.rem(uint256(3));
111
+ processAllOperations();
112
+ assertEq(getUint256Value(c), 1);
113
+ }
114
+
115
+ function testRemUint256Euint256() public {
116
+ euint256 b = e.asEuint256(3);
117
+ euint256 c = uint256(10).rem(b);
118
+ processAllOperations();
119
+ assertEq(getUint256Value(c), 1);
120
+ }
121
+
122
+ // ============ AND SCALAR/EBOOL VARIANTS ============
123
+
124
+ function testAndEuint256Uint256() public {
125
+ euint256 a = e.asEuint256(0xFF);
126
+ euint256 c = a.and(uint256(0x0F));
127
+ processAllOperations();
128
+ assertEq(getUint256Value(c), 0x0F);
129
+ }
130
+
131
+ function testAndUint256Euint256() public {
132
+ euint256 b = e.asEuint256(0x0F);
133
+ euint256 c = uint256(0xFF).and(b);
134
+ processAllOperations();
135
+ assertEq(getUint256Value(c), 0x0F);
136
+ }
137
+
138
+ function testAndEboolEbool() public {
139
+ ebool a = e.asEbool(true);
140
+ ebool b = e.asEbool(false);
141
+ ebool c = a.and(b);
142
+ processAllOperations();
143
+ assertEq(getBoolValue(c), false);
144
+ }
145
+
146
+ function testAndEboolBool() public {
147
+ ebool a = e.asEbool(true);
148
+ ebool c = a.and(false);
149
+ processAllOperations();
150
+ assertEq(getBoolValue(c), false);
151
+ }
152
+
153
+ function testAndBoolEbool() public {
154
+ ebool b = e.asEbool(true);
155
+ ebool c = true.and(b);
156
+ processAllOperations();
157
+ assertEq(getBoolValue(c), true);
158
+ }
159
+
160
+ // ============ OR SCALAR/EBOOL VARIANTS ============
161
+
162
+ function testOrEuint256Uint256() public {
163
+ euint256 a = e.asEuint256(0xF0);
164
+ euint256 c = a.or(uint256(0x0F));
165
+ processAllOperations();
166
+ assertEq(getUint256Value(c), 0xFF);
167
+ }
168
+
169
+ function testOrUint256Euint256() public {
170
+ euint256 b = e.asEuint256(0x0F);
171
+ euint256 c = uint256(0xF0).or(b);
172
+ processAllOperations();
173
+ assertEq(getUint256Value(c), 0xFF);
174
+ }
175
+
176
+ function testOrEboolEbool() public {
177
+ ebool a = e.asEbool(false);
178
+ ebool b = e.asEbool(true);
179
+ ebool c = a.or(b);
180
+ processAllOperations();
181
+ assertEq(getBoolValue(c), true);
182
+ }
183
+
184
+ function testOrEboolBool() public {
185
+ ebool a = e.asEbool(false);
186
+ ebool c = a.or(true);
187
+ processAllOperations();
188
+ assertEq(getBoolValue(c), true);
189
+ }
190
+
191
+ function testOrBoolEbool() public {
192
+ ebool b = e.asEbool(false);
193
+ ebool c = true.or(b);
194
+ processAllOperations();
195
+ assertEq(getBoolValue(c), true);
196
+ }
197
+
198
+ // ============ XOR SCALAR/EBOOL VARIANTS ============
199
+
200
+ function testXorEuint256Uint256() public {
201
+ euint256 a = e.asEuint256(0xFF);
202
+ euint256 c = a.xor(uint256(0x0F));
203
+ processAllOperations();
204
+ assertEq(getUint256Value(c), 0xF0);
205
+ }
206
+
207
+ function testXorUint256Euint256() public {
208
+ euint256 b = e.asEuint256(0x0F);
209
+ euint256 c = uint256(0xFF).xor(b);
210
+ processAllOperations();
211
+ assertEq(getUint256Value(c), 0xF0);
212
+ }
213
+
214
+ function testXorEboolEbool() public {
215
+ ebool a = e.asEbool(true);
216
+ ebool b = e.asEbool(true);
217
+ ebool c = a.xor(b);
218
+ processAllOperations();
219
+ assertEq(getBoolValue(c), false);
220
+ }
221
+
222
+ function testXorEboolBool() public {
223
+ ebool a = e.asEbool(true);
224
+ ebool c = a.xor(false);
225
+ processAllOperations();
226
+ assertEq(getBoolValue(c), true);
227
+ }
228
+
229
+ function testXorBoolEbool() public {
230
+ ebool b = e.asEbool(false);
231
+ ebool c = true.xor(b);
232
+ processAllOperations();
233
+ assertEq(getBoolValue(c), true);
234
+ }
235
+
236
+ // ============ SHL SCALAR VARIANTS ============
237
+
238
+ function testShlEuint256Uint256() public {
239
+ euint256 a = e.asEuint256(1);
240
+ euint256 c = a.shl(uint256(4));
241
+ processAllOperations();
242
+ assertEq(getUint256Value(c), 16);
243
+ }
244
+
245
+ function testShlUint256Euint256() public {
246
+ euint256 b = e.asEuint256(4);
247
+ euint256 c = uint256(1).shl(b);
248
+ processAllOperations();
249
+ assertEq(getUint256Value(c), 16);
250
+ }
251
+
252
+ // ============ SHR SCALAR VARIANTS ============
253
+
254
+ function testShrEuint256Uint256() public {
255
+ euint256 a = e.asEuint256(16);
256
+ euint256 c = a.shr(uint256(2));
257
+ processAllOperations();
258
+ assertEq(getUint256Value(c), 4);
259
+ }
260
+
261
+ function testShrUint256Euint256() public {
262
+ euint256 b = e.asEuint256(2);
263
+ euint256 c = uint256(16).shr(b);
264
+ processAllOperations();
265
+ assertEq(getUint256Value(c), 4);
266
+ }
267
+
268
+ // ============ ROTL SCALAR VARIANTS ============
269
+
270
+ function testRotlEuint256Uint256() public {
271
+ euint256 a = e.asEuint256(1);
272
+ euint256 c = a.rotl(uint256(4));
273
+ processAllOperations();
274
+ assertEq(getUint256Value(c), 16);
275
+ }
276
+
277
+ function testRotlUint256Euint256() public {
278
+ euint256 b = e.asEuint256(4);
279
+ euint256 c = uint256(1).rotl(b);
280
+ processAllOperations();
281
+ assertEq(getUint256Value(c), 16);
282
+ }
283
+
284
+ // ============ ROTR SCALAR VARIANTS ============
285
+
286
+ function testRotrEuint256Uint256() public {
287
+ euint256 a = e.asEuint256(16);
288
+ euint256 c = a.rotr(uint256(4));
289
+ processAllOperations();
290
+ assertEq(getUint256Value(c), 1);
291
+ }
292
+
293
+ function testRotrUint256Euint256() public {
294
+ euint256 b = e.asEuint256(4);
295
+ euint256 c = uint256(16).rotr(b);
296
+ processAllOperations();
297
+ assertEq(getUint256Value(c), 1);
298
+ }
299
+
300
+ // ============ EQ SCALAR/EADDRESS VARIANTS ============
301
+
302
+ function testEqEuint256Uint256() public {
303
+ euint256 a = e.asEuint256(10);
304
+ ebool c = a.eq(uint256(10));
305
+ processAllOperations();
306
+ assertEq(getBoolValue(c), true);
307
+ }
308
+
309
+ function testEqUint256Euint256() public {
310
+ euint256 b = e.asEuint256(10);
311
+ ebool c = uint256(10).eq(b);
312
+ processAllOperations();
313
+ assertEq(getBoolValue(c), true);
314
+ }
315
+
316
+ function testEqEaddressAddress() public {
317
+ eaddress a = e.asEaddress(alice);
318
+ processAllOperations();
319
+ ebool c = a.eq(alice);
320
+ processAllOperations();
321
+ assertEq(getBoolValue(c), true);
322
+ }
323
+
324
+ function testEqEaddressEaddress() public {
325
+ eaddress a = e.asEaddress(alice);
326
+ eaddress b = e.asEaddress(alice);
327
+ processAllOperations();
328
+ ebool c = a.eq(b);
329
+ processAllOperations();
330
+ assertEq(getBoolValue(c), true);
331
+ }
332
+
333
+ function testEqAddressEaddress() public {
334
+ eaddress b = e.asEaddress(alice);
335
+ processAllOperations();
336
+ ebool c = alice.eq(b);
337
+ processAllOperations();
338
+ assertEq(getBoolValue(c), true);
339
+ }
340
+
341
+ // ============ NE SCALAR/EADDRESS VARIANTS ============
342
+
343
+ function testNeEuint256Uint256() public {
344
+ euint256 a = e.asEuint256(10);
345
+ ebool c = a.ne(uint256(5));
346
+ processAllOperations();
347
+ assertEq(getBoolValue(c), true);
348
+ }
349
+
350
+ function testNeUint256Euint256() public {
351
+ euint256 b = e.asEuint256(5);
352
+ ebool c = uint256(10).ne(b);
353
+ processAllOperations();
354
+ assertEq(getBoolValue(c), true);
355
+ }
356
+
357
+ function testNeEaddressEaddress() public {
358
+ eaddress a = e.asEaddress(alice);
359
+ eaddress b = e.asEaddress(bob);
360
+ processAllOperations();
361
+ ebool c = a.ne(b);
362
+ processAllOperations();
363
+ assertEq(getBoolValue(c), true);
364
+ }
365
+
366
+ function testNeEaddressAddress() public {
367
+ eaddress a = e.asEaddress(alice);
368
+ processAllOperations();
369
+ ebool c = a.ne(bob);
370
+ processAllOperations();
371
+ assertEq(getBoolValue(c), true);
372
+ }
373
+
374
+ function testNeAddressEaddress() public {
375
+ eaddress b = e.asEaddress(bob);
376
+ processAllOperations();
377
+ ebool c = alice.ne(b);
378
+ processAllOperations();
379
+ assertEq(getBoolValue(c), true);
380
+ }
381
+
382
+ // ============ GE SCALAR VARIANTS ============
383
+
384
+ function testGeEuint256Uint256() public {
385
+ euint256 a = e.asEuint256(10);
386
+ ebool c = a.ge(uint256(10));
387
+ processAllOperations();
388
+ assertEq(getBoolValue(c), true);
389
+ }
390
+
391
+ function testGeUint256Euint256() public {
392
+ euint256 b = e.asEuint256(5);
393
+ ebool c = uint256(10).ge(b);
394
+ processAllOperations();
395
+ assertEq(getBoolValue(c), true);
396
+ }
397
+
398
+ // ============ GT SCALAR VARIANTS ============
399
+
400
+ function testGtEuint256Uint256() public {
401
+ euint256 a = e.asEuint256(10);
402
+ ebool c = a.gt(uint256(5));
403
+ processAllOperations();
404
+ assertEq(getBoolValue(c), true);
405
+ }
406
+
407
+ function testGtUint256Euint256() public {
408
+ euint256 b = e.asEuint256(5);
409
+ ebool c = uint256(10).gt(b);
410
+ processAllOperations();
411
+ assertEq(getBoolValue(c), true);
412
+ }
413
+
414
+ // ============ LE SCALAR VARIANTS ============
415
+
416
+ function testLeEuint256Uint256() public {
417
+ euint256 a = e.asEuint256(5);
418
+ ebool c = a.le(uint256(10));
419
+ processAllOperations();
420
+ assertEq(getBoolValue(c), true);
421
+ }
422
+
423
+ function testLeUint256Euint256() public {
424
+ euint256 b = e.asEuint256(10);
425
+ ebool c = uint256(5).le(b);
426
+ processAllOperations();
427
+ assertEq(getBoolValue(c), true);
428
+ }
429
+
430
+ // ============ LT SCALAR VARIANTS ============
431
+
432
+ function testLtEuint256Uint256() public {
433
+ euint256 a = e.asEuint256(5);
434
+ ebool c = a.lt(uint256(10));
435
+ processAllOperations();
436
+ assertEq(getBoolValue(c), true);
437
+ }
438
+
439
+ function testLtUint256Euint256() public {
440
+ euint256 b = e.asEuint256(10);
441
+ ebool c = uint256(5).lt(b);
442
+ processAllOperations();
443
+ assertEq(getBoolValue(c), true);
444
+ }
445
+
446
+ // ============ MIN SCALAR VARIANTS ============
447
+
448
+ function testMinEuint256Uint256() public {
449
+ euint256 a = e.asEuint256(10);
450
+ euint256 c = a.min(uint256(5));
451
+ processAllOperations();
452
+ assertEq(getUint256Value(c), 5);
453
+ }
454
+
455
+ function testMinUint256Euint256() public {
456
+ euint256 b = e.asEuint256(5);
457
+ euint256 c = uint256(10).min(b);
458
+ processAllOperations();
459
+ assertEq(getUint256Value(c), 5);
460
+ }
461
+
462
+ // ============ MAX SCALAR VARIANTS ============
463
+
464
+ function testMaxEuint256Uint256() public {
465
+ euint256 a = e.asEuint256(5);
466
+ euint256 c = a.max(uint256(10));
467
+ processAllOperations();
468
+ assertEq(getUint256Value(c), 10);
469
+ }
470
+
471
+ function testMaxUint256Euint256() public {
472
+ euint256 b = e.asEuint256(10);
473
+ euint256 c = uint256(5).max(b);
474
+ processAllOperations();
475
+ assertEq(getUint256Value(c), 10);
476
+ }
477
+
478
+ // ============ NEW ENCRYPTED TYPE (msg.sender variant) ============
479
+
480
+ function testNewEuint256MsgSender() public {
481
+ // Use a helper contract to test the msg.sender variant
482
+ LibTestHelper helper = new LibTestHelper();
483
+ vm.deal(address(helper), 1 ether);
484
+
485
+ bytes memory ciphertext = fakePrepareEuint256Ciphertext(42, address(helper), address(helper));
486
+ vm.prank(address(helper));
487
+ helper.callNewEuint256(ciphertext);
488
+ processAllOperations();
489
+
490
+ assertEq(getUint256Value(helper.storedEuint256()), 42);
491
+ }
492
+
493
+ function testNewEboolMsgSender() public {
494
+ LibTestHelper helper = new LibTestHelper();
495
+ vm.deal(address(helper), 1 ether);
496
+
497
+ bytes memory ciphertext = fakePrepareEboolCiphertext(true, address(helper), address(helper));
498
+ vm.prank(address(helper));
499
+ helper.callNewEbool(ciphertext);
500
+ processAllOperations();
501
+
502
+ assertEq(getBoolValue(helper.storedEbool()), true);
503
+ }
504
+
505
+ function testNewEaddressMsgSender() public {
506
+ LibTestHelper helper = new LibTestHelper();
507
+ vm.deal(address(helper), 1 ether);
508
+
509
+ bytes memory ciphertext = fakePrepareEaddressCiphertext(alice, address(helper), address(helper));
510
+ vm.prank(address(helper));
511
+ helper.callNewEaddress(ciphertext);
512
+ processAllOperations();
513
+
514
+ assertEq(getAddressValue(helper.storedEaddress()), alice);
515
+ }
516
+
517
+ // ============ ALLOW/REVEAL VARIANTS ============
518
+ // Note: When creating encrypted values (e.g., e.asEbool()), the caller (msg.sender)
519
+ // automatically receives TRANSIENT access via allowTransientInternal().
520
+ // isAllowed() returns true if transient OR persistent access is granted.
521
+ // The allow/allowThis functions grant PERSISTENT access (stored in contract storage).
522
+ // These tests verify persistent access by checking persistAllowed().
523
+
524
+ function testAllowEbool() public {
525
+ ebool a = e.asEbool(true);
526
+ processAllOperations();
527
+ // Creator has transient access but not persistent access
528
+ assertFalse(inco.persistAllowed(ebool.unwrap(a), alice));
529
+ e.allow(a, alice);
530
+ assertTrue(inco.persistAllowed(ebool.unwrap(a), alice));
531
+ }
532
+
533
+ function testAllowEaddress() public {
534
+ eaddress a = e.asEaddress(alice);
535
+ processAllOperations();
536
+ assertFalse(inco.persistAllowed(eaddress.unwrap(a), bob));
537
+ e.allow(a, bob);
538
+ assertTrue(inco.persistAllowed(eaddress.unwrap(a), bob));
539
+ }
540
+
541
+ function testRevealEuint256() public {
542
+ euint256 a = e.asEuint256(42);
543
+ processAllOperations();
544
+ assertFalse(inco.isRevealed(euint256.unwrap(a)));
545
+ e.reveal(a);
546
+ assertTrue(inco.isRevealed(euint256.unwrap(a)));
547
+ }
548
+
549
+ function testRevealEbool() public {
550
+ ebool a = e.asEbool(true);
551
+ processAllOperations();
552
+ assertFalse(inco.isRevealed(ebool.unwrap(a)));
553
+ e.reveal(a);
554
+ assertTrue(inco.isRevealed(ebool.unwrap(a)));
555
+ }
556
+
557
+ function testRevealEaddress() public {
558
+ eaddress a = e.asEaddress(alice);
559
+ processAllOperations();
560
+ assertFalse(inco.isRevealed(eaddress.unwrap(a)));
561
+ e.reveal(a);
562
+ assertTrue(inco.isRevealed(eaddress.unwrap(a)));
563
+ }
564
+
565
+ function testAllowThisEbool() public {
566
+ ebool a = e.asEbool(true);
567
+ processAllOperations();
568
+ // Creator has transient access but not persistent access yet
569
+ assertFalse(inco.persistAllowed(ebool.unwrap(a), address(this)));
570
+ e.allowThis(a);
571
+ assertTrue(inco.persistAllowed(ebool.unwrap(a), address(this)));
572
+ }
573
+
574
+ function testAllowThisEaddress() public {
575
+ eaddress a = e.asEaddress(alice);
576
+ processAllOperations();
577
+ // Creator has transient access but not persistent access yet
578
+ assertFalse(inco.persistAllowed(eaddress.unwrap(a), address(this)));
579
+ e.allowThis(a);
580
+ assertTrue(inco.persistAllowed(eaddress.unwrap(a), address(this)));
581
+ }
582
+
583
+ function testIsAllowed() public {
584
+ euint256 a = e.asEuint256(42);
585
+ processAllOperations();
586
+ e.allow(a, alice);
587
+ // isAllowed should return true for allowed address
588
+ bool allowed = e.isAllowed(alice, a);
589
+ assertTrue(allowed);
590
+ }
591
+
592
+ // ============ SELECT VARIANTS ============
593
+
594
+ function testSelectEboolEboolEbool() public {
595
+ ebool control = e.asEbool(true);
596
+ ebool ifTrue = e.asEbool(true);
597
+ ebool ifFalse = e.asEbool(false);
598
+ ebool result = control.select(ifTrue, ifFalse);
599
+ processAllOperations();
600
+ assertEq(getBoolValue(result), true);
601
+ }
602
+
603
+ function testSelectEboolEaddressEaddress() public {
604
+ ebool control = e.asEbool(false);
605
+ eaddress ifTrue = e.asEaddress(alice);
606
+ eaddress ifFalse = e.asEaddress(bob);
607
+ processAllOperations();
608
+ eaddress result = control.select(ifTrue, ifFalse);
609
+ processAllOperations();
610
+ assertEq(getAddressValue(result), bob);
611
+ }
612
+
613
+ // ============ SELECT WITH EUINT256 ============
614
+
615
+ function testSelectEboolEuint256Euint256() public {
616
+ ebool control = e.asEbool(true);
617
+ euint256 ifTrue = e.asEuint256(42);
618
+ euint256 ifFalse = e.asEuint256(100);
619
+ euint256 result = control.select(ifTrue, ifFalse);
620
+ processAllOperations();
621
+ assertEq(getUint256Value(result), 42);
622
+ }
623
+
624
+ function testSelectEboolEuint256Euint256False() public {
625
+ ebool control = e.asEbool(false);
626
+ euint256 ifTrue = e.asEuint256(42);
627
+ euint256 ifFalse = e.asEuint256(100);
628
+ euint256 result = control.select(ifTrue, ifFalse);
629
+ processAllOperations();
630
+ assertEq(getUint256Value(result), 100);
631
+ }
632
+
633
+ // ============ CAST OPERATIONS ============
634
+
635
+ function testCastEuint256ToEboolTrue() public {
636
+ euint256 a = e.asEuint256(1);
637
+ ebool b = e.asEbool(a);
638
+ processAllOperations();
639
+ assertEq(getBoolValue(b), true);
640
+ }
641
+
642
+ function testCastEuint256ToEboolFalse() public {
643
+ euint256 a = e.asEuint256(0);
644
+ ebool b = e.asEbool(a);
645
+ processAllOperations();
646
+ assertEq(getBoolValue(b), false);
647
+ }
648
+
649
+ function testCastEboolToEuint256True() public {
650
+ ebool a = e.asEbool(true);
651
+ euint256 b = e.asEuint256(a);
652
+ processAllOperations();
653
+ assertEq(getUint256Value(b), 1);
654
+ }
655
+
656
+ function testCastEboolToEuint256False() public {
657
+ ebool a = e.asEbool(false);
658
+ euint256 b = e.asEuint256(a);
659
+ processAllOperations();
660
+ assertEq(getUint256Value(b), 0);
661
+ }
662
+
663
+ // ============ EADDRESS CAST OPERATIONS (via inco.eCast directly) ============
664
+ // These test the underlying eCast functionality for eaddress conversions
665
+
666
+ function testCastEuint256ToEaddress() public {
667
+ euint256 a = e.asEuint256(uint256(uint160(alice)));
668
+ eaddress b = eaddress.wrap(inco.eCast(euint256.unwrap(a), ETypes.AddressOrUint160OrBytes20));
669
+ processAllOperations();
670
+ assertEq(getAddressValue(b), alice);
671
+ }
672
+
673
+ function testCastEuint256ToEaddressZero() public {
674
+ euint256 a = e.asEuint256(0);
675
+ eaddress b = eaddress.wrap(inco.eCast(euint256.unwrap(a), ETypes.AddressOrUint160OrBytes20));
676
+ processAllOperations();
677
+ assertEq(getAddressValue(b), address(0));
678
+ }
679
+
680
+ function testCastEboolToEaddressTrue() public {
681
+ ebool a = e.asEbool(true);
682
+ eaddress b = eaddress.wrap(inco.eCast(ebool.unwrap(a), ETypes.AddressOrUint160OrBytes20));
683
+ processAllOperations();
684
+ assertEq(getAddressValue(b), address(1));
685
+ }
686
+
687
+ function testCastEboolToEaddressFalse() public {
688
+ ebool a = e.asEbool(false);
689
+ eaddress b = eaddress.wrap(inco.eCast(ebool.unwrap(a), ETypes.AddressOrUint160OrBytes20));
690
+ processAllOperations();
691
+ assertEq(getAddressValue(b), address(0));
692
+ }
693
+
694
+ function testCastEaddressToEuint256() public {
695
+ eaddress a = e.asEaddress(alice);
696
+ processAllOperations();
697
+ euint256 b = euint256.wrap(inco.eCast(eaddress.unwrap(a), ETypes.Uint256));
698
+ processAllOperations();
699
+ assertEq(getUint256Value(b), uint256(uint160(alice)));
700
+ }
701
+
702
+ function testCastEaddressToEuint256Zero() public {
703
+ eaddress a = e.asEaddress(address(0));
704
+ processAllOperations();
705
+ euint256 b = euint256.wrap(inco.eCast(eaddress.unwrap(a), ETypes.Uint256));
706
+ processAllOperations();
707
+ assertEq(getUint256Value(b), 0);
708
+ }
709
+
710
+ function testCastEaddressToEboolNonZero() public {
711
+ eaddress a = e.asEaddress(alice);
712
+ processAllOperations();
713
+ ebool b = ebool.wrap(inco.eCast(eaddress.unwrap(a), ETypes.Bool));
714
+ processAllOperations();
715
+ assertEq(getBoolValue(b), true);
716
+ }
717
+
718
+ function testCastEaddressToEboolZero() public {
719
+ eaddress a = e.asEaddress(address(0));
720
+ processAllOperations();
721
+ ebool b = ebool.wrap(inco.eCast(eaddress.unwrap(a), ETypes.Bool));
722
+ processAllOperations();
723
+ assertEq(getBoolValue(b), false);
724
+ }
725
+
726
+ // ============ NOT OPERATION ============
727
+
728
+ function testNotEboolTrue() public {
729
+ ebool a = e.asEbool(true);
730
+ ebool b = a.not();
731
+ processAllOperations();
732
+ assertEq(getBoolValue(b), false);
733
+ }
734
+
735
+ function testNotEboolFalse() public {
736
+ ebool a = e.asEbool(false);
737
+ ebool b = a.not();
738
+ processAllOperations();
739
+ assertEq(getBoolValue(b), true);
740
+ }
741
+
742
+ // ============ RANDOM OPERATIONS ============
743
+
744
+ function testRand() public {
745
+ vm.deal(address(this), 1 ether);
746
+ euint256 r = e.rand();
747
+ processAllOperations();
748
+ // Just verify it returns a valid handle (non-zero after processing)
749
+ assertTrue(euint256.unwrap(r) != bytes32(0));
750
+ }
751
+
752
+ function testRandBoundedPlaintext() public {
753
+ vm.deal(address(this), 1 ether);
754
+ euint256 r = e.randBounded(uint256(100));
755
+ processAllOperations();
756
+ // Verify result is less than bound
757
+ uint256 value = getUint256Value(r);
758
+ assertTrue(value < 100);
759
+ }
760
+
761
+ function testRandBoundedEncrypted() public {
762
+ vm.deal(address(this), 1 ether);
763
+ euint256 bound = e.asEuint256(50);
764
+ euint256 r = e.randBounded(bound);
765
+ processAllOperations();
766
+ // Verify result is less than bound
767
+ uint256 value = getUint256Value(r);
768
+ assertTrue(value < 50);
769
+ }
770
+
771
+ }
772
+
773
+ /// @notice Helper contract for testing msg.sender variants
774
+ contract LibTestHelper {
775
+
776
+ using e for bytes;
777
+
778
+ euint256 public storedEuint256;
779
+ ebool public storedEbool;
780
+ eaddress public storedEaddress;
781
+
782
+ function callNewEuint256(bytes memory ciphertext) external {
783
+ storedEuint256 = ciphertext.newEuint256();
784
+ }
785
+
786
+ function callNewEbool(bytes memory ciphertext) external {
787
+ storedEbool = ciphertext.newEbool();
788
+ }
789
+
790
+ function callNewEaddress(bytes memory ciphertext) external {
791
+ storedEaddress = ciphertext.newEaddress();
792
+ }
793
+
794
+ }