@coti-io/coti-contracts 1.0.8 → 1.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.
- package/contracts/mocks/utils/mpc/Arithmetic128BitTestsContract.sol +260 -0
- package/contracts/mocks/utils/mpc/Arithmetic256BitTestsContract.sol +264 -0
- package/contracts/mocks/utils/mpc/Bitwise128BitTestsContract.sol +91 -0
- package/contracts/mocks/utils/mpc/Bitwise256BitTestsContract.sol +91 -0
- package/contracts/mocks/utils/mpc/Comparison128BitTestsContract.sol +158 -0
- package/contracts/mocks/utils/mpc/Comparison256BitTestsContract.sol +158 -0
- package/contracts/mocks/utils/mpc/Miscellaneous128BitTestsContract.sol +185 -0
- package/contracts/mocks/utils/mpc/Miscellaneous256BitTestsContract.sol +190 -0
- package/contracts/utils/mpc/MpcCore.sol +2804 -180
- package/hardhat.config.ts +6 -2
- package/package.json +2 -2
- package/test/token/PrivateERC20/PrivateERC20.test.ts +3 -3
- package/test/utils/mpc/Unsigned128BitIntegers.test.ts +945 -0
- package/test/utils/mpc/Unsigned256BitIntegers.test.ts +1124 -0
- package/test/utils/mpc/helpers.ts +77 -0
- package/typechain-types/contracts/mocks/utils/mpc/Arithmetic128BitTestsContract.ts +341 -0
- package/typechain-types/contracts/mocks/utils/mpc/Arithmetic256BitTestsContract.ts +347 -0
- package/typechain-types/contracts/mocks/utils/mpc/Bitwise128BitTestsContract.ts +186 -0
- package/typechain-types/contracts/mocks/utils/mpc/Bitwise256BitTestsContract.ts +186 -0
- package/typechain-types/contracts/mocks/utils/mpc/Comparison128BitTestsContract.ts +260 -0
- package/typechain-types/contracts/mocks/utils/mpc/Comparison256BitTestsContract.ts +260 -0
- package/typechain-types/contracts/mocks/utils/mpc/Miscellaneous128BitTestsContract.ts +302 -0
- package/typechain-types/contracts/mocks/utils/mpc/Miscellaneous256BitTestsContract.ts +322 -0
- package/typechain-types/contracts/mocks/utils/mpc/index.ts +8 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Arithmetic128BitTestsContract__factory.ts +349 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Arithmetic256BitTestsContract__factory.ts +364 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Bitwise128BitTestsContract__factory.ts +182 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Bitwise256BitTestsContract__factory.ts +182 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison128BitTestsContract__factory.ts +255 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Comparison256BitTestsContract__factory.ts +255 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous128BitTestsContract__factory.ts +332 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/Miscellaneous256BitTestsContract__factory.ts +380 -0
- package/typechain-types/factories/contracts/mocks/utils/mpc/index.ts +8 -0
- package/typechain-types/hardhat.d.ts +144 -0
- package/typechain-types/index.ts +16 -0
- package/contracts/package.json +0 -11
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.19;
|
|
3
|
+
|
|
4
|
+
import "../../../utils/mpc/MpcCore.sol";
|
|
5
|
+
|
|
6
|
+
contract Arithmetic128BitTestsContract {
|
|
7
|
+
|
|
8
|
+
bool[] public overflows;
|
|
9
|
+
bool[] public overflowsLHS;
|
|
10
|
+
bool[] public overflowsRHS;
|
|
11
|
+
uint128[] public numbers;
|
|
12
|
+
uint128[] public numbersLHS;
|
|
13
|
+
uint128[] public numbersRHS;
|
|
14
|
+
|
|
15
|
+
function addTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
16
|
+
require(a.length == b.length, "Input length mismatch");
|
|
17
|
+
|
|
18
|
+
_resetNumbers(a.length);
|
|
19
|
+
|
|
20
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
21
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
22
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
23
|
+
|
|
24
|
+
numbers[i] = MpcCore.decrypt(MpcCore.add(gtA, gtB));
|
|
25
|
+
|
|
26
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.add(a[i], gtB)));
|
|
27
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.add(gtA, b[i])));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function checkedAddTest(uint128 a, uint128 b) public {
|
|
32
|
+
_resetNumbers(1);
|
|
33
|
+
|
|
34
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a);
|
|
35
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b);
|
|
36
|
+
|
|
37
|
+
numbers[0] = MpcCore.decrypt(MpcCore.checkedAdd(gtA, gtB));
|
|
38
|
+
|
|
39
|
+
assert(numbers[0] == MpcCore.decrypt(MpcCore.checkedAdd(a, gtB)));
|
|
40
|
+
assert(numbers[0] == MpcCore.decrypt(MpcCore.checkedAdd(gtA, b)));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function checkedAddWithOverflowBitTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
44
|
+
require(a.length == b.length, "Input length mismatch");
|
|
45
|
+
|
|
46
|
+
_resetOverflows(a.length);
|
|
47
|
+
_resetOverflowsLHS(a.length);
|
|
48
|
+
_resetOverflowsRHS(a.length);
|
|
49
|
+
_resetNumbers(a.length);
|
|
50
|
+
_resetNumbersLHS(a.length);
|
|
51
|
+
_resetNumbersRHS(a.length);
|
|
52
|
+
|
|
53
|
+
gtBool bit;
|
|
54
|
+
gtBool bitLHS;
|
|
55
|
+
gtBool bitRHS;
|
|
56
|
+
gtUint128 memory result;
|
|
57
|
+
gtUint128 memory resultLHS;
|
|
58
|
+
gtUint128 memory resultRHS;
|
|
59
|
+
|
|
60
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
61
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
62
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
63
|
+
|
|
64
|
+
(bit, result) = MpcCore.checkedAddWithOverflowBit(gtA, gtB);
|
|
65
|
+
(bitLHS, resultLHS) = MpcCore.checkedAddWithOverflowBit(a[i], gtB);
|
|
66
|
+
(bitRHS, resultRHS) = MpcCore.checkedAddWithOverflowBit(gtA, b[i]);
|
|
67
|
+
|
|
68
|
+
overflows[i] = MpcCore.decrypt(bit);
|
|
69
|
+
overflowsLHS[i] = MpcCore.decrypt(bitLHS);
|
|
70
|
+
overflowsRHS[i] = MpcCore.decrypt(bitRHS);
|
|
71
|
+
numbers[i] = MpcCore.decrypt(result);
|
|
72
|
+
numbersLHS[i] = MpcCore.decrypt(resultLHS);
|
|
73
|
+
numbersRHS[i] = MpcCore.decrypt(resultRHS);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function subTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
78
|
+
require(a.length == b.length, "Input length mismatch");
|
|
79
|
+
|
|
80
|
+
_resetNumbers(a.length);
|
|
81
|
+
|
|
82
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
83
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
84
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
85
|
+
|
|
86
|
+
numbers[i] = MpcCore.decrypt(MpcCore.sub(gtA, gtB));
|
|
87
|
+
|
|
88
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.sub(a[i], gtB)));
|
|
89
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.sub(gtA, b[i])));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function checkedSubTest(uint128 a, uint128 b) public {
|
|
94
|
+
_resetNumbers(1);
|
|
95
|
+
|
|
96
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a);
|
|
97
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b);
|
|
98
|
+
|
|
99
|
+
numbers[0] = MpcCore.decrypt(MpcCore.checkedSub(gtA, gtB));
|
|
100
|
+
|
|
101
|
+
assert(numbers[0] == MpcCore.decrypt(MpcCore.checkedSub(a, gtB)));
|
|
102
|
+
assert(numbers[0] == MpcCore.decrypt(MpcCore.checkedSub(gtA, b)));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function checkedSubWithOverflowBitTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
106
|
+
require(a.length == b.length, "Input length mismatch");
|
|
107
|
+
|
|
108
|
+
_resetOverflows(a.length);
|
|
109
|
+
_resetOverflowsLHS(a.length);
|
|
110
|
+
_resetOverflowsRHS(a.length);
|
|
111
|
+
_resetNumbers(a.length);
|
|
112
|
+
_resetNumbersLHS(a.length);
|
|
113
|
+
_resetNumbersRHS(a.length);
|
|
114
|
+
|
|
115
|
+
gtBool bit;
|
|
116
|
+
gtBool bitLHS;
|
|
117
|
+
gtBool bitRHS;
|
|
118
|
+
gtUint128 memory result;
|
|
119
|
+
gtUint128 memory resultLHS;
|
|
120
|
+
gtUint128 memory resultRHS;
|
|
121
|
+
|
|
122
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
123
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
124
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
125
|
+
|
|
126
|
+
(bit, result) = MpcCore.checkedSubWithOverflowBit(gtA, gtB);
|
|
127
|
+
(bitLHS, resultLHS) = MpcCore.checkedSubWithOverflowBit(a[i], gtB);
|
|
128
|
+
(bitRHS, resultRHS) = MpcCore.checkedSubWithOverflowBit(gtA, b[i]);
|
|
129
|
+
|
|
130
|
+
overflows[i] = MpcCore.decrypt(bit);
|
|
131
|
+
overflowsLHS[i] = MpcCore.decrypt(bitLHS);
|
|
132
|
+
overflowsRHS[i] = MpcCore.decrypt(bitRHS);
|
|
133
|
+
numbers[i] = MpcCore.decrypt(result);
|
|
134
|
+
numbersLHS[i] = MpcCore.decrypt(resultLHS);
|
|
135
|
+
numbersRHS[i] = MpcCore.decrypt(resultRHS);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function mulTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
140
|
+
require(a.length == b.length, "Input length mismatch");
|
|
141
|
+
|
|
142
|
+
_resetNumbers(a.length);
|
|
143
|
+
|
|
144
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
145
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
146
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
147
|
+
|
|
148
|
+
numbers[i] = MpcCore.decrypt(MpcCore.mul(gtA, gtB));
|
|
149
|
+
|
|
150
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.mul(a[i], gtB)));
|
|
151
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.mul(gtA, b[i])));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function checkedMulTest(uint128 a, uint128 b) public {
|
|
156
|
+
_resetNumbers(1);
|
|
157
|
+
|
|
158
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a);
|
|
159
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b);
|
|
160
|
+
|
|
161
|
+
numbers[0] = MpcCore.decrypt(MpcCore.checkedMul(gtA, gtB));
|
|
162
|
+
|
|
163
|
+
assert(numbers[0] == MpcCore.decrypt(MpcCore.checkedMul(a, gtB)));
|
|
164
|
+
assert(numbers[0] == MpcCore.decrypt(MpcCore.checkedMul(gtA, b)));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function checkedMulWithOverflowBitTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
168
|
+
require(a.length == b.length, "Input length mismatch");
|
|
169
|
+
|
|
170
|
+
_resetOverflows(a.length);
|
|
171
|
+
_resetOverflowsLHS(a.length);
|
|
172
|
+
_resetOverflowsRHS(a.length);
|
|
173
|
+
_resetNumbers(a.length);
|
|
174
|
+
_resetNumbersLHS(a.length);
|
|
175
|
+
_resetNumbersRHS(a.length);
|
|
176
|
+
|
|
177
|
+
gtBool bit;
|
|
178
|
+
gtBool bitLHS;
|
|
179
|
+
gtBool bitRHS;
|
|
180
|
+
gtUint128 memory result;
|
|
181
|
+
gtUint128 memory resultLHS;
|
|
182
|
+
gtUint128 memory resultRHS;
|
|
183
|
+
|
|
184
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
185
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
186
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
187
|
+
|
|
188
|
+
(bit, result) = MpcCore.checkedMulWithOverflowBit(gtA, gtB);
|
|
189
|
+
(bitLHS, resultLHS) = MpcCore.checkedMulWithOverflowBit(a[i], gtB);
|
|
190
|
+
(bitRHS, resultRHS) = MpcCore.checkedMulWithOverflowBit(gtA, b[i]);
|
|
191
|
+
|
|
192
|
+
overflows[i] = MpcCore.decrypt(bit);
|
|
193
|
+
overflowsLHS[i] = MpcCore.decrypt(bitLHS);
|
|
194
|
+
overflowsRHS[i] = MpcCore.decrypt(bitRHS);
|
|
195
|
+
numbers[i] = MpcCore.decrypt(result);
|
|
196
|
+
numbersLHS[i] = MpcCore.decrypt(resultLHS);
|
|
197
|
+
numbersRHS[i] = MpcCore.decrypt(resultRHS);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function _resetOverflows(uint256 length) internal {
|
|
202
|
+
// Reset the overflows array
|
|
203
|
+
delete overflows;
|
|
204
|
+
|
|
205
|
+
// Resize the overflows array to match input length
|
|
206
|
+
for(uint i = 0; i < length; i++) {
|
|
207
|
+
overflows.push(false);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function _resetOverflowsLHS(uint256 length) internal {
|
|
212
|
+
// Reset the overflows array
|
|
213
|
+
delete overflowsLHS;
|
|
214
|
+
|
|
215
|
+
// Resize the overflows array to match input length
|
|
216
|
+
for(uint i = 0; i < length; i++) {
|
|
217
|
+
overflowsLHS.push(false);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function _resetOverflowsRHS(uint256 length) internal {
|
|
222
|
+
// Reset the overflows array
|
|
223
|
+
delete overflowsRHS;
|
|
224
|
+
|
|
225
|
+
// Resize the overflows array to match input length
|
|
226
|
+
for(uint i = 0; i < length; i++) {
|
|
227
|
+
overflowsRHS.push(false);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function _resetNumbers(uint256 length) internal {
|
|
232
|
+
// Reset the numbers array
|
|
233
|
+
delete numbers;
|
|
234
|
+
|
|
235
|
+
// Resize the numbers array to match input length
|
|
236
|
+
for(uint i = 0; i < length; i++) {
|
|
237
|
+
numbers.push(0);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function _resetNumbersLHS(uint256 length) internal {
|
|
242
|
+
// Reset the numbers array
|
|
243
|
+
delete numbersLHS;
|
|
244
|
+
|
|
245
|
+
// Resize the numbers array to match input length
|
|
246
|
+
for(uint i = 0; i < length; i++) {
|
|
247
|
+
numbersLHS.push(0);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function _resetNumbersRHS(uint256 length) internal {
|
|
252
|
+
// Reset the numbers array
|
|
253
|
+
delete numbersRHS;
|
|
254
|
+
|
|
255
|
+
// Resize the numbers array to match input length
|
|
256
|
+
for(uint i = 0; i < length; i++) {
|
|
257
|
+
numbersRHS.push(0);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.19;
|
|
3
|
+
|
|
4
|
+
import "../../../utils/mpc/MpcCore.sol";
|
|
5
|
+
|
|
6
|
+
contract Arithmetic256BitTestsContract {
|
|
7
|
+
|
|
8
|
+
enum PrivateInput {
|
|
9
|
+
BOTH,
|
|
10
|
+
LHS,
|
|
11
|
+
RHS
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
bool[] public overflows;
|
|
15
|
+
bool[] public overflowsLHS;
|
|
16
|
+
bool[] public overflowsRHS;
|
|
17
|
+
uint256[] public numbers2;
|
|
18
|
+
uint256[] public numbersLHS2;
|
|
19
|
+
uint256[] public numbersRHS2;
|
|
20
|
+
|
|
21
|
+
function addTest(uint256[] calldata a, uint256[] calldata b) public {
|
|
22
|
+
require(a.length == b.length, "Input length mismatch");
|
|
23
|
+
|
|
24
|
+
_resetNumbers2(a.length);
|
|
25
|
+
|
|
26
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
27
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
28
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
29
|
+
|
|
30
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.add(gtA, gtB));
|
|
31
|
+
|
|
32
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.add(a[i], gtB)));
|
|
33
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.add(gtA, b[i])));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function checkedAddTest(uint256 a, uint256 b) public {
|
|
38
|
+
_resetNumbers2(1);
|
|
39
|
+
|
|
40
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a);
|
|
41
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b);
|
|
42
|
+
|
|
43
|
+
numbers2[0] = MpcCore.decrypt(MpcCore.checkedAdd(gtA, gtB));
|
|
44
|
+
|
|
45
|
+
assert(numbers2[0] == MpcCore.decrypt(MpcCore.checkedAdd(a, gtB)));
|
|
46
|
+
assert(numbers2[0] == MpcCore.decrypt(MpcCore.checkedAdd(gtA, b)));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function checkedAddWithOverflowBitTest(uint256[] calldata a, uint256[] calldata b) public {
|
|
50
|
+
require(a.length == b.length, "Input length mismatch");
|
|
51
|
+
|
|
52
|
+
_resetOverflows(a.length);
|
|
53
|
+
_resetOverflowsLHS(a.length);
|
|
54
|
+
_resetOverflowsRHS(a.length);
|
|
55
|
+
_resetNumbers2(a.length);
|
|
56
|
+
_resetNumbersLHS2(a.length);
|
|
57
|
+
_resetNumbersRHS2(a.length);
|
|
58
|
+
|
|
59
|
+
gtBool bit;
|
|
60
|
+
gtBool bitLHS;
|
|
61
|
+
gtBool bitRHS;
|
|
62
|
+
gtUint256 memory result;
|
|
63
|
+
gtUint256 memory resultLHS;
|
|
64
|
+
gtUint256 memory resultRHS;
|
|
65
|
+
|
|
66
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
67
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
68
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
69
|
+
|
|
70
|
+
(bit, result) = MpcCore.checkedAddWithOverflowBit(gtA, gtB);
|
|
71
|
+
(bitLHS, resultLHS) = MpcCore.checkedAddWithOverflowBit(a[i], gtB);
|
|
72
|
+
(bitRHS, resultRHS) = MpcCore.checkedAddWithOverflowBit(gtA, b[i]);
|
|
73
|
+
|
|
74
|
+
overflows[i] = MpcCore.decrypt(bit);
|
|
75
|
+
overflowsLHS[i] = MpcCore.decrypt(bitLHS);
|
|
76
|
+
overflowsRHS[i] = MpcCore.decrypt(bitRHS);
|
|
77
|
+
numbers2[i] = MpcCore.decrypt(result);
|
|
78
|
+
numbersLHS2[i] = MpcCore.decrypt(resultLHS);
|
|
79
|
+
numbersRHS2[i] = MpcCore.decrypt(resultRHS);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function subTest(uint256[] calldata a, uint256[] calldata b) public {
|
|
84
|
+
require(a.length == b.length, "Input length mismatch");
|
|
85
|
+
|
|
86
|
+
_resetNumbers2(a.length);
|
|
87
|
+
|
|
88
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
89
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
90
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
91
|
+
|
|
92
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.sub(gtA, gtB));
|
|
93
|
+
|
|
94
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.sub(a[i], gtB)));
|
|
95
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.sub(gtA, b[i])));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function checkedSubTest(uint256 a, uint256 b) public {
|
|
100
|
+
_resetNumbers2(1);
|
|
101
|
+
|
|
102
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a);
|
|
103
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b);
|
|
104
|
+
|
|
105
|
+
numbers2[0] = MpcCore.decrypt(MpcCore.checkedSub(gtA, gtB));
|
|
106
|
+
|
|
107
|
+
assert(numbers2[0] == MpcCore.decrypt(MpcCore.checkedSub(a, gtB)));
|
|
108
|
+
assert(numbers2[0] == MpcCore.decrypt(MpcCore.checkedSub(gtA, b)));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function checkedSubWithOverflowBitTest(uint256[] calldata a, uint256[] calldata b) public {
|
|
112
|
+
require(a.length == b.length, "Input length mismatch");
|
|
113
|
+
|
|
114
|
+
_resetOverflows(a.length);
|
|
115
|
+
_resetOverflowsLHS(a.length);
|
|
116
|
+
_resetOverflowsRHS(a.length);
|
|
117
|
+
_resetNumbers2(a.length);
|
|
118
|
+
_resetNumbersLHS2(a.length);
|
|
119
|
+
_resetNumbersRHS2(a.length);
|
|
120
|
+
|
|
121
|
+
gtBool bit;
|
|
122
|
+
gtBool bitLHS;
|
|
123
|
+
gtBool bitRHS;
|
|
124
|
+
gtUint256 memory result;
|
|
125
|
+
gtUint256 memory resultLHS;
|
|
126
|
+
gtUint256 memory resultRHS;
|
|
127
|
+
|
|
128
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
129
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
130
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
131
|
+
|
|
132
|
+
(bit, result) = MpcCore.checkedSubWithOverflowBit(gtA, gtB);
|
|
133
|
+
(bitLHS, resultLHS) = MpcCore.checkedSubWithOverflowBit(a[i], gtB);
|
|
134
|
+
(bitRHS, resultRHS) = MpcCore.checkedSubWithOverflowBit(gtA, b[i]);
|
|
135
|
+
|
|
136
|
+
overflows[i] = MpcCore.decrypt(bit);
|
|
137
|
+
overflowsLHS[i] = MpcCore.decrypt(bitLHS);
|
|
138
|
+
overflowsRHS[i] = MpcCore.decrypt(bitRHS);
|
|
139
|
+
numbers2[i] = MpcCore.decrypt(result);
|
|
140
|
+
numbersLHS2[i] = MpcCore.decrypt(resultLHS);
|
|
141
|
+
numbersRHS2[i] = MpcCore.decrypt(resultRHS);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function mulTest(uint256[] calldata a, uint256[] calldata b, PrivateInput privateInput) public {
|
|
146
|
+
require(a.length == b.length, "Input length mismatch");
|
|
147
|
+
|
|
148
|
+
_resetNumbers2(a.length);
|
|
149
|
+
|
|
150
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
151
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
152
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
153
|
+
|
|
154
|
+
if (privateInput == PrivateInput.BOTH) {
|
|
155
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.mul(gtA, gtB));
|
|
156
|
+
} else if (privateInput == PrivateInput.LHS) {
|
|
157
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.mul(gtA, b[i]));
|
|
158
|
+
} else if (privateInput == PrivateInput.RHS) {
|
|
159
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.mul(a[i], gtB));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function checkedMulTest(uint256 a, uint256 b, PrivateInput privateInput) public {
|
|
165
|
+
_resetNumbers2(1);
|
|
166
|
+
|
|
167
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a);
|
|
168
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b);
|
|
169
|
+
|
|
170
|
+
if (privateInput == PrivateInput.BOTH) {
|
|
171
|
+
numbers2[0] = MpcCore.decrypt(MpcCore.checkedMul(gtA, gtB));
|
|
172
|
+
} else if (privateInput == PrivateInput.LHS) {
|
|
173
|
+
numbers2[0] = MpcCore.decrypt(MpcCore.checkedMul(gtA, b));
|
|
174
|
+
} else if (privateInput == PrivateInput.RHS) {
|
|
175
|
+
numbers2[0] = MpcCore.decrypt(MpcCore.checkedMul(a, gtB));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function checkedMulWithOverflowBitTest(uint256[] calldata a, uint256[] calldata b, PrivateInput privateInput) public {
|
|
180
|
+
require(a.length == b.length, "Input length mismatch");
|
|
181
|
+
|
|
182
|
+
_resetOverflows(a.length);
|
|
183
|
+
_resetNumbers2(a.length);
|
|
184
|
+
|
|
185
|
+
gtBool bit;
|
|
186
|
+
gtUint256 memory result;
|
|
187
|
+
|
|
188
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
189
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
190
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
191
|
+
|
|
192
|
+
if (privateInput == PrivateInput.BOTH) {
|
|
193
|
+
(bit, result) = MpcCore.checkedMulWithOverflowBit(gtA, gtB);
|
|
194
|
+
} else if (privateInput == PrivateInput.LHS) {
|
|
195
|
+
(bit, result) = MpcCore.checkedMulWithOverflowBit(a[i], gtB);
|
|
196
|
+
} else if (privateInput == PrivateInput.RHS) {
|
|
197
|
+
(bit, result) = MpcCore.checkedMulWithOverflowBit(gtA, b[i]);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
overflows[i] = MpcCore.decrypt(bit);
|
|
201
|
+
numbers2[i] = MpcCore.decrypt(result);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function _resetOverflows(uint256 length) internal {
|
|
206
|
+
// Reset the overflows array
|
|
207
|
+
delete overflows;
|
|
208
|
+
|
|
209
|
+
// Resize the overflows array to match input length
|
|
210
|
+
for(uint i = 0; i < length; i++) {
|
|
211
|
+
overflows.push(false);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function _resetOverflowsLHS(uint256 length) internal {
|
|
216
|
+
// Reset the overflows array
|
|
217
|
+
delete overflowsLHS;
|
|
218
|
+
|
|
219
|
+
// Resize the overflows array to match input length
|
|
220
|
+
for(uint i = 0; i < length; i++) {
|
|
221
|
+
overflowsLHS.push(false);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function _resetOverflowsRHS(uint256 length) internal {
|
|
226
|
+
// Reset the overflows array
|
|
227
|
+
delete overflowsRHS;
|
|
228
|
+
|
|
229
|
+
// Resize the overflows array to match input length
|
|
230
|
+
for(uint i = 0; i < length; i++) {
|
|
231
|
+
overflowsRHS.push(false);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function _resetNumbers2(uint256 length) internal {
|
|
236
|
+
// Reset the numbers array
|
|
237
|
+
delete numbers2;
|
|
238
|
+
|
|
239
|
+
// Resize the numbers array to match input length
|
|
240
|
+
for(uint i = 0; i < length; i++) {
|
|
241
|
+
numbers2.push(0);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function _resetNumbersLHS2(uint256 length) internal {
|
|
246
|
+
// Reset the numbers array
|
|
247
|
+
delete numbersLHS2;
|
|
248
|
+
|
|
249
|
+
// Resize the numbers array to match input length
|
|
250
|
+
for(uint i = 0; i < length; i++) {
|
|
251
|
+
numbersLHS2.push(0);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function _resetNumbersRHS2(uint256 length) internal {
|
|
256
|
+
// Reset the numbers array
|
|
257
|
+
delete numbersRHS2;
|
|
258
|
+
|
|
259
|
+
// Resize the numbers array to match input length
|
|
260
|
+
for(uint i = 0; i < length; i++) {
|
|
261
|
+
numbersRHS2.push(0);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.19;
|
|
3
|
+
|
|
4
|
+
import "../../../utils/mpc/MpcCore.sol";
|
|
5
|
+
|
|
6
|
+
contract Bitwise128BitTestsContract {
|
|
7
|
+
|
|
8
|
+
uint128[] public numbers;
|
|
9
|
+
|
|
10
|
+
function andTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
11
|
+
require(a.length == b.length, "Input length mismatch");
|
|
12
|
+
|
|
13
|
+
_resetNumbers(a.length);
|
|
14
|
+
|
|
15
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
16
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
17
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
18
|
+
|
|
19
|
+
numbers[i] = MpcCore.decrypt(MpcCore.and(gtA, gtB));
|
|
20
|
+
|
|
21
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.and(a[i], gtB)));
|
|
22
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.and(gtA, b[i])));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function orTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
27
|
+
require(a.length == b.length, "Input length mismatch");
|
|
28
|
+
|
|
29
|
+
_resetNumbers(a.length);
|
|
30
|
+
|
|
31
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
32
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
33
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
34
|
+
|
|
35
|
+
numbers[i] = MpcCore.decrypt(MpcCore.or(gtA, gtB));
|
|
36
|
+
|
|
37
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.or(a[i], gtB)));
|
|
38
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.or(gtA, b[i])));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function xorTest(uint128[] calldata a, uint128[] calldata b) public {
|
|
43
|
+
require(a.length == b.length, "Input length mismatch");
|
|
44
|
+
|
|
45
|
+
_resetNumbers(a.length);
|
|
46
|
+
|
|
47
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
48
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
49
|
+
gtUint128 memory gtB = MpcCore.setPublic128(b[i]);
|
|
50
|
+
|
|
51
|
+
numbers[i] = MpcCore.decrypt(MpcCore.xor(gtA, gtB));
|
|
52
|
+
|
|
53
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.xor(a[i], gtB)));
|
|
54
|
+
assert(numbers[i] == MpcCore.decrypt(MpcCore.xor(gtA, b[i])));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function shlTest(uint128[] calldata a, uint8[] calldata b) public {
|
|
59
|
+
require(a.length == b.length, "Input length mismatch");
|
|
60
|
+
|
|
61
|
+
_resetNumbers(a.length);
|
|
62
|
+
|
|
63
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
64
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
65
|
+
|
|
66
|
+
numbers[i] = MpcCore.decrypt(MpcCore.shl(gtA, b[i]));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function shrTest(uint128[] calldata a, uint8[] calldata b) public {
|
|
71
|
+
require(a.length == b.length, "Input length mismatch");
|
|
72
|
+
|
|
73
|
+
_resetNumbers(a.length);
|
|
74
|
+
|
|
75
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
76
|
+
gtUint128 memory gtA = MpcCore.setPublic128(a[i]);
|
|
77
|
+
|
|
78
|
+
numbers[i] = MpcCore.decrypt(MpcCore.shr(gtA, b[i]));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function _resetNumbers(uint256 length) internal {
|
|
83
|
+
// Reset the numbers array
|
|
84
|
+
delete numbers;
|
|
85
|
+
|
|
86
|
+
// Resize the numbers array to match input length
|
|
87
|
+
for(uint i = 0; i < length; i++) {
|
|
88
|
+
numbers.push(0);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.19;
|
|
3
|
+
|
|
4
|
+
import "../../../utils/mpc/MpcCore.sol";
|
|
5
|
+
|
|
6
|
+
contract Bitwise256BitTestsContract {
|
|
7
|
+
|
|
8
|
+
uint256[] public numbers2;
|
|
9
|
+
|
|
10
|
+
function andTest(uint256[] calldata a, uint256[] calldata b) public {
|
|
11
|
+
require(a.length == b.length, "Input length mismatch");
|
|
12
|
+
|
|
13
|
+
_resetNumbers2(a.length);
|
|
14
|
+
|
|
15
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
16
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
17
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
18
|
+
|
|
19
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.and(gtA, gtB));
|
|
20
|
+
|
|
21
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.and(a[i], gtB)));
|
|
22
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.and(gtA, b[i])));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function orTest(uint256[] calldata a, uint256[] calldata b) public {
|
|
27
|
+
require(a.length == b.length, "Input length mismatch");
|
|
28
|
+
|
|
29
|
+
_resetNumbers2(a.length);
|
|
30
|
+
|
|
31
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
32
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
33
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
34
|
+
|
|
35
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.or(gtA, gtB));
|
|
36
|
+
|
|
37
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.or(a[i], gtB)));
|
|
38
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.or(gtA, b[i])));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function xorTest(uint256[] calldata a, uint256[] calldata b) public {
|
|
43
|
+
require(a.length == b.length, "Input length mismatch");
|
|
44
|
+
|
|
45
|
+
_resetNumbers2(a.length);
|
|
46
|
+
|
|
47
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
48
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
49
|
+
gtUint256 memory gtB = MpcCore.setPublic256(b[i]);
|
|
50
|
+
|
|
51
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.xor(gtA, gtB));
|
|
52
|
+
|
|
53
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.xor(a[i], gtB)));
|
|
54
|
+
assert(numbers2[i] == MpcCore.decrypt(MpcCore.xor(gtA, b[i])));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function shlTest(uint256[] calldata a, uint8[] calldata b) public {
|
|
59
|
+
require(a.length == b.length, "Input length mismatch");
|
|
60
|
+
|
|
61
|
+
_resetNumbers2(a.length);
|
|
62
|
+
|
|
63
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
64
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
65
|
+
|
|
66
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.shl(gtA, b[i]));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function shrTest(uint256[] calldata a, uint8[] calldata b) public {
|
|
71
|
+
require(a.length == b.length, "Input length mismatch");
|
|
72
|
+
|
|
73
|
+
_resetNumbers2(a.length);
|
|
74
|
+
|
|
75
|
+
for (uint256 i = 0; i < a.length; ++i) {
|
|
76
|
+
gtUint256 memory gtA = MpcCore.setPublic256(a[i]);
|
|
77
|
+
|
|
78
|
+
numbers2[i] = MpcCore.decrypt(MpcCore.shr(gtA, b[i]));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function _resetNumbers2(uint256 length) internal {
|
|
83
|
+
// Reset the numbers array
|
|
84
|
+
delete numbers2;
|
|
85
|
+
|
|
86
|
+
// Resize the numbers array to match input length
|
|
87
|
+
for(uint i = 0; i < length; i++) {
|
|
88
|
+
numbers2.push(0);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|