Run prettier

This commit is contained in:
Cayman 2020-11-05 14:55:25 -06:00
parent 9f6b89fd12
commit 49d6d9f62c
No known key found for this signature in database
GPG Key ID: 54B21AEC3C53E1F5
16 changed files with 86 additions and 202 deletions

View File

@ -8,10 +8,7 @@ import { PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH } from "../constants";
* @param length * @param length
*/ */
export function padLeft(source: Uint8Array, length: number): Buffer { export function padLeft(source: Uint8Array, length: number): Buffer {
assert( assert(source.length <= length, "Given array must be smaller or equal to desired array size");
source.length <= length,
"Given array must be smaller or equal to desired array size"
);
const result = Buffer.alloc(length, 0); const result = Buffer.alloc(length, 0);
result.set(source, length - source.length); result.set(source, length - source.length);
return result; return result;

View File

@ -47,10 +47,7 @@ export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
* @param signatures * @param signatures
*/ */
export function aggregateSignatures(signatures: Uint8Array[]): Buffer { export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
assert( assert(signatures && signatures.length > 0, "signatures is null or undefined or empty array");
signatures && signatures.length > 0,
"signatures is null or undefined or empty array"
);
return Signature.aggregate( return Signature.aggregate(
signatures.map( signatures.map(
(signature): Signature => { (signature): Signature => {
@ -81,11 +78,7 @@ export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
* @param messageHash * @param messageHash
* @param signature * @param signature
*/ */
export function verify( export function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature: Uint8Array): boolean {
publicKey: Uint8Array,
messageHash: Uint8Array,
signature: Uint8Array
): boolean {
assert(publicKey, "publicKey is null or undefined"); assert(publicKey, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined"); assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined"); assert(signature, "signature is null or undefined");
@ -105,11 +98,7 @@ export function verify(
* @param messageHash * @param messageHash
* @param signature * @param signature
*/ */
export function verifyAggregate( export function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Array, signature: Uint8Array): boolean {
publicKeys: Uint8Array[],
messageHash: Uint8Array,
signature: Uint8Array
): boolean {
assert(publicKeys, "publicKey is null or undefined"); assert(publicKeys, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined"); assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined"); assert(signature, "signature is null or undefined");

View File

@ -14,10 +14,7 @@ export class PrivateKey {
} }
public static fromBytes(bytes: Uint8Array): PrivateKey { public static fromBytes(bytes: Uint8Array): PrivateKey {
assert( assert(bytes.length === SECRET_KEY_LENGTH, "Private key should have 32 bytes");
bytes.length === SECRET_KEY_LENGTH,
"Private key should have 32 bytes"
);
const context = getContext(); const context = getContext();
const secretKey = new context.SecretKey(); const secretKey = new context.SecretKey();
secretKey.deserialize(Buffer.from(bytes)); secretKey.deserialize(Buffer.from(bytes));
@ -26,10 +23,7 @@ export class PrivateKey {
public static fromHexString(value: string): PrivateKey { public static fromHexString(value: string): PrivateKey {
value = value.replace("0x", ""); value = value.replace("0x", "");
assert( assert(value.length === SECRET_KEY_LENGTH * 2, "secret key must have 32 bytes");
value.length === SECRET_KEY_LENGTH * 2,
"secret key must have 32 bytes"
);
const context = getContext(); const context = getContext();
return new PrivateKey(context.deserializeHexStrToSecretKey(value)); return new PrivateKey(context.deserializeHexStrToSecretKey(value));
} }

View File

@ -14,10 +14,7 @@ export class Signature {
} }
public static fromCompressedBytes(value: Uint8Array): Signature { public static fromCompressedBytes(value: Uint8Array): Signature {
assert( assert(value.length === 2 * FP_POINT_LENGTH, `Signature must have ${2 * FP_POINT_LENGTH} bytes`);
value.length === 2 * FP_POINT_LENGTH,
`Signature must have ${2 * FP_POINT_LENGTH} bytes`
);
const context = getContext(); const context = getContext();
const signature = new context.Signature(); const signature = new context.Signature();
if (!EMPTY_SIGNATURE.equals(value)) { if (!EMPTY_SIGNATURE.equals(value)) {
@ -47,21 +44,14 @@ export class Signature {
return this.value; return this.value;
} }
public verifyAggregate( public verifyAggregate(publicKeys: PublicKey[], message: Uint8Array): boolean {
publicKeys: PublicKey[],
message: Uint8Array
): boolean {
return this.value.fastAggregateVerify( return this.value.fastAggregateVerify(
publicKeys.map((key) => key.getValue()), publicKeys.map((key) => key.getValue()),
message message
); );
} }
public verifyMultiple( public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[], fast = false): boolean {
publicKeys: PublicKey[],
messages: Uint8Array[],
fast = false
): boolean {
const msgs = Buffer.concat(messages); const msgs = Buffer.concat(messages);
if (!fast && !getContext().areAllMsgDifferent(msgs)) { if (!fast && !getContext().areAllMsgDifferent(msgs)) {
return false; return false;

View File

@ -1,9 +1,6 @@
import path from "path"; import path from "path";
import bls, {initBLS} from "../../src"; import bls, {initBLS} from "../../src";
import { import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
describeDirectorySpecTest,
InputType,
} from "@chainsafe/lodestar-spec-test-util";
interface IAggregateSigsTestCase { interface IAggregateSigsTestCase {
data: { data: {
@ -18,10 +15,7 @@ before(async function f() {
describeDirectorySpecTest<IAggregateSigsTestCase, string>( describeDirectorySpecTest<IAggregateSigsTestCase, string>(
"BLS - aggregate sigs", "BLS - aggregate sigs",
path.join( path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate/small"),
__dirname,
"../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate/small"
),
(testCase) => { (testCase) => {
try { try {
const result = bls.aggregateSignatures( const result = bls.aggregateSignatures(

View File

@ -1,9 +1,6 @@
import path from "path"; import path from "path";
import bls, {initBLS} from "../../src"; import bls, {initBLS} from "../../src";
import { import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
describeDirectorySpecTest,
InputType,
} from "@chainsafe/lodestar-spec-test-util";
interface AggregateSigsVerifyTestCase { interface AggregateSigsVerifyTestCase {
data: { data: {
@ -26,10 +23,7 @@ before(async function f() {
describeDirectorySpecTest<AggregateSigsVerifyTestCase, boolean>( describeDirectorySpecTest<AggregateSigsVerifyTestCase, boolean>(
"BLS - aggregate sigs verify", "BLS - aggregate sigs verify",
path.join( path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate_verify/small"),
__dirname,
"../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate_verify/small"
),
(testCase) => { (testCase) => {
const pubkeys = testCase.data.input.pubkeys.map((pubkey) => { const pubkeys = testCase.data.input.pubkeys.map((pubkey) => {
return Buffer.from(pubkey.replace("0x", ""), "hex"); return Buffer.from(pubkey.replace("0x", ""), "hex");
@ -37,11 +31,7 @@ describeDirectorySpecTest<AggregateSigsVerifyTestCase, boolean>(
const messages = testCase.data.input.messages.map((msg) => { const messages = testCase.data.input.messages.map((msg) => {
return Buffer.from(msg.replace("0x", ""), "hex"); return Buffer.from(msg.replace("0x", ""), "hex");
}); });
return bls.verifyMultiple( return bls.verifyMultiple(pubkeys, messages, Buffer.from(testCase.data.input.signature.replace("0x", ""), "hex"));
pubkeys,
messages,
Buffer.from(testCase.data.input.signature.replace("0x", ""), "hex")
);
}, },
{ {
inputTypes: { inputTypes: {

View File

@ -1,9 +1,6 @@
import path from "path"; import path from "path";
import bls, {initBLS} from "../../src"; import bls, {initBLS} from "../../src";
import { import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
describeDirectorySpecTest,
InputType,
} from "@chainsafe/lodestar-spec-test-util";
interface AggregateSigsVerifyTestCase { interface AggregateSigsVerifyTestCase {
data: { data: {
@ -32,9 +29,7 @@ describeDirectorySpecTest<AggregateSigsVerifyTestCase, boolean>(
), ),
(testCase) => { (testCase) => {
return bls.verifyAggregate( return bls.verifyAggregate(
testCase.data.input.pubkeys.map((key) => testCase.data.input.pubkeys.map((key) => Buffer.from(key.replace("0x", ""), "hex")),
Buffer.from(key.replace("0x", ""), "hex")
),
Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"), Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"),
Buffer.from(testCase.data.input.signature.replace("0x", ""), "hex") Buffer.from(testCase.data.input.signature.replace("0x", ""), "hex")
); );

View File

@ -1,9 +1,6 @@
import path from "path"; import path from "path";
import bls, {initBLS} from "../../src"; import bls, {initBLS} from "../../src";
import { import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
describeDirectorySpecTest,
InputType,
} from "@chainsafe/lodestar-spec-test-util";
interface ISignMessageTestCase { interface ISignMessageTestCase {
data: { data: {
@ -21,10 +18,7 @@ before(async function f() {
describeDirectorySpecTest<ISignMessageTestCase, string>( describeDirectorySpecTest<ISignMessageTestCase, string>(
"BLS - sign", "BLS - sign",
path.join( path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/sign/small"),
__dirname,
"../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/sign/small"
),
(testCase) => { (testCase) => {
const signature = bls.sign( const signature = bls.sign(
Buffer.from(testCase.data.input.privkey.replace("0x", ""), "hex"), Buffer.from(testCase.data.input.privkey.replace("0x", ""), "hex"),

View File

@ -1,9 +1,6 @@
import path from "path"; import path from "path";
import bls, {initBLS} from "../../src"; import bls, {initBLS} from "../../src";
import { import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
describeDirectorySpecTest,
InputType,
} from "@chainsafe/lodestar-spec-test-util";
interface IVerifyTestCase { interface IVerifyTestCase {
data: { data: {
@ -22,10 +19,7 @@ before(async function f() {
describeDirectorySpecTest<IVerifyTestCase, boolean>( describeDirectorySpecTest<IVerifyTestCase, boolean>(
"BLS - verify", "BLS - verify",
path.join( path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/verify/small"),
__dirname,
"../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/verify/small"
),
(testCase) => { (testCase) => {
return bls.verify( return bls.verify(
Buffer.from(testCase.data.input.pubkey.replace("0x", ""), "hex"), Buffer.from(testCase.data.input.pubkey.replace("0x", ""), "hex"),

View File

@ -1,11 +1,4 @@
import bls, { import bls, {aggregatePubkeys, aggregateSignatures, initBLS, Keypair, verify, verifyMultiple} from "../../src";
aggregatePubkeys,
aggregateSignatures,
initBLS,
Keypair,
verify,
verifyMultiple,
} from "../../src";
import SHA256 from "@chainsafe/as-sha256"; import SHA256 from "@chainsafe/as-sha256";
import {expect} from "chai"; import {expect} from "chai";
import {destroy} from "../../src/context"; import {destroy} from "../../src/context";
@ -31,11 +24,7 @@ describe("test bls", function () {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test"))); const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test")));
const signature = keypair.privateKey.signMessage(messageHash); const signature = keypair.privateKey.signMessage(messageHash);
const result = verify( const result = verify(keypair.publicKey.toBytesCompressed(), messageHash, signature.toBytesCompressed());
keypair.publicKey.toBytesCompressed(),
messageHash,
signature.toBytesCompressed()
);
expect(result).to.be.true; expect(result).to.be.true;
}); });
@ -45,54 +34,32 @@ describe("test bls", function () {
const signature = keypair.privateKey.signMessage(messageHash); const signature = keypair.privateKey.signMessage(messageHash);
const pubKey = keypair.publicKey.toBytesCompressed(); const pubKey = keypair.publicKey.toBytesCompressed();
verify(pubKey, messageHash, signature.toBytesCompressed()); verify(pubKey, messageHash, signature.toBytesCompressed());
expect("0x" + pubKey.toString("hex")).to.be.equal( expect("0x" + pubKey.toString("hex")).to.be.equal(keypair.publicKey.toHexString());
keypair.publicKey.toHexString()
);
}); });
it("should fail verify empty signature", () => { it("should fail verify empty signature", () => {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const messageHash2 = Buffer.from( const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
SHA256.digest(Buffer.from("Test message2"))
);
const signature = Buffer.alloc(96); const signature = Buffer.alloc(96);
const result = verify( const result = verify(keypair.publicKey.toBytesCompressed(), messageHash2, signature);
keypair.publicKey.toBytesCompressed(),
messageHash2,
signature
);
expect(result).to.be.false; expect(result).to.be.false;
}); });
it("should fail verify signature of different message", () => { it("should fail verify signature of different message", () => {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const messageHash = Buffer.from( const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
SHA256.digest(Buffer.from("Test message")) const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
);
const messageHash2 = Buffer.from(
SHA256.digest(Buffer.from("Test message2"))
);
const signature = keypair.privateKey.signMessage(messageHash); const signature = keypair.privateKey.signMessage(messageHash);
const result = verify( const result = verify(keypair.publicKey.toBytesCompressed(), messageHash2, signature.toBytesCompressed());
keypair.publicKey.toBytesCompressed(),
messageHash2,
signature.toBytesCompressed()
);
expect(result).to.be.false; expect(result).to.be.false;
}); });
it("should fail verify signature signed by different key", () => { it("should fail verify signature signed by different key", () => {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const keypair2 = Keypair.generate(); const keypair2 = Keypair.generate();
const messageHash = Buffer.from( const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
SHA256.digest(Buffer.from("Test message"))
);
const signature = keypair.privateKey.signMessage(messageHash); const signature = keypair.privateKey.signMessage(messageHash);
const result = verify( const result = verify(keypair2.publicKey.toBytesCompressed(), messageHash, signature.toBytesCompressed());
keypair2.publicKey.toBytesCompressed(),
messageHash,
signature.toBytesCompressed()
);
expect(result).to.be.false; expect(result).to.be.false;
}); });
}); });
@ -131,11 +98,7 @@ describe("test bls", function () {
signature4.toBytesCompressed(), signature4.toBytesCompressed(),
]); ]);
const result = verifyMultiple( const result = verifyMultiple([aggregatePubKey12, aggregatePubKey34], [message1, message2], aggregateSignature);
[aggregatePubKey12, aggregatePubKey34],
[message1, message2],
aggregateSignature
);
expect(result).to.be.true; expect(result).to.be.true;
}); });
@ -245,11 +208,7 @@ describe("test bls", function () {
signature4.toBytesCompressed(), signature4.toBytesCompressed(),
]); ]);
const result = bls.verifyMultiple( const result = bls.verifyMultiple([aggregatePubKey12], [message2, message1], aggregateSignature);
[aggregatePubKey12],
[message2, message1],
aggregateSignature
);
expect(result).to.be.false; expect(result).to.be.false;
}); });

View File

@ -14,9 +14,7 @@ describe("keypair", function () {
it("should create from private and public key", () => { it("should create from private and public key", () => {
const secret = PrivateKey.random(); const secret = PrivateKey.random();
const secret2 = PrivateKey.random(); const secret2 = PrivateKey.random();
const publicKey = PublicKey.fromBytes( const publicKey = PublicKey.fromBytes(PublicKey.fromPrivateKey(secret2).toBytesCompressed());
PublicKey.fromPrivateKey(secret2).toBytesCompressed()
);
const keypair = new Keypair(secret, publicKey); const keypair = new Keypair(secret, publicKey);
expect(keypair.publicKey).to.be.equal(publicKey); expect(keypair.publicKey).to.be.equal(publicKey);
expect(keypair.privateKey).to.be.equal(secret); expect(keypair.privateKey).to.be.equal(secret);

View File

@ -15,25 +15,17 @@ describe("privateKey", function () {
it("should generate random private key", function () { it("should generate random private key", function () {
const privateKey1 = PrivateKey.random(); const privateKey1 = PrivateKey.random();
const privateKey2 = PrivateKey.random(); const privateKey2 = PrivateKey.random();
expect(privateKey1.toHexString()).to.not.be.equal( expect(privateKey1.toHexString()).to.not.be.equal(privateKey2.toHexString());
privateKey2.toHexString()
);
}); });
it("should export private key to hex string", function () { it("should export private key to hex string", function () {
const privateKey = const privateKey = "0x07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
"0x07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
expect(PrivateKey.fromHexString(privateKey).toHexString()).to.be.equal( expect(PrivateKey.fromHexString(privateKey).toHexString()).to.be.equal(privateKey);
privateKey
);
const privateKey2 = const privateKey2 = "07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
"07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
expect(PrivateKey.fromHexString(privateKey2).toHexString()).to.be.equal( expect(PrivateKey.fromHexString(privateKey2).toHexString()).to.be.equal(privateKey);
privateKey
);
}); });
it("should export private key to bytes", function () { it("should export private key to bytes", function () {

View File

@ -20,9 +20,7 @@ describe("public key", function () {
it("from bytes", function () { it("from bytes", function () {
const publicKey = const publicKey =
"b6f21199594b56d77670564bf422cb331d5281ca2c1f9a45588a56881d8287ef8619efa6456d6cd2ef61306aa5b21311"; "b6f21199594b56d77670564bf422cb331d5281ca2c1f9a45588a56881d8287ef8619efa6456d6cd2ef61306aa5b21311";
expect( expect(PublicKey.fromBytes(Buffer.from(publicKey, "hex")).toHexString()).to.be.equal(`0x${publicKey}`);
PublicKey.fromBytes(Buffer.from(publicKey, "hex")).toHexString()
).to.be.equal(`0x${publicKey}`);
}); });
it("from private key", function () { it("from private key", function () {