@coti-io/coti-contracts 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.
Files changed (180) hide show
  1. package/.github/workflows/npm-publish.yml +52 -0
  2. package/CODE_OF_CONDUCT.md +44 -0
  3. package/CONTRIBUTING.md +101 -0
  4. package/LICENSE +201 -0
  5. package/README.md +48 -0
  6. package/contracts/access/DataPrivacyFramework/DataPrivacyFramework.sol +480 -0
  7. package/contracts/access/DataPrivacyFramework/README.md +68 -0
  8. package/contracts/access/DataPrivacyFramework/extensions/DataPrivacyFrameworkMpc.sol +1139 -0
  9. package/contracts/mocks/README.md +5 -0
  10. package/contracts/mocks/access/DataPrivacyFramework/DataPrivacyFrameworkMock.sol +9 -0
  11. package/contracts/mocks/token/PrivateERC20/PrivateERC20Mock.sol +17 -0
  12. package/contracts/mocks/token/PrivateERC721/PrivateERC721URIStorageMock.sol +34 -0
  13. package/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol +431 -0
  14. package/contracts/mocks/utils/mpc/BitwiseTestsContract.sol +361 -0
  15. package/contracts/mocks/utils/mpc/Comparison1Tests.Contract.sol +307 -0
  16. package/contracts/mocks/utils/mpc/Comparison2TestsContract.sol +307 -0
  17. package/contracts/mocks/utils/mpc/MinMaxTestsContract.sol +297 -0
  18. package/contracts/mocks/utils/mpc/Miscellaneous1TestsContract.sol +185 -0
  19. package/contracts/mocks/utils/mpc/Miscellaneous2TestsContract.sol +448 -0
  20. package/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol +81 -0
  21. package/contracts/mocks/utils/mpc/README.md +486 -0
  22. package/contracts/mocks/utils/mpc/ShiftTestsContract.sol +311 -0
  23. package/contracts/mocks/utils/mpc/StringTestsContract.sol +59 -0
  24. package/contracts/mocks/utils/mpc/TransferScalarTestsContract.sol +271 -0
  25. package/contracts/mocks/utils/mpc/TransferTestsContract.sol +715 -0
  26. package/contracts/mocks/wallet/PrivateERC20Wallet/PrivateERC20WalletMock.sol +25 -0
  27. package/contracts/onboard/AccountOnboard.sol +21 -0
  28. package/contracts/onboard/README.md +65 -0
  29. package/contracts/package.json +11 -0
  30. package/contracts/token/PrivateERC20/IPrivateERC20.sol +142 -0
  31. package/contracts/token/PrivateERC20/PrivateERC20.sol +478 -0
  32. package/contracts/token/PrivateERC20/README.md +104 -0
  33. package/contracts/token/PrivateERC721/PrivateERC721.sol +458 -0
  34. package/contracts/token/PrivateERC721/README.md +282 -0
  35. package/contracts/token/PrivateERC721/extensions/PrivateERC721URIStorage.sol +87 -0
  36. package/contracts/utils/mpc/MpcCore.sol +3215 -0
  37. package/contracts/utils/mpc/MpcInterface.sol +41 -0
  38. package/contracts/utils/mpc/README.md +82 -0
  39. package/hardhat.config.ts +18 -0
  40. package/package.json +14 -0
  41. package/test/access/DataPrivacyFramework/DataPrivacyFramework.test.ts +780 -0
  42. package/test/onboard/AccountOnboard.test.ts +45 -0
  43. package/test/token/PrivateERC20/PrivateERC20.test.ts +546 -0
  44. package/test/token/PrivateERC721/PrivateERC721.test.ts +223 -0
  45. package/test/utils/accounts.ts +55 -0
  46. package/test/utils/mpc/Precompile.test.ts +113 -0
  47. package/test/utils/mpc/String.test.ts +227 -0
  48. package/tsconfig.json +11 -0
  49. package/typechain-types/@openzeppelin/contracts/index.ts +9 -0
  50. package/typechain-types/@openzeppelin/contracts/interfaces/IERC4906.ts +462 -0
  51. package/typechain-types/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/IERC1155Errors.ts +69 -0
  52. package/typechain-types/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/IERC20Errors.ts +69 -0
  53. package/typechain-types/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/IERC721Errors.ts +69 -0
  54. package/typechain-types/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/index.ts +6 -0
  55. package/typechain-types/@openzeppelin/contracts/interfaces/index.ts +6 -0
  56. package/typechain-types/@openzeppelin/contracts/token/ERC721/IERC721.ts +393 -0
  57. package/typechain-types/@openzeppelin/contracts/token/ERC721/IERC721Receiver.ts +110 -0
  58. package/typechain-types/@openzeppelin/contracts/token/ERC721/index.ts +5 -0
  59. package/typechain-types/@openzeppelin/contracts/token/index.ts +5 -0
  60. package/typechain-types/@openzeppelin/contracts/utils/index.ts +5 -0
  61. package/typechain-types/@openzeppelin/contracts/utils/introspection/ERC165.ts +94 -0
  62. package/typechain-types/@openzeppelin/contracts/utils/introspection/IERC165.ts +94 -0
  63. package/typechain-types/@openzeppelin/contracts/utils/introspection/index.ts +5 -0
  64. package/typechain-types/@openzeppelin/index.ts +5 -0
  65. package/typechain-types/common.ts +131 -0
  66. package/typechain-types/contracts/access/DataPrivacyFramework/DataPrivacyFramework.ts +598 -0
  67. package/typechain-types/contracts/access/DataPrivacyFramework/extensions/DataPrivacyFrameworkMpc.ts +598 -0
  68. package/typechain-types/contracts/access/DataPrivacyFramework/extensions/index.ts +4 -0
  69. package/typechain-types/contracts/access/DataPrivacyFramework/index.ts +6 -0
  70. package/typechain-types/contracts/access/index.ts +5 -0
  71. package/typechain-types/contracts/index.ts +13 -0
  72. package/typechain-types/contracts/mocks/access/DataPrivacyFramework/DataPrivacyFrameworkMock.ts +598 -0
  73. package/typechain-types/contracts/mocks/access/DataPrivacyFramework/index.ts +4 -0
  74. package/typechain-types/contracts/mocks/access/index.ts +5 -0
  75. package/typechain-types/contracts/mocks/index.ts +11 -0
  76. package/typechain-types/contracts/mocks/token/PrivateERC20/PrivateERC20Mock.ts +543 -0
  77. package/typechain-types/contracts/mocks/token/PrivateERC20/index.ts +4 -0
  78. package/typechain-types/contracts/mocks/token/PrivateERC721/PrivateERC721URIStorageMock.ts +576 -0
  79. package/typechain-types/contracts/mocks/token/PrivateERC721/index.ts +4 -0
  80. package/typechain-types/contracts/mocks/token/index.ts +7 -0
  81. package/typechain-types/contracts/mocks/utils/index.ts +5 -0
  82. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.ts +354 -0
  83. package/typechain-types/contracts/mocks/utils/mpc/BitwiseTestsContract.ts +340 -0
  84. package/typechain-types/contracts/mocks/utils/mpc/Comparison1Tests.Contract.sol/Comparison1TestsContract.ts +340 -0
  85. package/typechain-types/contracts/mocks/utils/mpc/Comparison1Tests.Contract.sol/index.ts +4 -0
  86. package/typechain-types/contracts/mocks/utils/mpc/Comparison2TestsContract.ts +340 -0
  87. package/typechain-types/contracts/mocks/utils/mpc/MinMaxTestsContract.ts +321 -0
  88. package/typechain-types/contracts/mocks/utils/mpc/Miscellaneous1TestsContract.ts +199 -0
  89. package/typechain-types/contracts/mocks/utils/mpc/Miscellaneous2TestsContract.ts +384 -0
  90. package/typechain-types/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/OffboardToUserKeyTestContract.ts +218 -0
  91. package/typechain-types/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/index.ts +4 -0
  92. package/typechain-types/contracts/mocks/utils/mpc/ShiftTestsContract.ts +339 -0
  93. package/typechain-types/contracts/mocks/utils/mpc/StringTestsContract.ts +211 -0
  94. package/typechain-types/contracts/mocks/utils/mpc/TransferScalarTestsContract.ts +266 -0
  95. package/typechain-types/contracts/mocks/utils/mpc/TransferTestsContract.ts +288 -0
  96. package/typechain-types/contracts/mocks/utils/mpc/index.ts +17 -0
  97. package/typechain-types/contracts/mocks/wallet/PrivateERC20Wallet/PrivateERC20WalletMock.ts +173 -0
  98. package/typechain-types/contracts/mocks/wallet/PrivateERC20Wallet/index.ts +4 -0
  99. package/typechain-types/contracts/mocks/wallet/index.ts +5 -0
  100. package/typechain-types/contracts/onboard/AccountOnboard.ts +135 -0
  101. package/typechain-types/contracts/onboard/index.ts +4 -0
  102. package/typechain-types/contracts/token/PrivateERC20/IPrivateERC20.ts +437 -0
  103. package/typechain-types/contracts/token/PrivateERC20/PrivateERC20.ts +505 -0
  104. package/typechain-types/contracts/token/PrivateERC20/index.ts +5 -0
  105. package/typechain-types/contracts/token/PrivateERC721/PrivateERC721.ts +409 -0
  106. package/typechain-types/contracts/token/PrivateERC721/extensions/PrivateERC721URIStorage.ts +501 -0
  107. package/typechain-types/contracts/token/PrivateERC721/extensions/index.ts +4 -0
  108. package/typechain-types/contracts/token/PrivateERC721/index.ts +6 -0
  109. package/typechain-types/contracts/token/index.ts +7 -0
  110. package/typechain-types/contracts/utils/index.ts +5 -0
  111. package/typechain-types/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations.ts +698 -0
  112. package/typechain-types/contracts/utils/mpc/MpcInterface.sol/index.ts +4 -0
  113. package/typechain-types/contracts/utils/mpc/index.ts +5 -0
  114. package/typechain-types/factories/@openzeppelin/contracts/index.ts +6 -0
  115. package/typechain-types/factories/@openzeppelin/contracts/interfaces/IERC4906__factory.ts +339 -0
  116. package/typechain-types/factories/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/IERC1155Errors__factory.ts +127 -0
  117. package/typechain-types/factories/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/IERC20Errors__factory.ts +111 -0
  118. package/typechain-types/factories/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/IERC721Errors__factory.ts +128 -0
  119. package/typechain-types/factories/@openzeppelin/contracts/interfaces/draft-IERC6093.sol/index.ts +6 -0
  120. package/typechain-types/factories/@openzeppelin/contracts/interfaces/index.ts +5 -0
  121. package/typechain-types/factories/@openzeppelin/contracts/token/ERC721/IERC721Receiver__factory.ts +59 -0
  122. package/typechain-types/factories/@openzeppelin/contracts/token/ERC721/IERC721__factory.ts +307 -0
  123. package/typechain-types/factories/@openzeppelin/contracts/token/ERC721/index.ts +5 -0
  124. package/typechain-types/factories/@openzeppelin/contracts/token/index.ts +4 -0
  125. package/typechain-types/factories/@openzeppelin/contracts/utils/index.ts +4 -0
  126. package/typechain-types/factories/@openzeppelin/contracts/utils/introspection/ERC165__factory.ts +41 -0
  127. package/typechain-types/factories/@openzeppelin/contracts/utils/introspection/IERC165__factory.ts +41 -0
  128. package/typechain-types/factories/@openzeppelin/contracts/utils/introspection/index.ts +5 -0
  129. package/typechain-types/factories/@openzeppelin/index.ts +4 -0
  130. package/typechain-types/factories/contracts/access/DataPrivacyFramework/DataPrivacyFramework__factory.ts +621 -0
  131. package/typechain-types/factories/contracts/access/DataPrivacyFramework/extensions/DataPrivacyFrameworkMpc__factory.ts +621 -0
  132. package/typechain-types/factories/contracts/access/DataPrivacyFramework/extensions/index.ts +4 -0
  133. package/typechain-types/factories/contracts/access/DataPrivacyFramework/index.ts +5 -0
  134. package/typechain-types/factories/contracts/access/index.ts +4 -0
  135. package/typechain-types/factories/contracts/index.ts +8 -0
  136. package/typechain-types/factories/contracts/mocks/access/DataPrivacyFramework/DataPrivacyFrameworkMock__factory.ts +670 -0
  137. package/typechain-types/factories/contracts/mocks/access/DataPrivacyFramework/index.ts +4 -0
  138. package/typechain-types/factories/contracts/mocks/access/index.ts +4 -0
  139. package/typechain-types/factories/contracts/mocks/index.ts +7 -0
  140. package/typechain-types/factories/contracts/mocks/token/PrivateERC20/PrivateERC20Mock__factory.ts +615 -0
  141. package/typechain-types/factories/contracts/mocks/token/PrivateERC20/index.ts +4 -0
  142. package/typechain-types/factories/contracts/mocks/token/PrivateERC721/PrivateERC721URIStorageMock__factory.ts +630 -0
  143. package/typechain-types/factories/contracts/mocks/token/PrivateERC721/index.ts +4 -0
  144. package/typechain-types/factories/contracts/mocks/token/index.ts +5 -0
  145. package/typechain-types/factories/contracts/mocks/utils/index.ts +4 -0
  146. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract__factory.ts +374 -0
  147. package/typechain-types/factories/contracts/mocks/utils/mpc/BitwiseTestsContract__factory.ts +361 -0
  148. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison1Tests.Contract.sol/Comparison1TestsContract__factory.ts +361 -0
  149. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison1Tests.Contract.sol/index.ts +4 -0
  150. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison2TestsContract__factory.ts +361 -0
  151. package/typechain-types/factories/contracts/mocks/utils/mpc/MinMaxTestsContract__factory.ts +337 -0
  152. package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous1TestsContract__factory.ts +236 -0
  153. package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous2TestsContract__factory.ts +432 -0
  154. package/typechain-types/factories/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/OffboardToUserKeyTestContract__factory.ts +245 -0
  155. package/typechain-types/factories/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/index.ts +4 -0
  156. package/typechain-types/factories/contracts/mocks/utils/mpc/ShiftTestsContract__factory.ts +374 -0
  157. package/typechain-types/factories/contracts/mocks/utils/mpc/StringTestsContract__factory.ts +264 -0
  158. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferScalarTestsContract__factory.ts +360 -0
  159. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferTestsContract__factory.ts +441 -0
  160. package/typechain-types/factories/contracts/mocks/utils/mpc/index.ts +15 -0
  161. package/typechain-types/factories/contracts/mocks/wallet/PrivateERC20Wallet/PrivateERC20WalletMock__factory.ts +194 -0
  162. package/typechain-types/factories/contracts/mocks/wallet/PrivateERC20Wallet/index.ts +4 -0
  163. package/typechain-types/factories/contracts/mocks/wallet/index.ts +4 -0
  164. package/typechain-types/factories/contracts/onboard/AccountOnboard__factory.ts +104 -0
  165. package/typechain-types/factories/contracts/onboard/index.ts +4 -0
  166. package/typechain-types/factories/contracts/token/PrivateERC20/IPrivateERC20__factory.ts +406 -0
  167. package/typechain-types/factories/contracts/token/PrivateERC20/PrivateERC20__factory.ts +532 -0
  168. package/typechain-types/factories/contracts/token/PrivateERC20/index.ts +5 -0
  169. package/typechain-types/factories/contracts/token/PrivateERC721/PrivateERC721__factory.ts +439 -0
  170. package/typechain-types/factories/contracts/token/PrivateERC721/extensions/PrivateERC721URIStorage__factory.ts +512 -0
  171. package/typechain-types/factories/contracts/token/PrivateERC721/extensions/index.ts +4 -0
  172. package/typechain-types/factories/contracts/token/PrivateERC721/index.ts +5 -0
  173. package/typechain-types/factories/contracts/token/index.ts +5 -0
  174. package/typechain-types/factories/contracts/utils/index.ts +4 -0
  175. package/typechain-types/factories/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations__factory.ts +919 -0
  176. package/typechain-types/factories/contracts/utils/mpc/MpcInterface.sol/index.ts +4 -0
  177. package/typechain-types/factories/contracts/utils/mpc/index.ts +4 -0
  178. package/typechain-types/factories/index.ts +5 -0
  179. package/typechain-types/hardhat.d.ts +621 -0
  180. package/typechain-types/index.ts +72 -0
