@iexec-nox/nox-protocol-contracts 0.1.0-beta.8 → 0.1.0

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.
@@ -1,6 +1,7 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.0;
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.27;
3
3
 
4
+ import {HandleUtils} from "../shared/HandleUtils.sol";
4
5
  import {TEEType, TypeUtils} from "../shared/TypeUtils.sol";
5
6
  import {INoxCompute} from "../interfaces/INoxCompute.sol";
6
7
  import "encrypted-types/EncryptedTypes.sol";
@@ -12,7 +13,7 @@ import "encrypted-types/EncryptedTypes.sol";
12
13
  library Nox {
13
14
  // ============ Errors ============
14
15
 
15
- error UninitializedHandle();
16
+ error MalformedDecryptedData(bytes data);
16
17
 
17
18
  // ============ Address resolution ============
18
19
 
@@ -33,7 +34,7 @@ library Nox {
33
34
  }
34
35
  // Local development chain
35
36
  if (block.chainid == 31337) {
36
- return 0x6cC89dEd8c25E1460fa8408099C8Ab5b902D8b6B;
37
+ return 0x39847AeBa923Cc7367d4684194091D022B3F8548;
37
38
  }
38
39
  revert("Nox: Unsupported chain");
39
40
  }
@@ -47,7 +48,7 @@ library Nox {
47
48
  * Public handles are already accessible by everyone and don't need ACL.
48
49
  */
49
50
  function _allowIfNotPublic(bytes32 handle, address account) private {
50
- if (!TypeUtils.isPublicHandle(handle)) {
51
+ if (!HandleUtils.isPublicHandle(handle)) {
51
52
  _noxComputeContract().allow(handle, account);
52
53
  }
53
54
  }
@@ -57,7 +58,7 @@ library Nox {
57
58
  * Public handles are already accessible by everyone and don't need ACL.
58
59
  */
59
60
  function _allowTransientIfNotPublic(bytes32 handle, address account) private {
60
- if (!TypeUtils.isPublicHandle(handle)) {
61
+ if (!HandleUtils.isPublicHandle(handle)) {
61
62
  _noxComputeContract().allowTransient(handle, account);
62
63
  }
63
64
  }
@@ -67,7 +68,7 @@ library Nox {
67
68
  * Public handles are already accessible by everyone and don't need ACL.
68
69
  */
69
70
  function _disallowTransientIfNotPublic(bytes32 handle, address account) private {
70
- if (!TypeUtils.isPublicHandle(handle)) {
71
+ if (!HandleUtils.isPublicHandle(handle)) {
71
72
  _noxComputeContract().disallowTransient(handle, account);
72
73
  }
73
74
  }
@@ -251,153 +252,301 @@ library Nox {
251
252
  // ============ Arithmetic primitives ============
252
253
 
253
254
  function add(euint16 a, euint16 b) internal returns (euint16) {
254
- return euint16.wrap(_add(euint16.unwrap(a), euint16.unwrap(b)));
255
+ return
256
+ euint16.wrap(
257
+ _noxComputeContract().add(
258
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
259
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
260
+ )
261
+ );
255
262
  }
256
263
 
257
264
  function add(euint256 a, euint256 b) internal returns (euint256) {
258
- return euint256.wrap(_add(euint256.unwrap(a), euint256.unwrap(b)));
265
+ return
266
+ euint256.wrap(
267
+ _noxComputeContract().add(
268
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
269
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
270
+ )
271
+ );
259
272
  }
260
273
 
261
274
  function add(eint16 a, eint16 b) internal returns (eint16) {
262
- return eint16.wrap(_add(eint16.unwrap(a), eint16.unwrap(b)));
275
+ return
276
+ eint16.wrap(
277
+ _noxComputeContract().add(
278
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
279
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
280
+ )
281
+ );
263
282
  }
264
283
 
265
284
  function add(eint256 a, eint256 b) internal returns (eint256) {
266
- return eint256.wrap(_add(eint256.unwrap(a), eint256.unwrap(b)));
285
+ return
286
+ eint256.wrap(
287
+ _noxComputeContract().add(
288
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
289
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
290
+ )
291
+ );
267
292
  }
268
293
 
269
294
  function sub(euint16 a, euint16 b) internal returns (euint16) {
270
- return euint16.wrap(_sub(euint16.unwrap(a), euint16.unwrap(b)));
295
+ return
296
+ euint16.wrap(
297
+ _noxComputeContract().sub(
298
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
299
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
300
+ )
301
+ );
271
302
  }
272
303
 
273
304
  function sub(euint256 a, euint256 b) internal returns (euint256) {
274
- return euint256.wrap(_sub(euint256.unwrap(a), euint256.unwrap(b)));
305
+ return
306
+ euint256.wrap(
307
+ _noxComputeContract().sub(
308
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
309
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
310
+ )
311
+ );
275
312
  }
276
313
 
277
314
  function sub(eint16 a, eint16 b) internal returns (eint16) {
278
- return eint16.wrap(_sub(eint16.unwrap(a), eint16.unwrap(b)));
315
+ return
316
+ eint16.wrap(
317
+ _noxComputeContract().sub(
318
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
319
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
320
+ )
321
+ );
279
322
  }
280
323
 
281
324
  function sub(eint256 a, eint256 b) internal returns (eint256) {
282
- return eint256.wrap(_sub(eint256.unwrap(a), eint256.unwrap(b)));
325
+ return
326
+ eint256.wrap(
327
+ _noxComputeContract().sub(
328
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
329
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
330
+ )
331
+ );
283
332
  }
284
333
 
285
334
  function mul(euint16 a, euint16 b) internal returns (euint16) {
286
- return euint16.wrap(_mul(euint16.unwrap(a), euint16.unwrap(b)));
335
+ return
336
+ euint16.wrap(
337
+ _noxComputeContract().mul(
338
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
339
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
340
+ )
341
+ );
287
342
  }
288
343
 
289
344
  function mul(euint256 a, euint256 b) internal returns (euint256) {
290
- return euint256.wrap(_mul(euint256.unwrap(a), euint256.unwrap(b)));
345
+ return
346
+ euint256.wrap(
347
+ _noxComputeContract().mul(
348
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
349
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
350
+ )
351
+ );
291
352
  }
292
353
 
293
354
  function mul(eint16 a, eint16 b) internal returns (eint16) {
294
- return eint16.wrap(_mul(eint16.unwrap(a), eint16.unwrap(b)));
355
+ return
356
+ eint16.wrap(
357
+ _noxComputeContract().mul(
358
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
359
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
360
+ )
361
+ );
295
362
  }
296
363
 
297
364
  function mul(eint256 a, eint256 b) internal returns (eint256) {
298
- return eint256.wrap(_mul(eint256.unwrap(a), eint256.unwrap(b)));
365
+ return
366
+ eint256.wrap(
367
+ _noxComputeContract().mul(
368
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
369
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
370
+ )
371
+ );
299
372
  }
300
373
 
301
374
  function div(euint16 a, euint16 b) internal returns (euint16) {
302
- return euint16.wrap(_div(euint16.unwrap(a), euint16.unwrap(b)));
375
+ return
376
+ euint16.wrap(
377
+ _noxComputeContract().div(
378
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
379
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
380
+ )
381
+ );
303
382
  }
304
383
 
305
384
  function div(euint256 a, euint256 b) internal returns (euint256) {
306
- return euint256.wrap(_div(euint256.unwrap(a), euint256.unwrap(b)));
385
+ return
386
+ euint256.wrap(
387
+ _noxComputeContract().div(
388
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
389
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
390
+ )
391
+ );
307
392
  }
308
393
 
309
394
  function div(eint16 a, eint16 b) internal returns (eint16) {
310
- return eint16.wrap(_div(eint16.unwrap(a), eint16.unwrap(b)));
395
+ return
396
+ eint16.wrap(
397
+ _noxComputeContract().div(
398
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
399
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
400
+ )
401
+ );
311
402
  }
312
403
 
313
404
  function div(eint256 a, eint256 b) internal returns (eint256) {
314
- return eint256.wrap(_div(eint256.unwrap(a), eint256.unwrap(b)));
405
+ return
406
+ eint256.wrap(
407
+ _noxComputeContract().div(
408
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
409
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
410
+ )
411
+ );
315
412
  }
316
413
 
317
414
  function safeAdd(euint16 a, euint16 b) internal returns (ebool, euint16) {
318
- (bytes32 success, bytes32 result) = _safeAdd(euint16.unwrap(a), euint16.unwrap(b));
415
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeAdd(
416
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
417
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
418
+ );
319
419
  return (ebool.wrap(success), euint16.wrap(result));
320
420
  }
321
421
 
322
422
  function safeAdd(euint256 a, euint256 b) internal returns (ebool, euint256) {
323
- (bytes32 success, bytes32 result) = _safeAdd(euint256.unwrap(a), euint256.unwrap(b));
423
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeAdd(
424
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
425
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
426
+ );
324
427
  return (ebool.wrap(success), euint256.wrap(result));
325
428
  }
326
429
 
327
430
  function safeAdd(eint16 a, eint16 b) internal returns (ebool, eint16) {
328
- (bytes32 success, bytes32 result) = _safeAdd(eint16.unwrap(a), eint16.unwrap(b));
431
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeAdd(
432
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
433
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
434
+ );
329
435
  return (ebool.wrap(success), eint16.wrap(result));
330
436
  }
331
437
 
332
438
  function safeAdd(eint256 a, eint256 b) internal returns (ebool, eint256) {
333
- (bytes32 success, bytes32 result) = _safeAdd(eint256.unwrap(a), eint256.unwrap(b));
439
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeAdd(
440
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
441
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
442
+ );
334
443
  return (ebool.wrap(success), eint256.wrap(result));
335
444
  }
336
445
 
337
446
  function safeSub(euint16 a, euint16 b) internal returns (ebool, euint16) {
338
- (bytes32 success, bytes32 result) = _safeSub(euint16.unwrap(a), euint16.unwrap(b));
447
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeSub(
448
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
449
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
450
+ );
339
451
  return (ebool.wrap(success), euint16.wrap(result));
340
452
  }
341
453
 
342
454
  function safeSub(euint256 a, euint256 b) internal returns (ebool, euint256) {
343
- (bytes32 success, bytes32 result) = _safeSub(euint256.unwrap(a), euint256.unwrap(b));
455
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeSub(
456
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
457
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
458
+ );
344
459
  return (ebool.wrap(success), euint256.wrap(result));
345
460
  }
346
461
 
347
462
  function safeSub(eint16 a, eint16 b) internal returns (ebool, eint16) {
348
- (bytes32 success, bytes32 result) = _safeSub(eint16.unwrap(a), eint16.unwrap(b));
463
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeSub(
464
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
465
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
466
+ );
349
467
  return (ebool.wrap(success), eint16.wrap(result));
350
468
  }
351
469
 
352
470
  function safeSub(eint256 a, eint256 b) internal returns (ebool, eint256) {
353
- (bytes32 success, bytes32 result) = _safeSub(eint256.unwrap(a), eint256.unwrap(b));
471
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeSub(
472
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
473
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
474
+ );
354
475
  return (ebool.wrap(success), eint256.wrap(result));
355
476
  }
356
477
 
357
478
  function safeMul(euint16 a, euint16 b) internal returns (ebool, euint16) {
358
- (bytes32 success, bytes32 result) = _safeMul(euint16.unwrap(a), euint16.unwrap(b));
479
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeMul(
480
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
481
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
482
+ );
359
483
  return (ebool.wrap(success), euint16.wrap(result));
360
484
  }
361
485
 
362
486
  function safeMul(euint256 a, euint256 b) internal returns (ebool, euint256) {
363
- (bytes32 success, bytes32 result) = _safeMul(euint256.unwrap(a), euint256.unwrap(b));
487
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeMul(
488
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
489
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
490
+ );
364
491
  return (ebool.wrap(success), euint256.wrap(result));
365
492
  }
366
493
 
367
494
  function safeMul(eint16 a, eint16 b) internal returns (ebool, eint16) {
368
- (bytes32 success, bytes32 result) = _safeMul(eint16.unwrap(a), eint16.unwrap(b));
495
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeMul(
496
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
497
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
498
+ );
369
499
  return (ebool.wrap(success), eint16.wrap(result));
370
500
  }
371
501
 
372
502
  function safeMul(eint256 a, eint256 b) internal returns (ebool, eint256) {
373
- (bytes32 success, bytes32 result) = _safeMul(eint256.unwrap(a), eint256.unwrap(b));
503
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeMul(
504
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
505
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
506
+ );
374
507
  return (ebool.wrap(success), eint256.wrap(result));
375
508
  }
376
509
 
377
510
  function safeDiv(euint16 a, euint16 b) internal returns (ebool, euint16) {
378
- (bytes32 success, bytes32 result) = _safeDiv(euint16.unwrap(a), euint16.unwrap(b));
511
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeDiv(
512
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
513
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
514
+ );
379
515
  return (ebool.wrap(success), euint16.wrap(result));
380
516
  }
381
517
 
382
518
  function safeDiv(euint256 a, euint256 b) internal returns (ebool, euint256) {
383
- (bytes32 success, bytes32 result) = _safeDiv(euint256.unwrap(a), euint256.unwrap(b));
519
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeDiv(
520
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
521
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
522
+ );
384
523
  return (ebool.wrap(success), euint256.wrap(result));
385
524
  }
386
525
 
387
526
  function safeDiv(eint16 a, eint16 b) internal returns (ebool, eint16) {
388
- (bytes32 success, bytes32 result) = _safeDiv(eint16.unwrap(a), eint16.unwrap(b));
527
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeDiv(
528
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
529
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
530
+ );
389
531
  return (ebool.wrap(success), eint16.wrap(result));
390
532
  }
391
533
 
392
534
  function safeDiv(eint256 a, eint256 b) internal returns (ebool, eint256) {
393
- (bytes32 success, bytes32 result) = _safeDiv(eint256.unwrap(a), eint256.unwrap(b));
535
+ (bytes32 success, bytes32 result) = _noxComputeContract().safeDiv(
536
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
537
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
538
+ );
394
539
  return (ebool.wrap(success), eint256.wrap(result));
395
540
  }
396
541
 
397
542
  function select(ebool condition, euint16 ifTrue, euint16 ifFalse) internal returns (euint16) {
398
543
  return
399
544
  euint16.wrap(
400
- _select(ebool.unwrap(condition), euint16.unwrap(ifTrue), euint16.unwrap(ifFalse))
545
+ _noxComputeContract().select(
546
+ _resolveUndefinedHandle(ebool.unwrap(condition), TEEType.Bool),
547
+ _resolveUndefinedHandle(euint16.unwrap(ifTrue), TEEType.Uint16),
548
+ _resolveUndefinedHandle(euint16.unwrap(ifFalse), TEEType.Uint16)
549
+ )
401
550
  );
402
551
  }
403
552
 
@@ -408,118 +557,274 @@ library Nox {
408
557
  ) internal returns (euint256) {
409
558
  return
410
559
  euint256.wrap(
411
- _select(ebool.unwrap(condition), euint256.unwrap(ifTrue), euint256.unwrap(ifFalse))
560
+ _noxComputeContract().select(
561
+ _resolveUndefinedHandle(ebool.unwrap(condition), TEEType.Bool),
562
+ _resolveUndefinedHandle(euint256.unwrap(ifTrue), TEEType.Uint256),
563
+ _resolveUndefinedHandle(euint256.unwrap(ifFalse), TEEType.Uint256)
564
+ )
412
565
  );
413
566
  }
414
567
 
415
568
  function select(ebool condition, eint16 ifTrue, eint16 ifFalse) internal returns (eint16) {
416
569
  return
417
570
  eint16.wrap(
418
- _select(ebool.unwrap(condition), eint16.unwrap(ifTrue), eint16.unwrap(ifFalse))
571
+ _noxComputeContract().select(
572
+ _resolveUndefinedHandle(ebool.unwrap(condition), TEEType.Bool),
573
+ _resolveUndefinedHandle(eint16.unwrap(ifTrue), TEEType.Int16),
574
+ _resolveUndefinedHandle(eint16.unwrap(ifFalse), TEEType.Int16)
575
+ )
419
576
  );
420
577
  }
421
578
 
422
579
  function select(ebool condition, eint256 ifTrue, eint256 ifFalse) internal returns (eint256) {
423
580
  return
424
581
  eint256.wrap(
425
- _select(ebool.unwrap(condition), eint256.unwrap(ifTrue), eint256.unwrap(ifFalse))
582
+ _noxComputeContract().select(
583
+ _resolveUndefinedHandle(ebool.unwrap(condition), TEEType.Bool),
584
+ _resolveUndefinedHandle(eint256.unwrap(ifTrue), TEEType.Int256),
585
+ _resolveUndefinedHandle(eint256.unwrap(ifFalse), TEEType.Int256)
586
+ )
426
587
  );
427
588
  }
428
589
 
429
590
  function eq(euint16 a, euint16 b) internal returns (ebool) {
430
- return ebool.wrap(_eq(euint16.unwrap(a), euint16.unwrap(b)));
591
+ return
592
+ ebool.wrap(
593
+ _noxComputeContract().eq(
594
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
595
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
596
+ )
597
+ );
431
598
  }
432
599
 
433
600
  function eq(euint256 a, euint256 b) internal returns (ebool) {
434
- return ebool.wrap(_eq(euint256.unwrap(a), euint256.unwrap(b)));
601
+ return
602
+ ebool.wrap(
603
+ _noxComputeContract().eq(
604
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
605
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
606
+ )
607
+ );
435
608
  }
436
609
 
437
610
  function eq(eint16 a, eint16 b) internal returns (ebool) {
438
- return ebool.wrap(_eq(eint16.unwrap(a), eint16.unwrap(b)));
611
+ return
612
+ ebool.wrap(
613
+ _noxComputeContract().eq(
614
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
615
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
616
+ )
617
+ );
439
618
  }
440
619
 
441
620
  function eq(eint256 a, eint256 b) internal returns (ebool) {
442
- return ebool.wrap(_eq(eint256.unwrap(a), eint256.unwrap(b)));
621
+ return
622
+ ebool.wrap(
623
+ _noxComputeContract().eq(
624
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
625
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
626
+ )
627
+ );
443
628
  }
444
629
 
445
630
  function ne(euint16 a, euint16 b) internal returns (ebool) {
446
- return ebool.wrap(_ne(euint16.unwrap(a), euint16.unwrap(b)));
631
+ return
632
+ ebool.wrap(
633
+ _noxComputeContract().ne(
634
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
635
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
636
+ )
637
+ );
447
638
  }
448
639
 
449
640
  function ne(euint256 a, euint256 b) internal returns (ebool) {
450
- return ebool.wrap(_ne(euint256.unwrap(a), euint256.unwrap(b)));
641
+ return
642
+ ebool.wrap(
643
+ _noxComputeContract().ne(
644
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
645
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
646
+ )
647
+ );
451
648
  }
452
649
 
453
650
  function ne(eint16 a, eint16 b) internal returns (ebool) {
454
- return ebool.wrap(_ne(eint16.unwrap(a), eint16.unwrap(b)));
651
+ return
652
+ ebool.wrap(
653
+ _noxComputeContract().ne(
654
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
655
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
656
+ )
657
+ );
455
658
  }
456
659
 
457
660
  function ne(eint256 a, eint256 b) internal returns (ebool) {
458
- return ebool.wrap(_ne(eint256.unwrap(a), eint256.unwrap(b)));
661
+ return
662
+ ebool.wrap(
663
+ _noxComputeContract().ne(
664
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
665
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
666
+ )
667
+ );
459
668
  }
460
669
 
461
670
  function lt(euint16 a, euint16 b) internal returns (ebool) {
462
- return ebool.wrap(_lt(euint16.unwrap(a), euint16.unwrap(b)));
671
+ return
672
+ ebool.wrap(
673
+ _noxComputeContract().lt(
674
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
675
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
676
+ )
677
+ );
463
678
  }
464
679
 
465
680
  function lt(euint256 a, euint256 b) internal returns (ebool) {
466
- return ebool.wrap(_lt(euint256.unwrap(a), euint256.unwrap(b)));
681
+ return
682
+ ebool.wrap(
683
+ _noxComputeContract().lt(
684
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
685
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
686
+ )
687
+ );
467
688
  }
468
689
 
469
690
  function lt(eint16 a, eint16 b) internal returns (ebool) {
470
- return ebool.wrap(_lt(eint16.unwrap(a), eint16.unwrap(b)));
691
+ return
692
+ ebool.wrap(
693
+ _noxComputeContract().lt(
694
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
695
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
696
+ )
697
+ );
471
698
  }
472
699
 
473
700
  function lt(eint256 a, eint256 b) internal returns (ebool) {
474
- return ebool.wrap(_lt(eint256.unwrap(a), eint256.unwrap(b)));
701
+ return
702
+ ebool.wrap(
703
+ _noxComputeContract().lt(
704
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
705
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
706
+ )
707
+ );
475
708
  }
476
709
 
477
710
  function le(euint16 a, euint16 b) internal returns (ebool) {
478
- return ebool.wrap(_le(euint16.unwrap(a), euint16.unwrap(b)));
711
+ return
712
+ ebool.wrap(
713
+ _noxComputeContract().le(
714
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
715
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
716
+ )
717
+ );
479
718
  }
480
719
 
481
720
  function le(euint256 a, euint256 b) internal returns (ebool) {
482
- return ebool.wrap(_le(euint256.unwrap(a), euint256.unwrap(b)));
721
+ return
722
+ ebool.wrap(
723
+ _noxComputeContract().le(
724
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
725
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
726
+ )
727
+ );
483
728
  }
484
729
 
485
730
  function le(eint16 a, eint16 b) internal returns (ebool) {
486
- return ebool.wrap(_le(eint16.unwrap(a), eint16.unwrap(b)));
731
+ return
732
+ ebool.wrap(
733
+ _noxComputeContract().le(
734
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
735
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
736
+ )
737
+ );
487
738
  }
488
739
 
489
740
  function le(eint256 a, eint256 b) internal returns (ebool) {
490
- return ebool.wrap(_le(eint256.unwrap(a), eint256.unwrap(b)));
741
+ return
742
+ ebool.wrap(
743
+ _noxComputeContract().le(
744
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
745
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
746
+ )
747
+ );
491
748
  }
492
749
 
493
750
  function gt(euint16 a, euint16 b) internal returns (ebool) {
494
- return ebool.wrap(_gt(euint16.unwrap(a), euint16.unwrap(b)));
751
+ return
752
+ ebool.wrap(
753
+ _noxComputeContract().gt(
754
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
755
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
756
+ )
757
+ );
495
758
  }
496
759
 
497
760
  function gt(euint256 a, euint256 b) internal returns (ebool) {
498
- return ebool.wrap(_gt(euint256.unwrap(a), euint256.unwrap(b)));
761
+ return
762
+ ebool.wrap(
763
+ _noxComputeContract().gt(
764
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
765
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
766
+ )
767
+ );
499
768
  }
500
769
 
501
770
  function gt(eint16 a, eint16 b) internal returns (ebool) {
502
- return ebool.wrap(_gt(eint16.unwrap(a), eint16.unwrap(b)));
771
+ return
772
+ ebool.wrap(
773
+ _noxComputeContract().gt(
774
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
775
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
776
+ )
777
+ );
503
778
  }
504
779
 
505
780
  function gt(eint256 a, eint256 b) internal returns (ebool) {
506
- return ebool.wrap(_gt(eint256.unwrap(a), eint256.unwrap(b)));
781
+ return
782
+ ebool.wrap(
783
+ _noxComputeContract().gt(
784
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
785
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
786
+ )
787
+ );
507
788
  }
508
789
 
509
790
  function ge(euint16 a, euint16 b) internal returns (ebool) {
510
- return ebool.wrap(_ge(euint16.unwrap(a), euint16.unwrap(b)));
791
+ return
792
+ ebool.wrap(
793
+ _noxComputeContract().ge(
794
+ _resolveUndefinedHandle(euint16.unwrap(a), TEEType.Uint16),
795
+ _resolveUndefinedHandle(euint16.unwrap(b), TEEType.Uint16)
796
+ )
797
+ );
511
798
  }
512
799
 
513
800
  function ge(euint256 a, euint256 b) internal returns (ebool) {
514
- return ebool.wrap(_ge(euint256.unwrap(a), euint256.unwrap(b)));
801
+ return
802
+ ebool.wrap(
803
+ _noxComputeContract().ge(
804
+ _resolveUndefinedHandle(euint256.unwrap(a), TEEType.Uint256),
805
+ _resolveUndefinedHandle(euint256.unwrap(b), TEEType.Uint256)
806
+ )
807
+ );
515
808
  }
516
809
 
517
810
  function ge(eint16 a, eint16 b) internal returns (ebool) {
518
- return ebool.wrap(_ge(eint16.unwrap(a), eint16.unwrap(b)));
811
+ return
812
+ ebool.wrap(
813
+ _noxComputeContract().ge(
814
+ _resolveUndefinedHandle(eint16.unwrap(a), TEEType.Int16),
815
+ _resolveUndefinedHandle(eint16.unwrap(b), TEEType.Int16)
816
+ )
817
+ );
519
818
  }
520
819
 
521
820
  function ge(eint256 a, eint256 b) internal returns (ebool) {
522
- return ebool.wrap(_ge(eint256.unwrap(a), eint256.unwrap(b)));
821
+ return
822
+ ebool.wrap(
823
+ _noxComputeContract().ge(
824
+ _resolveUndefinedHandle(eint256.unwrap(a), TEEType.Int256),
825
+ _resolveUndefinedHandle(eint256.unwrap(b), TEEType.Int256)
826
+ )
827
+ );
523
828
  }
524
829
 
525
830
  // ============ ADVANCED FUNCTIONS ============
@@ -534,11 +839,12 @@ library Nox {
534
839
  euint256 balanceTo,
535
840
  euint256 amount
536
841
  ) internal returns (ebool success, euint256 newBalanceFrom, euint256 newBalanceTo) {
537
- (bytes32 _success, bytes32 _newBalanceFrom, bytes32 _newBalanceTo) = _transfer(
538
- euint256.unwrap(balanceFrom),
539
- euint256.unwrap(balanceTo),
540
- euint256.unwrap(amount)
541
- );
842
+ (bytes32 _success, bytes32 _newBalanceFrom, bytes32 _newBalanceTo) = _noxComputeContract()
843
+ .transfer(
844
+ _resolveUndefinedHandle(euint256.unwrap(balanceFrom), TEEType.Uint256),
845
+ _resolveUndefinedHandle(euint256.unwrap(balanceTo), TEEType.Uint256),
846
+ _resolveUndefinedHandle(euint256.unwrap(amount), TEEType.Uint256)
847
+ );
542
848
  success = ebool.wrap(_success);
543
849
  newBalanceFrom = euint256.wrap(_newBalanceFrom);
544
850
  newBalanceTo = euint256.wrap(_newBalanceTo);
@@ -554,11 +860,12 @@ library Nox {
554
860
  euint256 amount,
555
861
  euint256 totalSupply
556
862
  ) internal returns (ebool success, euint256 newBalanceTo, euint256 newTotalSupply) {
557
- (bytes32 _success, bytes32 _newBalanceTo, bytes32 _newTotalSupply) = _mint(
558
- euint256.unwrap(balanceTo),
559
- euint256.unwrap(amount),
560
- euint256.unwrap(totalSupply)
561
- );
863
+ (bytes32 _success, bytes32 _newBalanceTo, bytes32 _newTotalSupply) = _noxComputeContract()
864
+ .mint(
865
+ _resolveUndefinedHandle(euint256.unwrap(balanceTo), TEEType.Uint256),
866
+ _resolveUndefinedHandle(euint256.unwrap(amount), TEEType.Uint256),
867
+ _resolveUndefinedHandle(euint256.unwrap(totalSupply), TEEType.Uint256)
868
+ );
562
869
  success = ebool.wrap(_success);
563
870
  newBalanceTo = euint256.wrap(_newBalanceTo);
564
871
  newTotalSupply = euint256.wrap(_newTotalSupply);
@@ -574,11 +881,12 @@ library Nox {
574
881
  euint256 amount,
575
882
  euint256 totalSupply
576
883
  ) internal returns (ebool success, euint256 newBalanceFrom, euint256 newTotalSupply) {
577
- (bytes32 _success, bytes32 _newBalanceFrom, bytes32 _newTotalSupply) = _burn(
578
- euint256.unwrap(balanceFrom),
579
- euint256.unwrap(amount),
580
- euint256.unwrap(totalSupply)
581
- );
884
+ (bytes32 _success, bytes32 _newBalanceFrom, bytes32 _newTotalSupply) = _noxComputeContract()
885
+ .burn(
886
+ _resolveUndefinedHandle(euint256.unwrap(balanceFrom), TEEType.Uint256),
887
+ _resolveUndefinedHandle(euint256.unwrap(amount), TEEType.Uint256),
888
+ _resolveUndefinedHandle(euint256.unwrap(totalSupply), TEEType.Uint256)
889
+ );
582
890
  success = ebool.wrap(_success);
583
891
  newBalanceFrom = euint256.wrap(_newBalanceFrom);
584
892
  newTotalSupply = euint256.wrap(_newTotalSupply);
@@ -1005,7 +1313,9 @@ library Nox {
1005
1313
  ebool.unwrap(handle),
1006
1314
  decryptionProof
1007
1315
  );
1008
- return abi.decode(result, (bool));
1316
+ require(result.length == 1, MalformedDecryptedData(result));
1317
+ require(result[0] == 0x00 || result[0] == 0x01, MalformedDecryptedData(result));
1318
+ return result[0] != 0x00;
1009
1319
  }
1010
1320
 
1011
1321
  /**
@@ -1019,7 +1329,8 @@ library Nox {
1019
1329
  eaddress.unwrap(handle),
1020
1330
  decryptionProof
1021
1331
  );
1022
- return abi.decode(result, (address));
1332
+ require(result.length == 20, MalformedDecryptedData(result));
1333
+ return address(bytes20(result));
1023
1334
  }
1024
1335
 
1025
1336
  /**
@@ -1033,7 +1344,8 @@ library Nox {
1033
1344
  euint16.unwrap(handle),
1034
1345
  decryptionProof
1035
1346
  );
1036
- return abi.decode(result, (uint16));
1347
+ require(result.length == 2, MalformedDecryptedData(result));
1348
+ return uint16(bytes2(result));
1037
1349
  }
1038
1350
 
1039
1351
  /**
@@ -1047,7 +1359,8 @@ library Nox {
1047
1359
  euint256.unwrap(handle),
1048
1360
  decryptionProof
1049
1361
  );
1050
- return abi.decode(result, (uint256));
1362
+ require(result.length == 32, MalformedDecryptedData(result));
1363
+ return uint256(bytes32(result));
1051
1364
  }
1052
1365
 
1053
1366
  /**
@@ -1061,7 +1374,8 @@ library Nox {
1061
1374
  eint16.unwrap(handle),
1062
1375
  decryptionProof
1063
1376
  );
1064
- return abi.decode(result, (int16));
1377
+ require(result.length == 2, MalformedDecryptedData(result));
1378
+ return int16(uint16(bytes2(result)));
1065
1379
  }
1066
1380
 
1067
1381
  /**
@@ -1075,139 +1389,20 @@ library Nox {
1075
1389
  eint256.unwrap(handle),
1076
1390
  decryptionProof
1077
1391
  );
1078
- return abi.decode(result, (int256));
1392
+ require(result.length == 32, MalformedDecryptedData(result));
1393
+ return int256(uint256(bytes32(result)));
1079
1394
  }
1080
1395
 
1081
1396
  // ============ Private helpers ============
1082
1397
 
1083
- function _assertInitialized(bytes32 handle) private pure {
1084
- // TODO remove this function and all related asserts and use
1085
- // default zero values offchain in runner (0, address(0), ...)
1086
- // when the handle is uninitialized (0x00...00).
1087
- require(handle != bytes32(0), UninitializedHandle());
1088
- }
1089
-
1090
- function _add(bytes32 a, bytes32 b) private returns (bytes32) {
1091
- _assertInitialized(a);
1092
- _assertInitialized(b);
1093
- return _noxComputeContract().add(a, b);
1094
- }
1095
-
1096
- function _sub(bytes32 a, bytes32 b) private returns (bytes32) {
1097
- _assertInitialized(a);
1098
- _assertInitialized(b);
1099
- return _noxComputeContract().sub(a, b);
1100
- }
1101
-
1102
- function _mul(bytes32 a, bytes32 b) private returns (bytes32) {
1103
- _assertInitialized(a);
1104
- _assertInitialized(b);
1105
- return _noxComputeContract().mul(a, b);
1106
- }
1107
-
1108
- function _div(bytes32 a, bytes32 b) private returns (bytes32) {
1109
- _assertInitialized(a);
1110
- _assertInitialized(b);
1111
- return _noxComputeContract().div(a, b);
1112
- }
1113
-
1114
- function _safeAdd(bytes32 a, bytes32 b) private returns (bytes32, bytes32) {
1115
- _assertInitialized(a);
1116
- _assertInitialized(b);
1117
- return _noxComputeContract().safeAdd(a, b);
1118
- }
1119
-
1120
- function _safeSub(bytes32 a, bytes32 b) private returns (bytes32, bytes32) {
1121
- _assertInitialized(a);
1122
- _assertInitialized(b);
1123
- return _noxComputeContract().safeSub(a, b);
1124
- }
1125
-
1126
- function _safeMul(bytes32 a, bytes32 b) private returns (bytes32, bytes32) {
1127
- _assertInitialized(a);
1128
- _assertInitialized(b);
1129
- return _noxComputeContract().safeMul(a, b);
1130
- }
1131
-
1132
- function _safeDiv(bytes32 a, bytes32 b) private returns (bytes32, bytes32) {
1133
- _assertInitialized(a);
1134
- _assertInitialized(b);
1135
- return _noxComputeContract().safeDiv(a, b);
1136
- }
1137
-
1138
- function _select(bytes32 condition, bytes32 ifTrue, bytes32 ifFalse) private returns (bytes32) {
1139
- _assertInitialized(condition);
1140
- _assertInitialized(ifTrue);
1141
- _assertInitialized(ifFalse);
1142
- return _noxComputeContract().select(condition, ifTrue, ifFalse);
1143
- }
1144
-
1145
- function _eq(bytes32 a, bytes32 b) private returns (bytes32) {
1146
- _assertInitialized(a);
1147
- _assertInitialized(b);
1148
- return _noxComputeContract().eq(a, b);
1149
- }
1150
-
1151
- function _ne(bytes32 a, bytes32 b) private returns (bytes32) {
1152
- _assertInitialized(a);
1153
- _assertInitialized(b);
1154
- return _noxComputeContract().ne(a, b);
1155
- }
1156
-
1157
- function _lt(bytes32 a, bytes32 b) private returns (bytes32) {
1158
- _assertInitialized(a);
1159
- _assertInitialized(b);
1160
- return _noxComputeContract().lt(a, b);
1161
- }
1162
-
1163
- function _le(bytes32 a, bytes32 b) private returns (bytes32) {
1164
- _assertInitialized(a);
1165
- _assertInitialized(b);
1166
- return _noxComputeContract().le(a, b);
1167
- }
1168
-
1169
- function _gt(bytes32 a, bytes32 b) private returns (bytes32) {
1170
- _assertInitialized(a);
1171
- _assertInitialized(b);
1172
- return _noxComputeContract().gt(a, b);
1173
- }
1174
-
1175
- function _ge(bytes32 a, bytes32 b) private returns (bytes32) {
1176
- _assertInitialized(a);
1177
- _assertInitialized(b);
1178
- return _noxComputeContract().ge(a, b);
1179
- }
1180
-
1181
- function _transfer(
1182
- bytes32 balanceFrom,
1183
- bytes32 balanceTo,
1184
- bytes32 amount
1185
- ) private returns (bytes32, bytes32, bytes32) {
1186
- _assertInitialized(balanceFrom);
1187
- _assertInitialized(balanceTo);
1188
- _assertInitialized(amount);
1189
- return _noxComputeContract().transfer(balanceFrom, balanceTo, amount);
1190
- }
1191
-
1192
- function _mint(
1193
- bytes32 balanceTo,
1194
- bytes32 amount,
1195
- bytes32 totalSupply
1196
- ) private returns (bytes32, bytes32, bytes32) {
1197
- _assertInitialized(balanceTo);
1198
- _assertInitialized(amount);
1199
- _assertInitialized(totalSupply);
1200
- return _noxComputeContract().mint(balanceTo, amount, totalSupply);
1201
- }
1202
-
1203
- function _burn(
1204
- bytes32 balanceFrom,
1205
- bytes32 amount,
1206
- bytes32 totalSupply
1207
- ) private returns (bytes32, bytes32, bytes32) {
1208
- _assertInitialized(balanceFrom);
1209
- _assertInitialized(amount);
1210
- _assertInitialized(totalSupply);
1211
- return _noxComputeContract().burn(balanceFrom, amount, totalSupply);
1398
+ /**
1399
+ * @dev Resolves an undefined (bytes32(0)) handle to the typed zero handle for the given type.
1400
+ * If the handle is already non-zero, returns it unchanged.
1401
+ */
1402
+ function _resolveUndefinedHandle(
1403
+ bytes32 handle,
1404
+ TEEType teeType
1405
+ ) private view returns (bytes32) {
1406
+ return handle == bytes32(0) ? HandleUtils.zeroHandle(teeType) : handle;
1212
1407
  }
1213
1408
  }