@coti-io/coti-contracts 1.0.0 → 1.0.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.
Files changed (102) hide show
  1. package/.github/workflows/npm-publish.yml +2 -1
  2. package/CONTRIBUTING.md +2 -2
  3. package/LICENSE +1 -1
  4. package/README.md +18 -35
  5. package/contracts/access/DataPrivacyFramework/extensions/DataPrivacyFrameworkMpc.sol +112 -0
  6. package/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol +152 -0
  7. package/contracts/mocks/utils/mpc/BitwiseTestsContract.sol +11 -1
  8. package/contracts/mocks/utils/mpc/Comparison2TestsContract.sol +8 -0
  9. package/contracts/mocks/utils/mpc/Miscellaneous1TestsContract.sol +33 -4
  10. package/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract.sol +35 -10
  11. package/contracts/mocks/utils/mpc/ShiftTestsContract.sol +126 -0
  12. package/contracts/mocks/utils/mpc/TransferTestsContract.sol +3 -0
  13. package/contracts/mocks/utils/mpc/TransferWithAllowance64_16TestsContract.sol +222 -0
  14. package/contracts/mocks/utils/mpc/TransferWithAllowance64_32TestsContract.sol +222 -0
  15. package/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol +223 -0
  16. package/contracts/mocks/utils/mpc/TransferWithAllowance64_8TestsContract.sol +222 -0
  17. package/contracts/mocks/utils/mpc/TransferWithAllowanceScalarTestsContract.sol +265 -0
  18. package/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol +281 -0
  19. package/contracts/token/PrivateERC20/PrivateERC20.sol +1 -1
  20. package/contracts/token/PrivateERC721/IERC721Errors.sol +57 -0
  21. package/contracts/token/PrivateERC721/PrivateERC721.sol +1 -1
  22. package/contracts/utils/mpc/MpcCore.sol +40 -0
  23. package/contracts/utils/mpc/MpcInterface.sol +2 -0
  24. package/hardhat.config.ts +9 -1
  25. package/package.json +20 -4
  26. package/test/onboard/AccountOnboard.test.ts +2 -2
  27. package/test/token/PrivateERC20/PrivateERC20.test.ts +2 -2
  28. package/test/utils/accounts.ts +2 -2
  29. package/test/utils/mpc/Precompile.test.ts +24 -2
  30. package/typechain-types/@openzeppelin/contracts/interfaces/index.ts +0 -2
  31. package/typechain-types/contracts/GasTest.ts +91 -0
  32. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.ts +66 -0
  33. package/typechain-types/contracts/mocks/utils/mpc/BitwiseTestsContract.ts +6 -0
  34. package/typechain-types/contracts/mocks/utils/mpc/Comparison2TestsContract.ts +6 -0
  35. package/typechain-types/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract.ts +36 -6
  36. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowance64TestsContract.ts +251 -0
  37. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowanceTestsContract.ts +250 -0
  38. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/index.ts +5 -0
  39. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.ts +389 -0
  40. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_16TestsContract.ts +391 -0
  41. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_32TestsContract.ts +391 -0
  42. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_64TestsContract.ts +391 -0
  43. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_8TestsContract.ts +391 -0
  44. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/index.ts +5 -0
  45. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.ts +391 -0
  46. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_8TestsContract.ts +391 -0
  47. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceScalarTestsContract.ts +306 -0
  48. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferTestsContract.ts +125 -0
  49. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferWithAllowanceTestsContract.ts +125 -0
  50. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/index.ts +5 -0
  51. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.ts +296 -0
  52. package/typechain-types/contracts/mocks/utils/mpc/index.ts +7 -0
  53. package/typechain-types/contracts/token/PrivateERC721/IERC721Errors.ts +69 -0
  54. package/typechain-types/contracts/token/PrivateERC721/index.ts +1 -0
  55. package/typechain-types/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations.ts +38 -0
  56. package/typechain-types/factories/@openzeppelin/contracts/interfaces/index.ts +0 -1
  57. package/typechain-types/factories/contracts/GasTest__factory.ts +75 -0
  58. package/typechain-types/factories/contracts/mocks/access/DataPrivacyFramework/DataPrivacyFrameworkMock__factory.ts +1 -1
  59. package/typechain-types/factories/contracts/mocks/token/PrivateERC20/PrivateERC20Mock__factory.ts +1 -1
  60. package/typechain-types/factories/contracts/mocks/token/PrivateERC721/PrivateERC721URIStorageMock__factory.ts +1 -1
  61. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract__factory.ts +73 -1
  62. package/typechain-types/factories/contracts/mocks/utils/mpc/BitwiseTestsContract__factory.ts +11 -1
  63. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison1TestsContract__factory.ts +1 -1
  64. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison2TestsContract__factory.ts +11 -1
  65. package/typechain-types/factories/contracts/mocks/utils/mpc/MinMaxTestsContract__factory.ts +1 -1
  66. package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous1TestsContract__factory.ts +1 -1
  67. package/typechain-types/factories/contracts/mocks/utils/mpc/MiscellaneousTestsContract__factory.ts +1 -1
  68. package/typechain-types/factories/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract__factory.ts +26 -30
  69. package/typechain-types/factories/contracts/mocks/utils/mpc/ShiftTestsContract__factory.ts +1 -1
  70. package/typechain-types/factories/contracts/mocks/utils/mpc/StringTestsContract__factory.ts +1 -1
  71. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferScalarTestsContract__factory.ts +1 -1
  72. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferTestsContract__factory.ts +1 -1
  73. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowance64TestsContract__factory.ts +294 -0
  74. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowanceTestsContract__factory.ts +292 -0
  75. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/index.ts +5 -0
  76. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract__factory.ts +720 -0
  77. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_16TestsContract__factory.ts +724 -0
  78. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_32TestsContract__factory.ts +724 -0
  79. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_64TestsContract__factory.ts +724 -0
  80. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_8TestsContract__factory.ts +724 -0
  81. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/index.ts +5 -0
  82. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract__factory.ts +724 -0
  83. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_8TestsContract__factory.ts +724 -0
  84. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceScalarTestsContract__factory.ts +507 -0
  85. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferTestsContract__factory.ts +150 -0
  86. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferWithAllowanceTestsContract__factory.ts +150 -0
  87. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/index.ts +5 -0
  88. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract__factory.ts +434 -0
  89. package/typechain-types/factories/contracts/mocks/utils/mpc/index.ts +7 -0
  90. package/typechain-types/factories/contracts/mocks/wallet/PrivateERC20Wallet/PrivateERC20WalletMock__factory.ts +1 -1
  91. package/typechain-types/factories/contracts/onboard/AccountOnboard__factory.ts +1 -1
  92. package/typechain-types/factories/contracts/token/PrivateERC721/IERC721Errors__factory.ts +128 -0
  93. package/typechain-types/factories/contracts/token/PrivateERC721/index.ts +1 -0
  94. package/typechain-types/factories/contracts/utils/mpc/MpcCore__factory.ts +1 -1
  95. package/typechain-types/factories/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations__factory.ts +58 -0
  96. package/typechain-types/hardhat.d.ts +144 -54
  97. package/typechain-types/index.ts +16 -6
  98. package/contracts/access/DataPrivacyFramework/README.md +0 -68
  99. package/contracts/onboard/README.md +0 -65
  100. package/contracts/token/PrivateERC20/README.md +0 -104
  101. package/contracts/token/PrivateERC721/README.md +0 -282
  102. package/contracts/utils/mpc/README.md +0 -82