@@ -0,0 +1,307 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity ^0.8.19;
4
+
5
+ import "../../../utils/mpc/MpcCore.sol";
6
+
7
+ contract Comparison2TestsContract {
8
+
9
+ struct AllGTCastingValues {
10
+ gtUint8 a8_s;
11
+ gtUint8 b8_s;
12
+ gtUint16 a16_s;
13
+ gtUint16 b16_s;
14
+ gtUint32 a32_s;
15
+ gtUint32 b32_s;
16
+ gtUint64 a64_s;
17
+ gtUint64 b64_s;
18
+ }
19
+
20
+ struct Check16 {
21
+ gtBool res16_16;
22
+ gtBool res8_16;
23
+ gtBool res16_8;
24
+ }
25
+
26
+ struct Check32 {
27
+ gtBool res32_32;
28
+ gtBool res8_32;
29
+ gtBool res32_8;
30
+ gtBool res16_32;
31
+ gtBool res32_16;
32
+ }
33
+
34
+ struct Check64 {
35
+ gtBool res64_64;
36
+ gtBool res8_64;
37
+ gtBool res64_8;
38
+ gtBool res16_64;
39
+ gtBool res64_16;
40
+ gtBool res32_64;
41
+ gtBool res64_32;
42
+ }
43
+
44
+ function setPublicValues(
45
+ AllGTCastingValues memory castingValues,
46
+ uint8 a,
47
+ uint8 b
48
+ ) public {
49
+ castingValues.a8_s = MpcCore.setPublic8(a);
50
+ castingValues.b8_s = MpcCore.setPublic8(b);
51
+ castingValues.a16_s = MpcCore.setPublic16(a);
52
+ castingValues.b16_s = MpcCore.setPublic16(b);
53
+ castingValues.a32_s = MpcCore.setPublic32(a);
54
+ castingValues.b32_s = MpcCore.setPublic32(b);
55
+ castingValues.a64_s = MpcCore.setPublic64(a);
56
+ castingValues.b64_s = MpcCore.setPublic64(b);
57
+ }
58
+
59
+ bool result;
60
+
61
+ function getResult() public view returns (bool) {
62
+ return result;
63
+ }
64
+
65
+ function decryptAndCompareResults16(
66
+ Check16 memory check16
67
+ ) public returns (bool) {
68
+ // Calculate the result
69
+ bool result = MpcCore.decrypt(check16.res16_16);
70
+
71
+ require(
72
+ result == MpcCore.decrypt(check16.res8_16) &&
73
+ result == MpcCore.decrypt(check16.res16_8),
74
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results"
75
+ );
76
+
77
+ return result;
78
+ }
79
+
80
+ function decryptAndCompareResults32(
81
+ Check32 memory check32
82
+ ) public returns (bool) {
83
+ // Calculate the result
84
+ bool result = MpcCore.decrypt(check32.res32_32);
85
+
86
+ require(
87
+ result == MpcCore.decrypt(check32.res8_32) &&
88
+ result == MpcCore.decrypt(check32.res32_8) &&
89
+ result == MpcCore.decrypt(check32.res32_16) &&
90
+ result == MpcCore.decrypt(check32.res16_32),
91
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results"
92
+ );
93
+
94
+ return result;
95
+ }
96
+
97
+ function decryptAndCompareResults64(
98
+ Check64 memory check64
99
+ ) public returns (bool) {
100
+ // Calculate the result
101
+ bool result = MpcCore.decrypt(check64.res64_64);
102
+
103
+ require(
104
+ result == MpcCore.decrypt(check64.res8_64) &&
105
+ result == MpcCore.decrypt(check64.res64_8) &&
106
+ result == MpcCore.decrypt(check64.res64_16) &&
107
+ result == MpcCore.decrypt(check64.res16_64) &&
108
+ result == MpcCore.decrypt(check64.res64_32) &&
109
+ result == MpcCore.decrypt(check64.res32_64),
110
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results"
111
+ );
112
+
113
+ return result;
114
+ }
115
+
116
+ function eqTest(uint8 a, uint8 b) public returns (bool) {
117
+ AllGTCastingValues memory castingValues;
118
+ Check16 memory check16;
119
+ Check32 memory check32;
120
+ Check64 memory check64;
121
+ setPublicValues(castingValues, a, b);
122
+
123
+ // Calculate the expected result
124
+ result = MpcCore.decrypt(
125
+ MpcCore.eq(castingValues.a8_s, castingValues.b8_s)
126
+ );
127
+
128
+ // Calculate the results with casting to 16
129
+ check16.res16_16 = MpcCore.eq(castingValues.a16_s, castingValues.b16_s);
130
+ check16.res8_16 = MpcCore.eq(castingValues.a8_s, castingValues.b16_s);
131
+ check16.res16_8 = MpcCore.eq(castingValues.a16_s, castingValues.b8_s);
132
+ bool res16 = decryptAndCompareResults16(check16);
133
+ require(res16 == result, "eqTest: cast 16 failed");
134
+
135
+ // Calculate the result with casting to 32
136
+ check32.res32_32 = MpcCore.eq(castingValues.a32_s, castingValues.b32_s);
137
+ check32.res8_32 = MpcCore.eq(castingValues.a8_s, castingValues.b32_s);
138
+ check32.res32_8 = MpcCore.eq(castingValues.a32_s, castingValues.b8_s);
139
+ check32.res16_32 = MpcCore.eq(castingValues.a16_s, castingValues.b32_s);
140
+ check32.res32_16 = MpcCore.eq(castingValues.a32_s, castingValues.b16_s);
141
+ bool res32 = decryptAndCompareResults32(check32);
142
+ require(result == res32, "eqTest: cast 32 failed");
143
+
144
+ // Calculate the result with casting to 64
145
+ check64.res64_64 = MpcCore.eq(castingValues.a64_s, castingValues.b64_s);
146
+ check64.res8_64 = MpcCore.eq(castingValues.a8_s, castingValues.b64_s);
147
+ check64.res64_8 = MpcCore.eq(castingValues.a64_s, castingValues.b8_s);
148
+ check64.res16_64 = MpcCore.eq(castingValues.a16_s, castingValues.b64_s);
149
+ check64.res64_16 = MpcCore.eq(castingValues.a64_s, castingValues.b16_s);
150
+ check64.res32_64 = MpcCore.eq(castingValues.a32_s, castingValues.b64_s);
151
+ check64.res64_32 = MpcCore.eq(castingValues.a64_s, castingValues.b32_s);
152
+ bool res64 = decryptAndCompareResults64(check64);
153
+ require(result == res64, "eqTest: cast 64 failed");
154
+
155
+ // Check the result with scalar
156
+ require(
157
+ result == MpcCore.decrypt(MpcCore.eq(a, castingValues.b8_s)) &&
158
+ result == MpcCore.decrypt(MpcCore.eq(castingValues.a8_s, b)),
159
+ "eqTest: test 8 bits with scalar failed"
160
+ );
161
+ require(
162
+ result == MpcCore.decrypt(MpcCore.eq(a, castingValues.b16_s)) &&
163
+ result == MpcCore.decrypt(MpcCore.eq(castingValues.a16_s, b)),
164
+ "eqTest: test 16 bits with scalar failed"
165
+ );
166
+ require(
167
+ result == MpcCore.decrypt(MpcCore.eq(a, castingValues.b32_s)) &&
168
+ result == MpcCore.decrypt(MpcCore.eq(castingValues.a32_s, b)),
169
+ "eqTest: test 32 bits with scalar failed"
170
+ );
171
+ require(
172
+ result == MpcCore.decrypt(MpcCore.eq(a, castingValues.b64_s)) &&
173
+ result == MpcCore.decrypt(MpcCore.eq(castingValues.a64_s, b)),
174
+ "eqTest: test 64 bits with scalar failed"
175
+ );
176
+
177
+ return result;
178
+ }
179
+
180
+ function neTest(uint8 a, uint8 b) public returns (bool) {
181
+ AllGTCastingValues memory castingValues;
182
+ Check16 memory check16;
183
+ Check32 memory check32;
184
+ Check64 memory check64;
185
+ setPublicValues(castingValues, a, b);
186
+
187
+ // Calculate the expected result
188
+ result = MpcCore.decrypt(
189
+ MpcCore.ne(castingValues.a8_s, castingValues.b8_s)
190
+ );
191
+
192
+ // Calculate the results with casting to 16
193
+ check16.res16_16 = MpcCore.ne(castingValues.a16_s, castingValues.b16_s);
194
+ check16.res8_16 = MpcCore.ne(castingValues.a8_s, castingValues.b16_s);
195
+ check16.res16_8 = MpcCore.ne(castingValues.a16_s, castingValues.b8_s);
196
+ bool res16 = decryptAndCompareResults16(check16);
197
+ require(res16 == result, "neTest: cast 16 failed");
198
+
199
+ // Calculate the result with casting to 32
200
+ check32.res32_32 = MpcCore.ne(castingValues.a32_s, castingValues.b32_s);
201
+ check32.res8_32 = MpcCore.ne(castingValues.a8_s, castingValues.b32_s);
202
+ check32.res32_8 = MpcCore.ne(castingValues.a32_s, castingValues.b8_s);
203
+ check32.res16_32 = MpcCore.ne(castingValues.a16_s, castingValues.b32_s);
204
+ check32.res32_16 = MpcCore.ne(castingValues.a32_s, castingValues.b16_s);
205
+ bool res32 = decryptAndCompareResults32(check32);
206
+ require(result == res32, "neTest: cast 32 failed");
207
+
208
+ // Calculate the result with casting to 64
209
+ check64.res64_64 = MpcCore.ne(castingValues.a64_s, castingValues.b64_s);
210
+ check64.res8_64 = MpcCore.ne(castingValues.a8_s, castingValues.b64_s);
211
+ check64.res64_8 = MpcCore.ne(castingValues.a64_s, castingValues.b8_s);
212
+ check64.res16_64 = MpcCore.ne(castingValues.a16_s, castingValues.b64_s);
213
+ check64.res64_16 = MpcCore.ne(castingValues.a64_s, castingValues.b16_s);
214
+ check64.res32_64 = MpcCore.ne(castingValues.a32_s, castingValues.b64_s);
215
+ check64.res64_32 = MpcCore.ne(castingValues.a64_s, castingValues.b32_s);
216
+ bool res64 = decryptAndCompareResults64(check64);
217
+ require(result == res64, "neTest: cast 64 failed");
218
+
219
+ // Check the result with scalar
220
+ require(
221
+ result == MpcCore.decrypt(MpcCore.ne(a, castingValues.b8_s)) &&
222
+ result == MpcCore.decrypt(MpcCore.ne(castingValues.a8_s, b)),
223
+ "neTest: test 8 bits with scalar failed"
224
+ );
225
+ require(
226
+ result == MpcCore.decrypt(MpcCore.ne(a, castingValues.b16_s)) &&
227
+ result == MpcCore.decrypt(MpcCore.ne(castingValues.a16_s, b)),
228
+ "neTest: test 16 bits with scalar failed"
229
+ );
230
+ require(
231
+ result == MpcCore.decrypt(MpcCore.ne(a, castingValues.b32_s)) &&
232
+ result == MpcCore.decrypt(MpcCore.ne(castingValues.a32_s, b)),
233
+ "neTest: test 32 bits with scalar failed"
234
+ );
235
+ require(
236
+ result == MpcCore.decrypt(MpcCore.ne(a, castingValues.b64_s)) &&
237
+ result == MpcCore.decrypt(MpcCore.ne(castingValues.a64_s, b)),
238
+ "neTest: test 64 bits with scalar failed"
239
+ );
240
+
241
+ return result;
242
+ }
243
+
244
+ function geTest(uint8 a, uint8 b) public returns (bool) {
245
+ AllGTCastingValues memory castingValues;
246
+ Check16 memory check16;
247
+ Check32 memory check32;
248
+ Check64 memory check64;
249
+ setPublicValues(castingValues, a, b);
250
+
251
+ // Calculate the expected result
252
+ result = MpcCore.decrypt(
253
+ MpcCore.ge(castingValues.a8_s, castingValues.b8_s)
254
+ );
255
+
256
+ // Calculate the results with casting to 16
257
+ check16.res16_16 = MpcCore.ge(castingValues.a16_s, castingValues.b16_s);
258
+ check16.res8_16 = MpcCore.ge(castingValues.a8_s, castingValues.b16_s);
259
+ check16.res16_8 = MpcCore.ge(castingValues.a16_s, castingValues.b8_s);
260
+ bool res16 = decryptAndCompareResults16(check16);
261
+ require(res16 == result, "geTest: cast 16 failed");
262
+
263
+ // Calculate the result with casting to 32
264
+ check32.res32_32 = MpcCore.ge(castingValues.a32_s, castingValues.b32_s);
265
+ check32.res8_32 = MpcCore.ge(castingValues.a8_s, castingValues.b32_s);
266
+ check32.res32_8 = MpcCore.ge(castingValues.a32_s, castingValues.b8_s);
267
+ check32.res16_32 = MpcCore.ge(castingValues.a16_s, castingValues.b32_s);
268
+ check32.res32_16 = MpcCore.ge(castingValues.a32_s, castingValues.b16_s);
269
+ bool res32 = decryptAndCompareResults32(check32);
270
+ require(result == res32, "geTest: cast 32 failed");
271
+
272
+ // Calculate the result with casting to 64
273
+ check64.res64_64 = MpcCore.ge(castingValues.a64_s, castingValues.b64_s);
274
+ check64.res8_64 = MpcCore.ge(castingValues.a8_s, castingValues.b64_s);
275
+ check64.res64_8 = MpcCore.ge(castingValues.a64_s, castingValues.b8_s);
276
+ check64.res16_64 = MpcCore.ge(castingValues.a16_s, castingValues.b64_s);
277
+ check64.res64_16 = MpcCore.ge(castingValues.a64_s, castingValues.b16_s);
278
+ check64.res32_64 = MpcCore.ge(castingValues.a32_s, castingValues.b64_s);
279
+ check64.res64_32 = MpcCore.ge(castingValues.a64_s, castingValues.b32_s);
280
+ bool res64 = decryptAndCompareResults64(check64);
281
+ require(result == res64, "geTest: cast 64 failed");
282
+
283
+ // Check the result with scalar
284
+ require(
285
+ result == MpcCore.decrypt(MpcCore.ge(a, castingValues.b8_s)) &&
286
+ result == MpcCore.decrypt(MpcCore.ge(castingValues.a8_s, b)),
287
+ "geTest: test 8 bits with scalar failed"
288
+ );
289
+ require(
290
+ result == MpcCore.decrypt(MpcCore.ge(a, castingValues.b16_s)) &&
291
+ result == MpcCore.decrypt(MpcCore.ge(castingValues.a16_s, b)),
292
+ "geTest: test 16 bits with scalar failed"
293
+ );
294
+ require(
295
+ result == MpcCore.decrypt(MpcCore.ge(a, castingValues.b32_s)) &&
296
+ result == MpcCore.decrypt(MpcCore.ge(castingValues.a32_s, b)),
297
+ "geTest: test 32 bits with scalar failed"
298
+ );
299
+ require(
300
+ result == MpcCore.decrypt(MpcCore.ge(a, castingValues.b64_s)) &&
301
+ result == MpcCore.decrypt(MpcCore.ge(castingValues.a64_s, b)),
302
+ "geTest: test 64 bits with scalar failed"
303
+ );
304
+
305
+ return result;
306
+ }
307
+ }
@@ -0,0 +1,297 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity ^0.8.19;
4
+
5
+ import "../../../utils/mpc/MpcCore.sol";
6
+
7
+ contract MinMaxTestsContract {
8
+
9
+ struct AllGTCastingValues {
10
+ gtUint8 a8_s;
11
+ gtUint8 b8_s;
12
+ gtUint16 a16_s;
13
+ gtUint16 b16_s;
14
+ gtUint32 a32_s;
15
+ gtUint32 b32_s;
16
+ gtUint64 a64_s;
17
+ gtUint64 b64_s;
18
+ }
19
+
20
+ struct Check16 {
21
+ gtUint16 res16_16;
22
+ gtUint16 res8_16;
23
+ gtUint16 res16_8;
24
+ }
25
+
26
+ struct Check32 {
27
+ gtUint32 res32_32;
28
+ gtUint32 res8_32;
29
+ gtUint32 res32_8;
30
+ gtUint32 res16_32;
31
+ gtUint32 res32_16;
32
+ }
33
+
34
+ struct Check64 {
35
+ gtUint64 res64_64;
36
+ gtUint64 res8_64;
37
+ gtUint64 res64_8;
38
+ gtUint64 res16_64;
39
+ gtUint64 res64_16;
40
+ gtUint64 res32_64;
41
+ gtUint64 res64_32;
42
+ }
43
+
44
+ function setPublicValues(
45
+ AllGTCastingValues memory castingValues,
46
+ uint8 a,
47
+ uint8 b
48
+ ) public {
49
+ castingValues.a8_s = MpcCore.setPublic8(a);
50
+ castingValues.b8_s = MpcCore.setPublic8(b);
51
+ castingValues.a16_s = MpcCore.setPublic16(a);
52
+ castingValues.b16_s = MpcCore.setPublic16(b);
53
+ castingValues.a32_s = MpcCore.setPublic32(a);
54
+ castingValues.b32_s = MpcCore.setPublic32(b);
55
+ castingValues.a64_s = MpcCore.setPublic64(a);
56
+ castingValues.b64_s = MpcCore.setPublic64(b);
57
+ }
58
+
59
+ uint8 result;
60
+
61
+ function getResult() public view returns (uint8) {
62
+ return result;
63
+ }
64
+
65
+ function decryptAndCompareResults16(
66
+ Check16 memory check16
67
+ ) public returns (uint16) {
68
+ // Calculate the result
69
+ uint16 result = MpcCore.decrypt(check16.res16_16);
70
+
71
+ require(
72
+ result == MpcCore.decrypt(check16.res8_16) &&
73
+ result == MpcCore.decrypt(check16.res16_8),
74
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results"
75
+ );
76
+
77
+ return result;
78
+ }
79
+
80
+ function decryptAndCompareResults32(
81
+ Check32 memory check32
82
+ ) public returns (uint32) {
83
+ // Calculate the result
84
+ uint32 result = MpcCore.decrypt(check32.res32_32);
85
+
86
+ require(
87
+ result == MpcCore.decrypt(check32.res8_32) &&
88
+ result == MpcCore.decrypt(check32.res32_8) &&
89
+ result == MpcCore.decrypt(check32.res32_16) &&
90
+ result == MpcCore.decrypt(check32.res16_32),
91
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results"
92
+ );
93
+
94
+ return result;
95
+ }
96
+
97
+ function decryptAndCompareResults64(
98
+ Check64 memory check64
99
+ ) public returns (uint64) {
100
+ // Calculate the result
101
+ uint64 result = MpcCore.decrypt(check64.res64_64);
102
+
103
+ require(
104
+ result == MpcCore.decrypt(check64.res8_64) &&
105
+ result == MpcCore.decrypt(check64.res64_8) &&
106
+ result == MpcCore.decrypt(check64.res64_16) &&
107
+ result == MpcCore.decrypt(check64.res16_64) &&
108
+ result == MpcCore.decrypt(check64.res64_32) &&
109
+ result == MpcCore.decrypt(check64.res32_64),
110
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results"
111
+ );
112
+
113
+ return result;
114
+ }
115
+
116
+ function minTest(uint8 a, uint8 b) public returns (uint8) {
117
+ AllGTCastingValues memory castingValues;
118
+ Check16 memory check16;
119
+ Check32 memory check32;
120
+ Check64 memory check64;
121
+ setPublicValues(castingValues, a, b);
122
+
123
+ // Calculate the expected result
124
+ result = MpcCore.decrypt(
125
+ MpcCore.min(castingValues.a8_s, castingValues.b8_s)
126
+ );
127
+
128
+ // Calculate the results with casting to 16
129
+ check16.res16_16 = MpcCore.min(
130
+ castingValues.a16_s,
131
+ castingValues.b16_s
132
+ );
133
+ check16.res8_16 = MpcCore.min(castingValues.a8_s, castingValues.b16_s);
134
+ check16.res16_8 = MpcCore.min(castingValues.a16_s, castingValues.b8_s);
135
+ uint16 res16 = decryptAndCompareResults16(check16);
136
+ require(res16 == result, "minTest: cast 16 failed");
137
+
138
+ // Calculate the result with casting to 32
139
+ check32.res32_32 = MpcCore.min(
140
+ castingValues.a32_s,
141
+ castingValues.b32_s
142
+ );
143
+ check32.res8_32 = MpcCore.min(castingValues.a8_s, castingValues.b32_s);
144
+ check32.res32_8 = MpcCore.min(castingValues.a32_s, castingValues.b8_s);
145
+ check32.res16_32 = MpcCore.min(
146
+ castingValues.a16_s,
147
+ castingValues.b32_s
148
+ );
149
+ check32.res32_16 = MpcCore.min(
150
+ castingValues.a32_s,
151
+ castingValues.b16_s
152
+ );
153
+ uint32 res32 = decryptAndCompareResults32(check32);
154
+ require(result == res32, "minTest: cast 32 failed");
155
+
156
+ // Calculate the result with casting to 64
157
+ check64.res64_64 = MpcCore.min(
158
+ castingValues.a64_s,
159
+ castingValues.b64_s
160
+ );
161
+ check64.res8_64 = MpcCore.min(castingValues.a8_s, castingValues.b64_s);
162
+ check64.res64_8 = MpcCore.min(castingValues.a64_s, castingValues.b8_s);
163
+ check64.res16_64 = MpcCore.min(
164
+ castingValues.a16_s,
165
+ castingValues.b64_s
166
+ );
167
+ check64.res64_16 = MpcCore.min(
168
+ castingValues.a64_s,
169
+ castingValues.b16_s
170
+ );
171
+ check64.res32_64 = MpcCore.min(
172
+ castingValues.a32_s,
173
+ castingValues.b64_s
174
+ );
175
+ check64.res64_32 = MpcCore.min(
176
+ castingValues.a64_s,
177
+ castingValues.b32_s
178
+ );
179
+ uint64 res64 = decryptAndCompareResults64(check64);
180
+ require(result == res64, "minTest: cast 64 failed");
181
+
182
+ // Check the result with scalar
183
+ require(
184
+ result == MpcCore.decrypt(MpcCore.min(a, castingValues.b8_s)) &&
185
+ result == MpcCore.decrypt(MpcCore.min(castingValues.a8_s, b)),
186
+ "minTest: test 8 bits with scalar failed"
187
+ );
188
+ require(
189
+ res16 == MpcCore.decrypt(MpcCore.min(a, castingValues.b16_s)) &&
190
+ res16 == MpcCore.decrypt(MpcCore.min(castingValues.a16_s, b)),
191
+ "minTest: test 16 bits with scalar failed"
192
+ );
193
+ require(
194
+ res32 == MpcCore.decrypt(MpcCore.min(a, castingValues.b32_s)) &&
195
+ res32 == MpcCore.decrypt(MpcCore.min(castingValues.a32_s, b)),
196
+ "minTest: test 32 bits with scalar failed"
197
+ );
198
+ require(
199
+ res64 == MpcCore.decrypt(MpcCore.min(a, castingValues.b64_s)) &&
200
+ res64 == MpcCore.decrypt(MpcCore.min(castingValues.a64_s, b)),
201
+ "minTest: test 64 bits with scalar failed"
202
+ );
203
+
204
+ return result;
205
+ }
206
+
207
+ function maxTest(uint8 a, uint8 b) public returns (uint8) {
208
+ AllGTCastingValues memory castingValues;
209
+ Check16 memory check16;
210
+ Check32 memory check32;
211
+ Check64 memory check64;
212
+ setPublicValues(castingValues, a, b);
213
+
214
+ // Calculate the expected result
215
+ result = MpcCore.decrypt(
216
+ MpcCore.max(castingValues.a8_s, castingValues.b8_s)
217
+ );
218
+
219
+ // Calculate the results with casting to 16
220
+ check16.res16_16 = MpcCore.max(
221
+ castingValues.a16_s,
222
+ castingValues.b16_s
223
+ );
224
+ check16.res8_16 = MpcCore.max(castingValues.a8_s, castingValues.b16_s);
225
+ check16.res16_8 = MpcCore.max(castingValues.a16_s, castingValues.b8_s);
226
+ uint16 res16 = decryptAndCompareResults16(check16);
227
+ require(res16 == result, "maxTest: cast 16 failed");
228
+
229
+ // Calculate the result with casting to 32
230
+ check32.res32_32 = MpcCore.max(
231
+ castingValues.a32_s,
232
+ castingValues.b32_s
233
+ );
234
+ check32.res8_32 = MpcCore.max(castingValues.a8_s, castingValues.b32_s);
235
+ check32.res32_8 = MpcCore.max(castingValues.a32_s, castingValues.b8_s);
236
+ check32.res16_32 = MpcCore.max(
237
+ castingValues.a16_s,
238
+ castingValues.b32_s
239
+ );
240
+ check32.res32_16 = MpcCore.max(
241
+ castingValues.a32_s,
242
+ castingValues.b16_s
243
+ );
244
+ uint32 res32 = decryptAndCompareResults32(check32);
245
+ require(result == res32, "maxTest: cast 32 failed");
246
+
247
+ // Calculate the result with casting to 64
248
+ check64.res64_64 = MpcCore.max(
249
+ castingValues.a64_s,
250
+ castingValues.b64_s
251
+ );
252
+ check64.res8_64 = MpcCore.max(castingValues.a8_s, castingValues.b64_s);
253
+ check64.res64_8 = MpcCore.max(castingValues.a64_s, castingValues.b8_s);
254
+ check64.res16_64 = MpcCore.max(
255
+ castingValues.a16_s,
256
+ castingValues.b64_s
257
+ );
258
+ check64.res64_16 = MpcCore.max(
259
+ castingValues.a64_s,
260
+ castingValues.b16_s
261
+ );
262
+ check64.res32_64 = MpcCore.max(
263
+ castingValues.a32_s,
264
+ castingValues.b64_s
265
+ );
266
+ check64.res64_32 = MpcCore.max(
267
+ castingValues.a64_s,
268
+ castingValues.b32_s
269
+ );
270
+ uint64 res64 = decryptAndCompareResults64(check64);
271
+ require(result == res64, "maxTest: cast 64 failed");
272
+
273
+ // Check the result with scalar
274
+ require(
275
+ result == MpcCore.decrypt(MpcCore.max(a, castingValues.b8_s)) &&
276
+ result == MpcCore.decrypt(MpcCore.max(castingValues.a8_s, b)),
277
+ "minTest: test 8 bits with scalar failed"
278
+ );
279
+ require(
280
+ res16 == MpcCore.decrypt(MpcCore.max(a, castingValues.b16_s)) &&
281
+ res16 == MpcCore.decrypt(MpcCore.max(castingValues.a16_s, b)),
282
+ "minTest: test 16 bits with scalar failed"
283
+ );
284
+ require(
285
+ res32 == MpcCore.decrypt(MpcCore.max(a, castingValues.b32_s)) &&
286
+ res32 == MpcCore.decrypt(MpcCore.max(castingValues.a32_s, b)),
287
+ "minTest: test 32 bits with scalar failed"
288
+ );
289
+ require(
290
+ res64 == MpcCore.decrypt(MpcCore.max(a, castingValues.b64_s)) &&
291
+ res64 == MpcCore.decrypt(MpcCore.max(castingValues.a64_s, b)),
292
+ "minTest: test 64 bits with scalar failed"
293
+ );
294
+
295
+ return result;
296
+ }
297
+ }