Class GenericSqrtRatioCalculator

java.lang.Object
org.bouncycastle.crypto.hash2curve.impl.GenericSqrtRatioCalculator
All Implemented Interfaces:
SqrtRatioCalculator

public class GenericSqrtRatioCalculator extends Object implements SqrtRatioCalculator
Generic implementation of the sqrt_ratio(u, v) operation defined in RFC 9380.

This computes a square root of u/v in the prime field Fp associated with an elliptic curve, when such a square root exists, and otherwise returns a valid square root of z·u/v for a fixed quadratic non-residue z. This function is a required component of all map-to-curve constructions in RFC 9380, including the Simplified SWU and Elligator 2 maps.

RFC 9380 defines optimized sqrt_ratio formulas for certain curves where the field prime p satisfies special congruences (e.g. p ≡ 3 mod 4 or p ≡ 5 mod 8). However, those optimizations are curve-specific and do not apply to all hash-to-curve suites. This implementation instead follows the fully generic algorithm from Section 5.6.3 of RFC 9380, which is valid for any elliptic curve defined over a prime field Fp.

This generic version supports all curves used in the RFC 9830 test vectors, including the NIST P-256 / P-384 / P-521 curves, Curve25519, Edwards25519 (Ristretto255), Curve448, and Edwards448 (Decaf448). It provides a single uniform implementation suitable for all supported hash-to-curve suites.

  • Constructor Details

    • GenericSqrtRatioCalculator

      public GenericSqrtRatioCalculator(ECCurve curve, BigInteger z)
      Constructs a GenericSqrtRatioCalculator instance with the specified elliptic curve and a given value z.
      Parameters:
      curve - the elliptic curve over a finite field to be used for square root and ratio calculations
      z - a non-square element of F
  • Method Details

    • sqrtRatio

      public SqrtRatio sqrtRatio(BigInteger u, BigInteger v)
      he sqrtRatio subroutine of hash2Curve in the field F
      Specified by:
      sqrtRatio in interface SqrtRatioCalculator
      Parameters:
      u - u parameter, element of F
      v - v parameter, element of F, such that v != 0
      Returns:
      SqrtRatio result