From 0c0bfd532459a2b13de7020e6d12c188c0b73130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Mon, 12 Aug 2019 09:51:18 +0200 Subject: [PATCH] add proper bls exports --- package.json | 1 + src/@types/keccak256/index.d.ts | 5 ----- src/helpers/g1point.ts | 2 +- src/helpers/g2point.ts | 8 ++++---- src/index.ts | 31 +++++++++++++------------------ src/privateKey.ts | 4 ++-- src/publicKey.ts | 2 +- src/signature.ts | 2 +- src/types.ts | 9 --------- 9 files changed, 23 insertions(+), 41 deletions(-) delete mode 100644 src/@types/keccak256/index.d.ts delete mode 100644 src/types.ts diff --git a/package.json b/package.json index 812e47c..34da2a1 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "coverage": "codecov -F bls" }, "dependencies": { + "@chainsafe/eth2.0-types": "^0.1.0", "@chainsafe/milagro-crypto-js": "0.1.3", "assert": "^1.4.1", "js-sha256": "^0.9.0", diff --git a/src/@types/keccak256/index.d.ts b/src/@types/keccak256/index.d.ts deleted file mode 100644 index 75e63d9..0000000 --- a/src/@types/keccak256/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -declare module 'keccak256' { - - export default function hash(a: Buffer | (Buffer | string | number)[]): Buffer; - -} diff --git a/src/helpers/g1point.ts b/src/helpers/g1point.ts index 8e087b4..7133f3b 100644 --- a/src/helpers/g1point.ts +++ b/src/helpers/g1point.ts @@ -1,11 +1,11 @@ import {BIG} from "@chainsafe/milagro-crypto-js/src/big"; import {ECP} from "@chainsafe/milagro-crypto-js/src/ecp"; import ctx from "../ctx"; -import {bytes48} from "../types"; import assert from "assert"; import {calculateYFlag, getModulus} from "./utils"; import * as random from "secure-random"; import {FP_POINT_LENGTH} from "../constants"; +import {bytes48} from "@chainsafe/eth2.0-types"; export class G1point { diff --git a/src/helpers/g2point.ts b/src/helpers/g2point.ts index 2eaab98..e58d1ae 100644 --- a/src/helpers/g2point.ts +++ b/src/helpers/g2point.ts @@ -1,12 +1,12 @@ import {BIG} from "@chainsafe/milagro-crypto-js/src/big"; import {ECP2} from "@chainsafe/milagro-crypto-js/src/ecp2"; -import {BLSDomain, bytes32, bytes96} from "../types"; import { sha256 } from 'js-sha256'; import ctx from "../ctx"; import * as random from "secure-random"; import {calculateYFlag, getModulus, padLeft} from "./utils"; import assert from "assert"; import {FP_POINT_LENGTH, G2_HASH_PADDING} from "../constants"; +import {bytes32, bytes48, Domain} from "@chainsafe/eth2.0-types"; export class G2point { @@ -37,7 +37,7 @@ export class G2point { return this.point; } - public toBytesCompressed(): Buffer { + public toBytesCompressed(): bytes48 { const xReBytes = Buffer.alloc(FP_POINT_LENGTH, 0); const xImBytes = Buffer.alloc(FP_POINT_LENGTH, 0); this.point.getX().getA().tobytearray(xReBytes, 0); @@ -58,7 +58,7 @@ export class G2point { ]); } - public static hashToG2(message: bytes32, domain: BLSDomain): G2point { + public static hashToG2(message: bytes32, domain: Domain): G2point { const padding = Buffer.alloc(G2_HASH_PADDING, 0); const xReBytes = Buffer.concat([ padding, @@ -94,7 +94,7 @@ export class G2point { return new G2point(G2point.scaleWithCofactor(G2point.normaliseY(point))); } - public static fromCompressedBytes(value: bytes96): G2point { + public static fromCompressedBytes(value: bytes48): G2point { assert(value.length === 2 * FP_POINT_LENGTH, 'Expected signature of 96 bytes'); value = Buffer.from(value); const xImBytes = value.slice(0, FP_POINT_LENGTH); diff --git a/src/index.ts b/src/index.ts index 31616a7..2888c71 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,3 @@ -import { - BLSDomain, - BLSSecretKey, - BLSPubkey, - BLSSignature, - bytes32, - bytes8 -} from "./types"; import {Keypair} from "./keypair"; import {PrivateKey} from "./privateKey"; import {G2point} from "./helpers/g2point"; @@ -14,11 +6,14 @@ import {PublicKey} from "./publicKey"; import {Signature} from "./signature"; import {ElipticCurvePairing} from "./helpers/ec-pairing"; import ctx from "./ctx"; +import {BLSPubkey, BLSSecretKey, BLSSignature, bytes32, Domain} from "@chainsafe/eth2.0-types"; + +export {Keypair, PrivateKey, PublicKey, Signature}; /** * Generates new secret and public key */ -function generateKeyPair(): Keypair { +export function generateKeyPair(): Keypair { return Keypair.generate(); } @@ -26,7 +21,7 @@ function generateKeyPair(): Keypair { * Generates public key from given secret. * @param {BLSSecretKey} secretKey */ -function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey { +export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey { const keypair = new Keypair(PrivateKey.fromBytes(secretKey)); return keypair.publicKey.toBytesCompressed(); } @@ -37,7 +32,7 @@ function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey { * @param messageHash * @param domain */ -function sign(secretKey: BLSSecretKey, messageHash: bytes32, domain: BLSDomain): BLSSignature { +export function sign(secretKey: BLSSecretKey, messageHash: bytes32, domain: Domain): BLSSignature { const privateKey = PrivateKey.fromBytes(secretKey); const hash = G2point.hashToG2(messageHash, domain); return privateKey.sign(hash).toBytesCompressed(); @@ -47,9 +42,9 @@ function sign(secretKey: BLSSecretKey, messageHash: bytes32, domain: BLSDomain): * Compines all given signature into one. * @param signatures */ -function aggregateSignatures(signatures: BLSSignature[]): BLSSignature { +export function aggregateSignatures(signatures: BLSSignature[]): BLSSignature { return signatures.map((signature): Signature => { - return Signature.fromCompressedBytes(signature) + return Signature.fromCompressedBytes(signature); }).reduce((previousValue, currentValue): Signature => { return previousValue.add(currentValue); }).toBytesCompressed(); @@ -59,12 +54,12 @@ function aggregateSignatures(signatures: BLSSignature[]): BLSSignature { * Combines all given public keys into single one * @param publicKeys */ -function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey { +export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey { if(publicKeys.length === 0) { return new G1point(new ctx.ECP()).toBytesCompressed(); } return publicKeys.map((publicKey): G1point => { - return G1point.fromBytesCompressed(publicKey) + return G1point.fromBytesCompressed(publicKey); }).reduce((previousValue, currentValue): G1point => { return previousValue.add(currentValue); }).toBytesCompressed(); @@ -77,7 +72,7 @@ function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey { * @param signature * @param domain */ -function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BLSSignature, domain: bytes8): boolean { +export function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BLSSignature, domain: Domain): boolean { try { const key = PublicKey.fromBytes(publicKey); const sig = Signature.fromCompressedBytes(signature); @@ -98,7 +93,7 @@ function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BLSSignat * @param signature * @param domain */ -function verifyMultiple(publicKeys: BLSPubkey[], messageHashes: bytes32[], signature: BLSSignature, domain: bytes8): boolean { +export function verifyMultiple(publicKeys: BLSPubkey[], messageHashes: bytes32[], signature: BLSSignature, domain: Domain): boolean { if(publicKeys.length === 0 || publicKeys.length != messageHashes.length) { return false; } @@ -129,4 +124,4 @@ export default { aggregatePubkeys, verify, verifyMultiple -} +}; diff --git a/src/privateKey.ts b/src/privateKey.ts index e99dba2..0bb4161 100644 --- a/src/privateKey.ts +++ b/src/privateKey.ts @@ -5,7 +5,7 @@ import ctx from "./ctx"; import {padLeft} from "./helpers/utils"; import {G2point} from "./helpers/g2point"; import * as random from "secure-random"; -import {BLSDomain, BLSSecretKey, bytes32} from "./types"; +import {BLSSecretKey, bytes32, Domain} from "@chainsafe/eth2.0-types"; export class PrivateKey { @@ -23,7 +23,7 @@ export class PrivateKey { return message.mul(this.value); } - public signMessage(message: bytes32, domain: BLSDomain): G2point { + public signMessage(message: bytes32, domain: Domain): G2point { return G2point.hashToG2(message, domain).mul(this.value); } diff --git a/src/publicKey.ts b/src/publicKey.ts index 6d734a1..3697aaf 100644 --- a/src/publicKey.ts +++ b/src/publicKey.ts @@ -1,6 +1,6 @@ import {G1point} from "./helpers/g1point"; import {PrivateKey} from "./privateKey"; -import {BLSPubkey} from "./types"; +import {BLSPubkey} from "@chainsafe/eth2.0-types"; export class PublicKey { diff --git a/src/signature.ts b/src/signature.ts index 89dee16..76a1a16 100644 --- a/src/signature.ts +++ b/src/signature.ts @@ -1,7 +1,7 @@ import {G2point} from "./helpers/g2point"; -import {BLSSignature} from "./types"; import assert from "assert"; import {FP_POINT_LENGTH} from "./constants"; +import {BLSSignature} from "@chainsafe/eth2.0-types"; export class Signature { diff --git a/src/types.ts b/src/types.ts deleted file mode 100644 index 6a6346a..0000000 --- a/src/types.ts +++ /dev/null @@ -1,9 +0,0 @@ -export type bytes8 = Buffer; -export type bytes32 = Buffer; -export type bytes48 = Buffer; -export type bytes96 = Buffer; - -export type BLSDomain = bytes8; -export type BLSPubkey = bytes48; -export type BLSSecretKey = bytes32; -export type BLSSignature = bytes96;