fromBytes static method

Tuple<BigInt, BigInt> fromBytes(
  1. Curve curve,
  2. List<int> data, {
  3. bool validateEncoding = true,
  4. EncodeType? encodeType,
})

Creates an elliptic curve point from its byte representation.

Implementation

static Tuple<BigInt, BigInt> fromBytes(
  Curve curve,
  List<int> data, {
  bool validateEncoding = true,
  EncodeType? encodeType,
}) {
  if (curve is CurveED) {
    return _fromEdwards(curve, data);
  }
  final keyLen = data.length;
  final rawEncodingLength = 2 * BigintUtils.orderLen(curve.p);
  if (encodeType == null) {
    if (keyLen == rawEncodingLength) {
      encodeType = EncodeType.raw;
    } else if (keyLen == rawEncodingLength + 1) {
      final prefix = data[0];
      if (prefix == 0x04) {
        encodeType = EncodeType.uncompressed;
      } else if (prefix == 0x06 || prefix == 0x07) {
        encodeType = EncodeType.hybrid;
      } else {
        throw const ArgumentException("invalid key length");
      }
    } else if (keyLen == rawEncodingLength ~/ 2 + 1) {
      encodeType = EncodeType.comprossed;
    } else {
      throw const ArgumentException("invalid key length");
    }
  }
  curve as CurveFp;
  switch (encodeType) {
    case EncodeType.comprossed:
      return _fromCompressed(data, curve);
    case EncodeType.uncompressed:
      return _fromRawEncoding(data.sublist(1), rawEncodingLength);
    case EncodeType.hybrid:
      return _fromHybrid(data, rawEncodingLength);
    default:
      return _fromRawEncoding(data, rawEncodingLength);
  }
}