add proper bls exports

This commit is contained in:
Marin Petrunić 2019-08-12 09:51:18 +02:00
parent 9b8502b90a
commit 0c0bfd5324
9 changed files with 23 additions and 41 deletions

View File

@ -33,6 +33,7 @@
"coverage": "codecov -F bls" "coverage": "codecov -F bls"
}, },
"dependencies": { "dependencies": {
"@chainsafe/eth2.0-types": "^0.1.0",
"@chainsafe/milagro-crypto-js": "0.1.3", "@chainsafe/milagro-crypto-js": "0.1.3",
"assert": "^1.4.1", "assert": "^1.4.1",
"js-sha256": "^0.9.0", "js-sha256": "^0.9.0",

View File

@ -1,5 +0,0 @@
declare module 'keccak256' {
export default function hash(a: Buffer | (Buffer | string | number)[]): Buffer;
}

View File

@ -1,11 +1,11 @@
import {BIG} from "@chainsafe/milagro-crypto-js/src/big"; import {BIG} from "@chainsafe/milagro-crypto-js/src/big";
import {ECP} from "@chainsafe/milagro-crypto-js/src/ecp"; import {ECP} from "@chainsafe/milagro-crypto-js/src/ecp";
import ctx from "../ctx"; import ctx from "../ctx";
import {bytes48} from "../types";
import assert from "assert"; import assert from "assert";
import {calculateYFlag, getModulus} from "./utils"; import {calculateYFlag, getModulus} from "./utils";
import * as random from "secure-random"; import * as random from "secure-random";
import {FP_POINT_LENGTH} from "../constants"; import {FP_POINT_LENGTH} from "../constants";
import {bytes48} from "@chainsafe/eth2.0-types";
export class G1point { export class G1point {

View File

@ -1,12 +1,12 @@
import {BIG} from "@chainsafe/milagro-crypto-js/src/big"; import {BIG} from "@chainsafe/milagro-crypto-js/src/big";
import {ECP2} from "@chainsafe/milagro-crypto-js/src/ecp2"; import {ECP2} from "@chainsafe/milagro-crypto-js/src/ecp2";
import {BLSDomain, bytes32, bytes96} from "../types";
import { sha256 } from 'js-sha256'; import { sha256 } from 'js-sha256';
import ctx from "../ctx"; import ctx from "../ctx";
import * as random from "secure-random"; import * as random from "secure-random";
import {calculateYFlag, getModulus, padLeft} from "./utils"; import {calculateYFlag, getModulus, padLeft} from "./utils";
import assert from "assert"; import assert from "assert";
import {FP_POINT_LENGTH, G2_HASH_PADDING} from "../constants"; import {FP_POINT_LENGTH, G2_HASH_PADDING} from "../constants";
import {bytes32, bytes48, Domain} from "@chainsafe/eth2.0-types";
export class G2point { export class G2point {
@ -37,7 +37,7 @@ export class G2point {
return this.point; return this.point;
} }
public toBytesCompressed(): Buffer { public toBytesCompressed(): bytes48 {
const xReBytes = Buffer.alloc(FP_POINT_LENGTH, 0); const xReBytes = Buffer.alloc(FP_POINT_LENGTH, 0);
const xImBytes = Buffer.alloc(FP_POINT_LENGTH, 0); const xImBytes = Buffer.alloc(FP_POINT_LENGTH, 0);
this.point.getX().getA().tobytearray(xReBytes, 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 padding = Buffer.alloc(G2_HASH_PADDING, 0);
const xReBytes = Buffer.concat([ const xReBytes = Buffer.concat([
padding, padding,
@ -94,7 +94,7 @@ export class G2point {
return new G2point(G2point.scaleWithCofactor(G2point.normaliseY(point))); 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'); assert(value.length === 2 * FP_POINT_LENGTH, 'Expected signature of 96 bytes');
value = Buffer.from(value); value = Buffer.from(value);
const xImBytes = value.slice(0, FP_POINT_LENGTH); const xImBytes = value.slice(0, FP_POINT_LENGTH);

View File

@ -1,11 +1,3 @@
import {
BLSDomain,
BLSSecretKey,
BLSPubkey,
BLSSignature,
bytes32,
bytes8
} from "./types";
import {Keypair} from "./keypair"; import {Keypair} from "./keypair";
import {PrivateKey} from "./privateKey"; import {PrivateKey} from "./privateKey";
import {G2point} from "./helpers/g2point"; import {G2point} from "./helpers/g2point";
@ -14,11 +6,14 @@ import {PublicKey} from "./publicKey";
import {Signature} from "./signature"; import {Signature} from "./signature";
import {ElipticCurvePairing} from "./helpers/ec-pairing"; import {ElipticCurvePairing} from "./helpers/ec-pairing";
import ctx from "./ctx"; 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 * Generates new secret and public key
*/ */
function generateKeyPair(): Keypair { export function generateKeyPair(): Keypair {
return Keypair.generate(); return Keypair.generate();
} }
@ -26,7 +21,7 @@ function generateKeyPair(): Keypair {
* Generates public key from given secret. * Generates public key from given secret.
* @param {BLSSecretKey} secretKey * @param {BLSSecretKey} secretKey
*/ */
function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey { export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
const keypair = new Keypair(PrivateKey.fromBytes(secretKey)); const keypair = new Keypair(PrivateKey.fromBytes(secretKey));
return keypair.publicKey.toBytesCompressed(); return keypair.publicKey.toBytesCompressed();
} }
@ -37,7 +32,7 @@ function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
* @param messageHash * @param messageHash
* @param domain * @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 privateKey = PrivateKey.fromBytes(secretKey);
const hash = G2point.hashToG2(messageHash, domain); const hash = G2point.hashToG2(messageHash, domain);
return privateKey.sign(hash).toBytesCompressed(); return privateKey.sign(hash).toBytesCompressed();
@ -47,9 +42,9 @@ function sign(secretKey: BLSSecretKey, messageHash: bytes32, domain: BLSDomain):
* Compines all given signature into one. * Compines all given signature into one.
* @param signatures * @param signatures
*/ */
function aggregateSignatures(signatures: BLSSignature[]): BLSSignature { export function aggregateSignatures(signatures: BLSSignature[]): BLSSignature {
return signatures.map((signature): Signature => { return signatures.map((signature): Signature => {
return Signature.fromCompressedBytes(signature) return Signature.fromCompressedBytes(signature);
}).reduce((previousValue, currentValue): Signature => { }).reduce((previousValue, currentValue): Signature => {
return previousValue.add(currentValue); return previousValue.add(currentValue);
}).toBytesCompressed(); }).toBytesCompressed();
@ -59,12 +54,12 @@ function aggregateSignatures(signatures: BLSSignature[]): BLSSignature {
* Combines all given public keys into single one * Combines all given public keys into single one
* @param publicKeys * @param publicKeys
*/ */
function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey { export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
if(publicKeys.length === 0) { if(publicKeys.length === 0) {
return new G1point(new ctx.ECP()).toBytesCompressed(); return new G1point(new ctx.ECP()).toBytesCompressed();
} }
return publicKeys.map((publicKey): G1point => { return publicKeys.map((publicKey): G1point => {
return G1point.fromBytesCompressed(publicKey) return G1point.fromBytesCompressed(publicKey);
}).reduce((previousValue, currentValue): G1point => { }).reduce((previousValue, currentValue): G1point => {
return previousValue.add(currentValue); return previousValue.add(currentValue);
}).toBytesCompressed(); }).toBytesCompressed();
@ -77,7 +72,7 @@ function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
* @param signature * @param signature
* @param domain * @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 { try {
const key = PublicKey.fromBytes(publicKey); const key = PublicKey.fromBytes(publicKey);
const sig = Signature.fromCompressedBytes(signature); const sig = Signature.fromCompressedBytes(signature);
@ -98,7 +93,7 @@ function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BLSSignat
* @param signature * @param signature
* @param domain * @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) { if(publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false; return false;
} }
@ -129,4 +124,4 @@ export default {
aggregatePubkeys, aggregatePubkeys,
verify, verify,
verifyMultiple verifyMultiple
} };

View File

@ -5,7 +5,7 @@ import ctx from "./ctx";
import {padLeft} from "./helpers/utils"; import {padLeft} from "./helpers/utils";
import {G2point} from "./helpers/g2point"; import {G2point} from "./helpers/g2point";
import * as random from "secure-random"; import * as random from "secure-random";
import {BLSDomain, BLSSecretKey, bytes32} from "./types"; import {BLSSecretKey, bytes32, Domain} from "@chainsafe/eth2.0-types";
export class PrivateKey { export class PrivateKey {
@ -23,7 +23,7 @@ export class PrivateKey {
return message.mul(this.value); 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); return G2point.hashToG2(message, domain).mul(this.value);
} }

View File

@ -1,6 +1,6 @@
import {G1point} from "./helpers/g1point"; import {G1point} from "./helpers/g1point";
import {PrivateKey} from "./privateKey"; import {PrivateKey} from "./privateKey";
import {BLSPubkey} from "./types"; import {BLSPubkey} from "@chainsafe/eth2.0-types";
export class PublicKey { export class PublicKey {

View File

@ -1,7 +1,7 @@
import {G2point} from "./helpers/g2point"; import {G2point} from "./helpers/g2point";
import {BLSSignature} from "./types";
import assert from "assert"; import assert from "assert";
import {FP_POINT_LENGTH} from "./constants"; import {FP_POINT_LENGTH} from "./constants";
import {BLSSignature} from "@chainsafe/eth2.0-types";
export class Signature { export class Signature {

View File

@ -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;