Skip to content
Business logic Audit finding Intermediate

Standard deviation calculation is biased

The standard deviation calculation in the code uses a biased estimator by dividing by N instead of N-1, leading to an underestimation of the true standard deviation. This affects the regret and time step scores used in the protocol, causing slightly incorrect weight calculations.

Lambda Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/27

Found by

Lambda

Summary

The estimation of the regrets & time steps scores will be based because of a biased estimator.

Vulnerability Detail

The function StdDev calculates the sample standard deviation of an arbitrary data vector. To do so, it uses the formula sqrt((Σ(x - μ))^2/ N). Because the data is also used to calculate the sample mean, this estimator will be biased, i.e. its expectation value will not be σ, but rather sqrt((n - 1)/n) σ.

The utility function is used in synth_palette_weight.go to get the std. deviation of the regrets and in rewards_internal.go to get the std. deviation of the latest time steps scores.

Impact

The std. deviation estimate in the two mentioned places will be slightly off, leading to a wrong estimate.

Code Snippet

https://github.com/sherlock-audit/2024-06-allora/blob/main/allora-chain/math/utils.go#L129

Go
	sdOverLen, err := sd.Quo(lenData)
	if err != nil {
		return Dec{}, err
	}

Tool used

Manual Review

Recommendation

Apply Bessel's correction, i.e. divide by N - 1 instead of N, which means subtracting one from lenData in the code.

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-chain/pull/460

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

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 code divides by N instead of N-1 when computing the sample standard deviation, resulting in a biased estimator.

Impact

The standard deviation estimates for regrets and time step scores are slightly off, leading to incorrect weight calculations in the protocol.

What the attacker needed

None

The fix

The issue was fixed by applying Bessel's correction, dividing by N-1 instead of N.

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.

When you calculate the spread of data (standard deviation) from a sample, dividing by the number of items (N) tends to underestimate the true spread. A simple fix is to divide by N-1 instead, which gives a better estimate. This report points out that the code uses the biased version, so the calculated spread is slightly too small.

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 Lambda

View all

64 more disclosed reports, best first.

More from Allora

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.