toBytes method

  1. @override
List<int> toBytes([
  1. EncodeType encodeType = EncodeType.comprossed
])
override

Overrides the 'toBytes' method to convert the RistrettoPoint to a byte array.

This method converts the RistrettoPoint to a byte array by computing its coordinates and encoding them according to the default Ristretto encoding rules. The resulting byte array represents the RistrettoPoint in a format suitable for serialization and other data storage or transmission purposes.

Returns:

  • List

Details:

  • The method calculates intermediate values and applies encoding-specific transformations to the point's coordinates in accordance with Ristretto encoding rules. The result is a compact byte array representation. encodeType works only for toEdwardBytes

Implementation

@override
List<int> toBytes([EncodeType encodeType = EncodeType.comprossed]) {
  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 = ristretto_tools.positiveMod(
      ristretto_tools.positiveMod(z + y, primeP) *
          ristretto_tools.positiveMod(z - y, primeP),
      primeP);
  final u2 = ristretto_tools.positiveMod(x * y, primeP);

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