@coti-io/coti-contracts 0.1.0 → 1.0.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 (71) hide show
  1. package/contracts/access/DataPrivacyFramework/extensions/DataPrivacyFrameworkMpc.sol +3 -115
  2. package/contracts/mocks/README.md +1 -1
  3. package/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol +105 -280
  4. package/contracts/mocks/utils/mpc/BitwiseTestsContract.sol +83 -187
  5. package/contracts/mocks/utils/mpc/{Comparison1Tests.Contract.sol → Comparison1TestsContract.sol} +67 -115
  6. package/contracts/mocks/utils/mpc/Comparison2TestsContract.sol +68 -116
  7. package/contracts/mocks/utils/mpc/MinMaxTestsContract.sol +69 -164
  8. package/contracts/mocks/utils/mpc/Miscellaneous1TestsContract.sol +70 -52
  9. package/contracts/mocks/utils/mpc/MiscellaneousTestsContract.sol +270 -0
  10. package/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract.sol +102 -0
  11. package/contracts/mocks/utils/mpc/README.md +12 -12
  12. package/contracts/mocks/utils/mpc/TransferScalarTestsContract.sol +54 -211
  13. package/contracts/mocks/utils/mpc/TransferTestsContract.sol +162 -640
  14. package/contracts/onboard/AccountOnboard.sol +3 -3
  15. package/contracts/utils/mpc/MpcCore.sol +2835 -988
  16. package/contracts/utils/mpc/MpcInterface.sol +6 -4
  17. package/hardhat.config.ts +8 -5
  18. package/package.json +2 -2
  19. package/test/utils/accounts.ts +1 -1
  20. package/test/utils/mpc/Precompile.test.ts +26 -37
  21. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/ArithmeticTestsContract.ts +374 -0
  22. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/PrecompilesArythmeticTestsContract.ts +374 -0
  23. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/index.ts +5 -0
  24. package/typechain-types/contracts/mocks/utils/mpc/ArithmeticTestsContract.ts +30 -10
  25. package/typechain-types/contracts/mocks/utils/mpc/BitwiseTestsContract.ts +39 -5
  26. package/typechain-types/contracts/mocks/utils/mpc/Comparison1TestsContract.ts +374 -0
  27. package/typechain-types/contracts/mocks/utils/mpc/Comparison2TestsContract.ts +39 -5
  28. package/typechain-types/contracts/mocks/utils/mpc/MinMaxTestsContract.ts +25 -5
  29. package/typechain-types/contracts/mocks/utils/mpc/Miscellaneous1TestsContract.ts +62 -0
  30. package/typechain-types/contracts/mocks/utils/mpc/MiscellaneousTestsContract.ts +396 -0
  31. package/typechain-types/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract.ts +221 -0
  32. package/typechain-types/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/OffboardToUserKeyTestContract.ts +11 -8
  33. package/typechain-types/contracts/mocks/utils/mpc/TransferScalarTestsContract.ts +15 -15
  34. package/typechain-types/contracts/mocks/utils/mpc/TransferTestsContract.ts +15 -15
  35. package/typechain-types/contracts/mocks/utils/mpc/index.ts +3 -6
  36. package/typechain-types/contracts/onboard/AccountOnboard.ts +9 -4
  37. package/typechain-types/contracts/utils/mpc/MpcCore.ts +84 -0
  38. package/typechain-types/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations.ts +81 -40
  39. package/typechain-types/contracts/utils/mpc/index.ts +1 -0
  40. package/typechain-types/factories/contracts/mocks/access/DataPrivacyFramework/DataPrivacyFrameworkMock__factory.ts +1 -1
  41. package/typechain-types/factories/contracts/mocks/token/PrivateERC20/PrivateERC20Mock__factory.ts +1 -1
  42. package/typechain-types/factories/contracts/mocks/token/PrivateERC721/PrivateERC721URIStorageMock__factory.ts +1 -1
  43. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/ArithmeticTestsContract__factory.ts +387 -0
  44. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/PrecompilesArythmeticTestsContract__factory.ts +388 -0
  45. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract.sol/index.ts +5 -0
  46. package/typechain-types/factories/contracts/mocks/utils/mpc/ArithmeticTestsContract__factory.ts +20 -7
  47. package/typechain-types/factories/contracts/mocks/utils/mpc/BitwiseTestsContract__factory.ts +28 -2
  48. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison1Tests.Contract.sol/Comparison1TestsContract__factory.ts +1 -1
  49. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison1TestsContract__factory.ts +387 -0
  50. package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison2TestsContract__factory.ts +28 -2
  51. package/typechain-types/factories/contracts/mocks/utils/mpc/MinMaxTestsContract__factory.ts +15 -2
  52. package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous1TestsContract__factory.ts +66 -1
  53. package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous2TestsContract__factory.ts +1 -1
  54. package/typechain-types/factories/contracts/mocks/utils/mpc/MiscellaneousTestsContract__factory.ts +424 -0
  55. package/typechain-types/factories/contracts/mocks/utils/mpc/OffboardToUserKeyTestContract__factory.ts +289 -0
  56. package/typechain-types/factories/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol/OffboardToUserKeyTestContract__factory.ts +19 -3
  57. package/typechain-types/factories/contracts/mocks/utils/mpc/ShiftTestsContract__factory.ts +1 -1
  58. package/typechain-types/factories/contracts/mocks/utils/mpc/StringTestsContract__factory.ts +1 -1
  59. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferScalarTestsContract__factory.ts +4 -4
  60. package/typechain-types/factories/contracts/mocks/utils/mpc/TransferTestsContract__factory.ts +4 -4
  61. package/typechain-types/factories/contracts/mocks/utils/mpc/index.ts +3 -4
  62. package/typechain-types/factories/contracts/mocks/wallet/PrivateERC20Wallet/PrivateERC20WalletMock__factory.ts +1 -1
  63. package/typechain-types/factories/contracts/onboard/AccountOnboard__factory.ts +8 -2
  64. package/typechain-types/factories/contracts/utils/mpc/MpcCore__factory.ts +77 -0
  65. package/typechain-types/factories/contracts/utils/mpc/MpcInterface.sol/ExtendedOperations__factory.ts +124 -61
  66. package/typechain-types/factories/contracts/utils/mpc/index.ts +1 -0
  67. package/typechain-types/hardhat.d.ts +26 -26
  68. package/typechain-types/index.ts +8 -8
  69. package/contracts/mocks/utils/mpc/Miscellaneous2TestsContract.sol +0 -448
  70. package/contracts/mocks/utils/mpc/OffboardToUserKeyTestsContract.sol +0 -81
  71. package/contracts/mocks/utils/mpc/ShiftTestsContract.sol +0 -311
