@coti-io/coti-contracts 0.1.0 → 1.0.2

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 (125) 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 +3 -115
  6. package/contracts/mocks/README.md +1 -1
  7. package/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol +257 -280
  8. package/contracts/mocks/utils/mpc/BitwiseTestsContract.sol +93 -187
  9. package/contracts/mocks/utils/mpc/{Comparison1Tests.Contract.sol → Comparison1TestsContract.sol} +67 -115
  10. package/contracts/mocks/utils/mpc/Comparison2TestsContract.sol +76 -116
  11. package/contracts/mocks/utils/mpc/MinMaxTestsContract.sol +69 -164
  12. package/contracts/mocks/utils/mpc/Miscellaneous1TestsContract.sol +103 -56
  13. package/contracts/mocks/utils/mpc/MiscellaneousTestsContract.sol +270 -0
  14. package/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract.sol +127 -0
  15. package/contracts/mocks/utils/mpc/README.md +12 -12
  16. package/contracts/mocks/utils/mpc/TransferScalarTestsContract.sol +54 -211
  17. package/contracts/mocks/utils/mpc/TransferTestsContract.sol +165 -640
  18. package/contracts/mocks/utils/mpc/TransferWithAllowance64_16TestsContract.sol +222 -0
  19. package/contracts/mocks/utils/mpc/TransferWithAllowance64_32TestsContract.sol +222 -0
  20. package/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol +223 -0
  21. package/contracts/mocks/utils/mpc/TransferWithAllowance64_8TestsContract.sol +222 -0
  22. package/contracts/mocks/utils/mpc/TransferWithAllowanceScalarTestsContract.sol +265 -0
  23. package/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol +281 -0
  24. package/contracts/onboard/AccountOnboard.sol +3 -3
  25. package/contracts/token/PrivateERC20/PrivateERC20.sol +1 -1
  26. package/contracts/token/PrivateERC721/IERC721Errors.sol +57 -0
  27. package/contracts/token/PrivateERC721/PrivateERC721.sol +1 -1
  28. package/contracts/utils/mpc/MpcCore.sol +2835 -988
  29. package/contracts/utils/mpc/MpcInterface.sol +6 -4
  30. package/hardhat.config.ts +17 -6
  31. package/package.json +20 -4
  32. package/test/token/PrivateERC20/PrivateERC20.test.ts +2 -2
  33. package/test/utils/accounts.ts +1 -1
  34. package/test/utils/mpc/Precompile.test.ts +42 -39
  35. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/ArithmeticTestsContract.ts +374 -0
  36. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/PrecompilesArythmeticTestsContract.ts +374 -0
  37. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/index.ts +5 -0
  38. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.ts +96 -10
  39. package/typechain-types/contracts/mocks/utils/mpc/BitwiseTestsContract.ts +45 -5
  40. package/typechain-types/contracts/mocks/utils/mpc/Comparison1TestsContract.ts +374 -0
  41. package/typechain-types/contracts/mocks/utils/mpc/Comparison2TestsContract.ts +45 -5
  42. package/typechain-types/contracts/mocks/utils/mpc/MinMaxTestsContract.ts +25 -5
  43. package/typechain-types/contracts/mocks/utils/mpc/Miscellaneous1TestsContract.ts +62 -0
  44. package/typechain-types/contracts/mocks/utils/mpc/MiscellaneousTestsContract.ts +396 -0
  45. package/typechain-types/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract.ts +251 -0
  46. package/typechain-types/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/OffboardToUserKeyTestContract.ts +11 -8
  47. package/typechain-types/contracts/mocks/utils/mpc/TransferScalarTestsContract.ts +15 -15
  48. package/typechain-types/contracts/mocks/utils/mpc/TransferTestsContract.ts +15 -15
  49. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowance64TestsContract.ts +251 -0
  50. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowanceTestsContract.ts +250 -0
  51. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/index.ts +5 -0
  52. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.ts +389 -0
  53. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_16TestsContract.ts +391 -0
  54. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_32TestsContract.ts +391 -0
  55. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_64TestsContract.ts +391 -0
  56. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_8TestsContract.ts +391 -0
  57. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/index.ts +5 -0
  58. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.ts +391 -0
  59. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowance64_8TestsContract.ts +391 -0
  60. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceScalarTestsContract.ts +306 -0
  61. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferTestsContract.ts +125 -0
  62. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferWithAllowanceTestsContract.ts +125 -0
  63. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/index.ts +5 -0
  64. package/typechain-types/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.ts +296 -0
  65. package/typechain-types/contracts/mocks/utils/mpc/index.ts +9 -6
  66. package/typechain-types/contracts/onboard/AccountOnboard.ts +9 -4
  67. package/typechain-types/contracts/token/PrivateERC721/IERC721Errors.ts +69 -0
  68. package/typechain-types/contracts/token/PrivateERC721/index.ts +1 -0
  69. package/typechain-types/contracts/utils/mpc/MpcCore.ts +84 -0
  70. package/typechain-types/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations.ts +81 -40
  71. package/typechain-types/contracts/utils/mpc/index.ts +1 -0
  72. package/typechain-types/factories/contracts/mocks/access/DataPrivacyFramework/DataPrivacyFrameworkMock__factory.ts +1 -1
  73. package/typechain-types/factories/contracts/mocks/token/PrivateERC20/PrivateERC20Mock__factory.ts +1 -1
  74. package/typechain-types/factories/contracts/mocks/token/PrivateERC721/PrivateERC721URIStorageMock__factory.ts +1 -1
  75. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/ArithmeticTestsContract__factory.ts +387 -0
  76. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/PrecompilesArythmeticTestsContract__factory.ts +388 -0
  77. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/index.ts +5 -0
  78. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract__factory.ts +92 -7
  79. package/typechain-types/factories/contracts/mocks/utils/mpc/BitwiseTestsContract__factory.ts +38 -2
  80. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison1Tests.Contract.sol/Comparison1TestsContract__factory.ts +1 -1
  81. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison1TestsContract__factory.ts +387 -0
  82. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison2TestsContract__factory.ts +38 -2
  83. package/typechain-types/factories/contracts/mocks/utils/mpc/MinMaxTestsContract__factory.ts +15 -2
  84. package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous1TestsContract__factory.ts +66 -1
  85. package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous2TestsContract__factory.ts +1 -1
  86. package/typechain-types/factories/contracts/mocks/utils/mpc/MiscellaneousTestsContract__factory.ts +424 -0
  87. package/typechain-types/factories/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract__factory.ts +285 -0
  88. package/typechain-types/factories/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/OffboardToUserKeyTestContract__factory.ts +19 -3
  89. package/typechain-types/factories/contracts/mocks/utils/mpc/ShiftTestsContract__factory.ts +1 -1
  90. package/typechain-types/factories/contracts/mocks/utils/mpc/StringTestsContract__factory.ts +1 -1
  91. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferScalarTestsContract__factory.ts +4 -4
  92. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferTestsContract__factory.ts +4 -4
  93. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowance64TestsContract__factory.ts +294 -0
  94. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/TransferWithAllowanceTestsContract__factory.ts +292 -0
  95. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract.sol/index.ts +5 -0
  96. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64TestsContract__factory.ts +720 -0
  97. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_16TestsContract__factory.ts +724 -0
  98. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_32TestsContract__factory.ts +724 -0
  99. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_64TestsContract__factory.ts +724 -0
  100. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/TransferWithAllowance64_8TestsContract__factory.ts +724 -0
  101. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract.sol/index.ts +5 -0
  102. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_64TestsContract__factory.ts +724 -0
  103. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowance64_8TestsContract__factory.ts +724 -0
  104. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceScalarTestsContract__factory.ts +507 -0
  105. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferTestsContract__factory.ts +150 -0
  106. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/TransferWithAllowanceTestsContract__factory.ts +150 -0
  107. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract.sol/index.ts +5 -0
  108. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferWithAllowanceTestsContract__factory.ts +434 -0
  109. package/typechain-types/factories/contracts/mocks/utils/mpc/index.ts +9 -4
  110. package/typechain-types/factories/contracts/mocks/wallet/PrivateERC20Wallet/PrivateERC20WalletMock__factory.ts +1 -1
  111. package/typechain-types/factories/contracts/onboard/AccountOnboard__factory.ts +8 -2
  112. package/typechain-types/factories/contracts/token/PrivateERC721/IERC721Errors__factory.ts +128 -0
  113. package/typechain-types/factories/contracts/token/PrivateERC721/index.ts +1 -0
  114. package/typechain-types/factories/contracts/utils/mpc/MpcCore__factory.ts +77 -0
  115. package/typechain-types/factories/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations__factory.ts +124 -61
  116. package/typechain-types/factories/contracts/utils/mpc/index.ts +1 -0
  117. package/typechain-types/hardhat.d.ts +152 -26
  118. package/typechain-types/index.ts +20 -8
  119. package/contracts/access/DataPrivacyFramework/README.md +0 -68
  120. package/contracts/mocks/utils/mpc/Miscellaneous2TestsContract.sol +0 -448
  121. package/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol +0 -81
  122. package/contracts/mocks/utils/mpc/ShiftTestsContract.sol +0 -311
  123. package/contracts/token/PrivateERC20/README.md +0 -104
  124. package/contracts/token/PrivateERC721/README.md +0 -282
  125. package/contracts/utils/mpc/README.md +0 -82
