toBytes method

  1. @override
List<int> toBytes()
override

convert the RistrettoPoint to a byte array.

Implementation

@override
List<int> toBytes() {
  final sqrtM1 = BigInt.parse(
    '19681161376707505956807079304988542015446066515923890162744021073123829784752',
  );

  final invSqrt = BigInt.parse(
    '54469307008909316920995813868745141605393597292927456921205312896311721017578',
  );
  final primeP = Curves.curveEd25519.p;
  final pointCoords = getCoords();
  BigInt x = pointCoords[0];
  BigInt y = pointCoords[1];
  final BigInt z = pointCoords[2];
  final BigInt t = pointCoords[3];

  final u1 = positiveMod(
    positiveMod(z + y, primeP) * positiveMod(z - y, primeP),
    primeP,
  );
  final u2 = positiveMod(x * y, primeP);

  final u2Squared = positiveMod(u2 * u2, primeP);
  final invS = sqrtUV(BigInt.one, positiveMod(u1 * u2Squared, primeP)).$2;
  final d1 = positiveMod(invS * u1, primeP);
  final d2 = positiveMod(invS * u2, primeP);
  final zInverse = positiveMod(d1 * d2 * t, primeP);
  BigInt D;
  if (isOdd(t * zInverse, primeP)) {
    final x2 = positiveMod(y * sqrtM1, primeP);
    final y2 = positiveMod(x * sqrtM1, primeP);
    x = x2;
    y = y2;
    D = positiveMod(d1 * invSqrt, primeP);
  } else {
    D = d2;
  }
  if (isOdd(x * zInverse, primeP)) {
    y = positiveMod(-y, primeP);
  }
  BigInt s = positiveMod((z - y) * D, primeP);
  if (isOdd(s, primeP)) {
    s = positiveMod(-s, primeP);
  }
  return BigintUtils.toBytes(s, order: Endian.little, length: 32);
}