Critical Bug in FastLucasSequence Causes Incorrect Square Root Calculation
Summary
ECFieldElement.FastLucasSequence() contains a critical arithmetic bug that produces incorrect results for square root calculations on elliptic curve field elements. This affects signature verification and other cryptographic operations.
Location
src/Neo/Cryptography/ECC/ECFieldElement.cs, line 92
Root Cause
for (var j = 1; j <= s; ++j)
{
Uh = Uh * Vl * p; // BUG: Missing .Mod(p)
Vl = ((Vl * Vl) - (Ql << 1)).Mod(p);
Ql = (Ql * Ql).Mod(p);
}
Line 92: Uh = Uh * Vl * p; should be Uh = (Uh * Vl).Mod(p);
The current code multiplies by p instead of taking modulo p, causing Uh to grow exponentially and produce incorrect values.
Impact
This bug affects the Sqrt() method which is used in elliptic curve point decompression and signature verification. Any operation relying on correct square root calculation in the field will fail or produce incorrect results.
Proof of Concept
// Create a field element that requires square root calculation
var curve = ECCurve.Secp256r1;
var value = new BigInteger(4);
var element = new ECFieldElement(value, curve);
// This will call FastLucasSequence internally
var sqrt = element.Sqrt();
// The result will be incorrect due to the bug
Recommended Fix
for (var j = 1; j <= s; ++j)
{
Uh = (Uh * Vl).Mod(p); // Fixed: added .Mod(p)
Vl = ((Vl * Vl) - (Ql << 1)).Mod(p);
Ql = (Ql * Ql).Mod(p);
}
Severity
CRITICAL - This is a fundamental bug in the elliptic curve cryptography layer that could affect signature verification and other security-critical operations.
Critical Bug in FastLucasSequence Causes Incorrect Square Root Calculation
Summary
ECFieldElement.FastLucasSequence()contains a critical arithmetic bug that produces incorrect results for square root calculations on elliptic curve field elements. This affects signature verification and other cryptographic operations.Location
src/Neo/Cryptography/ECC/ECFieldElement.cs, line 92Root Cause
Line 92:
Uh = Uh * Vl * p;should beUh = (Uh * Vl).Mod(p);The current code multiplies by
pinstead of taking modulop, causingUhto grow exponentially and produce incorrect values.Impact
This bug affects the
Sqrt()method which is used in elliptic curve point decompression and signature verification. Any operation relying on correct square root calculation in the field will fail or produce incorrect results.Proof of Concept
Recommended Fix
Severity
CRITICAL - This is a fundamental bug in the elliptic curve cryptography layer that could affect signature verification and other security-critical operations.