sqrtRatioM1 static method

Implementation

static SqrtRatioM1Result sqrtRatioM1(FieldElement u, FieldElement v) {
  final FieldElement v3 = v.square() * v;
  final FieldElement v7 = v3.square() * v;
  FieldElement r = u * v3 * (u * v7).powP58();
  final FieldElement check = v * r.square();
  final FieldElement uNeg = -u;
  final int correctSignSqrt = check.fastEqual(u);
  final int flippedSignSqrt = check.fastEqual(uNeg);
  final int flippedSignSqrtM1 = check.fastEqual(uNeg * sqrtM1);
  final FieldElement rPrime = r * sqrtM1;
  r = r.select(rPrime, flippedSignSqrt | flippedSignSqrtM1);
  // Choose the non-negative square root.
  r = r.abs();
  return SqrtRatioM1Result(correctSignSqrt | flippedSignSqrt, r);
}