@@ -43,8 +43,9 @@ jobs:
43
43
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
44
  - uses: actions/setup-node@v3
45
45
  with:
46
- node-version: 16
46
+ node-version: 18
47
47
  registry-url: https://registry.npmjs.org/
48
+ scope: '@coti-io'
48
49
  - run: npm ci
49
50
  - run: npm publish --access=public
50
51
  env:
package/CONTRIBUTING.md CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
- # Contributing to COTI SDK TypeScript Examples
2
+ # Contributing to COTI Contracts
3
3
 
4
- Thank you for your interest in contributing to the COTI SDK TypeScript Examples project! We welcome contributions from the community and appreciate your efforts to improve our project.
4
+ Thank you for your interest in contributing to the COTI Contracts project! We welcome contributions from the community and appreciate your efforts to improve our project.
5
5
 
6
6
  ## Table of Contents
7
7
 
package/LICENSE CHANGED
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright [yyyy] [name of copyright owner]
189
+ Copyright 2024 COTI
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -1,48 +1,31 @@
1
- # COTI Contracts
2
-
3
- A library for smart contract development on the COTI network.
4
-
5
- > [!NOTE]
6
- > This repository is meant to replace the now-deprecated [confidentiality-contracts](https://github.com/coti-io/confidentiality-contracts) repository.
7
-
8
- #### Important Links
9
-
10
- [Docs](https://docs.coti.io) | [Discord](https://discord.gg/cuCykh8P4m) | [Faucet](https://faucet.coti.io)
11
-
12
- #### Network-Specific Links
1
+ [![image](https://img.shields.io/badge/Telegram-2CA5E0?style=for-the-badge&logo=telegram&logoColor=white)](https://telegram.coti.io)
2
+ [![image](https://img.shields.io/badge/Discord-5865F2?style=for-the-badge&logo=discord&logoColor=white)](https://discord.coti.io)
3
+ [![image](https://img.shields.io/badge/X-000000?style=for-the-badge&logo=x&logoColor=white)](https://twitter.coti.io)
4
+ [![image](https://img.shields.io/badge/YouTube-FF0000?style=for-the-badge&logo=youtube&logoColor=white)](https://youtube.coti.io)
13
5
 
14
- [Devnet Explorer](https://explorer-devnet.coti.io) | [Testnet Explorer](https://testnet.cotiscan.io)
6
+ # COTI Contracts
15
7
 
16
- ## Overview
8
+ [![image](https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white)](https://www.npmjs.com/package/@coti-io/coti-contracts)
9
+ [![image](https://img.shields.io/badge/Node%20js-339933?style=for-the-badge&logo=nodedotjs&logoColor=white)](https://nodejs.org/download/release/v18.20.5/)
17
10
 
18
- > [!NOTE]
19
- > Due to the nature of ongoing development, future versions might break existing functionality
11
+ Welcome to COTI Contracts, a library of smart contracts for COTI's GC technology, including MPC contract for secure computations, private ERC20 and ERC721 contracts, and test mocks for validation. These components enable privacy-focused and decentralized DeFi solutions.
20
12
 
21
- The following contracts are included in the library:
22
- - [DataPrivacyFramework](/contracts/access/DataPrivacyFramework/)
23
- - [AccountOnboard](/contracts/onboard/)
24
- - [PrivateERC20](/contracts/token/PrivateERC20/)
25
- - [PrivateERC721](/contracts/token/PrivateERC721/)
26
- - [MpcCore](/contracts/utils/mpc/)
13
+ ## Documentation
27
14
 
28
- ### Installation
15
+ Full API documentation is available in the [COTI docs](https://docs.coti.io/coti-v2-documentation/build-on-coti/tools/ethers.js)
29
16
 
30
- #### Hardhat (npm)
17
+ ## Build and Install
31
18
 
32
19
  ```bash
33
- npm install @coti-io/coti-contracts
20
+ npm install
34
21
  ```
35
22
 
36
- ### Usage
37
-
38
- Once installed, you can use the contracts in the library by importing them:
23
+ ## Testing
39
24
 
40
- ```solidity
41
- pragma solidity ^0.8.20;
25
+ ```bash
26
+ npx hardhat test
27
+ ```
42
28
 
43
- import {PrivateERC20} from "@coti-io/coti-contracts/token/PrivateERC20/PrivateERC20.sol";
29
+ ---
44
30
 
45
- contract MyToken is PrivateERC20 {
46
- constructor() ERC721("MyToken", "MTOK") {}
47
- }
48
- ```
31
+ To report an issue, please see the [issues](https://github.com/coti-io/coti-contracts/issues/new) tab.
@@ -123,6 +123,34 @@ abstract contract DataPrivacyFrameworkMpc is DataPrivacyFramework {
123
123
  return MpcCore.xor(a, b);
124
124
  }
125
125
 
126
+ function shl(
127
+ gtUint8 a,
128
+ uint8 b,
129
+ uint256 uintParameter,
130
+ address addressParameter,
131
+ string calldata stringParameter
132
+ )
133
+ internal
134
+ onlyAllowedUserOperation("op_shl", uintParameter, addressParameter, stringParameter)
135
+ returns (gtUint8)
136
+ {
137
+ return MpcCore.shl(a, b);
138
+ }
139
+
140
+ function shr(
141
+ gtUint8 a,
142
+ uint8 b,
143
+ uint256 uintParameter,
144
+ address addressParameter,
145
+ string calldata stringParameter
146
+ )
147
+ internal
148
+ onlyAllowedUserOperation("op_shr", uintParameter, addressParameter, stringParameter)
149
+ returns (gtUint8)
150
+ {
151
+ return MpcCore.shr(a, b);
152
+ }
153
+
126
154
  function eq(
127
155
  gtUint8 a,
128
156
  gtUint8 b,
@@ -377,6 +405,34 @@ abstract contract DataPrivacyFrameworkMpc is DataPrivacyFramework {
377
405
  return MpcCore.xor(a, b);
378
406
  }
379
407
 
408
+ function shl(
409
+ gtUint16 a,
410
+ uint16 b,
411
+ uint256 uintParameter,
412
+ address addressParameter,
413
+ string calldata stringParameter
414
+ )
415
+ internal
416
+ onlyAllowedUserOperation("op_shl", uintParameter, addressParameter, stringParameter)
417
+ returns (gtUint16)
418
+ {
419
+ return MpcCore.shl(a, b);
420
+ }
421
+
422
+ function shr(
423
+ gtUint16 a,
424
+ uint16 b,
425
+ uint256 uintParameter,
426
+ address addressParameter,
427
+ string calldata stringParameter
428
+ )
429
+ internal
430
+ onlyAllowedUserOperation("op_shr", uintParameter, addressParameter, stringParameter)
431
+ returns (gtUint16)
432
+ {
433
+ return MpcCore.shr(a, b);
434
+ }
435
+
380
436
  function eq(
381
437
  gtUint16 a,
382
438
  gtUint16 b,
@@ -631,6 +687,34 @@ abstract contract DataPrivacyFrameworkMpc is DataPrivacyFramework {
631
687
  return MpcCore.xor(a, b);
632
688
  }
633
689
 
690
+ function shl(
691
+ gtUint32 a,
692
+ uint32 b,
693
+ uint256 uintParameter,
694
+ address addressParameter,
695
+ string calldata stringParameter
696
+ )
697
+ internal
698
+ onlyAllowedUserOperation("op_shl", uintParameter, addressParameter, stringParameter)
699
+ returns (gtUint32)
700
+ {
701
+ return MpcCore.shl(a, b);
702
+ }
703
+
704
+ function shr(
705
+ gtUint32 a,
706
+ uint32 b,
707
+ uint256 uintParameter,
708
+ address addressParameter,
709
+ string calldata stringParameter
710
+ )
711
+ internal
712
+ onlyAllowedUserOperation("op_shr", uintParameter, addressParameter, stringParameter)
713
+ returns (gtUint32)
714
+ {
715
+ return MpcCore.shr(a, b);
716
+ }
717
+
634
718
  function eq(
635
719
  gtUint32 a,
636
720
  gtUint32 b,
@@ -885,6 +969,34 @@ abstract contract DataPrivacyFrameworkMpc is DataPrivacyFramework {
885
969
  return MpcCore.xor(a, b);
886
970
  }
887
971
 
972
+ function shl(
973
+ gtUint64 a,
974
+ uint64 b,
975
+ uint256 uintParameter,
976
+ address addressParameter,
977
+ string calldata stringParameter
978
+ )
979
+ internal
980
+ onlyAllowedUserOperation("op_shl", uintParameter, addressParameter, stringParameter)
981
+ returns (gtUint64)
982
+ {
983
+ return MpcCore.shl(a, b);
984
+ }
985
+
986
+ function shr(
987
+ gtUint64 a,
988
+ uint64 b,
989
+ uint256 uintParameter,
990
+ address addressParameter,
991
+ string calldata stringParameter
992
+ )
993
+ internal
994
+ onlyAllowedUserOperation("op_shr", uintParameter, addressParameter, stringParameter)
995
+ returns (gtUint64)
996
+ {
997
+ return MpcCore.shr(a, b);
998
+ }
999
+
888
1000
  function eq(
889
1001
  gtUint64 a,
890
1002
  gtUint64 b,
@@ -153,6 +153,57 @@ contract ArithmeticTestsContract {
153
153
  return result;
154
154
  }
155
155
 
156
+ function checkedAddTest(uint8 a, uint8 b) public returns (uint8) {
157
+ AllGTCastingValues memory castingValues;
158
+ Check16 memory check16;
159
+ Check32 memory check32;
160
+ Check64 memory check64;
161
+ setPublicValues(castingValues, a, b);
162
+
163
+ // Calculate the expected result
164
+ uint8 result = MpcCore.decrypt(MpcCore.checkedAdd(castingValues.a8_s, castingValues.b8_s));
165
+ addResult = result;
166
+
167
+ // Calculate the results with casting to 16
168
+ check16.res16_16 = MpcCore.checkedAdd(castingValues.a16_s, castingValues.b16_s);
169
+ check16.res8_16 = MpcCore.checkedAdd(castingValues.a8_s, castingValues.b16_s);
170
+ check16.res16_8 = MpcCore.checkedAdd(castingValues.a16_s, castingValues.b8_s);
171
+ uint16 res16 = decryptAndCompareResults16(check16);
172
+ require(res16 == result, "addTest: cast 16 failed");
173
+
174
+ // Calculate the result with casting to 32
175
+ check32.res32_32 = MpcCore.checkedAdd(castingValues.a32_s, castingValues.b32_s);
176
+ check32.res8_32 = MpcCore.checkedAdd(castingValues.a8_s, castingValues.b32_s);
177
+ check32.res32_8 = MpcCore.checkedAdd(castingValues.a32_s, castingValues.b8_s);
178
+ check32.res16_32 = MpcCore.checkedAdd(castingValues.a16_s, castingValues.b32_s);
179
+ check32.res32_16 = MpcCore.checkedAdd(castingValues.a32_s, castingValues.b16_s);
180
+ uint32 res32 = decryptAndCompareResults32(check32);
181
+ require(result == res32, "addTest: cast 32 failed");
182
+
183
+ // Calculate the result with casting to 64
184
+ check64.res64_64 = MpcCore.checkedAdd(castingValues.a64_s, castingValues.b64_s);
185
+ check64.res8_64 = MpcCore.checkedAdd(castingValues.a8_s, castingValues.b64_s);
186
+ check64.res64_8 = MpcCore.checkedAdd(castingValues.a64_s, castingValues.b8_s);
187
+ check64.res16_64 = MpcCore.checkedAdd(castingValues.a16_s, castingValues.b64_s);
188
+ check64.res64_16 = MpcCore.checkedAdd(castingValues.a64_s, castingValues.b16_s);
189
+ check64.res32_64 = MpcCore.checkedAdd(castingValues.a32_s, castingValues.b64_s);
190
+ check64.res64_32 = MpcCore.checkedAdd(castingValues.a64_s, castingValues.b32_s);
191
+ uint64 res64 = decryptAndCompareResults64(check64);
192
+ require(result == res64, "addTest: cast 64 failed");
193
+
194
+ // Check the result with scalar
195
+ require(result == MpcCore.decrypt(MpcCore.checkedAdd(a, castingValues.b8_s)) && result == MpcCore.decrypt(MpcCore.checkedAdd(castingValues.a8_s, b)),
196
+ "addTest: test 8 bits with scalar failed");
197
+ require(result == MpcCore.decrypt(MpcCore.checkedAdd(a, castingValues.b16_s)) && result == MpcCore.decrypt(MpcCore.checkedAdd(castingValues.a16_s, b)),
198
+ "addTest: test 16 bits with scalar failed");
199
+ require(result == MpcCore.decrypt(MpcCore.checkedAdd(a, castingValues.b32_s)) && result == MpcCore.decrypt(MpcCore.checkedAdd(castingValues.a32_s, b)),
200
+ "addTest: test 32 bits with scalar failed");
201
+ require(result == MpcCore.decrypt(MpcCore.checkedAdd(a, castingValues.b64_s)) && result == MpcCore.decrypt(MpcCore.checkedAdd(castingValues.a64_s, b)),
202
+ "addTest: test 64 bits with scalar failed");
203
+
204
+ return result;
205
+ }
206
+
156
207
  function subTest(uint8 a, uint8 b) public returns (uint8) {
157
208
  AllGTCastingValues memory castingValues;
158
209
  Check16 memory check16;
@@ -204,6 +255,57 @@ contract ArithmeticTestsContract {
204
255
  return result;
205
256
  }
206
257
 
258
+ function checkedSubTest(uint8 a, uint8 b) public returns (uint8) {
259
+ AllGTCastingValues memory castingValues;
260
+ Check16 memory check16;
261
+ Check32 memory check32;
262
+ Check64 memory check64;
263
+ setPublicValues(castingValues, a, b);
264
+
265
+ // Calculate the expected result
266
+ uint8 result = MpcCore.decrypt(MpcCore.checkedSub(castingValues.a8_s, castingValues.b8_s));
267
+ subResult = result;
268
+
269
+ // Calculate the results with casting to 16
270
+ check16.res16_16 = MpcCore.checkedSub(castingValues.a16_s, castingValues.b16_s);
271
+ check16.res8_16 = MpcCore.checkedSub(castingValues.a8_s, castingValues.b16_s);
272
+ check16.res16_8 = MpcCore.checkedSub(castingValues.a16_s, castingValues.b8_s);
273
+ uint16 res16 = decryptAndCompareResults16(check16);
274
+ require(res16 == result, "subTest: cast 16 failed");
275
+
276
+ // Calculate the result with casting to 32
277
+ check32.res32_32 = MpcCore.checkedSub(castingValues.a32_s, castingValues.b32_s);
278
+ check32.res8_32 = MpcCore.checkedSub(castingValues.a8_s, castingValues.b32_s);
279
+ check32.res32_8 = MpcCore.checkedSub(castingValues.a32_s, castingValues.b8_s);
280
+ check32.res16_32 = MpcCore.checkedSub(castingValues.a16_s, castingValues.b32_s);
281
+ check32.res32_16 = MpcCore.checkedSub(castingValues.a32_s, castingValues.b16_s);
282
+ uint32 res32 = decryptAndCompareResults32(check32);
283
+ require(result == res32, "subTest: cast 32 failed");
284
+
285
+ // Calculate the result with casting to 64
286
+ check64.res64_64 = MpcCore.checkedSub(castingValues.a64_s, castingValues.b64_s);
287
+ check64.res8_64 = MpcCore.checkedSub(castingValues.a8_s, castingValues.b64_s);
288
+ check64.res64_8 = MpcCore.checkedSub(castingValues.a64_s, castingValues.b8_s);
289
+ check64.res16_64 = MpcCore.checkedSub(castingValues.a16_s, castingValues.b64_s);
290
+ check64.res64_16 = MpcCore.checkedSub(castingValues.a64_s, castingValues.b16_s);
291
+ check64.res32_64 = MpcCore.checkedSub(castingValues.a32_s, castingValues.b64_s);
292
+ check64.res64_32 = MpcCore.checkedSub(castingValues.a64_s, castingValues.b32_s);
293
+ uint64 res64 = decryptAndCompareResults64(check64);
294
+ require(result == res64, "subTest: cast 64 failed");
295
+
296
+ // Check the result with scalar
297
+ require(result == MpcCore.decrypt(MpcCore.checkedSub(a, castingValues.b8_s)) && result == MpcCore.decrypt(MpcCore.checkedSub(castingValues.a8_s, b)),
298
+ "subTest: test 8 bits with scalar failed");
299
+ require(result == MpcCore.decrypt(MpcCore.checkedSub(a, castingValues.b16_s)) && result == MpcCore.decrypt(MpcCore.checkedSub(castingValues.a16_s, b)),
300
+ "subTest: test 16 bits with scalar failed");
301
+ require(result == MpcCore.decrypt(MpcCore.checkedSub(a, castingValues.b32_s)) && result == MpcCore.decrypt(MpcCore.checkedSub(castingValues.a32_s, b)),
302
+ "subTest: test 32 bits with scalar failed");
303
+ require(result == MpcCore.decrypt(MpcCore.checkedSub(a, castingValues.b64_s)) && result == MpcCore.decrypt(MpcCore.checkedSub(castingValues.a64_s, b)),
304
+ "subTest: test 64 bits with scalar failed");
305
+
306
+ return result;
307
+ }
308
+
207
309
  function mulTest(uint8 a, uint8 b) public returns (uint8) {
208
310
  AllGTCastingValues memory castingValues;
209
311
  Check16 memory check16;
@@ -253,4 +355,54 @@ contract ArithmeticTestsContract {
253
355
 
254
356
  return mulResult;
255
357
  }
358
+
359
+ function checkedMulTest(uint8 a, uint8 b) public returns (uint8) {
360
+ AllGTCastingValues memory castingValues;
361
+ Check16 memory check16;
362
+ Check32 memory check32;
363
+ Check64 memory check64;
364
+ setPublicValues(castingValues, a, b);
365
+
366
+ // Calculate the expected result
367
+ mulResult = MpcCore.decrypt(MpcCore.checkedMul(castingValues.a8_s, castingValues.b8_s));
368
+
369
+ // Calculate the result with casting to 16
370
+ check16.res16_16 = MpcCore.checkedMul(castingValues.a16_s, castingValues.b16_s);
371
+ check16.res8_16 = MpcCore.checkedMul(castingValues.a8_s, castingValues.b16_s);
372
+ check16.res16_8 = MpcCore.checkedMul(castingValues.a16_s, castingValues.b8_s);
373
+ uint16 res16 = decryptAndCompareResults16(check16);
374
+ require(mulResult == res16, "mulTest: cast 16 failed");
375
+
376
+ // Calculate the result with casting to 32
377
+ check32.res32_32 = MpcCore.checkedMul(castingValues.a32_s, castingValues.b32_s);
378
+ check32.res8_32 = MpcCore.checkedMul(castingValues.a8_s, castingValues.b32_s);
379
+ check32.res32_8 = MpcCore.checkedMul(castingValues.a32_s, castingValues.b8_s);
380
+ check32.res16_32 = MpcCore.checkedMul(castingValues.a16_s, castingValues.b32_s);
381
+ check32.res32_16 = MpcCore.checkedMul(castingValues.a32_s, castingValues.b16_s);
382
+ uint32 res32 = decryptAndCompareResults32(check32);
383
+ require(mulResult == res32, "mulTest: cast 32 failed");
384
+
385
+ // Calculate the result with casting to 64
386
+ check64.res64_64 = MpcCore.checkedMul(castingValues.a64_s, castingValues.b64_s);
387
+ check64.res8_64 = MpcCore.checkedMul(castingValues.a8_s, castingValues.b64_s);
388
+ check64.res64_8 = MpcCore.checkedMul(castingValues.a64_s, castingValues.b8_s);
389
+ check64.res16_64 = MpcCore.checkedMul(castingValues.a16_s, castingValues.b64_s);
390
+ check64.res64_16 = MpcCore.checkedMul(castingValues.a64_s, castingValues.b16_s);
391
+ check64.res32_64 = MpcCore.checkedMul(castingValues.a32_s, castingValues.b64_s);
392
+ check64.res64_32 = MpcCore.checkedMul(castingValues.a64_s, castingValues.b32_s);
393
+ uint64 res64 = decryptAndCompareResults64(check64);
394
+ require(mulResult == res64, "mulTest: cast 64 failed");
395
+
396
+ // Check the result with scalar
397
+ require(mulResult == MpcCore.decrypt(MpcCore.checkedMul(a, castingValues.b8_s)) && mulResult == MpcCore.decrypt(MpcCore.checkedMul(castingValues.a8_s, b)),
398
+ "mulTest: test 8 bits with scalar failed");
399
+ require(mulResult == MpcCore.decrypt(MpcCore.checkedMul(a, castingValues.b16_s)) && mulResult == MpcCore.decrypt(MpcCore.checkedMul(castingValues.a16_s, b)),
400
+ "mulTest: test 16 bits with scalar failed");
401
+ require(mulResult == MpcCore.decrypt(MpcCore.checkedMul(a, castingValues.b32_s)) && mulResult == MpcCore.decrypt(MpcCore.checkedMul(castingValues.a32_s, b)),
402
+ "mulTest: test 32 bits with scalar failed");
403
+ require(mulResult == MpcCore.decrypt(MpcCore.checkedMul(a, castingValues.b64_s)) && mulResult == MpcCore.decrypt(MpcCore.checkedMul(castingValues.a64_s, b)),
404
+ "mulTest: test 64 bits with scalar failed");
405
+
406
+ return mulResult;
407
+ }
256
408
  }
@@ -6,6 +6,8 @@ import "../../../utils/mpc/MpcCore.sol";
6
6
  contract BitwiseTestsContract {
7
7
 
8
8
  struct AllGTCastingValues {
9
+ gtBool aBool_s;
10
+ gtBool bBool_s;
9
11
  gtUint8 a8_s;
10
12
  gtUint8 b8_s;
11
13
  gtUint16 a16_s;
@@ -54,7 +56,9 @@ contract BitwiseTestsContract {
54
56
  return xorResult;
55
57
  }
56
58
 
57
- function setPublicValues(AllGTCastingValues memory castingValues, uint8 a, uint8 b) public{
59
+ function setPublicValues(AllGTCastingValues memory castingValues, uint8 a, uint8 b) public {
60
+ castingValues.aBool_s = MpcCore.setPublic(a > b);
61
+ castingValues.bBool_s = MpcCore.setPublic(b > a);
58
62
  castingValues.a8_s = MpcCore.setPublic8(a);
59
63
  castingValues.b8_s = MpcCore.setPublic8(b);
60
64
  castingValues.a16_s = MpcCore.setPublic16(a);
@@ -108,6 +112,8 @@ contract BitwiseTestsContract {
108
112
  Check64 memory check64;
109
113
  setPublicValues(castingValues, a, b);
110
114
 
115
+ MpcCore.decrypt(MpcCore.and(castingValues.aBool_s, castingValues.bBool_s));
116
+
111
117
  // Calculate the expected result
112
118
  uint8 result = MpcCore.decrypt(MpcCore.and(castingValues.a8_s, castingValues.b8_s));
113
119
  andResult = result;
@@ -159,6 +165,8 @@ contract BitwiseTestsContract {
159
165
  Check64 memory check64;
160
166
  setPublicValues(castingValues, a, b);
161
167
 
168
+ MpcCore.decrypt(MpcCore.or(castingValues.aBool_s, castingValues.bBool_s));
169
+
162
170
  // Calculate the expected result
163
171
  uint8 result = MpcCore.decrypt(MpcCore.or(castingValues.a8_s, castingValues.b8_s));
164
172
  orResult = result;
@@ -210,6 +218,8 @@ contract BitwiseTestsContract {
210
218
  Check64 memory check64;
211
219
  setPublicValues(castingValues, a, b);
212
220
 
221
+ MpcCore.decrypt(MpcCore.xor(castingValues.aBool_s, castingValues.bBool_s));
222
+
213
223
  // Calculate the expected result
214
224
  uint8 result = MpcCore.decrypt(MpcCore.xor(castingValues.a8_s, castingValues.b8_s));
215
225
  xorResult = result;
@@ -6,6 +6,8 @@ import "../../../utils/mpc/MpcCore.sol";
6
6
  contract Comparison2TestsContract {
7
7
 
8
8
  struct AllGTCastingValues {
9
+ gtBool aBool_s;
10
+ gtBool bBool_s;
9
11
  gtUint8 a8_s;
10
12
  gtUint8 b8_s;
11
13
  gtUint16 a16_s;
@@ -41,6 +43,8 @@ contract Comparison2TestsContract {
41
43
  }
42
44
 
43
45
  function setPublicValues(AllGTCastingValues memory castingValues, uint8 a, uint8 b) public{
46
+ castingValues.aBool_s = MpcCore.setPublic(a > b);
47
+ castingValues.bBool_s = MpcCore.setPublic(b > a);
44
48
  castingValues.a8_s = MpcCore.setPublic8(a);
45
49
  castingValues.b8_s = MpcCore.setPublic8(b);
46
50
  castingValues.a16_s = MpcCore.setPublic16(a);
@@ -110,6 +114,8 @@ contract Comparison2TestsContract {
110
114
  Check64 memory check64;
111
115
  setPublicValues(castingValues, a, b);
112
116
 
117
+ MpcCore.decrypt(MpcCore.eq(castingValues.aBool_s, castingValues.bBool_s));
118
+
113
119
  // Calculate the expected result
114
120
  bool result = MpcCore.decrypt(MpcCore.eq(castingValues.a8_s, castingValues.b8_s));
115
121
  eqResult = result;
@@ -161,6 +167,8 @@ contract Comparison2TestsContract {
161
167
  Check64 memory check64;
162
168
  setPublicValues(castingValues, a, b);
163
169
 
170
+ MpcCore.decrypt(MpcCore.ne(castingValues.aBool_s, castingValues.bBool_s));
171
+
164
172
  // Calculate the expected result
165
173
  bool result = MpcCore.decrypt(MpcCore.ne(castingValues.a8_s, castingValues.b8_s));
166
174
  neResult = result;
@@ -34,12 +34,24 @@ contract Miscellaneous1TestsContract {
34
34
  return validateCiphertextRes;
35
35
  }
36
36
 
37
+ uint constant MAX_SIZE_1_BIT = 10;
37
38
  uint constant MAX_SIZE_8_BITS = 10;
38
39
  uint constant MAX_SIZE_16_BITS = 3;
39
40
  uint constant MAX_SIZE_32_BITS = 3;
40
41
  uint constant MAX_SIZE_64_BITS = 2;
41
42
  uint constant MAX_BOOL_SIZE = 40;
42
43
 
44
+ function checkNotAllEqual(bool[MAX_SIZE_1_BIT] memory randoms, uint size) private {
45
+ // Count how many randoms are equal
46
+ uint numEqual = 1;
47
+ for (uint i = 1; i < size; i++) {
48
+ if (randoms[0] == randoms[i]){
49
+ numEqual++;
50
+ }
51
+ }
52
+ require(numEqual != size, "randomTest: random failed, all values are the same");
53
+ }
54
+
43
55
  function checkNotAllEqual(uint64[MAX_SIZE_8_BITS] memory randoms, uint size) private {
44
56
  // Count how many randoms are equal
45
57
  uint numEqual = 1;
@@ -66,8 +78,28 @@ contract Miscellaneous1TestsContract {
66
78
  }
67
79
 
68
80
  function randTest_(bool isBounded, uint8 numBits) public returns (uint64) {
69
- uint size = MAX_SIZE_8_BITS;
81
+ uint size = MAX_SIZE_1_BIT;
82
+ bool[MAX_SIZE_1_BIT] memory randomBools;
83
+
84
+ if (!isBounded) {
85
+ // Generate gtBool randoms
86
+ for (uint i = 0; i < size; i++) {
87
+ randomBools[i] = MpcCore.decrypt(MpcCore.rand());
88
+ }
89
+
90
+ // Check that not all the generated random values are the same
91
+ checkNotAllEqual(randomBools, size);
92
+ }
93
+
70
94
  uint64[MAX_SIZE_8_BITS] memory randoms;
95
+
96
+ // In case of bounded random, the bit size does not matter because the bounded bits can be small.
97
+ // So the max size remain as in 8 bits.
98
+ // In case of unbounded random, max size can be reduced.
99
+ if (!isBounded){
100
+ size = MAX_SIZE_8_BITS;
101
+ }
102
+
71
103
  // Generate gtUint8 randoms
72
104
  for (uint i = 0; i < size; i++) {
73
105
  if(!isBounded){
@@ -84,9 +116,6 @@ contract Miscellaneous1TestsContract {
84
116
  // Check that not all the generated random values are the same
85
117
  checkNotAllEqual(randoms, size);
86
118
 
87
- // In case of bounded random, the bit size does not matter because the bounded bits can be small.
88
- // So the max size remain as in 8 bits.
89
- // In case of unbounded random, max size can be reduced.
90
119
  if (!isBounded){
91
120
  size = MAX_SIZE_16_BITS;
92
121
  }
@@ -11,6 +11,7 @@ contract OffboardToUserKeyTestContract {
11
11
  uint8 x;
12
12
  ctUint8 ctUserKey;
13
13
 
14
+ uint256 ct;
14
15
  uint256 ct8;
15
16
  uint256 ct16;
16
17
  uint256 ct32;
@@ -22,8 +23,8 @@ contract OffboardToUserKeyTestContract {
22
23
  return onboardOffboardResult;
23
24
  }
24
25
 
25
- function getCTs() public view returns (uint256, uint256, uint256, uint256) {
26
- return (ct8, ct16, ct32, ct64);
26
+ function getCTs() public view returns (uint256, uint256, uint256, uint256, uint256) {
27
+ return (ct, ct8, ct16, ct32, ct64);
27
28
  }
28
29
 
29
30
  function getUserKeyTest(bytes calldata signedEK, bytes calldata signature, address addr) public returns (uint8) {
@@ -56,34 +57,60 @@ contract OffboardToUserKeyTestContract {
56
57
  return (keyShare0, keyShare1);
57
58
  }
58
59
 
59
- function offboardToUserTest(uint8 a, address addr) public returns (uint256, uint256, uint256, uint256) {
60
+ function offboardToUserTest(uint8 a, address addr) public {
61
+ gtBool aBool_s = MpcCore.setPublic(a > 0);
60
62
  gtUint8 a8_s = MpcCore.setPublic8(a);
61
63
  gtUint16 a16_s = MpcCore.setPublic16(a);
62
64
  gtUint32 a32_s = MpcCore.setPublic32(a);
63
65
  gtUint64 a64_s = MpcCore.setPublic64(a);
64
66
 
67
+ ctBool cipherBool = MpcCore.offBoardToUser(aBool_s, addr);
65
68
  ctUint8 cipher8 = MpcCore.offBoardToUser(a8_s, addr);
66
69
  ctUint16 cipher16 = MpcCore.offBoardToUser(a16_s, addr);
67
70
  ctUint32 cipher32 = MpcCore.offBoardToUser(a32_s, addr);
68
71
  ctUint64 cipher64 = MpcCore.offBoardToUser(a64_s, addr);
69
72
 
73
+ ct = ctBool.unwrap(cipherBool);
70
74
  ct8 = ctUint8.unwrap(cipher8);
71
75
  ct16 = ctUint16.unwrap(cipher16);
72
76
  ct32 = ctUint32.unwrap(cipher32);
73
77
  ct64 = ctUint64.unwrap(cipher64);
78
+ }
79
+
80
+ function offboardCombinedTest(uint8 a, address addr) public {
81
+ gtBool aBool_s = MpcCore.setPublic(a > 0);
82
+ gtUint8 a8_s = MpcCore.setPublic8(a);
83
+ gtUint16 a16_s = MpcCore.setPublic16(a);
84
+ gtUint32 a32_s = MpcCore.setPublic32(a);
85
+ gtUint64 a64_s = MpcCore.setPublic64(a);
74
86
 
75
- return (ct8, ct16, ct32, ct64);
87
+ utBool memory cipherBool = MpcCore.offBoardCombined(aBool_s, addr);
88
+ utUint8 memory cipher8 = MpcCore.offBoardCombined(a8_s, addr);
89
+ utUint16 memory cipher16 = MpcCore.offBoardCombined(a16_s, addr);
90
+ utUint32 memory cipher32 = MpcCore.offBoardCombined(a32_s, addr);
91
+ utUint64 memory cipher64 = MpcCore.offBoardCombined(a64_s, addr);
92
+
93
+ ct = ctBool.unwrap(cipherBool.userCiphertext);
94
+ ct8 = ctUint8.unwrap(cipher8.userCiphertext);
95
+ ct16 = ctUint16.unwrap(cipher16.userCiphertext);
96
+ ct32 = ctUint32.unwrap(cipher32.userCiphertext);
97
+ ct64 = ctUint64.unwrap(cipher64.userCiphertext);
76
98
  }
77
99
 
78
- function offboardOnboardTest(uint8 a8, uint16 a16, uint32 a32, uint32 a64) public returns (uint8) {
100
+ function offboardOnboardTest(uint8 a8, uint16 a16, uint32 a32, uint32 a64) public {
101
+ bool temp = a8 > 0;
102
+ gtBool aBool_s = MpcCore.setPublic(temp);
79
103
  gtUint8 a8_s = MpcCore.setPublic8(a8);
80
104
  gtUint16 a16_s = MpcCore.setPublic16(a16);
81
105
  gtUint32 a32_s = MpcCore.setPublic32(a32);
82
106
  gtUint64 a64_s = MpcCore.setPublic64(a64);
83
107
 
108
+ ctBool cipher = MpcCore.offBoard(aBool_s);
109
+ bool result = MpcCore.decrypt(MpcCore.onBoard(cipher));
110
+
84
111
  ctUint8 cipher8 = MpcCore.offBoard(a8_s);
85
- uint8 result = MpcCore.decrypt(MpcCore.onBoard(cipher8));
86
- onboardOffboardResult = result;
112
+ uint8 result8 = MpcCore.decrypt(MpcCore.onBoard(cipher8));
113
+ onboardOffboardResult = result8;
87
114
 
88
115
  ctUint16 cipher16 = MpcCore.offBoard(a16_s);
89
116
  uint16 result16 = MpcCore.decrypt(MpcCore.onBoard(cipher16));
@@ -94,9 +121,7 @@ contract OffboardToUserKeyTestContract {
94
121
  ctUint64 cipher64 = MpcCore.offBoard(a64_s);
95
122
  uint64 result64 = MpcCore.decrypt(MpcCore.onBoard(cipher64));
96
123
 
97
- require(result == result16 && result == result32 && result == result64,
124
+ require(result == temp && result8 == result16 && result8 == result32 && result8 == result64,
98
125
  "Failed to offboard and onboard all values");
99
-
100
- return result;
101
126
  }
102
127
  }