compressedHexToPublicKey method

  1. @override
PublicKey compressedHexToPublicKey(
  1. String hex
)
override

Implementation

@override
PublicKey compressedHexToPublicKey(String hex) {
  if (hex.substring(0, 2) != '03' && hex.substring(0, 2) != '02') {
    throw ('invalid public key hex string');
  }

  var byteLen = (bitSize + 7) >> 3;

  if (hex.length != 2 * (1 + byteLen)) {
    throw ('invalid public key hex string');
  }
  // y² = x³ - 3x + b

  var x = BigInt.parse(hex.substring(2 * 1, 2 * (1 + byteLen)), radix: 16);
  if (x > p) {
    throw ('invalid public key X value');
  }

  var y = _polynomial(x);

  var p1 = p + BigInt.one; // p+1
  var power = (p1 - p1 % BigInt.from(4)) >> 2;
  y = y.modPow(power, p); // get the sqrt mod

  if (y.isOdd != (hex.substring(0, 2) == '03')) {
    y = p - y;
  }

  var pub = PublicKey(this, x, y);
  if (!isOnCurve(pub)) {
    throw ('public key is not on this curve');
  }

  return pub;
}