Merge remote-tracking branch 'origin/master' into mpetrunic/rest-api
# Conflicts: # packages/bls/src/helpers/g2point.ts # packages/lodestar/package.json # packages/lodestar/src/validator/options.ts # packages/lodestar/test/e2e/chain/factory/block/assebleBlock.test.ts # yarn.lock
This commit is contained in:
commit
f92b1593d7
7
.babelrc
7
.babelrc
|
@ -1,3 +1,8 @@
|
||||||
{
|
{
|
||||||
"extends": "../../.babelrc"
|
"extends": "../../.babelrc",
|
||||||
|
"plugins": [
|
||||||
|
"@babel/proposal-class-properties",
|
||||||
|
"@babel/proposal-object-rest-spread",
|
||||||
|
"rewire-exports"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
declare module 'keccak256' {
|
|
||||||
|
|
||||||
export default function hash(a: Buffer | (Buffer | string | number)[]): Buffer;
|
|
||||||
|
|
||||||
}
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
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 {BLSDomain, bytes32, bytes96} from "../types";
|
||||||
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 +38,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 +59,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 +95,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);
|
||||||
|
|
25
src/index.ts
25
src/index.ts
|
@ -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,7 +42,7 @@ 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 => {
|
||||||
|
@ -59,7 +54,7 @@ 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();
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
|
@ -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;
|
|
Reference in New Issue