11pragma solidity 0.8.9 ;
22
3+ import "./Constants.sol " ;
4+
35/// @notice The implicit 8-ary trees of the sortition pool
46/// rely on packing 8 "slots" of 32-bit values into each uint256.
57/// The Branch library permits efficient calculations on these slots.
68library Branch {
7- ////////////////////////////////////////////////////////////////////////////
8- // Parameters for configuration
9-
10- // How many bits a position uses per level of the tree;
11- // each branch of the tree contains 2**SLOT_BITS slots.
12- uint256 private constant SLOT_BITS = 3 ;
13- ////////////////////////////////////////////////////////////////////////////
14-
15- ////////////////////////////////////////////////////////////////////////////
16- // Derived constants, do not touch
17- uint256 private constant SLOT_COUNT = 2 ** SLOT_BITS;
18- uint256 private constant SLOT_WIDTH = 256 / SLOT_COUNT;
19- uint256 private constant LAST_SLOT = SLOT_COUNT - 1 ;
20- uint256 private constant SLOT_MAX = (2 ** SLOT_WIDTH) - 1 ;
21-
22- ////////////////////////////////////////////////////////////////////////////
23-
249 /// @notice Calculate the right shift required
2510 /// to make the 32 least significant bits of an uint256
2611 /// be the bits of the `position`th slot
@@ -31,7 +16,7 @@ library Branch {
3116 /// I wish solidity had macros, even C macros.
3217 function slotShift (uint256 position ) internal pure returns (uint256 ) {
3318 unchecked {
34- return position * SLOT_WIDTH;
19+ return position * Constants. SLOT_WIDTH;
3520 }
3621 }
3722
@@ -43,12 +28,12 @@ library Branch {
4328 returns (uint256 )
4429 {
4530 unchecked {
46- uint256 shiftBits = position * SLOT_WIDTH;
31+ uint256 shiftBits = position * Constants. SLOT_WIDTH;
4732 // Doing a bitwise AND with `SLOT_MAX`
4833 // clears all but the 32 least significant bits.
4934 // Because of the right shift by `slotShift(position)` bits,
5035 // those 32 bits contain the 32 bits in the `position`th slot of `node`.
51- return (node >> shiftBits) & SLOT_MAX;
36+ return (node >> shiftBits) & Constants. SLOT_MAX;
5237 }
5338 }
5439
@@ -59,7 +44,7 @@ library Branch {
5944 returns (uint256 )
6045 {
6146 unchecked {
62- uint256 shiftBits = position * SLOT_WIDTH;
47+ uint256 shiftBits = position * Constants. SLOT_WIDTH;
6348 // Shifting `SLOT_MAX` left by `slotShift(position)` bits
6449 // gives us a number where all bits of the `position`th slot are set,
6550 // and all other bits are unset.
@@ -71,7 +56,7 @@ library Branch {
7156 // Bitwise ANDing the original `node` with this number
7257 // sets the bits of `position`th slot to zero,
7358 // leaving all other bits unchanged.
74- return node & ~ (SLOT_MAX << shiftBits);
59+ return node & ~ (Constants. SLOT_MAX << shiftBits);
7560 }
7661 }
7762
@@ -86,17 +71,17 @@ library Branch {
8671 uint256 weight
8772 ) internal pure returns (uint256 ) {
8873 unchecked {
89- uint256 shiftBits = position * SLOT_WIDTH;
74+ uint256 shiftBits = position * Constants. SLOT_WIDTH;
9075 // Clear the `position`th slot like in `clearSlot()`.
91- uint256 clearedNode = node & ~ (SLOT_MAX << shiftBits);
76+ uint256 clearedNode = node & ~ (Constants. SLOT_MAX << shiftBits);
9277 // Bitwise AND `weight` with `SLOT_MAX`
9378 // to clear all but the 32 least significant bits.
9479 //
9580 // Shift this left by `slotShift(position)` bits
9681 // to obtain a uint256 with all bits unset
9782 // except in the `position`th slot
9883 // which contains the 32-bit value of `weight`.
99- uint256 shiftedWeight = (weight & SLOT_MAX) << shiftBits;
84+ uint256 shiftedWeight = (weight & Constants. SLOT_MAX) << shiftBits;
10085 // When we bitwise OR these together,
10186 // all other slots except the `position`th one come from the left argument,
10287 // and the `position`th gets filled with `weight` from the right argument.
@@ -107,14 +92,14 @@ library Branch {
10792 /// @notice Calculate the summed weight of all slots in the `node`.
10893 function sumWeight (uint256 node ) internal pure returns (uint256 sum ) {
10994 unchecked {
110- sum = node & SLOT_MAX;
95+ sum = node & Constants. SLOT_MAX;
11196 // Iterate through each slot
11297 // by shifting `node` right in increments of 32 bits,
11398 // and adding the 32 least significant bits to the `sum`.
114- uint256 newNode = node >> SLOT_WIDTH;
99+ uint256 newNode = node >> Constants. SLOT_WIDTH;
115100 while (newNode > 0 ) {
116- sum += (newNode & SLOT_MAX);
117- newNode = newNode >> SLOT_WIDTH;
101+ sum += (newNode & Constants. SLOT_MAX);
102+ newNode = newNode >> Constants. SLOT_WIDTH;
118103 }
119104 return sum;
120105 }
@@ -143,12 +128,12 @@ library Branch {
143128 unchecked {
144129 newIndex = index;
145130 uint256 newNode = node;
146- uint256 currentSlotWeight = newNode & SLOT_MAX;
131+ uint256 currentSlotWeight = newNode & Constants. SLOT_MAX;
147132 while (newIndex >= currentSlotWeight) {
148133 newIndex -= currentSlotWeight;
149134 slot++ ;
150- newNode = newNode >> SLOT_WIDTH;
151- currentSlotWeight = newNode & SLOT_MAX;
135+ newNode = newNode >> Constants. SLOT_WIDTH;
136+ currentSlotWeight = newNode & Constants. SLOT_MAX;
152137 }
153138 return (slot, newIndex);
154139 }
0 commit comments