From 2b38c8750170d5aa8228418cb99645183d927456 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 1/3] 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; From 05741442a02d3c2fd0956fa62b963c647cf8657d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Mon, 12 Aug 2019 10:20:40 +0200 Subject: [PATCH 2/3] fix bls exports --- .babelrc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.babelrc b/.babelrc index 633f93f..353bc18 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,8 @@ { - "extends": "../../.babelrc" + "extends": "../../.babelrc", + "plugins": [ + "@babel/proposal-class-properties", + "@babel/proposal-object-rest-spread", + "rewire-exports" + ] } From 80d07d780f130f556401148ae7d971df58797728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 16 Aug 2019 13:49:51 +0200 Subject: [PATCH 3/3] fix lint --- src/helpers/g1point.ts | 4 ++-- src/helpers/g2point.ts | 8 ++++---- src/helpers/utils.ts | 2 +- src/privateKey.ts | 2 +- src/web.ts | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/helpers/g1point.ts b/src/helpers/g1point.ts index 7133f3b..60b0218 100644 --- a/src/helpers/g1point.ts +++ b/src/helpers/g1point.ts @@ -96,7 +96,7 @@ export class G1point { const x = new ctx.FP(point.getX()); const yneg = new ctx.FP(point.getY()); yneg.neg(); - point.setxy(x.redc(), yneg.redc()) + point.setxy(x.redc(), yneg.redc()); } return new G1point(point); @@ -115,7 +115,7 @@ export class G1point { random.randomBuffer(FP_POINT_LENGTH), 0 ) - ) + ); } while (ecp.is_infinity()); return new G1point(ecp); } diff --git a/src/helpers/g2point.ts b/src/helpers/g2point.ts index e58d1ae..82e1155 100644 --- a/src/helpers/g2point.ts +++ b/src/helpers/g2point.ts @@ -1,6 +1,6 @@ import {BIG} from "@chainsafe/milagro-crypto-js/src/big"; import {ECP2} from "@chainsafe/milagro-crypto-js/src/ecp2"; -import { sha256 } from 'js-sha256'; +import {sha256} from 'js-sha256'; import ctx from "../ctx"; import * as random from "secure-random"; import {calculateYFlag, getModulus, padLeft} from "./utils"; @@ -89,7 +89,7 @@ export class G2point { xRe.add(one); xRe.norm(); point = new ctx.ECP2(); - point.setx(new ctx.FP2(xRe, xIm)) + point.setx(new ctx.FP2(xRe, xIm)); } return new G2point(G2point.scaleWithCofactor(G2point.normaliseY(point))); } @@ -189,7 +189,7 @@ export class G2point { 0 ) ) - ) + ); } while (point.is_infinity()); return new G2point(point); } @@ -240,7 +240,7 @@ export class G2point { ) { const newPoint = new ctx.ECP2(); newPoint.setxy(point.getX(), yNeg); - return newPoint + return newPoint; } else { return point; } diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 4bac7ba..25f1367 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -23,7 +23,7 @@ export function getModulus(): BIG { 'hex' ), 0 - ) + ); } export function calculateYFlag(yIm: BIG): boolean { diff --git a/src/privateKey.ts b/src/privateKey.ts index 0bb4161..e525a0c 100644 --- a/src/privateKey.ts +++ b/src/privateKey.ts @@ -48,7 +48,7 @@ export class PrivateKey { ), 0 ) - ) + ); } public static fromHexString(value: string): PrivateKey { diff --git a/src/web.ts b/src/web.ts index 08397b6..519e5e3 100644 --- a/src/web.ts +++ b/src/web.ts @@ -1,8 +1,8 @@ -import bls from "./index" +import bls from "./index"; // eslint-disable-next-line @typescript-eslint/no-explicit-any // @ts-ignore (function (window: any) { - window.bls = bls + window.bls = bls; // @ts-ignore })(window);