|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import {BN254} from "../../src/libraries/BN254.sol"; |
| 5 | +import { |
| 6 | + BLSSigCheckUtils, |
| 7 | + Comparators, |
| 8 | + SlotDerivation, |
| 9 | + Arrays |
| 10 | +} from "../../src/unaudited/BLSSigCheckUtils.sol"; |
| 11 | +import {StorageSlot} from "@openzeppelin/contracts/utils/StorageSlot.sol"; |
| 12 | + |
| 13 | +/** |
| 14 | + * @title BLSSigCheckUtilsHarness |
| 15 | + * @notice Test harness to expose internal functions from BLSSigCheckUtils and its libraries for testing |
| 16 | + */ |
| 17 | +contract BLSSigCheckUtilsHarness { |
| 18 | + using BN254 for BN254.G1Point; |
| 19 | + using BLSSigCheckUtils for BN254.G1Point; |
| 20 | + using SlotDerivation for bytes32; |
| 21 | + using SlotDerivation for string; |
| 22 | + using Arrays for uint256[]; |
| 23 | + using Arrays for address[]; |
| 24 | + using Arrays for bytes32[]; |
| 25 | + using Arrays for bytes[]; |
| 26 | + using Arrays for string[]; |
| 27 | + |
| 28 | + // Storage arrays for testing storage-related functions |
| 29 | + uint256[] public uint256Array; |
| 30 | + address[] public addressArray; |
| 31 | + bytes32[] public bytes32Array; |
| 32 | + bytes[] public bytesArray; |
| 33 | + string[] public stringArray; |
| 34 | + |
| 35 | + /** |
| 36 | + * |
| 37 | + * BLSSigCheckUtils functions |
| 38 | + * |
| 39 | + */ |
| 40 | + function isOnCurve( |
| 41 | + BN254.G1Point memory p |
| 42 | + ) public pure returns (bool) { |
| 43 | + return p.isOnCurve(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * |
| 48 | + * Comparators library functions |
| 49 | + * |
| 50 | + */ |
| 51 | + function lt(uint256 a, uint256 b) public pure returns (bool) { |
| 52 | + return Comparators.lt(a, b); |
| 53 | + } |
| 54 | + |
| 55 | + function gt(uint256 a, uint256 b) public pure returns (bool) { |
| 56 | + return Comparators.gt(a, b); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * |
| 61 | + * SlotDerivation library functions |
| 62 | + * |
| 63 | + */ |
| 64 | + function erc7201Slot( |
| 65 | + string memory namespace |
| 66 | + ) public pure returns (bytes32) { |
| 67 | + return namespace.erc7201Slot(); |
| 68 | + } |
| 69 | + |
| 70 | + function offset(bytes32 slot, uint256 pos) public pure returns (bytes32) { |
| 71 | + return slot.offset(pos); |
| 72 | + } |
| 73 | + |
| 74 | + function deriveArray( |
| 75 | + bytes32 slot |
| 76 | + ) public pure returns (bytes32) { |
| 77 | + return slot.deriveArray(); |
| 78 | + } |
| 79 | + |
| 80 | + function deriveMappingAddress(bytes32 slot, address key) public pure returns (bytes32) { |
| 81 | + return slot.deriveMapping(key); |
| 82 | + } |
| 83 | + |
| 84 | + function deriveMappingBool(bytes32 slot, bool key) public pure returns (bytes32) { |
| 85 | + return slot.deriveMapping(key); |
| 86 | + } |
| 87 | + |
| 88 | + function deriveMappingBytes32(bytes32 slot, bytes32 key) public pure returns (bytes32) { |
| 89 | + return slot.deriveMapping(key); |
| 90 | + } |
| 91 | + |
| 92 | + function deriveMappingUint256(bytes32 slot, uint256 key) public pure returns (bytes32) { |
| 93 | + return slot.deriveMapping(key); |
| 94 | + } |
| 95 | + |
| 96 | + function deriveMappingInt256(bytes32 slot, int256 key) public pure returns (bytes32) { |
| 97 | + return slot.deriveMapping(key); |
| 98 | + } |
| 99 | + |
| 100 | + function deriveMappingString(bytes32 slot, string memory key) public pure returns (bytes32) { |
| 101 | + return slot.deriveMapping(key); |
| 102 | + } |
| 103 | + |
| 104 | + function deriveMappingBytes(bytes32 slot, bytes memory key) public pure returns (bytes32) { |
| 105 | + return slot.deriveMapping(key); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * |
| 110 | + * Arrays library functions - Sorting |
| 111 | + * |
| 112 | + */ |
| 113 | + function sortUint256( |
| 114 | + uint256[] memory array |
| 115 | + ) public pure returns (uint256[] memory) { |
| 116 | + return array.sort(); |
| 117 | + } |
| 118 | + |
| 119 | + function sortAddress( |
| 120 | + address[] memory array |
| 121 | + ) public pure returns (address[] memory) { |
| 122 | + return array.sort(); |
| 123 | + } |
| 124 | + |
| 125 | + function sortBytes32( |
| 126 | + bytes32[] memory array |
| 127 | + ) public pure returns (bytes32[] memory) { |
| 128 | + return array.sort(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * |
| 133 | + * Arrays library functions - Binary Search |
| 134 | + * |
| 135 | + */ |
| 136 | + function findUpperBound( |
| 137 | + uint256 element |
| 138 | + ) public view returns (uint256) { |
| 139 | + return uint256Array.findUpperBound(element); |
| 140 | + } |
| 141 | + |
| 142 | + function lowerBound( |
| 143 | + uint256 element |
| 144 | + ) public view returns (uint256) { |
| 145 | + return uint256Array.lowerBound(element); |
| 146 | + } |
| 147 | + |
| 148 | + function upperBound( |
| 149 | + uint256 element |
| 150 | + ) public view returns (uint256) { |
| 151 | + return uint256Array.upperBound(element); |
| 152 | + } |
| 153 | + |
| 154 | + function lowerBoundMemory( |
| 155 | + uint256[] memory array, |
| 156 | + uint256 element |
| 157 | + ) public pure returns (uint256) { |
| 158 | + return array.lowerBoundMemory(element); |
| 159 | + } |
| 160 | + |
| 161 | + function upperBoundMemory( |
| 162 | + uint256[] memory array, |
| 163 | + uint256 element |
| 164 | + ) public pure returns (uint256) { |
| 165 | + return array.upperBoundMemory(element); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * |
| 170 | + * Arrays library functions - Unsafe Access |
| 171 | + * |
| 172 | + */ |
| 173 | + function unsafeAccessAddress( |
| 174 | + uint256 pos |
| 175 | + ) public view returns (address) { |
| 176 | + return addressArray.unsafeAccess(pos).value; |
| 177 | + } |
| 178 | + |
| 179 | + function unsafeAccessBytes32( |
| 180 | + uint256 pos |
| 181 | + ) public view returns (bytes32) { |
| 182 | + return bytes32Array.unsafeAccess(pos).value; |
| 183 | + } |
| 184 | + |
| 185 | + function unsafeAccessUint256( |
| 186 | + uint256 pos |
| 187 | + ) public view returns (uint256) { |
| 188 | + return uint256Array.unsafeAccess(pos).value; |
| 189 | + } |
| 190 | + |
| 191 | + function unsafeAccessBytes( |
| 192 | + uint256 pos |
| 193 | + ) public view returns (bytes memory) { |
| 194 | + return bytesArray.unsafeAccess(pos).value; |
| 195 | + } |
| 196 | + |
| 197 | + function unsafeAccessString( |
| 198 | + uint256 pos |
| 199 | + ) public view returns (string memory) { |
| 200 | + return stringArray.unsafeAccess(pos).value; |
| 201 | + } |
| 202 | + |
| 203 | + function unsafeMemoryAccessAddress( |
| 204 | + address[] memory arr, |
| 205 | + uint256 pos |
| 206 | + ) public pure returns (address) { |
| 207 | + return arr.unsafeMemoryAccess(pos); |
| 208 | + } |
| 209 | + |
| 210 | + function unsafeMemoryAccessBytes32( |
| 211 | + bytes32[] memory arr, |
| 212 | + uint256 pos |
| 213 | + ) public pure returns (bytes32) { |
| 214 | + return arr.unsafeMemoryAccess(pos); |
| 215 | + } |
| 216 | + |
| 217 | + function unsafeMemoryAccessUint256( |
| 218 | + uint256[] memory arr, |
| 219 | + uint256 pos |
| 220 | + ) public pure returns (uint256) { |
| 221 | + return arr.unsafeMemoryAccess(pos); |
| 222 | + } |
| 223 | + |
| 224 | + function unsafeMemoryAccessBytes( |
| 225 | + bytes[] memory arr, |
| 226 | + uint256 pos |
| 227 | + ) public pure returns (bytes memory) { |
| 228 | + return arr.unsafeMemoryAccess(pos); |
| 229 | + } |
| 230 | + |
| 231 | + function unsafeMemoryAccessString( |
| 232 | + string[] memory arr, |
| 233 | + uint256 pos |
| 234 | + ) public pure returns (string memory) { |
| 235 | + return arr.unsafeMemoryAccess(pos); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * |
| 240 | + * Arrays library functions - Unsafe Set Length |
| 241 | + * |
| 242 | + */ |
| 243 | + function unsafeSetLengthAddress( |
| 244 | + uint256 len |
| 245 | + ) public { |
| 246 | + addressArray.unsafeSetLength(len); |
| 247 | + } |
| 248 | + |
| 249 | + function unsafeSetLengthBytes32( |
| 250 | + uint256 len |
| 251 | + ) public { |
| 252 | + bytes32Array.unsafeSetLength(len); |
| 253 | + } |
| 254 | + |
| 255 | + function unsafeSetLengthUint256( |
| 256 | + uint256 len |
| 257 | + ) public { |
| 258 | + uint256Array.unsafeSetLength(len); |
| 259 | + } |
| 260 | + |
| 261 | + function unsafeSetLengthBytes( |
| 262 | + uint256 len |
| 263 | + ) public { |
| 264 | + bytesArray.unsafeSetLength(len); |
| 265 | + } |
| 266 | + |
| 267 | + function unsafeSetLengthString( |
| 268 | + uint256 len |
| 269 | + ) public { |
| 270 | + stringArray.unsafeSetLength(len); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * |
| 275 | + * Helper functions for testing |
| 276 | + * |
| 277 | + */ |
| 278 | + |
| 279 | + // Initialize test arrays |
| 280 | + function initializeUint256Array( |
| 281 | + uint256[] memory values |
| 282 | + ) public { |
| 283 | + delete uint256Array; |
| 284 | + for (uint256 i = 0; i < values.length; i++) { |
| 285 | + uint256Array.push(values[i]); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + function initializeAddressArray( |
| 290 | + address[] memory values |
| 291 | + ) public { |
| 292 | + delete addressArray; |
| 293 | + for (uint256 i = 0; i < values.length; i++) { |
| 294 | + addressArray.push(values[i]); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + function initializeBytes32Array( |
| 299 | + bytes32[] memory values |
| 300 | + ) public { |
| 301 | + delete bytes32Array; |
| 302 | + for (uint256 i = 0; i < values.length; i++) { |
| 303 | + bytes32Array.push(values[i]); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + function initializeBytesArray( |
| 308 | + bytes[] memory values |
| 309 | + ) public { |
| 310 | + delete bytesArray; |
| 311 | + for (uint256 i = 0; i < values.length; i++) { |
| 312 | + bytesArray.push(values[i]); |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + function initializeStringArray( |
| 317 | + string[] memory values |
| 318 | + ) public { |
| 319 | + delete stringArray; |
| 320 | + for (uint256 i = 0; i < values.length; i++) { |
| 321 | + stringArray.push(values[i]); |
| 322 | + } |
| 323 | + } |
| 324 | + |
| 325 | + // Getters for array lengths |
| 326 | + function getUint256ArrayLength() public view returns (uint256) { |
| 327 | + return uint256Array.length; |
| 328 | + } |
| 329 | + |
| 330 | + function getAddressArrayLength() public view returns (uint256) { |
| 331 | + return addressArray.length; |
| 332 | + } |
| 333 | + |
| 334 | + function getBytes32ArrayLength() public view returns (uint256) { |
| 335 | + return bytes32Array.length; |
| 336 | + } |
| 337 | + |
| 338 | + function getBytesArrayLength() public view returns (uint256) { |
| 339 | + return bytesArray.length; |
| 340 | + } |
| 341 | + |
| 342 | + function getStringArrayLength() public view returns (uint256) { |
| 343 | + return stringArray.length; |
| 344 | + } |
| 345 | + |
| 346 | + // Getters for full arrays |
| 347 | + function getUint256Array() public view returns (uint256[] memory) { |
| 348 | + return uint256Array; |
| 349 | + } |
| 350 | + |
| 351 | + function getAddressArray() public view returns (address[] memory) { |
| 352 | + return addressArray; |
| 353 | + } |
| 354 | + |
| 355 | + function getBytes32Array() public view returns (bytes32[] memory) { |
| 356 | + return bytes32Array; |
| 357 | + } |
| 358 | +} |
0 commit comments