Skip to content

Critical Bug in FastLucasSequence Causes Incorrect Square Root Calculation #4732

Description

@xxcode66-source

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions