This repository has been archived on 2023-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
chainsafe-bls/src/functional.ts

115 lines
3.2 KiB
TypeScript
Raw Normal View History

import {validateBytes} from "./helpers";
import {IBls} from "./interface";
2020-11-25 11:50:47 +00:00
// Returned type is enforced at each implementation's index
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function functionalInterfaceFactory({
PrivateKey,
PublicKey,
Signature,
}: Pick<IBls, "PrivateKey" | "PublicKey" | "Signature">) {
/**
* Signs given message using secret key.
* @param secretKey
2020-11-28 19:11:21 +00:00
* @param message
*/
2020-11-28 19:11:21 +00:00
function sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array {
validateBytes(secretKey, "secretKey");
validateBytes(message, "message");
const privateKey = PrivateKey.fromBytes(secretKey);
2020-11-28 19:11:21 +00:00
return privateKey.sign(message).toBytes();
}
/**
* Compines all given signature into one.
* @param signatures
*/
2020-11-25 16:09:44 +00:00
function aggregateSignatures(signatures: Uint8Array[]): Uint8Array {
const agg = Signature.aggregate(signatures.map((p) => Signature.fromBytes(p)));
return agg.toBytes();
}
/**
* Combines all given public keys into single one
* @param publicKeys
*/
2020-11-25 16:09:44 +00:00
function aggregatePubkeys(publicKeys: Uint8Array[]): Uint8Array {
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
return agg.toBytes();
}
/**
* Verifies if signature is message signed with given public key.
* @param publicKey
2020-11-28 19:11:21 +00:00
* @param message
* @param signature
*/
2020-11-28 19:11:21 +00:00
function verify(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): boolean {
validateBytes(publicKey, "publicKey");
validateBytes(message, "message");
validateBytes(signature, "signature");
try {
2020-11-28 19:11:21 +00:00
return Signature.fromBytes(signature).verify(PublicKey.fromBytes(publicKey), message);
} catch (e) {
return false;
}
}
/**
* Verifies if aggregated signature is same message signed with given public keys.
* @param publicKeys
2020-11-28 19:11:21 +00:00
* @param message
* @param signature
*/
2020-11-28 19:11:21 +00:00
function verifyAggregate(publicKeys: Uint8Array[], message: Uint8Array, signature: Uint8Array): boolean {
validateBytes(publicKeys, "publicKey");
validateBytes(message, "message");
validateBytes(signature, "signature");
try {
return Signature.fromBytes(signature).verifyAggregate(
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
2020-11-28 19:11:21 +00:00
message
);
} catch (e) {
return false;
}
}
/**
* Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys
2020-11-28 19:11:21 +00:00
* @param messages
* @param signature
* @param fast Check if all messages are different
*/
2020-11-28 19:11:21 +00:00
function verifyMultiple(publicKeys: Uint8Array[], messages: Uint8Array[], signature: Uint8Array): boolean {
validateBytes(publicKeys, "publicKey");
validateBytes(messages, "message");
validateBytes(signature, "signature");
2020-11-28 19:11:21 +00:00
if (publicKeys.length === 0 || publicKeys.length != messages.length) {
return false;
}
try {
return Signature.fromBytes(signature).verifyMultiple(
publicKeys.map((publicKey) => PublicKey.fromBytes(publicKey)),
2020-11-28 19:11:21 +00:00
messages.map((msg) => msg)
);
} catch (e) {
return false;
}
}
return {
sign,
aggregateSignatures,
aggregatePubkeys,
verify,
verifyAggregate,
verifyMultiple,
};
}