askrene: fix integer overflow for dummy channels #8129
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.
We may introduce high capacity channels in askrene to represent problems
with multiple destinations (eg. multiple blinded paths) or to solve
self-payments. The integer computation of the deliverable amount through
these channels (I tested this with a channel with 21M bitcoin) would
fail due to an integer overflow in the function
amount_msat_sub_fee
,21M BTC = 21 x 10^17 msat, which overflows u64 integers when multiplied
by 10^6. We have fixed
amount_msat_sub_fee
operations, so that itdoesn't overflow.
Basically we had the following integer operation:
where
a<b
anda=10^6
, so the result fits in u64. The problem is that we first computea*x
and then divide byb
.a*x
can overflow.We fixed the operation by splitting
x = q_x * b + r_x
whereq_x = floor(x/b)
andr_x = x mod b
, so thatour computation reduces to:
The value
a*q_x <= x
does not overflow. So the only dangerous operation isa*r_x
,but
a*r_x < a*b
, so ifa*b
does not overflow then the entire operation is safe to compute.In our case
a=10^6
andb = 10^6 + ppm
, so unlessppm ~ 10^12
, which is an absurd value,the above operation is guaranteed to be successful.