diff --git a/contracts/test/Bitfield.t.sol b/contracts/test/Bitfield.t.sol new file mode 100644 index 0000000000..2334e399a4 --- /dev/null +++ b/contracts/test/Bitfield.t.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity 0.8.25; + +import {Test} from "forge-std/Test.sol"; +import {console} from "forge-std/console.sol"; +import {BitfieldWrapper} from "./mocks/BitfieldWrapper.sol"; +import {Bitfield} from "../src/utils/Bitfield.sol"; + +import {stdJson} from "forge-std/StdJson.sol"; + +contract BitfieldTest is Test { + using stdJson for string; + + function testBitfieldSubsampling() public { + BitfieldWrapper bw = new BitfieldWrapper(); + + string memory json = vm.readFile(string.concat(vm.projectRoot(), "/test/data/beefy-validator-set.json")); + uint32 setSize = uint32(json.readUint(".validatorSetSize")); + bytes32 root = json.readBytes32(".validatorRoot"); + uint256[] memory bitSetArray = json.readUintArray(".participants"); + + uint256[] memory initialBitField = bw.createBitfield(bitSetArray, setSize); + uint256[] memory finalBitfield = bw.subsample(67, initialBitField, 30, setSize); + + uint256 counter = 0; + for (uint256 i = 0; i < bitSetArray.length; i++) { + if (Bitfield.isSet(finalBitfield, bitSetArray[i])) { + counter++; + } + } + assertEq(30, counter); + assertEq(Bitfield.countSetBits(finalBitfield), counter); + } +} diff --git a/contracts/test/mocks/BitfieldWrapper.sol b/contracts/test/mocks/BitfieldWrapper.sol new file mode 100644 index 0000000000..7748608994 --- /dev/null +++ b/contracts/test/mocks/BitfieldWrapper.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity 0.8.25; + +import {Bitfield} from "../../src/utils/Bitfield.sol"; + +contract BitfieldWrapper { + function createBitfield(uint256[] calldata bitsToSet, uint256 length) + pure + public + returns (uint256[] memory bitfield) + { + return Bitfield.createBitfield(bitsToSet, length); + } + + function subsample(uint256 seed, uint256[] memory prior, uint256 n, uint256 length) public returns (uint256[] memory bitfield) { + return Bitfield.subsample(seed, prior, n, length); + } +} diff --git a/flake.nix b/flake.nix index 963315b94f..8529150e7b 100644 --- a/flake.nix +++ b/flake.nix @@ -54,7 +54,6 @@ gopls go-outline gopkgs - gocode-gomod godef golint mage