TOB-AJNA-2: global scalar (at index 8192) is never updated #753
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of change
High level
Deposits.treeSum
SLOAD
inDeposits.treeSum
as scalar is never updated (always1
)F5
Fenwick invariant to make sure global scalar is never updated. newPool.depositScale
external function added to support suchDescription of issue from ToB report:
findIndexAndSumOfSums
method ignores the global scalar of the scaled Fenwick treewhile calculating the smallest index at which the prefix sum is at least the given target
value.
underlying array up to that index. Similarly, scalars at power-of-two indices contain a
scaling factor by which all lower-index values should be multiplied to get the correct
underlying values and prefix sums.
findIndexAndSumOfSums
method performs a binary search starting from the middlepower-of-two index at 4096 (2^12). If the prefix sum up to that point is too small, the
algorithm checks higher indices and vice versa. But the global scalar at index 8192 (2^13) is
not visited by this method and its value is never considered. If the global scalar contains a
non-default value, then the indices and sums returned by
findIndexAndSumOfSums
willbe incorrect.
ToB reponse to us given we didn't resolve the issue:
leaving this issue unresolved introduces an informal invariant that says “the global scalar is never updated” and if this invariant does not hold, the findIndexAndSumOfSums method will break as described by this issue. Consider making this invariant explicit via tests & code comments and revisit existing Deposit methods with this new invariant in mind. For example, it appears that an SLOAD in the treeSum method could be optimized out
Gas usage
Pre Change
Post Change
Contract size
Pre Change
Post Change