Skip to content
Smart contracts Audit finding Advanced

YieldMath.sol / Log2: >= or > ?

The log_2 function in YieldMath.sol used a strict greater-than comparison instead of greater-than-or-equal, causing incorrect rounding in the logarithm calculation. This could lead to a slightly different or unpredictable yield curve in Pool.sol, potentially allowing pools to be slowly drained over time.

Yield Witch v2 contest

Originally disclosed on Code4rena

The V1 version of YieldMath.sol contains ”>=” (larger or equal), while the V2 version of YieldMath.sol contains ”>” (larger) in the log_2 function. This change doesn’t seem logical and might lead to miss calculations. The difference is present in several adjacent lines.

function log_2 (uint128 x)
...
b = b * b >> 127; if (b >= 0x100000000000000000000000000000000) {b >>= 1; l |= 0x1000000000000000000000000000000;}

and

function log_2(uint128 x)
...
b = b * b >> 127; if(b > 0x100000000000000000000000000000000) {b >>= 1; l |= 0x1000000000000000000000000000000;}

Recommend checking which version is the correct version and fix the incorrect version.

albertocuestacanada (Yield) confirmed:

That’s entirely my fault, and this is a scary one. We might be having a slightly different or impredictable curve in Pool.sol, and we might notice only after a long while with the Pools being slowly drained. We might never even have found this was the issue.

I would suggest increasing the severity of this issue to High.

albertocuestacanada (Yield) Resolved:

Fix

The original report as published on Code4rena. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely a typo in the comparison operator within the log_2 function, where '>' was used instead of '>='.

What the attacker needed

None

The fix

The issue was fixed by correcting the comparison operator in the log_2 function.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract calculates interest using a maths function that finds the logarithm of a number. The code used a 'greater than' check where it should have used 'greater than or equal to', so some values were rounded down incorrectly. Over time, this small error could let attackers take more money from the pool than they should.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Yield Witch v2 contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.