sign method
Signs a hash value using the private key.
Parameters:
hash: A hash value of the message to be signed.randomK: A random value for signature generation.
Returns: An ECDSA signature.
Implementation
ECDSASignature sign(BigInt hash, BigInt randomK) {
final BigInt? n = publicKey.generator.order;
if (n == null) {
throw ArgumentException.invalidOperationArguments(
"sign",
reason: "Invalid curve generator.",
);
}
final BigInt k = randomK % n;
final BigInt ks = k + n;
final BigInt kt = ks + n;
BigInt r;
if (ks.bitLength == n.bitLength) {
r = (publicKey.generator * kt).x % n;
} else {
r = (publicKey.generator * ks).x % n;
}
if (r == BigInt.zero) {
throw const CryptoSignException(
"ECDSA signing aborted. nonce generation failed.",
);
}
final BigInt s =
(BigintUtils.inverseMod(k, n) * (hash + (secretMultiplier * r) % n)) %
n;
if (s == BigInt.zero) {
throw const CryptoSignException(
"ECDSA signing aborted. s generation failed.",
);
}
return ECDSASignature(r, s);
}