@@ -1,5 +1,4 @@
1
1
  // SPDX-License-Identifier: MIT
2
-
3
2
  pragma solidity ^0.8.19;
4
3
 
5
4
  import "../../../utils/mpc/MpcCore.sol";
@@ -7,6 +6,7 @@ import "../../../utils/mpc/MpcCore.sol";
7
6
  contract Miscellaneous1TestsContract {
8
7
 
9
8
  uint64 random = 0;
9
+ uint64 randomBounded = 0;
10
10
  bool andRes;
11
11
  bool orRes;
12
12
  bool xorRes;
@@ -16,64 +16,60 @@ contract Miscellaneous1TestsContract {
16
16
  bool muxRes;
17
17
  bool onboardRes;
18
18
 
19
+ uint8 validateCiphertextRes;
20
+
19
21
  function getRandom() public view returns (uint64) {
20
22
  return random;
21
23
  }
22
24
 
23
- function getBooleanResults()
24
- public
25
- view
26
- returns (bool, bool, bool, bool, bool, bool, bool, bool)
27
- {
28
- return (
29
- andRes,
30
- orRes,
31
- xorRes,
32
- notRes,
33
- eqRes,
34
- neqRes,
35
- muxRes,
36
- onboardRes
37
- );
25
+ function getRandomBounded() public view returns (uint64) {
26
+ return randomBounded;
27
+ }
28
+
29
+ function getBooleanResults() public view returns (bool, bool, bool, bool, bool, bool, bool, bool) {
30
+ return (andRes, orRes, xorRes, notRes, eqRes, neqRes, muxRes, onboardRes);
31
+ }
32
+
33
+ function getValidateCiphertextResult() public view returns (uint8) {
34
+ return validateCiphertextRes;
38
35
  }
39
36
 
37
+ uint constant MAX_SIZE_1_BIT = 10;
40
38
  uint constant MAX_SIZE_8_BITS = 10;
41
39
  uint constant MAX_SIZE_16_BITS = 3;
42
40
  uint constant MAX_SIZE_32_BITS = 3;
43
41
  uint constant MAX_SIZE_64_BITS = 2;
44
42
  uint constant MAX_BOOL_SIZE = 40;
45
43
 
46
- function checkNotAllEqual(
47
- uint64[MAX_SIZE_8_BITS] memory randoms,
48
- uint size
49
- ) private {
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
+
55
+ function checkNotAllEqual(uint64[MAX_SIZE_8_BITS] memory randoms, uint size) private {
50
56
  // Count how many randoms are equal
51
57
  uint numEqual = 1;
52
58
  for (uint i = 1; i < size; i++) {
53
- if (randoms[0] == randoms[i]) {
59
+ if (randoms[0] == randoms[i]){
54
60
  numEqual++;
55
61
  }
56
62
  }
57
- require(
58
- numEqual != size,
59
- "randomTest: random failed, all values are the same"
60
- );
63
+ require(numEqual != size, "randomTest: random failed, all values are the same");
61
64
  }
62
65
 
63
66
  function randomTest() public returns (uint64) {
64
67
  return randTest_(false, 0);
65
68
  }
66
69
 
67
- function checkBound(
68
- uint64[MAX_SIZE_8_BITS] memory randoms,
69
- uint size,
70
- uint8 numBits
71
- ) public {
70
+ function checkBound(uint64[MAX_SIZE_8_BITS] memory randoms, uint size, uint8 numBits) public {
72
71
  for (uint i = 0; i < size; i++) {
73
- require(
74
- randoms[i] < (1 << numBits),
75
- "randomTest: random failed, out of bounds"
76
- );
72
+ require(randoms[i] < (1 << numBits), "randomTest: random failed, out of bounds");
77
73
  }
78
74
  }
79
75
 
@@ -82,17 +78,37 @@ contract Miscellaneous1TestsContract {
82
78
  }
83
79
 
84
80
  function randTest_(bool isBounded, uint8 numBits) public returns (uint64) {
85
- 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
+
86
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
+
87
103
  // Generate gtUint8 randoms
88
104
  for (uint i = 0; i < size; i++) {
89
- if (!isBounded) {
105
+ if(!isBounded){
90
106
  randoms[i] = MpcCore.decrypt(MpcCore.rand8());
91
107
  } else {
92
108
  randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits8(numBits));
93
109
  }
94
110
  }
95
- random = randoms[0];
111
+
96
112
  if (isBounded) {
97
113
  // Check that all randoms are in bounds
98
114
  checkBound(randoms, size, numBits);
@@ -100,20 +116,15 @@ contract Miscellaneous1TestsContract {
100
116
  // Check that not all the generated random values are the same
101
117
  checkNotAllEqual(randoms, size);
102
118
 
103
- // In case of bounded random, the bit size does not matter because the bounded bits can be small.
104
- // So the max size remain as in 8 bits.
105
- // In case of unbounded random, max size can be reduced.
106
- if (!isBounded) {
119
+ if (!isBounded){
107
120
  size = MAX_SIZE_16_BITS;
108
121
  }
109
122
  // Generate gtUint16 randoms
110
123
  for (uint i = 0; i < size; i++) {
111
- if (!isBounded) {
124
+ if(!isBounded){
112
125
  randoms[i] = MpcCore.decrypt(MpcCore.rand16());
113
126
  } else {
114
- randoms[i] = MpcCore.decrypt(
115
- MpcCore.randBoundedBits16(numBits)
116
- );
127
+ randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits16(numBits));
117
128
  }
118
129
  }
119
130
  if (isBounded) {
@@ -124,16 +135,14 @@ contract Miscellaneous1TestsContract {
124
135
  checkNotAllEqual(randoms, size);
125
136
 
126
137
  // Generate gtUint32 randoms
127
- if (!isBounded) {
138
+ if (!isBounded){
128
139
  size = MAX_SIZE_32_BITS;
129
140
  }
130
141
  for (uint i = 0; i < size; i++) {
131
- if (!isBounded) {
142
+ if(!isBounded){
132
143
  randoms[i] = MpcCore.decrypt(MpcCore.rand32());
133
144
  } else {
134
- randoms[i] = MpcCore.decrypt(
135
- MpcCore.randBoundedBits32(numBits)
136
- );
145
+ randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits32(numBits));
137
146
  }
138
147
  }
139
148
  if (isBounded) {
@@ -144,21 +153,23 @@ contract Miscellaneous1TestsContract {
144
153
  checkNotAllEqual(randoms, size);
145
154
 
146
155
  // Generate gtUint64 randoms
147
- if (!isBounded) {
156
+ if (!isBounded){
148
157
  size = MAX_SIZE_64_BITS;
149
158
  }
150
159
  for (uint i = 0; i < size; i++) {
151
- if (!isBounded) {
160
+ if(!isBounded){
152
161
  randoms[i] = MpcCore.decrypt(MpcCore.rand64());
153
162
  } else {
154
- randoms[i] = MpcCore.decrypt(
155
- MpcCore.randBoundedBits64(numBits)
156
- );
163
+ randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits64(numBits));
157
164
  }
158
165
  }
166
+
159
167
  if (isBounded) {
160
168
  // Check that all randoms are in bounds
161
169
  checkBound(randoms, size, numBits);
170
+ randomBounded = randoms[0];
171
+ } else {
172
+ random = randoms[0];
162
173
  }
163
174
  // Check that not all the generated random values are the same
164
175
  checkNotAllEqual(randoms, size);
@@ -182,4 +193,40 @@ contract Miscellaneous1TestsContract {
182
193
  ctBool cipher = MpcCore.offBoard(aGT);
183
194
  onboardRes = MpcCore.decrypt(MpcCore.onBoard(cipher));
184
195
  }
185
- }
196
+
197
+ // When invoking this test function, all ciphertexts share the same value but are
198
+ // cast to four different types: ctUint8, ctUint16, ctUint32, and ctUint64.
199
+ // Consequently, there is a single signature covering all these ciphertexts.
200
+ function validateCiphertextTest(ctUint8 ct8, ctUint16 ct16, ctUint32 ct32, ctUint64 ct64, bytes calldata signature) public returns (uint8){
201
+ // Create ITs from ciphertext and signature
202
+ itUint8 memory it8;
203
+ it8.ciphertext = ct8;
204
+ it8.signature = signature;
205
+
206
+ itUint16 memory it16;
207
+ it16.ciphertext = ct16;
208
+ it16.signature = signature;
209
+
210
+ itUint32 memory it32;
211
+ it32.ciphertext = ct32;
212
+ it32.signature = signature;
213
+
214
+ itUint64 memory it64;
215
+ it64.ciphertext = ct64;
216
+ it64.signature = signature;
217
+
218
+ validateCiphertextRes = MpcCore.decrypt(MpcCore.validateCiphertext(it8));
219
+
220
+ uint16 result16 = MpcCore.decrypt(MpcCore.validateCiphertext(it16));
221
+ require(result16 == validateCiphertextRes, "validateCiphertextTest: validateCiphertext with 16 bits failed");
222
+
223
+ uint32 result32 = MpcCore.decrypt(MpcCore.validateCiphertext(it32));
224
+ require(result32 == validateCiphertextRes, "validateCiphertextTest: validateCiphertext with 32 bits failed");
225
+
226
+ uint64 result64 = MpcCore.decrypt(MpcCore.validateCiphertext(it64));
227
+ require(result64 == validateCiphertextRes, "validateCiphertextTest: validateCiphertext with 64 bits failed");
228
+
229
+ return validateCiphertextRes;
230
+ }
231
+
232
+ }
@@ -0,0 +1,270 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.19;
3
+
4
+ import "../../../utils/mpc/MpcCore.sol";
5
+
6
+ contract MiscellaneousTestsContract {
7
+
8
+ struct AllGTCastingValues {
9
+ gtUint8 a8_s;
10
+ gtUint8 b8_s;
11
+ gtUint16 a16_s;
12
+ gtUint16 b16_s;
13
+ gtUint32 a32_s;
14
+ gtUint32 b32_s;
15
+ gtUint64 a64_s;
16
+ gtUint64 b64_s;
17
+ }
18
+
19
+ struct Check16 {
20
+ gtUint16 res16_16;
21
+ gtUint16 res8_16;
22
+ gtUint16 res16_8;
23
+ }
24
+
25
+ struct Check32 {
26
+ gtUint32 res32_32;
27
+ gtUint32 res8_32;
28
+ gtUint32 res32_8;
29
+ gtUint32 res16_32;
30
+ gtUint32 res32_16;
31
+ }
32
+
33
+ struct Check64 {
34
+ gtUint64 res64_64;
35
+ gtUint64 res8_64;
36
+ gtUint64 res64_8;
37
+ gtUint64 res16_64;
38
+ gtUint64 res64_16;
39
+ gtUint64 res32_64;
40
+ gtUint64 res64_32;
41
+ }
42
+
43
+ function setPublicValues(AllGTCastingValues memory castingValues, uint8 a, uint8 b) public{
44
+ castingValues.a8_s = MpcCore.setPublic8(a);
45
+ castingValues.b8_s = MpcCore.setPublic8(b);
46
+ castingValues.a16_s = MpcCore.setPublic16(a);
47
+ castingValues.b16_s = MpcCore.setPublic16(b);
48
+ castingValues.a32_s = MpcCore.setPublic32(a);
49
+ castingValues.b32_s = MpcCore.setPublic32(b);
50
+ castingValues.a64_s = MpcCore.setPublic64(a);
51
+ castingValues.b64_s = MpcCore.setPublic64(b);
52
+ }
53
+
54
+ uint8 divResult;
55
+ uint8 remResult;
56
+ uint8 muxResult;
57
+ bool boolResult;
58
+
59
+ function getDivResult() public view returns (uint8) {
60
+ return divResult;
61
+ }
62
+ function getRemResult() public view returns (uint8) {
63
+ return remResult;
64
+ }
65
+ function getMuxResult() public view returns (uint8) {
66
+ return muxResult;
67
+ }
68
+
69
+ function getBoolResult() public view returns (bool) {
70
+ return boolResult;
71
+ }
72
+
73
+ function decryptAndCompareResults16(Check16 memory check16) public returns (uint16){
74
+
75
+ // Calculate the result
76
+ uint16 result = MpcCore.decrypt(check16.res16_16);
77
+
78
+ require(result == MpcCore.decrypt(check16.res8_16) && result == MpcCore.decrypt(check16.res16_8),
79
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results");
80
+
81
+ return result;
82
+ }
83
+
84
+ function decryptAndCompareResults32(Check32 memory check32) public returns (uint32){
85
+
86
+ // Calculate the result
87
+ uint32 result = MpcCore.decrypt(check32.res32_32);
88
+
89
+ require(result == MpcCore.decrypt(check32.res8_32) && result == MpcCore.decrypt(check32.res32_8)
90
+ && result == MpcCore.decrypt(check32.res32_16) && result == MpcCore.decrypt(check32.res16_32),
91
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results");
92
+
93
+ return result;
94
+ }
95
+
96
+ function decryptAndCompareResults64(Check64 memory check64) public returns (uint64){
97
+ // Calculate the result
98
+ uint64 result = MpcCore.decrypt(check64.res64_64);
99
+
100
+ require(result == MpcCore.decrypt(check64.res8_64) && result == MpcCore.decrypt(check64.res64_8)
101
+ && result == MpcCore.decrypt(check64.res64_16) && result == MpcCore.decrypt(check64.res16_64)
102
+ && result == MpcCore.decrypt(check64.res64_32) && result == MpcCore.decrypt(check64.res32_64),
103
+ "decryptAndCompareAllResults: Failed to decrypt and compare all results");
104
+
105
+ return result;
106
+ }
107
+
108
+ function divTest(uint8 a, uint8 b) public returns (uint8) {
109
+ AllGTCastingValues memory castingValues;
110
+ Check16 memory check16;
111
+ Check32 memory check32;
112
+ Check64 memory check64;
113
+ setPublicValues(castingValues, a, b);
114
+
115
+ // Calculate the expected result
116
+ uint8 result = MpcCore.decrypt(MpcCore.div(castingValues.a8_s, castingValues.b8_s));
117
+ divResult = result;
118
+
119
+ // Calculate the results with casting to 16
120
+ check16.res16_16 = MpcCore.div(castingValues.a16_s, castingValues.b16_s);
121
+ check16.res8_16 = MpcCore.div(castingValues.a8_s, castingValues.b16_s);
122
+ check16.res16_8 = MpcCore.div(castingValues.a16_s, castingValues.b8_s);
123
+ uint16 res16 = decryptAndCompareResults16(check16);
124
+ require(res16 == result, "divTest: cast 16 failed");
125
+
126
+ // Calculate the result with casting to 32
127
+ check32.res32_32 = MpcCore.div(castingValues.a32_s, castingValues.b32_s);
128
+ check32.res8_32 = MpcCore.div(castingValues.a8_s, castingValues.b32_s);
129
+ check32.res32_8 = MpcCore.div(castingValues.a32_s, castingValues.b8_s);
130
+ check32.res16_32 = MpcCore.div(castingValues.a16_s, castingValues.b32_s);
131
+ check32.res32_16 = MpcCore.div(castingValues.a32_s, castingValues.b16_s);
132
+ uint32 res32 = decryptAndCompareResults32(check32);
133
+ require(result == res32, "divTest: cast 32 failed");
134
+
135
+ // Calculate the result with casting to 64
136
+ check64.res64_64 = MpcCore.div(castingValues.a64_s, castingValues.b64_s);
137
+ check64.res8_64 = MpcCore.div(castingValues.a8_s, castingValues.b64_s);
138
+ check64.res64_8 = MpcCore.div(castingValues.a64_s, castingValues.b8_s);
139
+ check64.res16_64 = MpcCore.div(castingValues.a16_s, castingValues.b64_s);
140
+ check64.res64_16 = MpcCore.div(castingValues.a64_s, castingValues.b16_s);
141
+ check64.res32_64 = MpcCore.div(castingValues.a32_s, castingValues.b64_s);
142
+ check64.res64_32 = MpcCore.div(castingValues.a64_s, castingValues.b32_s);
143
+ uint64 res64 = decryptAndCompareResults64(check64);
144
+ require(result == res64, "divTest: cast 64 failed");
145
+
146
+ // Check the result with scalar
147
+ require(result == MpcCore.decrypt(MpcCore.div(a, castingValues.b8_s)) && result == MpcCore.decrypt(MpcCore.div(castingValues.a8_s, b)),
148
+ "divTest: test 8 bits with scalar failed");
149
+ require(result == MpcCore.decrypt(MpcCore.div(a, castingValues.b16_s)) && result == MpcCore.decrypt(MpcCore.div(castingValues.a16_s, b)),
150
+ "divTest: test 16 bits with scalar failed");
151
+ require(result == MpcCore.decrypt(MpcCore.div(a, castingValues.b32_s)) && result == MpcCore.decrypt(MpcCore.div(castingValues.a32_s, b)),
152
+ "divTest: test 32 bits with scalar failed");
153
+ require(result == MpcCore.decrypt(MpcCore.div(a, castingValues.b64_s)) && result == MpcCore.decrypt(MpcCore.div(castingValues.a64_s, b)),
154
+ "divTest: test 64 bits with scalar failed");
155
+
156
+ return result;
157
+ }
158
+
159
+ function remTest(uint8 a, uint8 b) public returns (uint8) {
160
+ AllGTCastingValues memory castingValues;
161
+ Check16 memory check16;
162
+ Check32 memory check32;
163
+ Check64 memory check64;
164
+ setPublicValues(castingValues, a, b);
165
+
166
+ // Calculate the expected result
167
+ uint8 result = MpcCore.decrypt(MpcCore.rem(castingValues.a8_s, castingValues.b8_s));
168
+ remResult = result;
169
+
170
+ // Calculate the results with casting to 16
171
+ check16.res16_16 = MpcCore.rem(castingValues.a16_s, castingValues.b16_s);
172
+ check16.res8_16 = MpcCore.rem(castingValues.a8_s, castingValues.b16_s);
173
+ check16.res16_8 = MpcCore.rem(castingValues.a16_s, castingValues.b8_s);
174
+ uint16 res16 = decryptAndCompareResults16(check16);
175
+ require(res16 == result, "remTest: cast 16 failed");
176
+
177
+ // Calculate the result with casting to 32
178
+ check32.res32_32 = MpcCore.rem(castingValues.a32_s, castingValues.b32_s);
179
+ check32.res8_32 = MpcCore.rem(castingValues.a8_s, castingValues.b32_s);
180
+ check32.res32_8 = MpcCore.rem(castingValues.a32_s, castingValues.b8_s);
181
+ check32.res16_32 = MpcCore.rem(castingValues.a16_s, castingValues.b32_s);
182
+ check32.res32_16 = MpcCore.rem(castingValues.a32_s, castingValues.b16_s);
183
+ uint32 res32 = decryptAndCompareResults32(check32);
184
+ require(result == res32, "remTest: cast 32 failed");
185
+
186
+ // Calculate the result with casting to 64
187
+ check64.res64_64 = MpcCore.rem(castingValues.a64_s, castingValues.b64_s);
188
+ check64.res8_64 = MpcCore.rem(castingValues.a8_s, castingValues.b64_s);
189
+ check64.res64_8 = MpcCore.rem(castingValues.a64_s, castingValues.b8_s);
190
+ check64.res16_64 = MpcCore.rem(castingValues.a16_s, castingValues.b64_s);
191
+ check64.res64_16 = MpcCore.rem(castingValues.a64_s, castingValues.b16_s);
192
+ check64.res32_64 = MpcCore.rem(castingValues.a32_s, castingValues.b64_s);
193
+ check64.res64_32 = MpcCore.rem(castingValues.a64_s, castingValues.b32_s);
194
+ uint64 res64 = decryptAndCompareResults64(check64);
195
+ require(result == res64, "remTest: cast 64 failed");
196
+
197
+ // Check the result with scalar
198
+ require(result == MpcCore.decrypt(MpcCore.rem(a, castingValues.b8_s)) && result == MpcCore.decrypt(MpcCore.rem(castingValues.a8_s, b)),
199
+ "remTest: test 8 bits with scalar failed");
200
+ require(result == MpcCore.decrypt(MpcCore.rem(a, castingValues.b16_s)) && result == MpcCore.decrypt(MpcCore.rem(castingValues.a16_s, b)),
201
+ "remTest: test 16 bits with scalar failed");
202
+ require(result == MpcCore.decrypt(MpcCore.rem(a, castingValues.b32_s)) && result == MpcCore.decrypt(MpcCore.rem(castingValues.a32_s, b)),
203
+ "remTest: test 32 bits with scalar failed");
204
+ require(result == MpcCore.decrypt(MpcCore.rem(a, castingValues.b64_s)) && result == MpcCore.decrypt(MpcCore.rem(castingValues.a64_s, b)),
205
+ "remTest: test 64 bits with scalar failed");
206
+
207
+ return result;
208
+ }
209
+
210
+ function muxTest(bool selectionBit, uint8 a, uint8 b) public returns (uint8) {
211
+ AllGTCastingValues memory castingValues;
212
+ Check16 memory check16;
213
+ Check32 memory check32;
214
+ Check64 memory check64;
215
+ setPublicValues(castingValues, a, b);
216
+ gtBool selectionBit_s = MpcCore.setPublic(selectionBit);
217
+
218
+ // Calculate the expected result
219
+ uint8 result = MpcCore.decrypt(MpcCore.mux(selectionBit_s, castingValues.a8_s, castingValues.b8_s));
220
+ muxResult = result;
221
+
222
+ // Calculate the result with casting to 16
223
+ check16.res16_16 = MpcCore.mux(selectionBit_s, castingValues.a16_s, castingValues.b16_s);
224
+ check16.res8_16 = MpcCore.mux(selectionBit_s, castingValues.a8_s, castingValues.b16_s);
225
+ check16.res16_8 = MpcCore.mux(selectionBit_s, castingValues.a16_s, castingValues.b8_s);
226
+ uint16 res16 = decryptAndCompareResults16(check16);
227
+ require(res16 == result, "muxTest: cast 16 failed");
228
+
229
+ // Calculate the result with casting to 32
230
+ check32.res32_32 = MpcCore.mux(selectionBit_s, castingValues.a32_s, castingValues.b32_s);
231
+ check32.res8_32 = MpcCore.mux(selectionBit_s, castingValues.a8_s, castingValues.b32_s);
232
+ check32.res32_8 = MpcCore.mux(selectionBit_s, castingValues.a32_s, castingValues.b8_s);
233
+ check32.res16_32 = MpcCore.mux(selectionBit_s, castingValues.a16_s, castingValues.b32_s);
234
+ check32.res32_16 = MpcCore.mux(selectionBit_s, castingValues.a32_s, castingValues.b16_s);
235
+ uint32 res32 = decryptAndCompareResults32(check32);
236
+ require(result == res32, "muxTest: cast 32 failed");
237
+
238
+ // Calculate the result with casting to 64
239
+ check64.res64_64 = MpcCore.mux(selectionBit_s, castingValues.a64_s, castingValues.b64_s);
240
+ check64.res8_64 = MpcCore.mux(selectionBit_s, castingValues.a8_s, castingValues.b64_s);
241
+ check64.res64_8 = MpcCore.mux(selectionBit_s, castingValues.a64_s, castingValues.b8_s);
242
+ check64.res16_64 = MpcCore.mux(selectionBit_s, castingValues.a16_s, castingValues.b64_s);
243
+ check64.res64_16 = MpcCore.mux(selectionBit_s, castingValues.a64_s, castingValues.b16_s);
244
+ check64.res32_64 = MpcCore.mux(selectionBit_s, castingValues.a32_s, castingValues.b64_s);
245
+ check64.res64_32 = MpcCore.mux(selectionBit_s, castingValues.a64_s, castingValues.b32_s);
246
+ uint64 res64 = decryptAndCompareResults64(check64);
247
+ require(result == res64, "muxTest: cast 64 failed");
248
+
249
+ // Check the result with scalar
250
+ require(result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, a, castingValues.b8_s)) && result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, castingValues.a8_s, b)),
251
+ "muxTest: test 8 bits with scalar failed");
252
+ require(result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, a, castingValues.b16_s)) && result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, castingValues.a16_s, b)),
253
+ "muxTest: test 16 bits with scalar failed");
254
+ require(result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, a, castingValues.b32_s)) && result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, castingValues.a32_s, b)),
255
+ "muxTest: test 32 bits with scalar failed");
256
+ require(result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, a, castingValues.b64_s)) && result == MpcCore.decrypt(MpcCore.mux(selectionBit_s, castingValues.a64_s, b)),
257
+ "muxTest: test 64 bits with scalar failed");
258
+
259
+ return result;
260
+ }
261
+
262
+ function notTest(bool a) public returns (bool) {
263
+ gtBool a_s = MpcCore.setPublic(a);
264
+ gtBool not = MpcCore.not(a_s);
265
+ boolResult = MpcCore.decrypt(not);
266
+
267
+ return boolResult;
268
+ }
269
+
270
+ }
@@ -0,0 +1,127 @@
1
+
2
+ // SPDX-License-Identifier: MIT
3
+ pragma solidity ^0.8.19;
4
+
5
+ import "../../../utils/mpc/MpcCore.sol";
6
+
7
+ contract OffboardToUserKeyTestContract {
8
+
9
+ bytes keyShare0;
10
+ bytes keyShare1;
11
+ uint8 x;
12
+ ctUint8 ctUserKey;
13
+
14
+ uint256 ct;
15
+ uint256 ct8;
16
+ uint256 ct16;
17
+ uint256 ct32;
18
+ uint256 ct64;
19
+
20
+ uint8 onboardOffboardResult;
21
+
22
+ function getOnboardOffboardResult() public view returns (uint8) {
23
+ return onboardOffboardResult;
24
+ }
25
+
26
+ function getCTs() public view returns (uint256, uint256, uint256, uint256, uint256) {
27
+ return (ct, ct8, ct16, ct32, ct64);
28
+ }
29
+
30
+ function getUserKeyTest(bytes calldata signedEK, bytes calldata signature, address addr) public returns (uint8) {
31
+
32
+ gtUint8 a = MpcCore.setPublic8(uint8(5));
33
+ gtUint8 c = MpcCore.add(a, uint8(5)); // 10
34
+ (keyShare0, keyShare1) = MpcCore.getUserKey(signedEK, signature);
35
+ ctUserKey = MpcCore.offBoardToUser(c, addr);
36
+ ctUint8 ctSystemKey = MpcCore.offBoard(c);
37
+ gtUint8 c1 = MpcCore.onBoard(ctSystemKey);
38
+ x = MpcCore.decrypt(c1);
39
+ return x;
40
+ }
41
+
42
+ function getX() public view returns (uint8) {
43
+ return x;
44
+ }
45
+
46
+ function getUserKeyShares() public view returns (bytes memory, bytes memory) {
47
+ return (keyShare0, keyShare1);
48
+ }
49
+
50
+ function getCt() public view returns (ctUint8) {
51
+ return ctUserKey;
52
+ }
53
+
54
+ function userKeyTest(bytes calldata signedEK, bytes calldata signature) public returns (bytes memory, bytes memory) {
55
+
56
+ (keyShare0, keyShare1) = MpcCore.getUserKey(signedEK, signature);
57
+ return (keyShare0, keyShare1);
58
+ }
59
+
60
+ function offboardToUserTest(uint8 a, address addr) public {
61
+ gtBool aBool_s = MpcCore.setPublic(a > 0);
62
+ gtUint8 a8_s = MpcCore.setPublic8(a);
63
+ gtUint16 a16_s = MpcCore.setPublic16(a);
64
+ gtUint32 a32_s = MpcCore.setPublic32(a);
65
+ gtUint64 a64_s = MpcCore.setPublic64(a);
66
+
67
+ ctBool cipherBool = MpcCore.offBoardToUser(aBool_s, addr);
68
+ ctUint8 cipher8 = MpcCore.offBoardToUser(a8_s, addr);
69
+ ctUint16 cipher16 = MpcCore.offBoardToUser(a16_s, addr);
70
+ ctUint32 cipher32 = MpcCore.offBoardToUser(a32_s, addr);
71
+ ctUint64 cipher64 = MpcCore.offBoardToUser(a64_s, addr);
72
+
73
+ ct = ctBool.unwrap(cipherBool);
74
+ ct8 = ctUint8.unwrap(cipher8);
75
+ ct16 = ctUint16.unwrap(cipher16);
76
+ ct32 = ctUint32.unwrap(cipher32);
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);
86
+
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);
98
+ }
99
+
100
+ function offboardOnboardTest(uint8 a8, uint16 a16, uint32 a32, uint32 a64) public {
101
+ bool temp = a8 > 0;
102
+ gtBool aBool_s = MpcCore.setPublic(temp);
103
+ gtUint8 a8_s = MpcCore.setPublic8(a8);
104
+ gtUint16 a16_s = MpcCore.setPublic16(a16);
105
+ gtUint32 a32_s = MpcCore.setPublic32(a32);
106
+ gtUint64 a64_s = MpcCore.setPublic64(a64);
107
+
108
+ ctBool cipher = MpcCore.offBoard(aBool_s);
109
+ bool result = MpcCore.decrypt(MpcCore.onBoard(cipher));
110
+
111
+ ctUint8 cipher8 = MpcCore.offBoard(a8_s);
112
+ uint8 result8 = MpcCore.decrypt(MpcCore.onBoard(cipher8));
113
+ onboardOffboardResult = result8;
114
+
115
+ ctUint16 cipher16 = MpcCore.offBoard(a16_s);
116
+ uint16 result16 = MpcCore.decrypt(MpcCore.onBoard(cipher16));
117
+
118
+ ctUint32 cipher32 = MpcCore.offBoard(a32_s);
119
+ uint32 result32 = MpcCore.decrypt(MpcCore.onBoard(cipher32));
120
+
121
+ ctUint64 cipher64 = MpcCore.offBoard(a64_s);
122
+ uint64 result64 = MpcCore.decrypt(MpcCore.onBoard(cipher64));
123
+
124
+ require(result == temp && result8 == result16 && result8 == result32 && result8 == result64,
125
+ "Failed to offboard and onboard all values");
126
+ }
127
+ }