@@ -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,25 +16,22 @@ 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
 
40
37
  uint constant MAX_SIZE_8_BITS = 10;
@@ -43,37 +40,24 @@ contract Miscellaneous1TestsContract {
43
40
  uint constant MAX_SIZE_64_BITS = 2;
44
41
  uint constant MAX_BOOL_SIZE = 40;
45
42
 
46
- function checkNotAllEqual(
47
- uint64[MAX_SIZE_8_BITS] memory randoms,
48
- uint size
49
- ) private {
43
+ function checkNotAllEqual(uint64[MAX_SIZE_8_BITS] memory randoms, uint size) private {
50
44
  // Count how many randoms are equal
51
45
  uint numEqual = 1;
52
46
  for (uint i = 1; i < size; i++) {
53
- if (randoms[0] == randoms[i]) {
47
+ if (randoms[0] == randoms[i]){
54
48
  numEqual++;
55
49
  }
56
50
  }
57
- require(
58
- numEqual != size,
59
- "randomTest: random failed, all values are the same"
60
- );
51
+ require(numEqual != size, "randomTest: random failed, all values are the same");
61
52
  }
62
53
 
63
54
  function randomTest() public returns (uint64) {
64
55
  return randTest_(false, 0);
65
56
  }
66
57
 
67
- function checkBound(
68
- uint64[MAX_SIZE_8_BITS] memory randoms,
69
- uint size,
70
- uint8 numBits
71
- ) public {
58
+ function checkBound(uint64[MAX_SIZE_8_BITS] memory randoms, uint size, uint8 numBits) public {
72
59
  for (uint i = 0; i < size; i++) {
73
- require(
74
- randoms[i] < (1 << numBits),
75
- "randomTest: random failed, out of bounds"
76
- );
60
+ require(randoms[i] < (1 << numBits), "randomTest: random failed, out of bounds");
77
61
  }
78
62
  }
79
63
 
@@ -86,13 +70,13 @@ contract Miscellaneous1TestsContract {
86
70
  uint64[MAX_SIZE_8_BITS] memory randoms;
87
71
  // Generate gtUint8 randoms
88
72
  for (uint i = 0; i < size; i++) {
89
- if (!isBounded) {
73
+ if(!isBounded){
90
74
  randoms[i] = MpcCore.decrypt(MpcCore.rand8());
91
75
  } else {
92
76
  randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits8(numBits));
93
77
  }
94
78
  }
95
- random = randoms[0];
79
+
96
80
  if (isBounded) {
97
81
  // Check that all randoms are in bounds
98
82
  checkBound(randoms, size, numBits);
@@ -103,17 +87,15 @@ contract Miscellaneous1TestsContract {
103
87
  // In case of bounded random, the bit size does not matter because the bounded bits can be small.
104
88
  // So the max size remain as in 8 bits.
105
89
  // In case of unbounded random, max size can be reduced.
106
- if (!isBounded) {
90
+ if (!isBounded){
107
91
  size = MAX_SIZE_16_BITS;
108
92
  }
109
93
  // Generate gtUint16 randoms
110
94
  for (uint i = 0; i < size; i++) {
111
- if (!isBounded) {
95
+ if(!isBounded){
112
96
  randoms[i] = MpcCore.decrypt(MpcCore.rand16());
113
97
  } else {
114
- randoms[i] = MpcCore.decrypt(
115
- MpcCore.randBoundedBits16(numBits)
116
- );
98
+ randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits16(numBits));
117
99
  }
118
100
  }
119
101
  if (isBounded) {
@@ -124,16 +106,14 @@ contract Miscellaneous1TestsContract {
124
106
  checkNotAllEqual(randoms, size);
125
107
 
126
108
  // Generate gtUint32 randoms
127
- if (!isBounded) {
109
+ if (!isBounded){
128
110
  size = MAX_SIZE_32_BITS;
129
111
  }
130
112
  for (uint i = 0; i < size; i++) {
131
- if (!isBounded) {
113
+ if(!isBounded){
132
114
  randoms[i] = MpcCore.decrypt(MpcCore.rand32());
133
115
  } else {
134
- randoms[i] = MpcCore.decrypt(
135
- MpcCore.randBoundedBits32(numBits)
136
- );
116
+ randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits32(numBits));
137
117
  }
138
118
  }
139
119
  if (isBounded) {
@@ -144,21 +124,23 @@ contract Miscellaneous1TestsContract {
144
124
  checkNotAllEqual(randoms, size);
145
125
 
146
126
  // Generate gtUint64 randoms
147
- if (!isBounded) {
127
+ if (!isBounded){
148
128
  size = MAX_SIZE_64_BITS;
149
129
  }
150
130
  for (uint i = 0; i < size; i++) {
151
- if (!isBounded) {
131
+ if(!isBounded){
152
132
  randoms[i] = MpcCore.decrypt(MpcCore.rand64());
153
133
  } else {
154
- randoms[i] = MpcCore.decrypt(
155
- MpcCore.randBoundedBits64(numBits)
156
- );
134
+ randoms[i] = MpcCore.decrypt(MpcCore.randBoundedBits64(numBits));
157
135
  }
158
136
  }
137
+
159
138
  if (isBounded) {
160
139
  // Check that all randoms are in bounds
161
140
  checkBound(randoms, size, numBits);
141
+ randomBounded = randoms[0];
142
+ } else {
143
+ random = randoms[0];
162
144
  }
163
145
  // Check that not all the generated random values are the same
164
146
  checkNotAllEqual(randoms, size);
@@ -182,4 +164,40 @@ contract Miscellaneous1TestsContract {
182
164
  ctBool cipher = MpcCore.offBoard(aGT);
183
165
  onboardRes = MpcCore.decrypt(MpcCore.onBoard(cipher));
184
166
  }
185
- }
167
+
168
+ // When invoking this test function, all ciphertexts share the same value but are
169
+ // cast to four different types: ctUint8, ctUint16, ctUint32, and ctUint64.
170
+ // Consequently, there is a single signature covering all these ciphertexts.
171
+ function validateCiphertextTest(ctUint8 ct8, ctUint16 ct16, ctUint32 ct32, ctUint64 ct64, bytes calldata signature) public returns (uint8){
172
+ // Create ITs from ciphertext and signature
173
+ itUint8 memory it8;
174
+ it8.ciphertext = ct8;
175
+ it8.signature = signature;
176
+
177
+ itUint16 memory it16;
178
+ it16.ciphertext = ct16;
179
+ it16.signature = signature;
180
+
181
+ itUint32 memory it32;
182
+ it32.ciphertext = ct32;
183
+ it32.signature = signature;
184
+
185
+ itUint64 memory it64;
186
+ it64.ciphertext = ct64;
187
+ it64.signature = signature;
188
+
189
+ validateCiphertextRes = MpcCore.decrypt(MpcCore.validateCiphertext(it8));
190
+
191
+ uint16 result16 = MpcCore.decrypt(MpcCore.validateCiphertext(it16));
192
+ require(result16 == validateCiphertextRes, "validateCiphertextTest: validateCiphertext with 16 bits failed");
193
+
194
+ uint32 result32 = MpcCore.decrypt(MpcCore.validateCiphertext(it32));
195
+ require(result32 == validateCiphertextRes, "validateCiphertextTest: validateCiphertext with 32 bits failed");
196
+
197
+ uint64 result64 = MpcCore.decrypt(MpcCore.validateCiphertext(it64));
198
+ require(result64 == validateCiphertextRes, "validateCiphertextTest: validateCiphertext with 64 bits failed");
199
+
200
+ return validateCiphertextRes;
201
+ }
202
+
203
+ }
@@ -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,102 @@
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 ct8;
15
+ uint256 ct16;
16
+ uint256 ct32;
17
+ uint256 ct64;
18
+
19
+ uint8 onboardOffboardResult;
20
+
21
+ function getOnboardOffboardResult() public view returns (uint8) {
22
+ return onboardOffboardResult;
23
+ }
24
+
25
+ function getCTs() public view returns (uint256, uint256, uint256, uint256) {
26
+ return (ct8, ct16, ct32, ct64);
27
+ }
28
+
29
+ function getUserKeyTest(bytes calldata signedEK, bytes calldata signature, address addr) public returns (uint8) {
30
+
31
+ gtUint8 a = MpcCore.setPublic8(uint8(5));
32
+ gtUint8 c = MpcCore.add(a, uint8(5)); // 10
33
+ (keyShare0, keyShare1) = MpcCore.getUserKey(signedEK, signature);
34
+ ctUserKey = MpcCore.offBoardToUser(c, addr);
35
+ ctUint8 ctSystemKey = MpcCore.offBoard(c);
36
+ gtUint8 c1 = MpcCore.onBoard(ctSystemKey);
37
+ x = MpcCore.decrypt(c1);
38
+ return x;
39
+ }
40
+
41
+ function getX() public view returns (uint8) {
42
+ return x;
43
+ }
44
+
45
+ function getUserKeyShares() public view returns (bytes memory, bytes memory) {
46
+ return (keyShare0, keyShare1);
47
+ }
48
+
49
+ function getCt() public view returns (ctUint8) {
50
+ return ctUserKey;
51
+ }
52
+
53
+ function userKeyTest(bytes calldata signedEK, bytes calldata signature) public returns (bytes memory, bytes memory) {
54
+
55
+ (keyShare0, keyShare1) = MpcCore.getUserKey(signedEK, signature);
56
+ return (keyShare0, keyShare1);
57
+ }
58
+
59
+ function offboardToUserTest(uint8 a, address addr) public returns (uint256, uint256, uint256, uint256) {
60
+ gtUint8 a8_s = MpcCore.setPublic8(a);
61
+ gtUint16 a16_s = MpcCore.setPublic16(a);
62
+ gtUint32 a32_s = MpcCore.setPublic32(a);
63
+ gtUint64 a64_s = MpcCore.setPublic64(a);
64
+
65
+ ctUint8 cipher8 = MpcCore.offBoardToUser(a8_s, addr);
66
+ ctUint16 cipher16 = MpcCore.offBoardToUser(a16_s, addr);
67
+ ctUint32 cipher32 = MpcCore.offBoardToUser(a32_s, addr);
68
+ ctUint64 cipher64 = MpcCore.offBoardToUser(a64_s, addr);
69
+
70
+ ct8 = ctUint8.unwrap(cipher8);
71
+ ct16 = ctUint16.unwrap(cipher16);
72
+ ct32 = ctUint32.unwrap(cipher32);
73
+ ct64 = ctUint64.unwrap(cipher64);
74
+
75
+ return (ct8, ct16, ct32, ct64);
76
+ }
77
+
78
+ function offboardOnboardTest(uint8 a8, uint16 a16, uint32 a32, uint32 a64) public returns (uint8) {
79
+ gtUint8 a8_s = MpcCore.setPublic8(a8);
80
+ gtUint16 a16_s = MpcCore.setPublic16(a16);
81
+ gtUint32 a32_s = MpcCore.setPublic32(a32);
82
+ gtUint64 a64_s = MpcCore.setPublic64(a64);
83
+
84
+ ctUint8 cipher8 = MpcCore.offBoard(a8_s);
85
+ uint8 result = MpcCore.decrypt(MpcCore.onBoard(cipher8));
86
+ onboardOffboardResult = result;
87
+
88
+ ctUint16 cipher16 = MpcCore.offBoard(a16_s);
89
+ uint16 result16 = MpcCore.decrypt(MpcCore.onBoard(cipher16));
90
+
91
+ ctUint32 cipher32 = MpcCore.offBoard(a32_s);
92
+ uint32 result32 = MpcCore.decrypt(MpcCore.onBoard(cipher32));
93
+
94
+ ctUint64 cipher64 = MpcCore.offBoard(a64_s);
95
+ uint64 result64 = MpcCore.decrypt(MpcCore.onBoard(cipher64));
96
+
97
+ require(result == result16 && result == result32 && result == result64,
98
+ "Failed to offboard and onboard all values");
99
+
100
+ return result;
101
+ }
102
+ }