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

147 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-08-05 15:48:26 +00:00
import {Keypair} from "./keypair";
import {PrivateKey} from "./privateKey";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
2020-01-21 16:46:21 +00:00
import {BLSPubkey, BLSSecretKey, BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types";
2019-11-27 20:58:41 +00:00
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
2019-08-12 07:51:18 +00:00
export {Keypair, PrivateKey, PublicKey, Signature};
2019-08-05 15:48:26 +00:00
2019-12-02 12:44:45 +00:00
export {init as initBLS} from "./context";
2019-11-27 20:58:41 +00:00
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
*/
2019-08-12 07:51:18 +00:00
export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
2020-01-08 06:49:51 +00:00
assert(secretKey, "secretKey is null or undefined");
2020-02-04 22:30:10 +00:00
const keypair = new Keypair(PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array)));
2019-08-05 15:48:26 +00:00
return keypair.publicKey.toBytesCompressed();
}
/**
* Signs given message using secret key.
* @param secretKey
* @param messageHash
* @param domain
*/
2020-01-21 16:46:21 +00:00
export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Domain): BLSSignature {
2020-01-08 06:49:51 +00:00
assert(secretKey, "secretKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(domain, "domain is null or undefined");
2020-02-04 22:30:10 +00:00
const privateKey = PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array));
return privateKey.signMessage(
Buffer.from(messageHash as Uint8Array),
Buffer.from(domain as Uint8Array)
).toBytesCompressed();
2019-08-05 15:48:26 +00:00
}
/**
* Compines all given signature into one.
* @param signatures
*/
2019-08-12 07:51:18 +00:00
export function aggregateSignatures(signatures: BLSSignature[]): BLSSignature {
2020-01-08 06:49:51 +00:00
assert(signatures, "signatures is null or undefined");
2019-08-05 15:48:26 +00:00
return signatures.map((signature): Signature => {
2020-02-04 22:30:10 +00:00
return Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array));
2019-08-05 15:48:26 +00:00
}).reduce((previousValue, currentValue): Signature => {
return previousValue.add(currentValue);
}).toBytesCompressed();
}
/**
* Combines all given public keys into single one
* @param publicKeys
*/
2019-08-12 07:51:18 +00:00
export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
2020-01-08 06:49:51 +00:00
assert(publicKeys, "publicKeys is null or undefined");
2019-08-05 15:48:26 +00:00
if(publicKeys.length === 0) {
2019-11-27 20:58:41 +00:00
return Buffer.alloc(PUBLIC_KEY_LENGTH);
}
2020-02-04 22:30:10 +00:00
return publicKeys.map((p) => PublicKey.fromBytes(Buffer.from(p as Uint8Array))).reduce((agg, pubKey) => {
2019-11-27 20:58:41 +00:00
if(agg) {
return agg.add(pubKey);
} else {
return pubKey;
}
2019-08-05 15:48:26 +00:00
}
2019-11-27 20:58:41 +00:00
).toBytesCompressed();
2019-08-05 15:48:26 +00:00
}
/**
* Verifies if signature is message signed with given public key.
* @param publicKey
* @param messageHash
* @param signature
* @param domain
*/
2020-01-21 16:46:21 +00:00
export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BLSSignature, domain: Domain): 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");
assert(domain, "domain is null or undefined");
2019-08-05 15:48:26 +00:00
try {
2019-11-27 20:58:41 +00:00
return PublicKey
2020-02-04 22:30:10 +00:00
.fromBytes(Buffer.from(publicKey as Uint8Array))
.verifyMessage(
Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array)),
Buffer.from(messageHash as Uint8Array),
Buffer.from(domain as Uint8Array)
);
2019-08-05 15:48:26 +00:00
} catch (e) {
return false;
}
}
/**
* Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys
* @param messageHashes
* @param signature
* @param domain
*/
2019-09-17 19:05:13 +00:00
export function verifyMultiple(
publicKeys: BLSPubkey[],
2020-01-21 16:46:21 +00:00
messageHashes: Bytes32[],
2019-09-17 19:05:13 +00:00
signature: BLSSignature,
domain: Domain
): 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");
assert(domain, "domain is null or undefined");
2019-08-05 15:48:26 +00:00
if(publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false;
}
try {
2019-11-27 20:58:41 +00:00
return Signature
2020-02-04 22:30:10 +00:00
.fromCompressedBytes(Buffer.from(signature as Uint8Array))
2019-11-27 20:58:41 +00:00
.verifyMultiple(
2020-02-04 22:30:10 +00:00
publicKeys.map((key) => PublicKey.fromBytes(Buffer.from(key as Uint8Array))),
messageHashes.map((m) => Buffer.from(m as Uint8Array)),
Buffer.from(domain as Uint8Array),
2019-11-27 20:58:41 +00:00
);
2019-08-05 15:48:26 +00:00
} catch (e) {
return false;
}
}
export default {
generateKeyPair,
generatePublicKey,
sign,
aggregateSignatures,
aggregatePubkeys,
verify,
verifyMultiple
2019-08-12 07:51:18 +00:00
};