The core of the vulnerability lies in the construction of Fr (scalar field) types for both BN254 and BLS12-381 curves in the soroban-sdk. The Fr type is a wrapper around a U256 integer. The PartialEq trait, which is used for equality checks (==), was derived, meaning it performed a direct bit-for-bit comparison of the underlying U256 values.
The vulnerability arises because the constructors for Fr, such as from_u256, from_bytes, and the From<U256> trait implementation, accepted any U256 value without reducing it modulo the scalar field's modulus, r. In modular arithmetic, x and x + r are considered equal. However, because the raw U256 representations were stored, an Fr object representing 1 and another representing r + 1 would not be considered equal by the PartialEq implementation.
An attacker could exploit this by supplying a specially crafted, unreduced Fr value as input to a smart contract. If the contract's logic relies on equality checks involving this Fr value (e.g., for authorization), the check could produce an incorrect result, potentially leading to a bypass of security controls.
The patch addresses this by modifying the Fr constructors, specifically the From<U256> for Fr implementation, to always perform a modular reduction on the input value. This ensures that all Fr objects store their value in a canonical form (i.e., within the range [0, r)), guaranteeing that mathematically equal field elements will have identical internal representations and thus compare as equal.
Additionally, a related input validation vulnerability was fixed for the Fp and Fp2 base field types, which now validate that their values are strictly less than the field modulus upon construction.