Replace herumi src with BLST
This commit is contained in:
parent
f29898d9dc
commit
5b06e4f61e
|
@ -1,4 +1,5 @@
|
|||
import assert from "assert";
|
||||
import crypto from "crypto";
|
||||
import {PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH} from "../constants";
|
||||
|
||||
/**
|
||||
|
@ -14,5 +15,17 @@ export function padLeft(source: Uint8Array, length: number): Buffer {
|
|||
return result;
|
||||
}
|
||||
|
||||
export function hexToBytes(hex: string): Uint8Array {
|
||||
return Uint8Array.from(Buffer.from(hex.replace("0x", ""), "hex"));
|
||||
}
|
||||
|
||||
export function bytesToHex(bytes: Uint8Array): string {
|
||||
return "0x" + Buffer.from(bytes).toString("hex");
|
||||
}
|
||||
|
||||
export function getRandomBytes(size: number): Uint8Array {
|
||||
return Uint8Array.from(crypto.randomBytes(size));
|
||||
}
|
||||
|
||||
export const EMPTY_PUBLIC_KEY = Buffer.alloc(PUBLIC_KEY_LENGTH);
|
||||
export const EMPTY_SIGNATURE = Buffer.alloc(SIGNATURE_LENGTH);
|
||||
|
|
45
src/index.ts
45
src/index.ts
|
@ -27,7 +27,7 @@ export function generateKeyPair(): Keypair {
|
|||
export function generatePublicKey(secretKey: Uint8Array): Buffer {
|
||||
assert(secretKey, "secretKey is null or undefined");
|
||||
const keypair = new Keypair(PrivateKey.fromBytes(toBuffer(secretKey)));
|
||||
return keypair.publicKey.toBytesCompressed();
|
||||
return keypair.publicKey.toBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,7 +39,7 @@ export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
|
|||
assert(secretKey, "secretKey is null or undefined");
|
||||
assert(messageHash, "messageHash is null or undefined");
|
||||
const privateKey = PrivateKey.fromBytes(toBuffer(secretKey));
|
||||
return privateKey.signMessage(toBuffer(messageHash)).toBytesCompressed();
|
||||
return privateKey.signMessage(toBuffer(messageHash)).toBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,13 +48,14 @@ export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
|
|||
*/
|
||||
export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
|
||||
assert(signatures && signatures.length > 0, "signatures is null or undefined or empty array");
|
||||
return Signature.aggregate(
|
||||
signatures.map(
|
||||
(signature): Signature => {
|
||||
return Signature.fromCompressedBytes(signature);
|
||||
}
|
||||
)
|
||||
).toBytesCompressed();
|
||||
// return Signature.aggregate(
|
||||
// signatures.map(
|
||||
// (signature): Signature => {
|
||||
// return Signature.fromBytes(signature);
|
||||
// }
|
||||
// )
|
||||
// ).toBytes();
|
||||
return Buffer.alloc(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,14 +63,8 @@ export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
|
|||
* @param publicKeys
|
||||
*/
|
||||
export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
|
||||
assert(publicKeys, "publicKeys is null or undefined");
|
||||
if (publicKeys.length === 0) {
|
||||
return Buffer.alloc(PUBLIC_KEY_LENGTH);
|
||||
}
|
||||
return publicKeys
|
||||
.map((p) => PublicKey.fromBytes(toBuffer(p)))
|
||||
.reduce((agg, pubKey) => agg.add(pubKey))
|
||||
.toBytesCompressed();
|
||||
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
|
||||
return agg.toBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -84,7 +79,7 @@ export function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature
|
|||
assert(signature, "signature is null or undefined");
|
||||
try {
|
||||
return PublicKey.fromBytes(publicKey).verifyMessage(
|
||||
Signature.fromCompressedBytes(toBuffer(signature)),
|
||||
Signature.fromBytes(toBuffer(signature)),
|
||||
toBuffer(messageHash)
|
||||
);
|
||||
} catch (e) {
|
||||
|
@ -103,7 +98,7 @@ export function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Arra
|
|||
assert(messageHash, "messageHash is null or undefined");
|
||||
assert(signature, "signature is null or undefined");
|
||||
try {
|
||||
return Signature.fromCompressedBytes(signature).verifyAggregate(
|
||||
return Signature.fromBytes(signature).verifyAggregate(
|
||||
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
|
||||
messageHash
|
||||
);
|
||||
|
@ -119,12 +114,7 @@ export function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Arra
|
|||
* @param signature
|
||||
* @param fast Check if all messages are different
|
||||
*/
|
||||
export function verifyMultiple(
|
||||
publicKeys: Uint8Array[],
|
||||
messageHashes: Uint8Array[],
|
||||
signature: Uint8Array,
|
||||
fast = false
|
||||
): boolean {
|
||||
export function verifyMultiple(publicKeys: Uint8Array[], messageHashes: Uint8Array[], signature: Uint8Array): boolean {
|
||||
assert(publicKeys, "publicKey is null or undefined");
|
||||
assert(messageHashes, "messageHash is null or undefined");
|
||||
assert(signature, "signature is null or undefined");
|
||||
|
@ -133,10 +123,9 @@ export function verifyMultiple(
|
|||
return false;
|
||||
}
|
||||
try {
|
||||
return Signature.fromCompressedBytes(toBuffer(signature)).verifyMultiple(
|
||||
return Signature.fromBytes(toBuffer(signature)).verifyMultiple(
|
||||
publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))),
|
||||
messageHashes.map((m) => toBuffer(m)),
|
||||
fast
|
||||
messageHashes.map((m) => toBuffer(m))
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
|
|
|
@ -9,7 +9,7 @@ export class Keypair {
|
|||
public constructor(privateKey: PrivateKey, publicKey?: PublicKey) {
|
||||
this._privateKey = privateKey;
|
||||
if (!publicKey) {
|
||||
this._publicKey = PublicKey.fromPrivateKey(this._privateKey);
|
||||
this._publicKey = privateKey.toPublicKey();
|
||||
} else {
|
||||
this._publicKey = publicKey;
|
||||
}
|
||||
|
@ -24,6 +24,6 @@ export class Keypair {
|
|||
}
|
||||
|
||||
public static generate(): Keypair {
|
||||
return new Keypair(PrivateKey.random());
|
||||
return new Keypair(PrivateKey.fromKeygen());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,66 +1,46 @@
|
|||
import {SECRET_KEY_LENGTH} from "./constants";
|
||||
import assert from "assert";
|
||||
import {SecretKeyType} from "@chainsafe/eth2-bls-wasm";
|
||||
import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
|
||||
import {getContext} from "./context";
|
||||
import * as blst from "@chainsafe/blst-ts";
|
||||
import {blst as blstBindings} from "@chainsafe/blst-ts/dist/bindings";
|
||||
import {bytesToHex, getRandomBytes, hexToBytes} from "./helpers/utils";
|
||||
import {PublicKey} from "./publicKey";
|
||||
import {Signature} from "./signature";
|
||||
|
||||
export class PrivateKey {
|
||||
private value: SecretKeyType;
|
||||
readonly value: blst.SecretKey;
|
||||
|
||||
protected constructor(value: SecretKeyType) {
|
||||
constructor(value: blst.SecretKey) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static fromBytes(bytes: Uint8Array): PrivateKey {
|
||||
assert(bytes.length === SECRET_KEY_LENGTH, "Private key should have 32 bytes");
|
||||
const context = getContext();
|
||||
const secretKey = new context.SecretKey();
|
||||
secretKey.deserialize(Buffer.from(bytes));
|
||||
return new PrivateKey(secretKey);
|
||||
static fromBytes(bytes: Uint8Array): PrivateKey {
|
||||
const sk = blst.SecretKey.fromBytes(bytes);
|
||||
return new PrivateKey(sk);
|
||||
}
|
||||
|
||||
public static fromHexString(value: string): PrivateKey {
|
||||
value = value.replace("0x", "");
|
||||
assert(value.length === SECRET_KEY_LENGTH * 2, "secret key must have 32 bytes");
|
||||
const context = getContext();
|
||||
return new PrivateKey(context.deserializeHexStrToSecretKey(value));
|
||||
static fromHex(hex: string): PrivateKey {
|
||||
return this.fromBytes(hexToBytes(hex));
|
||||
}
|
||||
|
||||
public static fromInt(num: number): PrivateKey {
|
||||
const context = getContext();
|
||||
const secretKey = new context.SecretKey();
|
||||
secretKey.setInt(num);
|
||||
return new PrivateKey(secretKey);
|
||||
static fromKeygen(entropy?: Uint8Array): PrivateKey {
|
||||
const sk = blst.SecretKey.fromKeygen(entropy || getRandomBytes(32));
|
||||
return new PrivateKey(sk);
|
||||
}
|
||||
|
||||
public static random(): PrivateKey {
|
||||
const randomKey: Buffer = generateRandomSecretKey();
|
||||
return this.fromBytes(randomKey);
|
||||
}
|
||||
|
||||
public getValue(): SecretKeyType {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
// public sign(message: Uint8Array): Signature {
|
||||
// return Signature.fromValue(this.value.sign(message));
|
||||
// }
|
||||
|
||||
public signMessage(message: Uint8Array): Signature {
|
||||
signMessage(message: Uint8Array): Signature {
|
||||
return Signature.fromValue(this.value.sign(message));
|
||||
}
|
||||
|
||||
public toPublicKey(): PublicKey {
|
||||
return PublicKey.fromPublicKeyType(this.value.getPublicKey());
|
||||
toPublicKey(): PublicKey {
|
||||
const p1 = new blstBindings.P1(this.value.value);
|
||||
const jacobian = new blst.AggregatePublicKey(p1);
|
||||
const affine = jacobian.toPublicKey();
|
||||
return new PublicKey(affine, jacobian);
|
||||
}
|
||||
|
||||
public toBytes(): Buffer {
|
||||
return Buffer.from(this.value.serialize());
|
||||
toBytes(): Buffer {
|
||||
return Buffer.from(this.value.toBytes());
|
||||
}
|
||||
|
||||
public toHexString(): string {
|
||||
return `0x${this.value.serializeToHexStr()}`;
|
||||
toHex(): string {
|
||||
return bytesToHex(this.toBytes());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,61 +1,47 @@
|
|||
import {PrivateKey} from "./privateKey";
|
||||
import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
|
||||
import {getContext} from "./context";
|
||||
import {PUBLIC_KEY_LENGTH} from "./constants";
|
||||
import assert from "assert";
|
||||
import * as blst from "@chainsafe/blst-ts";
|
||||
import {blst as blstBindings} from "@chainsafe/blst-ts/dist/bindings";
|
||||
import {Signature} from "./signature";
|
||||
import {EMPTY_PUBLIC_KEY} from "./helpers/utils";
|
||||
import {bytesToHex, hexToBytes} from "./helpers/utils";
|
||||
|
||||
export class PublicKey {
|
||||
private value: PublicKeyType;
|
||||
readonly affine: blst.PublicKey;
|
||||
readonly jacobian: blst.AggregatePublicKey;
|
||||
|
||||
protected constructor(value: PublicKeyType) {
|
||||
this.value = value;
|
||||
constructor(affine: blst.PublicKey, jacobian: blst.AggregatePublicKey) {
|
||||
this.affine = affine;
|
||||
this.jacobian = jacobian;
|
||||
}
|
||||
|
||||
public static fromPrivateKey(privateKey: PrivateKey): PublicKey {
|
||||
return privateKey.toPublicKey();
|
||||
static fromBytes(bytes: Uint8Array): PublicKey {
|
||||
const affine = blst.PublicKey.fromBytes(bytes);
|
||||
const jacobian = blst.AggregatePublicKey.fromPublicKey(affine);
|
||||
return new PublicKey(affine, jacobian);
|
||||
}
|
||||
|
||||
public static fromBytes(bytes: Uint8Array): PublicKey {
|
||||
const context = getContext();
|
||||
const publicKey = new context.PublicKey();
|
||||
if (!EMPTY_PUBLIC_KEY.equals(bytes)) {
|
||||
publicKey.deserialize(bytes);
|
||||
}
|
||||
return new PublicKey(publicKey);
|
||||
static fromHex(hex: string): PublicKey {
|
||||
return this.fromBytes(hexToBytes(hex));
|
||||
}
|
||||
|
||||
public static fromHex(value: string): PublicKey {
|
||||
value = value.replace("0x", "");
|
||||
assert(value.length === PUBLIC_KEY_LENGTH * 2);
|
||||
const context = getContext();
|
||||
return new PublicKey(context.deserializeHexStrToPublicKey(value));
|
||||
static aggregate(pubkeys: PublicKey[]): PublicKey {
|
||||
const p1Arr = pubkeys.map((pk) => pk.jacobian.value);
|
||||
const aggP1 = p1Arr.reduce((_agg, pk) => {
|
||||
return blstBindings.P1.add(_agg, pk);
|
||||
});
|
||||
|
||||
const jacobian = new blst.AggregatePublicKey(aggP1);
|
||||
const affine = jacobian.toPublicKey();
|
||||
return new PublicKey(affine, jacobian);
|
||||
}
|
||||
|
||||
public static fromPublicKeyType(value: PublicKeyType): PublicKey {
|
||||
return new PublicKey(value);
|
||||
verifyMessage(signature: Signature, message: Uint8Array): boolean {
|
||||
return blst.verify(message, this.affine, signature.value);
|
||||
}
|
||||
|
||||
public add(other: PublicKey): PublicKey {
|
||||
const agg = new PublicKey(this.value.clone());
|
||||
agg.value.add(other.value);
|
||||
return agg;
|
||||
toBytes(): Buffer {
|
||||
return Buffer.from(this.affine.toBytes());
|
||||
}
|
||||
|
||||
public verifyMessage(signature: Signature, messageHash: Uint8Array): boolean {
|
||||
return this.value.verify(signature.getValue(), messageHash);
|
||||
}
|
||||
|
||||
public toBytesCompressed(): Buffer {
|
||||
return Buffer.from(this.value.serialize());
|
||||
}
|
||||
|
||||
public toHexString(): string {
|
||||
return `0x${this.toBytesCompressed().toString("hex")}`;
|
||||
}
|
||||
|
||||
public getValue(): PublicKeyType {
|
||||
return this.value;
|
||||
toHex(): string {
|
||||
return bytesToHex(this.toBytes());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,72 +1,53 @@
|
|||
import assert from "assert";
|
||||
import {FP_POINT_LENGTH} from "./constants";
|
||||
import {SignatureType} from "@chainsafe/eth2-bls-wasm";
|
||||
import {getContext} from "./context";
|
||||
import * as blst from "@chainsafe/blst-ts";
|
||||
import {blst as blstBindings} from "@chainsafe/blst-ts/dist/bindings";
|
||||
import {bytesToHex, hexToBytes} from "./helpers/utils";
|
||||
import {PublicKey} from "./publicKey";
|
||||
import {EMPTY_SIGNATURE} from "./helpers/utils";
|
||||
|
||||
export class Signature {
|
||||
private value: SignatureType;
|
||||
readonly value: blst.Signature;
|
||||
|
||||
protected constructor(value: SignatureType) {
|
||||
constructor(value: blst.Signature) {
|
||||
this.value = value;
|
||||
assert(this.value.isValidOrder());
|
||||
}
|
||||
|
||||
public static fromCompressedBytes(value: Uint8Array): Signature {
|
||||
assert(value.length === 2 * FP_POINT_LENGTH, `Signature must have ${2 * FP_POINT_LENGTH} bytes`);
|
||||
const context = getContext();
|
||||
const signature = new context.Signature();
|
||||
if (!EMPTY_SIGNATURE.equals(value)) {
|
||||
signature.deserialize(value);
|
||||
}
|
||||
static fromBytes(bytes: Uint8Array): Signature {
|
||||
return new Signature(blst.Signature.fromBytes(bytes));
|
||||
}
|
||||
|
||||
static fromHex(hex: string): Signature {
|
||||
return this.fromBytes(hexToBytes(hex));
|
||||
}
|
||||
|
||||
static fromValue(signature: blst.Signature): Signature {
|
||||
return new Signature(signature);
|
||||
}
|
||||
|
||||
public static fromValue(signature: SignatureType): Signature {
|
||||
return new Signature(signature);
|
||||
static aggregate(signatures: Signature[]): Signature {
|
||||
const agg = blst.AggregateSignature.fromSignatures(signatures.map((sig) => sig.value));
|
||||
return new Signature(agg.toSignature());
|
||||
}
|
||||
|
||||
public static aggregate(signatures: Signature[]): Signature {
|
||||
const context = getContext();
|
||||
const signature = new context.Signature();
|
||||
signature.aggregate(signatures.map((sig) => sig.getValue()));
|
||||
return new Signature(signature);
|
||||
}
|
||||
|
||||
public add(other: Signature): Signature {
|
||||
const agg = this.value.clone();
|
||||
agg.add(other.value);
|
||||
return new Signature(agg);
|
||||
}
|
||||
|
||||
public getValue(): SignatureType {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public verifyAggregate(publicKeys: PublicKey[], message: Uint8Array): boolean {
|
||||
return this.value.fastAggregateVerify(
|
||||
publicKeys.map((key) => key.getValue()),
|
||||
message
|
||||
verifyAggregate(publicKeys: PublicKey[], message: Uint8Array): boolean {
|
||||
return blst.fastAggregateVerify(
|
||||
message,
|
||||
publicKeys.map((pk) => pk.affine),
|
||||
this.value
|
||||
);
|
||||
}
|
||||
|
||||
public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[], fast = false): boolean {
|
||||
const msgs = Buffer.concat(messages);
|
||||
if (!fast && !getContext().areAllMsgDifferent(msgs)) {
|
||||
return false;
|
||||
}
|
||||
return this.value.aggregateVerifyNoCheck(
|
||||
publicKeys.map((key) => key.getValue()),
|
||||
msgs
|
||||
verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[]): boolean {
|
||||
return blst.aggregateVerify(
|
||||
messages,
|
||||
publicKeys.map((pk) => pk.affine),
|
||||
this.value
|
||||
);
|
||||
}
|
||||
|
||||
public toBytesCompressed(): Buffer {
|
||||
return Buffer.from(this.value.serialize());
|
||||
toBytes(): Buffer {
|
||||
return Buffer.from(this.value.toBytes());
|
||||
}
|
||||
|
||||
public toHex(): string {
|
||||
return "0x" + this.value.serializeToHexStr();
|
||||
toHex(): string {
|
||||
return bytesToHex(this.toBytes());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -145,7 +145,7 @@ import {runBenchmark} from "./runner";
|
|||
};
|
||||
},
|
||||
testRunner: (pks) => {
|
||||
pks.reduce((agg, pk) => agg.add(pk));
|
||||
herumi.PublicKey.aggregate(pks);
|
||||
},
|
||||
});
|
||||
})();
|
||||
|
|
|
@ -23,7 +23,7 @@ describe("test bls", function () {
|
|||
const keypair = Keypair.generate();
|
||||
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test")));
|
||||
const signature = keypair.privateKey.signMessage(messageHash);
|
||||
const result = verify(keypair.publicKey.toBytesCompressed(), messageHash, signature.toBytesCompressed());
|
||||
const result = verify(keypair.publicKey.toBytes(), messageHash, signature.toBytes());
|
||||
expect(result).to.be.true;
|
||||
});
|
||||
|
||||
|
@ -31,16 +31,16 @@ describe("test bls", function () {
|
|||
const keypair = Keypair.generate();
|
||||
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test")));
|
||||
const signature = keypair.privateKey.signMessage(messageHash);
|
||||
const pubKey = keypair.publicKey.toBytesCompressed();
|
||||
verify(pubKey, messageHash, signature.toBytesCompressed());
|
||||
expect("0x" + pubKey.toString("hex")).to.be.equal(keypair.publicKey.toHexString());
|
||||
const pubKey = keypair.publicKey.toBytes();
|
||||
verify(pubKey, messageHash, signature.toBytes());
|
||||
expect("0x" + pubKey.toString("hex")).to.be.equal(keypair.publicKey.toHex());
|
||||
});
|
||||
|
||||
it("should fail verify empty signature", () => {
|
||||
const keypair = Keypair.generate();
|
||||
const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
|
||||
const signature = Buffer.alloc(96);
|
||||
const result = verify(keypair.publicKey.toBytesCompressed(), messageHash2, signature);
|
||||
const result = verify(keypair.publicKey.toBytes(), messageHash2, signature);
|
||||
expect(result).to.be.false;
|
||||
});
|
||||
|
||||
|
@ -49,7 +49,7 @@ describe("test bls", function () {
|
|||
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
|
||||
const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
|
||||
const signature = keypair.privateKey.signMessage(messageHash);
|
||||
const result = verify(keypair.publicKey.toBytesCompressed(), messageHash2, signature.toBytesCompressed());
|
||||
const result = verify(keypair.publicKey.toBytes(), messageHash2, signature.toBytes());
|
||||
expect(result).to.be.false;
|
||||
});
|
||||
|
||||
|
@ -58,7 +58,7 @@ describe("test bls", function () {
|
|||
const keypair2 = Keypair.generate();
|
||||
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
|
||||
const signature = keypair.privateKey.signMessage(messageHash);
|
||||
const result = verify(keypair2.publicKey.toBytesCompressed(), messageHash, signature.toBytesCompressed());
|
||||
const result = verify(keypair2.publicKey.toBytes(), messageHash, signature.toBytes());
|
||||
expect(result).to.be.false;
|
||||
});
|
||||
});
|
||||
|
@ -80,21 +80,15 @@ describe("test bls", function () {
|
|||
const signature3 = keypair3.privateKey.signMessage(message2);
|
||||
const signature4 = keypair4.privateKey.signMessage(message2);
|
||||
|
||||
const aggregatePubKey12 = aggregatePubkeys([
|
||||
keypair1.publicKey.toBytesCompressed(),
|
||||
keypair2.publicKey.toBytesCompressed(),
|
||||
]);
|
||||
const aggregatePubKey12 = aggregatePubkeys([keypair1.publicKey.toBytes(), keypair2.publicKey.toBytes()]);
|
||||
|
||||
const aggregatePubKey34 = aggregatePubkeys([
|
||||
keypair3.publicKey.toBytesCompressed(),
|
||||
keypair4.publicKey.toBytesCompressed(),
|
||||
]);
|
||||
const aggregatePubKey34 = aggregatePubkeys([keypair3.publicKey.toBytes(), keypair4.publicKey.toBytes()]);
|
||||
|
||||
const aggregateSignature = aggregateSignatures([
|
||||
signature1.toBytesCompressed(),
|
||||
signature2.toBytesCompressed(),
|
||||
signature3.toBytesCompressed(),
|
||||
signature4.toBytesCompressed(),
|
||||
signature1.toBytes(),
|
||||
signature2.toBytes(),
|
||||
signature3.toBytes(),
|
||||
signature4.toBytes(),
|
||||
]);
|
||||
|
||||
const result = verifyMultiple([aggregatePubKey12, aggregatePubKey34], [message1, message2], aggregateSignature);
|
||||
|
@ -118,22 +112,21 @@ describe("test bls", function () {
|
|||
const signature4 = keypair4.privateKey.signMessage(message);
|
||||
|
||||
const aggregateSignature = aggregateSignatures([
|
||||
signature1.toBytesCompressed(),
|
||||
signature2.toBytesCompressed(),
|
||||
signature3.toBytesCompressed(),
|
||||
signature4.toBytesCompressed(),
|
||||
signature1.toBytes(),
|
||||
signature2.toBytes(),
|
||||
signature3.toBytes(),
|
||||
signature4.toBytes(),
|
||||
]);
|
||||
|
||||
const result = verifyMultiple(
|
||||
[
|
||||
keypair1.publicKey.toBytesCompressed(),
|
||||
keypair2.publicKey.toBytesCompressed(),
|
||||
keypair3.publicKey.toBytesCompressed(),
|
||||
keypair4.publicKey.toBytesCompressed(),
|
||||
keypair1.publicKey.toBytes(),
|
||||
keypair2.publicKey.toBytes(),
|
||||
keypair3.publicKey.toBytes(),
|
||||
keypair4.publicKey.toBytes(),
|
||||
],
|
||||
[message, message, message, message],
|
||||
aggregateSignature,
|
||||
true
|
||||
aggregateSignature
|
||||
);
|
||||
|
||||
expect(result).to.be.true;
|
||||
|
@ -155,21 +148,15 @@ describe("test bls", function () {
|
|||
const signature3 = keypair3.privateKey.signMessage(message2);
|
||||
const signature4 = keypair4.privateKey.signMessage(message2);
|
||||
|
||||
const aggregatePubKey12 = bls.aggregatePubkeys([
|
||||
keypair1.publicKey.toBytesCompressed(),
|
||||
keypair2.publicKey.toBytesCompressed(),
|
||||
]);
|
||||
const aggregatePubKey12 = bls.aggregatePubkeys([keypair1.publicKey.toBytes(), keypair2.publicKey.toBytes()]);
|
||||
|
||||
const aggregatePubKey34 = bls.aggregatePubkeys([
|
||||
keypair3.publicKey.toBytesCompressed(),
|
||||
keypair4.publicKey.toBytesCompressed(),
|
||||
]);
|
||||
const aggregatePubKey34 = bls.aggregatePubkeys([keypair3.publicKey.toBytes(), keypair4.publicKey.toBytes()]);
|
||||
|
||||
const aggregateSignature = bls.aggregateSignatures([
|
||||
signature1.toBytesCompressed(),
|
||||
signature2.toBytesCompressed(),
|
||||
signature3.toBytesCompressed(),
|
||||
signature4.toBytesCompressed(),
|
||||
signature1.toBytes(),
|
||||
signature2.toBytes(),
|
||||
signature3.toBytes(),
|
||||
signature4.toBytes(),
|
||||
]);
|
||||
|
||||
const result = bls.verifyMultiple(
|
||||
|
@ -195,16 +182,13 @@ describe("test bls", function () {
|
|||
const signature3 = keypair3.privateKey.signMessage(message2);
|
||||
const signature4 = keypair4.privateKey.signMessage(message2);
|
||||
|
||||
const aggregatePubKey12 = bls.aggregatePubkeys([
|
||||
keypair1.publicKey.toBytesCompressed(),
|
||||
keypair2.publicKey.toBytesCompressed(),
|
||||
]);
|
||||
const aggregatePubKey12 = bls.aggregatePubkeys([keypair1.publicKey.toBytes(), keypair2.publicKey.toBytes()]);
|
||||
|
||||
const aggregateSignature = bls.aggregateSignatures([
|
||||
signature1.toBytesCompressed(),
|
||||
signature2.toBytesCompressed(),
|
||||
signature3.toBytesCompressed(),
|
||||
signature4.toBytesCompressed(),
|
||||
signature1.toBytes(),
|
||||
signature2.toBytes(),
|
||||
signature3.toBytes(),
|
||||
signature4.toBytes(),
|
||||
]);
|
||||
|
||||
const result = bls.verifyMultiple([aggregatePubKey12], [message2, message1], aggregateSignature);
|
||||
|
|
|
@ -12,9 +12,9 @@ describe("keypair", function () {
|
|||
});
|
||||
|
||||
it("should create from private and public key", () => {
|
||||
const secret = PrivateKey.random();
|
||||
const secret2 = PrivateKey.random();
|
||||
const publicKey = PublicKey.fromBytes(PublicKey.fromPrivateKey(secret2).toBytesCompressed());
|
||||
const secret = PrivateKey.fromKeygen();
|
||||
const secret2 = PrivateKey.fromKeygen();
|
||||
const publicKey = PublicKey.fromBytes(secret2.toPublicKey().toBytes());
|
||||
const keypair = new Keypair(secret, publicKey);
|
||||
expect(keypair.publicKey).to.be.equal(publicKey);
|
||||
expect(keypair.privateKey).to.be.equal(secret);
|
||||
|
@ -22,11 +22,9 @@ describe("keypair", function () {
|
|||
});
|
||||
|
||||
it("should create from private", () => {
|
||||
const secret = PrivateKey.random();
|
||||
const publicKey = PublicKey.fromPrivateKey(secret);
|
||||
const secret = PrivateKey.fromKeygen();
|
||||
const publicKey = secret.toPublicKey();
|
||||
const keypair = new Keypair(secret);
|
||||
expect(keypair.publicKey.toBytesCompressed().toString("hex")).to.be.equal(
|
||||
publicKey.toBytesCompressed().toString("hex")
|
||||
);
|
||||
expect(keypair.publicKey.toBytes().toString("hex")).to.be.equal(publicKey.toBytes().toString("hex"));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -12,27 +12,27 @@ describe("privateKey", function () {
|
|||
destroy();
|
||||
});
|
||||
|
||||
it("should generate random private key", function () {
|
||||
const privateKey1 = PrivateKey.random();
|
||||
const privateKey2 = PrivateKey.random();
|
||||
expect(privateKey1.toHexString()).to.not.be.equal(privateKey2.toHexString());
|
||||
it("should generate fromKeygen private key", function () {
|
||||
const privateKey1 = PrivateKey.fromKeygen();
|
||||
const privateKey2 = PrivateKey.fromKeygen();
|
||||
expect(privateKey1.toHex()).to.not.be.equal(privateKey2.toHex());
|
||||
});
|
||||
|
||||
it("should export private key to hex string", function () {
|
||||
const privateKey = "0x07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
|
||||
|
||||
expect(PrivateKey.fromHexString(privateKey).toHexString()).to.be.equal(privateKey);
|
||||
expect(PrivateKey.fromHex(privateKey).toHex()).to.be.equal(privateKey);
|
||||
|
||||
const privateKey2 = "07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
|
||||
|
||||
expect(PrivateKey.fromHexString(privateKey2).toHexString()).to.be.equal(privateKey);
|
||||
expect(PrivateKey.fromHex(privateKey2).toHex()).to.be.equal(privateKey);
|
||||
});
|
||||
|
||||
it("should export private key to bytes", function () {
|
||||
expect(PrivateKey.random().toBytes().length).to.be.equal(SECRET_KEY_LENGTH);
|
||||
expect(PrivateKey.fromKeygen().toBytes().length).to.be.equal(SECRET_KEY_LENGTH);
|
||||
});
|
||||
|
||||
it("should not accept too short private key", function () {
|
||||
expect(() => PrivateKey.fromHexString("0x2123")).to.throw();
|
||||
expect(() => PrivateKey.fromHex("0x2123")).to.throw();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -14,16 +14,16 @@ describe("public key", function () {
|
|||
it("from hex", function () {
|
||||
const publicKey =
|
||||
"0xb6f21199594b56d77670564bf422cb331d5281ca2c1f9a45588a56881d8287ef8619efa6456d6cd2ef61306aa5b21311";
|
||||
expect(PublicKey.fromHex(publicKey).toHexString()).to.be.equal(publicKey);
|
||||
expect(PublicKey.fromHex(publicKey).toHex()).to.be.equal(publicKey);
|
||||
});
|
||||
|
||||
it("from bytes", function () {
|
||||
const publicKey =
|
||||
"b6f21199594b56d77670564bf422cb331d5281ca2c1f9a45588a56881d8287ef8619efa6456d6cd2ef61306aa5b21311";
|
||||
expect(PublicKey.fromBytes(Buffer.from(publicKey, "hex")).toHexString()).to.be.equal(`0x${publicKey}`);
|
||||
expect(PublicKey.fromBytes(Buffer.from(publicKey, "hex")).toHex()).to.be.equal(`0x${publicKey}`);
|
||||
});
|
||||
|
||||
it("from private key", function () {
|
||||
PublicKey.fromPrivateKey(PrivateKey.random());
|
||||
PrivateKey.fromKeygen().toPublicKey();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -821,7 +821,9 @@
|
|||
buffer "^5.4.3"
|
||||
|
||||
"@chainsafe/blst-ts@file:../blst-ts":
|
||||
version "1.0.0"
|
||||
version "0.1.1"
|
||||
dependencies:
|
||||
node-fetch "^2.6.1"
|
||||
|
||||
"@chainsafe/eth2-bls-wasm@^0.5.0":
|
||||
version "0.5.0"
|
||||
|
@ -4415,6 +4417,11 @@ node-environment-flags@1.0.5:
|
|||
object.getownpropertydescriptors "^2.0.3"
|
||||
semver "^5.7.0"
|
||||
|
||||
node-fetch@^2.6.1:
|
||||
version "2.6.1"
|
||||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
|
||||
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
|
||||
|
||||
node-libs-browser@^2.2.1:
|
||||
version "2.2.1"
|
||||
resolved "https://registry.yarnpkg.com/node-libs-browser/-/node-libs-browser-2.2.1.tgz#b64f513d18338625f90346d27b0d235e631f6425"
|
||||
|
|
Reference in New Issue