sign function

Uint8List sign(
  1. PrivateKey privateKey,
  2. Uint8List message
)

Sign signs the message with privateKey and returns a signature. It will throw ArumentError if privateKey.bytes.length is not PrivateKeySize.

Implementation

Uint8List sign(PrivateKey privateKey, Uint8List message) {
  if (privateKey.bytes.length != PrivateKeySize) {
    throw ArgumentError(
        'ed25519: bad privateKey length ${privateKey.bytes.length}');
  }
  var h = sha512.convert(privateKey.bytes.sublist(0, 32));
  var digest1 = h.bytes;
  var expandedSecretKey = digest1.sublist(0, 32);
  expandedSecretKey[0] &= 248;
  expandedSecretKey[31] &= 63;
  expandedSecretKey[31] |= 64;

  var output = AccumulatorSink<Digest>();
  var input = sha512.startChunkedConversion(output);
  input.add(digest1.sublist(32));
  input.add(message);
  input.close();
  var messageDigest = output.events.single.bytes;

  var messageDigestReduced = Uint8List(32);
  ScReduce(messageDigestReduced, messageDigest as Uint8List);
  var R = ExtendedGroupElement();
  GeScalarMultBase(R, messageDigestReduced);

  var encodedR = Uint8List(32);
  R.ToBytes(encodedR);

  output = AccumulatorSink<Digest>();
  input = sha512.startChunkedConversion(output);
  input.add(encodedR);
  input.add(privateKey.bytes.sublist(32));
  input.add(message);
  input.close();
  var hramDigest = output.events.single.bytes;
  var hramDigestReduced = Uint8List(32);
  ScReduce(hramDigestReduced, hramDigest as Uint8List);

  var s = Uint8List(32);
  ScMulAdd(s, hramDigestReduced, expandedSecretKey as Uint8List,
      messageDigestReduced);

  var signature = Uint8List(SignatureSize);
  arrayCopy(encodedR, 0, signature, 0, 32);
  arrayCopy(s, 0, signature, 32, 32);

  return signature;
}