Consistent naming
This commit is contained in:
parent
ab0baac84b
commit
b5b2e6dfc7
|
@ -1,11 +1,11 @@
|
||||||
import {PrivateKey} from "./privateKey";
|
import {SecretKey} from "./secretKey";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {Signature} from "./signature";
|
import {Signature} from "./signature";
|
||||||
import {IBls} from "../interface";
|
import {IBls} from "../interface";
|
||||||
import {functionalInterfaceFactory} from "../functional";
|
import {functionalInterfaceFactory} from "../functional";
|
||||||
export * from "../constants";
|
export * from "../constants";
|
||||||
|
|
||||||
export {PrivateKey, PublicKey, Signature};
|
export {SecretKey, PublicKey, Signature};
|
||||||
|
|
||||||
export async function init(): Promise<void> {
|
export async function init(): Promise<void> {
|
||||||
// Native bindings require no init() call
|
// Native bindings require no init() call
|
||||||
|
@ -15,11 +15,11 @@ export function destroy(): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const bls: IBls = {
|
export const bls: IBls = {
|
||||||
PrivateKey,
|
SecretKey,
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Signature,
|
Signature,
|
||||||
|
|
||||||
...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}),
|
...functionalInterfaceFactory({SecretKey, PublicKey, Signature}),
|
||||||
init,
|
init,
|
||||||
destroy,
|
destroy,
|
||||||
};
|
};
|
||||||
|
|
|
@ -26,12 +26,12 @@ export class PublicKey implements IPublicKey {
|
||||||
return this.fromBytes(hexToBytes(hex));
|
return this.fromBytes(hexToBytes(hex));
|
||||||
}
|
}
|
||||||
|
|
||||||
static aggregate(pubkeys: PublicKey[]): PublicKey {
|
static aggregate(publicKeys: PublicKey[]): PublicKey {
|
||||||
if (pubkeys.length === 0) {
|
if (publicKeys.length === 0) {
|
||||||
throw new EmptyAggregateError();
|
throw new EmptyAggregateError();
|
||||||
}
|
}
|
||||||
|
|
||||||
const jacobian = blst.aggregatePubkeys(pubkeys.map((pk) => pk.jacobian));
|
const jacobian = blst.aggregatePubkeys(publicKeys.map((pk) => pk.jacobian));
|
||||||
const affine = jacobian.toPublicKey();
|
const affine = jacobian.toPublicKey();
|
||||||
return new PublicKey(affine, jacobian);
|
return new PublicKey(affine, jacobian);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,35 +1,35 @@
|
||||||
import * as blst from "@chainsafe/blst";
|
import * as blst from "@chainsafe/blst";
|
||||||
import {bytesToHex, hexToBytes, isZeroUint8Array, randomBytes} from "../helpers";
|
import {bytesToHex, hexToBytes, isZeroUint8Array, randomBytes} from "../helpers";
|
||||||
import {SECRET_KEY_LENGTH} from "../constants";
|
import {SECRET_KEY_LENGTH} from "../constants";
|
||||||
import {IPrivateKey} from "../interface";
|
import {ISecretKey} from "../interface";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {Signature} from "./signature";
|
import {Signature} from "./signature";
|
||||||
import {ZeroPrivateKeyError} from "../errors";
|
import {ZeroSecretKeyError} from "../errors";
|
||||||
|
|
||||||
export class PrivateKey implements IPrivateKey {
|
export class SecretKey implements ISecretKey {
|
||||||
readonly value: blst.SecretKey;
|
readonly value: blst.SecretKey;
|
||||||
|
|
||||||
constructor(value: blst.SecretKey) {
|
constructor(value: blst.SecretKey) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromBytes(bytes: Uint8Array): PrivateKey {
|
static fromBytes(bytes: Uint8Array): SecretKey {
|
||||||
// draft-irtf-cfrg-bls-signature-04 does not allow SK == 0
|
// draft-irtf-cfrg-bls-signature-04 does not allow SK == 0
|
||||||
if (isZeroUint8Array(bytes)) {
|
if (isZeroUint8Array(bytes)) {
|
||||||
throw new ZeroPrivateKeyError();
|
throw new ZeroSecretKeyError();
|
||||||
}
|
}
|
||||||
|
|
||||||
const sk = blst.SecretKey.fromBytes(bytes);
|
const sk = blst.SecretKey.fromBytes(bytes);
|
||||||
return new PrivateKey(sk);
|
return new SecretKey(sk);
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromHex(hex: string): PrivateKey {
|
static fromHex(hex: string): SecretKey {
|
||||||
return this.fromBytes(hexToBytes(hex));
|
return this.fromBytes(hexToBytes(hex));
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromKeygen(entropy?: Uint8Array): PrivateKey {
|
static fromKeygen(entropy?: Uint8Array): SecretKey {
|
||||||
const sk = blst.SecretKey.fromKeygen(entropy || randomBytes(SECRET_KEY_LENGTH));
|
const sk = blst.SecretKey.fromKeygen(entropy || randomBytes(SECRET_KEY_LENGTH));
|
||||||
return new PrivateKey(sk);
|
return new SecretKey(sk);
|
||||||
}
|
}
|
||||||
|
|
||||||
sign(message: Uint8Array): Signature {
|
sign(message: Uint8Array): Signature {
|
|
@ -58,8 +58,8 @@ export class Signature implements ISignature {
|
||||||
}
|
}
|
||||||
|
|
||||||
private aggregateVerify(msgs: Uint8Array[], pks: blst.PublicKey[]): boolean {
|
private aggregateVerify(msgs: Uint8Array[], pks: blst.PublicKey[]): boolean {
|
||||||
// If this set is simply an infinity signature and infinity pubkey then skip verification.
|
// If this set is simply an infinity signature and infinity publicKey then skip verification.
|
||||||
// This has the effect of always declaring that this sig/pubkey combination is valid.
|
// This has the effect of always declaring that this sig/publicKey combination is valid.
|
||||||
// for Eth2.0 specs tests
|
// for Eth2.0 specs tests
|
||||||
if (this.affine.value.is_inf() && pks.length === 1 && pks[0].value.is_inf()) {
|
if (this.affine.value.is_inf() && pks.length === 1 && pks[0].value.is_inf()) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -8,9 +8,9 @@ export class NotInitializedError extends Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ZeroPrivateKeyError extends Error {
|
export class ZeroSecretKeyError extends Error {
|
||||||
constructor() {
|
constructor() {
|
||||||
super("ZERO_PRIVATE_KEY");
|
super("ZERO_SECRET_KEY");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,10 @@ import {NotInitializedError} from "./errors";
|
||||||
// Returned type is enforced at each implementation's index
|
// Returned type is enforced at each implementation's index
|
||||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||||
export function functionalInterfaceFactory({
|
export function functionalInterfaceFactory({
|
||||||
PrivateKey,
|
SecretKey,
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Signature,
|
Signature,
|
||||||
}: Pick<IBls, "PrivateKey" | "PublicKey" | "Signature">) {
|
}: Pick<IBls, "SecretKey" | "PublicKey" | "Signature">) {
|
||||||
/**
|
/**
|
||||||
* Signs given message using secret key.
|
* Signs given message using secret key.
|
||||||
* @param secretKey
|
* @param secretKey
|
||||||
|
@ -18,8 +18,7 @@ export function functionalInterfaceFactory({
|
||||||
validateBytes(secretKey, "secretKey");
|
validateBytes(secretKey, "secretKey");
|
||||||
validateBytes(message, "message");
|
validateBytes(message, "message");
|
||||||
|
|
||||||
const privateKey = PrivateKey.fromBytes(secretKey);
|
return SecretKey.fromBytes(secretKey).sign(message).toBytes();
|
||||||
return privateKey.sign(message).toBytes();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,7 +34,7 @@ export function functionalInterfaceFactory({
|
||||||
* Combines all given public keys into single one
|
* Combines all given public keys into single one
|
||||||
* @param publicKeys
|
* @param publicKeys
|
||||||
*/
|
*/
|
||||||
function aggregatePubkeys(publicKeys: Uint8Array[]): Uint8Array {
|
function aggregatePublicKeys(publicKeys: Uint8Array[]): Uint8Array {
|
||||||
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
|
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
|
||||||
return agg.toBytes();
|
return agg.toBytes();
|
||||||
}
|
}
|
||||||
|
@ -72,7 +71,7 @@ export function functionalInterfaceFactory({
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Signature.fromBytes(signature).verifyAggregate(
|
return Signature.fromBytes(signature).verifyAggregate(
|
||||||
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
|
publicKeys.map((publicKey) => PublicKey.fromBytes(publicKey)),
|
||||||
message
|
message
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -110,7 +109,7 @@ export function functionalInterfaceFactory({
|
||||||
return {
|
return {
|
||||||
sign,
|
sign,
|
||||||
aggregateSignatures,
|
aggregateSignatures,
|
||||||
aggregatePubkeys,
|
aggregatePublicKeys,
|
||||||
verify,
|
verify,
|
||||||
verifyAggregate,
|
verifyAggregate,
|
||||||
verifyMultiple,
|
verifyMultiple,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {PrivateKey} from "./privateKey";
|
import {SecretKey} from "./secretKey";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {Signature} from "./signature";
|
import {Signature} from "./signature";
|
||||||
import {init, destroy} from "./context";
|
import {init, destroy} from "./context";
|
||||||
|
@ -6,14 +6,14 @@ import {IBls} from "../interface";
|
||||||
import {functionalInterfaceFactory} from "../functional";
|
import {functionalInterfaceFactory} from "../functional";
|
||||||
export * from "../constants";
|
export * from "../constants";
|
||||||
|
|
||||||
export {PrivateKey, PublicKey, Signature, init, destroy};
|
export {SecretKey, PublicKey, Signature, init, destroy};
|
||||||
|
|
||||||
export const bls: IBls = {
|
export const bls: IBls = {
|
||||||
PrivateKey,
|
SecretKey,
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Signature,
|
Signature,
|
||||||
|
|
||||||
...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}),
|
...functionalInterfaceFactory({SecretKey, PublicKey, Signature}),
|
||||||
init,
|
init,
|
||||||
destroy,
|
destroy,
|
||||||
};
|
};
|
||||||
|
|
|
@ -33,13 +33,13 @@ export class PublicKey implements IPublicKey {
|
||||||
return this.fromBytes(hexToBytes(hex));
|
return this.fromBytes(hexToBytes(hex));
|
||||||
}
|
}
|
||||||
|
|
||||||
static aggregate(pubkeys: PublicKey[]): PublicKey {
|
static aggregate(publicKeys: PublicKey[]): PublicKey {
|
||||||
if (pubkeys.length === 0) {
|
if (publicKeys.length === 0) {
|
||||||
throw new EmptyAggregateError();
|
throw new EmptyAggregateError();
|
||||||
}
|
}
|
||||||
|
|
||||||
const agg = new PublicKey(pubkeys[0].value.clone());
|
const agg = new PublicKey(publicKeys[0].value.clone());
|
||||||
for (const pk of pubkeys.slice(1)) {
|
for (const pk of publicKeys.slice(1)) {
|
||||||
agg.value.add(pk.value);
|
agg.value.add(pk.value);
|
||||||
}
|
}
|
||||||
return agg;
|
return agg;
|
||||||
|
|
|
@ -5,36 +5,36 @@ import {getContext} from "./context";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {Signature} from "./signature";
|
import {Signature} from "./signature";
|
||||||
import {bytesToHex, hexToBytes} from "../helpers";
|
import {bytesToHex, hexToBytes} from "../helpers";
|
||||||
import {IPrivateKey} from "../interface";
|
import {ISecretKey} from "../interface";
|
||||||
import {InvalidLengthError, ZeroPrivateKeyError} from "../errors";
|
import {InvalidLengthError, ZeroSecretKeyError} from "../errors";
|
||||||
|
|
||||||
export class PrivateKey implements IPrivateKey {
|
export class SecretKey implements ISecretKey {
|
||||||
readonly value: SecretKeyType;
|
readonly value: SecretKeyType;
|
||||||
|
|
||||||
constructor(value: SecretKeyType) {
|
constructor(value: SecretKeyType) {
|
||||||
if (value.isZero()) {
|
if (value.isZero()) {
|
||||||
throw new ZeroPrivateKeyError();
|
throw new ZeroSecretKeyError();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromBytes(bytes: Uint8Array): PrivateKey {
|
static fromBytes(bytes: Uint8Array): SecretKey {
|
||||||
if (bytes.length !== SECRET_KEY_LENGTH) {
|
if (bytes.length !== SECRET_KEY_LENGTH) {
|
||||||
throw new InvalidLengthError("PrivateKey", SECRET_KEY_LENGTH);
|
throw new InvalidLengthError("SecretKey", SECRET_KEY_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
const secretKey = new context.SecretKey();
|
const secretKey = new context.SecretKey();
|
||||||
secretKey.deserialize(bytes);
|
secretKey.deserialize(bytes);
|
||||||
return new PrivateKey(secretKey);
|
return new SecretKey(secretKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromHex(hex: string): PrivateKey {
|
static fromHex(hex: string): SecretKey {
|
||||||
return this.fromBytes(hexToBytes(hex));
|
return this.fromBytes(hexToBytes(hex));
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromKeygen(entropy?: Uint8Array): PrivateKey {
|
static fromKeygen(entropy?: Uint8Array): SecretKey {
|
||||||
const sk = generateRandomSecretKey(entropy && Buffer.from(entropy));
|
const sk = generateRandomSecretKey(entropy && Buffer.from(entropy));
|
||||||
return this.fromBytes(sk);
|
return this.fromBytes(sk);
|
||||||
}
|
}
|
19
src/index.ts
19
src/index.ts
|
@ -1,4 +1,4 @@
|
||||||
import {IBls, IPrivateKey, IPublicKey, ISignature} from "./interface";
|
import {IBls, ISecretKey, IPublicKey, ISignature} from "./interface";
|
||||||
import {bls as blsHerumi} from "./herumi";
|
import {bls as blsHerumi} from "./herumi";
|
||||||
|
|
||||||
export type Implementation = "herumi" | "blst-native";
|
export type Implementation = "herumi" | "blst-native";
|
||||||
|
@ -40,15 +40,15 @@ export async function init(impl: Implementation): Promise<void> {
|
||||||
// Proxy named exports, will get set by `Object.assign(exports, blsImpl)`
|
// Proxy named exports, will get set by `Object.assign(exports, blsImpl)`
|
||||||
export declare let sign: IBls["sign"];
|
export declare let sign: IBls["sign"];
|
||||||
export declare let aggregateSignatures: IBls["aggregateSignatures"];
|
export declare let aggregateSignatures: IBls["aggregateSignatures"];
|
||||||
export declare let aggregatePubkeys: IBls["aggregatePubkeys"];
|
export declare let aggregatePublicKeys: IBls["aggregatePublicKeys"];
|
||||||
export declare let verify: IBls["verify"];
|
export declare let verify: IBls["verify"];
|
||||||
export declare let verifyAggregate: IBls["verifyAggregate"];
|
export declare let verifyAggregate: IBls["verifyAggregate"];
|
||||||
export declare let verifyMultiple: IBls["verifyMultiple"];
|
export declare let verifyMultiple: IBls["verifyMultiple"];
|
||||||
|
|
||||||
export declare class PrivateKey implements IPrivateKey {
|
export declare class SecretKey implements ISecretKey {
|
||||||
static fromBytes(bytes: Uint8Array): PrivateKey;
|
static fromBytes(bytes: Uint8Array): SecretKey;
|
||||||
static fromHex(hex: string): PrivateKey;
|
static fromHex(hex: string): SecretKey;
|
||||||
static fromKeygen(entropy?: Uint8Array): PrivateKey;
|
static fromKeygen(entropy?: Uint8Array): SecretKey;
|
||||||
sign(message: Uint8Array): Signature;
|
sign(message: Uint8Array): Signature;
|
||||||
toPublicKey(): PublicKey;
|
toPublicKey(): PublicKey;
|
||||||
toBytes(): Uint8Array;
|
toBytes(): Uint8Array;
|
||||||
|
@ -56,13 +56,14 @@ export declare class PrivateKey implements IPrivateKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare class PublicKey implements IPublicKey {
|
export declare class PublicKey implements IPublicKey {
|
||||||
|
// Virtual property so PublicKey type != Signature type
|
||||||
|
private isPublicKey: true;
|
||||||
|
|
||||||
static fromBytes(bytes: Uint8Array): PublicKey;
|
static fromBytes(bytes: Uint8Array): PublicKey;
|
||||||
static fromHex(hex: string): PublicKey;
|
static fromHex(hex: string): PublicKey;
|
||||||
static aggregate(pubkeys: PublicKey[]): PublicKey;
|
static aggregate(publicKeys: PublicKey[]): PublicKey;
|
||||||
toBytes(): Uint8Array;
|
toBytes(): Uint8Array;
|
||||||
toHex(): string;
|
toHex(): string;
|
||||||
// Virtual property so PublicKey type != Signature type
|
|
||||||
private isPublicKey: true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare class Signature implements ISignature {
|
export declare class Signature implements ISignature {
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
export interface IBls {
|
export interface IBls {
|
||||||
PrivateKey: {
|
SecretKey: {
|
||||||
fromBytes(bytes: Uint8Array): IPrivateKey;
|
fromBytes(bytes: Uint8Array): ISecretKey;
|
||||||
fromHex(hex: string): IPrivateKey;
|
fromHex(hex: string): ISecretKey;
|
||||||
fromKeygen(ikm?: Uint8Array): IPrivateKey;
|
fromKeygen(ikm?: Uint8Array): ISecretKey;
|
||||||
};
|
};
|
||||||
PublicKey: {
|
PublicKey: {
|
||||||
fromBytes(bytes: Uint8Array): IPublicKey;
|
fromBytes(bytes: Uint8Array): IPublicKey;
|
||||||
fromHex(hex: string): IPublicKey;
|
fromHex(hex: string): IPublicKey;
|
||||||
aggregate(pubkeys: IPublicKey[]): IPublicKey;
|
aggregate(publicKeys: IPublicKey[]): IPublicKey;
|
||||||
};
|
};
|
||||||
Signature: {
|
Signature: {
|
||||||
fromBytes(bytes: Uint8Array): ISignature;
|
fromBytes(bytes: Uint8Array): ISignature;
|
||||||
|
@ -16,7 +16,7 @@ export interface IBls {
|
||||||
};
|
};
|
||||||
|
|
||||||
sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array;
|
sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array;
|
||||||
aggregatePubkeys(publicKeys: Uint8Array[]): Uint8Array;
|
aggregatePublicKeys(publicKeys: Uint8Array[]): Uint8Array;
|
||||||
aggregateSignatures(signatures: Uint8Array[]): Uint8Array;
|
aggregateSignatures(signatures: Uint8Array[]): Uint8Array;
|
||||||
verify(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): boolean;
|
verify(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): boolean;
|
||||||
verifyAggregate(publicKeys: Uint8Array[], message: Uint8Array, signature: Uint8Array): boolean;
|
verifyAggregate(publicKeys: Uint8Array[], message: Uint8Array, signature: Uint8Array): boolean;
|
||||||
|
@ -28,7 +28,7 @@ export interface IBls {
|
||||||
|
|
||||||
export interface IKeypair {
|
export interface IKeypair {
|
||||||
publicKey: IPublicKey;
|
publicKey: IPublicKey;
|
||||||
privateKey: IPrivateKey;
|
secretKey: ISecretKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IPublicKey {
|
export interface IPublicKey {
|
||||||
|
@ -44,7 +44,7 @@ export interface ISignature {
|
||||||
verifyMultiple(publicKeys: IPublicKey[], messages: Uint8Array[]): boolean;
|
verifyMultiple(publicKeys: IPublicKey[], messages: Uint8Array[]): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IPrivateKey {
|
export interface ISecretKey {
|
||||||
toPublicKey(): IPublicKey;
|
toPublicKey(): IPublicKey;
|
||||||
sign(message: Uint8Array): ISignature;
|
sign(message: Uint8Array): ISignature;
|
||||||
toBytes(): Uint8Array;
|
toBytes(): Uint8Array;
|
||||||
|
|
|
@ -13,7 +13,7 @@ runForAllImplementations((bls, implementation) => {
|
||||||
|
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
const msg = randomMsg();
|
const msg = randomMsg();
|
||||||
const sk = bls.PrivateKey.fromKeygen();
|
const sk = bls.SecretKey.fromKeygen();
|
||||||
const pk = sk.toPublicKey();
|
const pk = sk.toPublicKey();
|
||||||
const sig = sk.sign(msg);
|
const sig = sk.sign(msg);
|
||||||
return {
|
return {
|
||||||
|
@ -34,7 +34,7 @@ runForAllImplementations((bls, implementation) => {
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
const msg = randomMsg();
|
const msg = randomMsg();
|
||||||
const dataArr = range(aggCount).map(() => {
|
const dataArr = range(aggCount).map(() => {
|
||||||
const sk = bls.PrivateKey.fromKeygen();
|
const sk = bls.SecretKey.fromKeygen();
|
||||||
const pk = sk.toPublicKey();
|
const pk = sk.toPublicKey();
|
||||||
const sig = sk.sign(msg);
|
const sig = sk.sign(msg);
|
||||||
return {pk, sig};
|
return {pk, sig};
|
||||||
|
@ -60,7 +60,7 @@ runForAllImplementations((bls, implementation) => {
|
||||||
|
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
return {
|
return {
|
||||||
input: range(aggCount).map(() => bls.PrivateKey.fromKeygen().toPublicKey()),
|
input: range(aggCount).map(() => bls.SecretKey.fromKeygen().toPublicKey()),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
testRunner: (pks) => {
|
testRunner: (pks) => {
|
||||||
|
@ -76,7 +76,7 @@ runForAllImplementations((bls, implementation) => {
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
const msg = randomMsg();
|
const msg = randomMsg();
|
||||||
const sigs = range(aggCount).map(() => {
|
const sigs = range(aggCount).map(() => {
|
||||||
const sk = bls.PrivateKey.fromKeygen();
|
const sk = bls.SecretKey.fromKeygen();
|
||||||
return sk.sign(msg);
|
return sk.sign(msg);
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -3,7 +3,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-tes
|
||||||
import {bytesToHex, hexToBytes} from "../../src/helpers";
|
import {bytesToHex, hexToBytes} from "../../src/helpers";
|
||||||
import {SPEC_TESTS_DIR} from "../params";
|
import {SPEC_TESTS_DIR} from "../params";
|
||||||
import {describeForAllImplementations} from "../switch";
|
import {describeForAllImplementations} from "../switch";
|
||||||
import {ZeroPrivateKeyError} from "../../src/errors";
|
import {ZeroSecretKeyError} from "../../src/errors";
|
||||||
|
|
||||||
interface ISignMessageTestCase {
|
interface ISignMessageTestCase {
|
||||||
data: {
|
data: {
|
||||||
|
@ -25,7 +25,7 @@ describeForAllImplementations((bls) => {
|
||||||
const signature = bls.sign(hexToBytes(privkey), hexToBytes(message));
|
const signature = bls.sign(hexToBytes(privkey), hexToBytes(message));
|
||||||
return bytesToHex(signature);
|
return bytesToHex(signature);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof ZeroPrivateKeyError) return null;
|
if (e instanceof ZeroSecretKeyError) return null;
|
||||||
else throw e;
|
else throw e;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import {expect} from "chai";
|
import {expect} from "chai";
|
||||||
import {PrivateKey, PublicKey, Signature, init} from "../../src";
|
import {SecretKey, PublicKey, Signature, init} from "../../src";
|
||||||
|
|
||||||
describe("index named exports", () => {
|
describe("index named exports", () => {
|
||||||
it("Classes and methods should be defined", async () => {
|
it("Classes and methods should be defined", async () => {
|
||||||
await init("herumi");
|
await init("herumi");
|
||||||
|
|
||||||
const sk = PrivateKey.fromKeygen();
|
const sk = SecretKey.fromKeygen();
|
||||||
const msg = new Uint8Array(32);
|
const msg = new Uint8Array(32);
|
||||||
const sig = sk.sign(msg);
|
const sig = sk.sign(msg);
|
||||||
const pk = sk.toPublicKey();
|
const pk = sk.toPublicKey();
|
||||||
|
|
|
@ -5,7 +5,7 @@ import {getN, randomMessage} from "../util";
|
||||||
export function runIndexTests(bls: IBls): void {
|
export function runIndexTests(bls: IBls): void {
|
||||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||||
function getRandomData() {
|
function getRandomData() {
|
||||||
const sk = bls.PrivateKey.fromKeygen();
|
const sk = bls.SecretKey.fromKeygen();
|
||||||
const pk = sk.toPublicKey();
|
const pk = sk.toPublicKey();
|
||||||
const msg = randomMessage();
|
const msg = randomMessage();
|
||||||
const sig = sk.sign(msg);
|
const sig = sk.sign(msg);
|
||||||
|
@ -47,15 +47,15 @@ export function runIndexTests(bls: IBls): void {
|
||||||
|
|
||||||
describe("verify multiple", () => {
|
describe("verify multiple", () => {
|
||||||
it("should verify aggregated signatures", () => {
|
it("should verify aggregated signatures", () => {
|
||||||
const sks = getN(4, () => bls.PrivateKey.fromKeygen());
|
const sks = getN(4, () => bls.SecretKey.fromKeygen());
|
||||||
const msgs = getN(2, () => randomMessage());
|
const msgs = getN(2, () => randomMessage());
|
||||||
const pks = sks.map((sk) => sk.toPublicKey());
|
const pks = sks.map((sk) => sk.toPublicKey());
|
||||||
|
|
||||||
const sigs = [sks[0].sign(msgs[0]), sks[1].sign(msgs[0]), sks[2].sign(msgs[1]), sks[3].sign(msgs[1])];
|
const sigs = [sks[0].sign(msgs[0]), sks[1].sign(msgs[0]), sks[2].sign(msgs[1]), sks[3].sign(msgs[1])];
|
||||||
|
|
||||||
const aggPubkeys = [
|
const aggPubkeys = [
|
||||||
bls.aggregatePubkeys([pks[0], pks[1]].map((pk) => pk.toBytes())),
|
bls.aggregatePublicKeys([pks[0], pks[1]].map((pk) => pk.toBytes())),
|
||||||
bls.aggregatePubkeys([pks[2], pks[3]].map((pk) => pk.toBytes())),
|
bls.aggregatePublicKeys([pks[2], pks[3]].map((pk) => pk.toBytes())),
|
||||||
];
|
];
|
||||||
|
|
||||||
const aggSig = bls.aggregateSignatures(sigs.map((sig) => sig.toBytes()));
|
const aggSig = bls.aggregateSignatures(sigs.map((sig) => sig.toBytes()));
|
||||||
|
@ -67,7 +67,7 @@ export function runIndexTests(bls: IBls): void {
|
||||||
it("should verify aggregated signatures - same message", () => {
|
it("should verify aggregated signatures - same message", () => {
|
||||||
const n = 4;
|
const n = 4;
|
||||||
const msg = randomMessage();
|
const msg = randomMessage();
|
||||||
const sks = getN(n, () => bls.PrivateKey.fromKeygen());
|
const sks = getN(n, () => bls.SecretKey.fromKeygen());
|
||||||
const pks = sks.map((sk) => sk.toPublicKey());
|
const pks = sks.map((sk) => sk.toPublicKey());
|
||||||
const sigs = sks.map((sk) => sk.sign(msg));
|
const sigs = sks.map((sk) => sk.sign(msg));
|
||||||
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
import {expect} from "chai";
|
|
||||||
import {IBls} from "../../src/interface";
|
|
||||||
|
|
||||||
export function runPrivateKeyTests(bls: IBls): void {
|
|
||||||
describe("PrivateKey", () => {
|
|
||||||
it("should generate fromKeygen private key", () => {
|
|
||||||
const privateKey1 = bls.PrivateKey.fromKeygen();
|
|
||||||
const privateKey2 = bls.PrivateKey.fromKeygen();
|
|
||||||
expect(privateKey1.toHex()).to.not.be.equal(privateKey2.toHex());
|
|
||||||
});
|
|
||||||
|
|
||||||
const privateKey = "0x07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
|
|
||||||
|
|
||||||
it("should export private key to hex string", () => {
|
|
||||||
expect(bls.PrivateKey.fromHex(privateKey).toHex()).to.be.equal(privateKey);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should export private key to hex string from non-prefixed hex", () => {
|
|
||||||
expect(bls.PrivateKey.fromHex(privateKey).toHex()).to.be.equal(privateKey);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not accept too short private key", () => {
|
|
||||||
expect(() => bls.PrivateKey.fromHex("0x2123")).to.throw();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -14,8 +14,8 @@ export function runPublicKeyTests(bls: IBls): void {
|
||||||
expect(bls.PublicKey.fromHex(publicKey).toHex()).to.be.equal(publicKey);
|
expect(bls.PublicKey.fromHex(publicKey).toHex()).to.be.equal(publicKey);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("from private key", () => {
|
it("from secret key", () => {
|
||||||
bls.PrivateKey.fromKeygen().toPublicKey();
|
bls.SecretKey.fromKeygen().toPublicKey();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {runPrivateKeyTests} from "./privateKey.test";
|
import {runSecretKeyTests} from "./secretKey.test";
|
||||||
import {runPublicKeyTests} from "./publicKey.test";
|
import {runPublicKeyTests} from "./publicKey.test";
|
||||||
// import {runKeypairTests} from "./keypair.test";
|
// import {runKeypairTests} from "./keypair.test";
|
||||||
import {runIndexTests} from "./index.test";
|
import {runIndexTests} from "./index.test";
|
||||||
|
@ -6,7 +6,7 @@ import {describeForAllImplementations} from "../switch";
|
||||||
|
|
||||||
// Import test's bls lib lazily to prevent breaking test with Karma
|
// Import test's bls lib lazily to prevent breaking test with Karma
|
||||||
describeForAllImplementations((bls) => {
|
describeForAllImplementations((bls) => {
|
||||||
runPrivateKeyTests(bls);
|
runSecretKeyTests(bls);
|
||||||
runPublicKeyTests(bls);
|
runPublicKeyTests(bls);
|
||||||
// runKeypairTests(bls);
|
// runKeypairTests(bls);
|
||||||
runIndexTests(bls);
|
runIndexTests(bls);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import herumi from "../../src/herumi";
|
import herumi from "../../src/herumi";
|
||||||
import {runPrivateKeyTests} from "./privateKey.test";
|
import {runSecretKeyTests} from "./secretKey.test";
|
||||||
import {runPublicKeyTests} from "./publicKey.test";
|
import {runPublicKeyTests} from "./publicKey.test";
|
||||||
// import {runKeypairTests} from "./keypair.test";
|
// import {runKeypairTests} from "./keypair.test";
|
||||||
import {runIndexTests} from "./index.test";
|
import {runIndexTests} from "./index.test";
|
||||||
|
@ -14,7 +14,7 @@ describe("herumi", () => {
|
||||||
await herumi.init();
|
await herumi.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
runPrivateKeyTests(herumi);
|
runSecretKeyTests(herumi);
|
||||||
runPublicKeyTests(herumi);
|
runPublicKeyTests(herumi);
|
||||||
// runKeypairTests(bls);
|
// runKeypairTests(bls);
|
||||||
runIndexTests(herumi);
|
runIndexTests(herumi);
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
import {expect} from "chai";
|
||||||
|
import {IBls} from "../../src/interface";
|
||||||
|
|
||||||
|
export function runSecretKeyTests(bls: IBls): void {
|
||||||
|
describe("SecretKey", () => {
|
||||||
|
it("should generate fromKeygen secret key", () => {
|
||||||
|
const secretKey1 = bls.SecretKey.fromKeygen();
|
||||||
|
const secretKey2 = bls.SecretKey.fromKeygen();
|
||||||
|
expect(secretKey1.toHex()).to.not.be.equal(secretKey2.toHex());
|
||||||
|
});
|
||||||
|
|
||||||
|
const secretKey = "0x07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7";
|
||||||
|
|
||||||
|
it("should export secret key to hex string", () => {
|
||||||
|
expect(bls.SecretKey.fromHex(secretKey).toHex()).to.be.equal(secretKey);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should export secret key to hex string from non-prefixed hex", () => {
|
||||||
|
expect(bls.SecretKey.fromHex(secretKey).toHex()).to.be.equal(secretKey);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not accept too short secret key", () => {
|
||||||
|
expect(() => bls.SecretKey.fromHex("0x2123")).to.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Reference in New Issue