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/index.ts

145 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-11-05 20:55:25 +00:00
import {Keypair} from "./keypair";
import {PrivateKey} from "./privateKey";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
import {PUBLIC_KEY_LENGTH} from "./constants";
2020-01-08 06:49:51 +00:00
import assert from "assert";
2019-11-28 11:13:20 +00:00
2020-11-05 20:55:25 +00:00
export {Keypair, PrivateKey, PublicKey, Signature};
2019-08-05 15:48:26 +00:00
2020-11-05 20:55:25 +00:00
export {init as initBLS} from "./context";
2019-11-27 20:58:41 +00:00
2020-02-12 15:39:40 +00:00
function toBuffer(input: Uint8Array): Buffer {
return Buffer.from(input.buffer, input.byteOffset, input.length);
}
2019-08-05 15:48:26 +00:00
/**
* Generates new secret and public key
*/
2019-08-12 07:51:18 +00:00
export function generateKeyPair(): Keypair {
2019-08-05 15:48:26 +00:00
return Keypair.generate();
}
/**
* Generates public key from given secret.
* @param {BLSSecretKey} secretKey
*/
2020-02-12 15:39:40 +00:00
export function generatePublicKey(secretKey: Uint8Array): Buffer {
2020-01-08 06:49:51 +00:00
assert(secretKey, "secretKey is null or undefined");
2020-02-12 15:39:40 +00:00
const keypair = new Keypair(PrivateKey.fromBytes(toBuffer(secretKey)));
2020-11-19 00:23:34 +00:00
return keypair.publicKey.toBytes();
2019-08-05 15:48:26 +00:00
}
/**
* Signs given message using secret key.
* @param secretKey
* @param messageHash
*/
export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
2020-01-08 06:49:51 +00:00
assert(secretKey, "secretKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
2020-02-12 15:39:40 +00:00
const privateKey = PrivateKey.fromBytes(toBuffer(secretKey));
2020-11-19 00:23:34 +00:00
return privateKey.signMessage(toBuffer(messageHash)).toBytes();
2019-08-05 15:48:26 +00:00
}
/**
* Compines all given signature into one.
* @param signatures
*/
2020-02-12 15:39:40 +00:00
export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
2020-11-05 20:55:25 +00:00
assert(signatures && signatures.length > 0, "signatures is null or undefined or empty array");
2020-11-19 00:23:34 +00:00
// return Signature.aggregate(
// signatures.map(
// (signature): Signature => {
// return Signature.fromBytes(signature);
// }
// )
// ).toBytes();
return Buffer.alloc(0);
2019-08-05 15:48:26 +00:00
}
/**
* Combines all given public keys into single one
* @param publicKeys
*/
2020-02-12 15:39:40 +00:00
export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
2020-11-19 00:23:34 +00:00
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
return agg.toBytes();
2019-08-05 15:48:26 +00:00
}
/**
* Verifies if signature is message signed with given public key.
* @param publicKey
* @param messageHash
* @param signature
*/
2020-11-05 20:55:25 +00:00
export function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature: Uint8Array): boolean {
2020-01-08 06:49:51 +00:00
assert(publicKey, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
2019-08-05 15:48:26 +00:00
try {
2020-11-04 17:40:36 +00:00
return PublicKey.fromBytes(publicKey).verifyMessage(
2020-11-19 00:23:34 +00:00
Signature.fromBytes(toBuffer(signature)),
2020-11-04 17:40:36 +00:00
toBuffer(messageHash)
);
2019-08-05 15:48:26 +00:00
} catch (e) {
return false;
}
}
2020-02-07 09:13:12 +00:00
/**
* Verifies if aggregated signature is same message signed with given public keys.
* @param publicKeys
* @param messageHash
* @param signature
*/
2020-11-05 20:55:25 +00:00
export function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Array, signature: Uint8Array): boolean {
2020-02-07 09:13:12 +00:00
assert(publicKeys, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
2020-11-19 00:23:34 +00:00
return Signature.fromBytes(signature).verifyAggregate(
2020-11-04 17:40:36 +00:00
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
messageHash
);
2020-02-07 09:13:12 +00:00
} catch (e) {
return false;
}
}
2019-08-05 15:48:26 +00:00
/**
* Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys
* @param messageHashes
* @param signature
2020-02-07 09:13:12 +00:00
* @param fast Check if all messages are different
2019-08-05 15:48:26 +00:00
*/
2020-11-19 00:23:34 +00:00
export function verifyMultiple(publicKeys: Uint8Array[], messageHashes: Uint8Array[], signature: Uint8Array): boolean {
2020-01-09 06:55:26 +00:00
assert(publicKeys, "publicKey is null or undefined");
assert(messageHashes, "messageHash is null or undefined");
2020-01-08 06:49:51 +00:00
assert(signature, "signature is null or undefined");
2020-11-04 17:40:36 +00:00
if (publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
2019-08-05 15:48:26 +00:00
return false;
}
try {
2020-11-19 00:23:34 +00:00
return Signature.fromBytes(toBuffer(signature)).verifyMultiple(
2020-11-04 17:40:36 +00:00
publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))),
2020-11-19 00:23:34 +00:00
messageHashes.map((m) => toBuffer(m))
2020-11-04 17:40:36 +00:00
);
2019-08-05 15:48:26 +00:00
} catch (e) {
return false;
}
}
export default {
generateKeyPair,
generatePublicKey,
sign,
aggregateSignatures,
aggregatePubkeys,
verify,
2020-02-07 09:13:12 +00:00
verifyAggregate,
2020-11-04 17:40:36 +00:00
verifyMultiple,
2019-08-12 07:51:18 +00:00
};