Skip to content
Business logic Audit finding Beginner

ExponentiationImpl::pow() returns 0 for 0^0

The ExponentiationImpl::pow() function returns 0 for 0^0 instead of the conventional value 1, because it checks for a zero base before considering a zero exponent. Future code relying on the standard mathematical convention could produce incorrect results.

RadiantLabs Follow with Bauchibred

Originally disclosed on Code4rena

Submitted by RadiantLabs, also found by Bauchibred

The ExponentiationImpl::pow() function in math.cairo incorrectly returns 0 when computing 0^0, instead of the mathematically accepted value of 1. This breaks a fundamental mathematical convention that is relied upon in many mathematical contexts, including polynomial evaluation, Taylor series, and combinatorial calculations.

The issue occurs because the function first checks if the base is zero and returns zero if true, without considering the special case where the exponent is also zero. This early return means that 0^0 evaluates to 0 instead of 1:

fn pow(self: T, mut exponent: T) -> T {
	let zero = Zero::zero();
	if self.is_zero() {
		return zero;
	}
	...

The mathematical definition of 0^0 = 1 is not arbitrary. It is the natural definition that makes many mathematical formulas and theorems work correctly. For example, this definition is necessary for:

  • The binomial theorem to work correctly when x=0.
  • Power series expansions to be valid at x=0.
  • Combinatorial formulas involving empty sets.
  • Preserving continuity in certain mathematical limits.

This function is not currently being used to compute 0^0 in the code in scope. However, given the critical nature of the function and fundamental incorrectness of its output, the expectation of this issue causing vulnerabilities in future code is fulfilled.

Impact

  • Mathematical operations that rely on the standard convention of 0^0 = 1 will produce incorrect results.
  • Future code that reaches this case in core Kakarot contracts, protocols built on top of Kakarot’s codebase or borrowing from it will experience material errors when processing edge cases

Recommended Mitigation Steps

Add a check for the 0^0 case before checking if the base is zero:

fn pow(self: T, mut exponent: T) -> T {
    // Handle 0^0 case first
    if self.is_zero() && exponent.is_zero() {
        return One::one();
    }

    // Rest of the existing function...
    if self.is_zero() {
        return Zero::zero();
    }
    // ...
}

This change preserves the mathematically correct behavior while maintaining all other functionality of the power function.

ClementWalter (Kakarot) confirmed and commented:

Severity: Medium

Kakarot mitigated:

PR 1579 and ssj PR 1022 fixes pow in SSJ.

Status: Mitigation confirmed.


The original report as published on Code4rena, by RadiantLabs. 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

The function returns zero as soon as it sees a zero base, without first checking whether the exponent is also zero.

Impact

Mathematical operations relying on the 0^0 = 1 convention could produce incorrect results, and future code reaching this edge case may experience material errors; the function is not currently used to compute 0^0 in the code in scope.

What the attacker needed

None

The fix

The issue was fixed by adding a check for the 0^0 case before the zero-base check, returning one in that case.

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 power function calculates a number multiplied by itself a certain number of times. By mathematical convention, any number to the power of zero equals one, including zero itself. This code returns zero instead, which could make calculations in other programs wrong if they assume the usual rule.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by RadiantLabs

View all

17 more disclosed reports, best first.

More from Kakarot

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.