Consistent naming

This commit is contained in:
Cayman 2020-11-30 12:01:13 -06:00
parent ab0baac84b
commit b5b2e6dfc7
No known key found for this signature in database
GPG Key ID: 54B21AEC3C53E1F5
20 changed files with 106 additions and 106 deletions

View File

@ -1,11 +1,11 @@
import {PrivateKey} from "./privateKey";
import {SecretKey} from "./secretKey";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
import {IBls} from "../interface";
import {functionalInterfaceFactory} from "../functional";
export * from "../constants";
export {PrivateKey, PublicKey, Signature};
export {SecretKey, PublicKey, Signature};
export async function init(): Promise<void> {
// Native bindings require no init() call
@ -15,11 +15,11 @@ export function destroy(): void {
}
export const bls: IBls = {
PrivateKey,
SecretKey,
PublicKey,
Signature,
...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}),
...functionalInterfaceFactory({SecretKey, PublicKey, Signature}),
init,
destroy,
};

View File

@ -26,12 +26,12 @@ export class PublicKey implements IPublicKey {
return this.fromBytes(hexToBytes(hex));
}
static aggregate(pubkeys: PublicKey[]): PublicKey {
if (pubkeys.length === 0) {
static aggregate(publicKeys: PublicKey[]): PublicKey {
if (publicKeys.length === 0) {
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();
return new PublicKey(affine, jacobian);
}

View File

@ -1,35 +1,35 @@
import * as blst from "@chainsafe/blst";
import {bytesToHex, hexToBytes, isZeroUint8Array, randomBytes} from "../helpers";
import {SECRET_KEY_LENGTH} from "../constants";
import {IPrivateKey} from "../interface";
import {ISecretKey} from "../interface";
import {PublicKey} from "./publicKey";
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;
constructor(value: blst.SecretKey) {
this.value = value;
}
static fromBytes(bytes: Uint8Array): PrivateKey {
static fromBytes(bytes: Uint8Array): SecretKey {
// draft-irtf-cfrg-bls-signature-04 does not allow SK == 0
if (isZeroUint8Array(bytes)) {
throw new ZeroPrivateKeyError();
throw new ZeroSecretKeyError();
}
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));
}
static fromKeygen(entropy?: Uint8Array): PrivateKey {
static fromKeygen(entropy?: Uint8Array): SecretKey {
const sk = blst.SecretKey.fromKeygen(entropy || randomBytes(SECRET_KEY_LENGTH));
return new PrivateKey(sk);
return new SecretKey(sk);
}
sign(message: Uint8Array): Signature {

View File

@ -58,8 +58,8 @@ export class Signature implements ISignature {
}
private aggregateVerify(msgs: Uint8Array[], pks: blst.PublicKey[]): boolean {
// If this set is simply an infinity signature and infinity pubkey then skip verification.
// This has the effect of always declaring that this sig/pubkey combination is valid.
// If this set is simply an infinity signature and infinity publicKey then skip verification.
// This has the effect of always declaring that this sig/publicKey combination is valid.
// for Eth2.0 specs tests
if (this.affine.value.is_inf() && pks.length === 1 && pks[0].value.is_inf()) {
return true;

View File

@ -8,9 +8,9 @@ export class NotInitializedError extends Error {
}
}
export class ZeroPrivateKeyError extends Error {
export class ZeroSecretKeyError extends Error {
constructor() {
super("ZERO_PRIVATE_KEY");
super("ZERO_SECRET_KEY");
}
}

View File

@ -5,10 +5,10 @@ import {NotInitializedError} from "./errors";
// Returned type is enforced at each implementation's index
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function functionalInterfaceFactory({
PrivateKey,
SecretKey,
PublicKey,
Signature,
}: Pick<IBls, "PrivateKey" | "PublicKey" | "Signature">) {
}: Pick<IBls, "SecretKey" | "PublicKey" | "Signature">) {
/**
* Signs given message using secret key.
* @param secretKey
@ -18,8 +18,7 @@ export function functionalInterfaceFactory({
validateBytes(secretKey, "secretKey");
validateBytes(message, "message");
const privateKey = PrivateKey.fromBytes(secretKey);
return privateKey.sign(message).toBytes();
return SecretKey.fromBytes(secretKey).sign(message).toBytes();
}
/**
@ -35,7 +34,7 @@ export function functionalInterfaceFactory({
* Combines all given public keys into single one
* @param publicKeys
*/
function aggregatePubkeys(publicKeys: Uint8Array[]): Uint8Array {
function aggregatePublicKeys(publicKeys: Uint8Array[]): Uint8Array {
const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p)));
return agg.toBytes();
}
@ -72,7 +71,7 @@ export function functionalInterfaceFactory({
try {
return Signature.fromBytes(signature).verifyAggregate(
publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)),
publicKeys.map((publicKey) => PublicKey.fromBytes(publicKey)),
message
);
} catch (e) {
@ -110,7 +109,7 @@ export function functionalInterfaceFactory({
return {
sign,
aggregateSignatures,
aggregatePubkeys,
aggregatePublicKeys,
verify,
verifyAggregate,
verifyMultiple,

View File

@ -1,4 +1,4 @@
import {PrivateKey} from "./privateKey";
import {SecretKey} from "./secretKey";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
import {init, destroy} from "./context";
@ -6,14 +6,14 @@ import {IBls} from "../interface";
import {functionalInterfaceFactory} from "../functional";
export * from "../constants";
export {PrivateKey, PublicKey, Signature, init, destroy};
export {SecretKey, PublicKey, Signature, init, destroy};
export const bls: IBls = {
PrivateKey,
SecretKey,
PublicKey,
Signature,
...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}),
...functionalInterfaceFactory({SecretKey, PublicKey, Signature}),
init,
destroy,
};

View File

@ -33,13 +33,13 @@ export class PublicKey implements IPublicKey {
return this.fromBytes(hexToBytes(hex));
}
static aggregate(pubkeys: PublicKey[]): PublicKey {
if (pubkeys.length === 0) {
static aggregate(publicKeys: PublicKey[]): PublicKey {
if (publicKeys.length === 0) {
throw new EmptyAggregateError();
}
const agg = new PublicKey(pubkeys[0].value.clone());
for (const pk of pubkeys.slice(1)) {
const agg = new PublicKey(publicKeys[0].value.clone());
for (const pk of publicKeys.slice(1)) {
agg.value.add(pk.value);
}
return agg;

View File

@ -5,36 +5,36 @@ import {getContext} from "./context";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
import {bytesToHex, hexToBytes} from "../helpers";
import {IPrivateKey} from "../interface";
import {InvalidLengthError, ZeroPrivateKeyError} from "../errors";
import {ISecretKey} from "../interface";
import {InvalidLengthError, ZeroSecretKeyError} from "../errors";
export class PrivateKey implements IPrivateKey {
export class SecretKey implements ISecretKey {
readonly value: SecretKeyType;
constructor(value: SecretKeyType) {
if (value.isZero()) {
throw new ZeroPrivateKeyError();
throw new ZeroSecretKeyError();
}
this.value = value;
}
static fromBytes(bytes: Uint8Array): PrivateKey {
static fromBytes(bytes: Uint8Array): SecretKey {
if (bytes.length !== SECRET_KEY_LENGTH) {
throw new InvalidLengthError("PrivateKey", SECRET_KEY_LENGTH);
throw new InvalidLengthError("SecretKey", SECRET_KEY_LENGTH);
}
const context = getContext();
const secretKey = new context.SecretKey();
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));
}
static fromKeygen(entropy?: Uint8Array): PrivateKey {
static fromKeygen(entropy?: Uint8Array): SecretKey {
const sk = generateRandomSecretKey(entropy && Buffer.from(entropy));
return this.fromBytes(sk);
}

View File

@ -1,4 +1,4 @@
import {IBls, IPrivateKey, IPublicKey, ISignature} from "./interface";
import {IBls, ISecretKey, IPublicKey, ISignature} from "./interface";
import {bls as blsHerumi} from "./herumi";
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)`
export declare let sign: IBls["sign"];
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 verifyAggregate: IBls["verifyAggregate"];
export declare let verifyMultiple: IBls["verifyMultiple"];
export declare class PrivateKey implements IPrivateKey {
static fromBytes(bytes: Uint8Array): PrivateKey;
static fromHex(hex: string): PrivateKey;
static fromKeygen(entropy?: Uint8Array): PrivateKey;
export declare class SecretKey implements ISecretKey {
static fromBytes(bytes: Uint8Array): SecretKey;
static fromHex(hex: string): SecretKey;
static fromKeygen(entropy?: Uint8Array): SecretKey;
sign(message: Uint8Array): Signature;
toPublicKey(): PublicKey;
toBytes(): Uint8Array;
@ -56,13 +56,14 @@ export declare class PrivateKey implements IPrivateKey {
}
export declare class PublicKey implements IPublicKey {
// Virtual property so PublicKey type != Signature type
private isPublicKey: true;
static fromBytes(bytes: Uint8Array): PublicKey;
static fromHex(hex: string): PublicKey;
static aggregate(pubkeys: PublicKey[]): PublicKey;
static aggregate(publicKeys: PublicKey[]): PublicKey;
toBytes(): Uint8Array;
toHex(): string;
// Virtual property so PublicKey type != Signature type
private isPublicKey: true;
}
export declare class Signature implements ISignature {

View File

@ -1,13 +1,13 @@
export interface IBls {
PrivateKey: {
fromBytes(bytes: Uint8Array): IPrivateKey;
fromHex(hex: string): IPrivateKey;
fromKeygen(ikm?: Uint8Array): IPrivateKey;
SecretKey: {
fromBytes(bytes: Uint8Array): ISecretKey;
fromHex(hex: string): ISecretKey;
fromKeygen(ikm?: Uint8Array): ISecretKey;
};
PublicKey: {
fromBytes(bytes: Uint8Array): IPublicKey;
fromHex(hex: string): IPublicKey;
aggregate(pubkeys: IPublicKey[]): IPublicKey;
aggregate(publicKeys: IPublicKey[]): IPublicKey;
};
Signature: {
fromBytes(bytes: Uint8Array): ISignature;
@ -16,7 +16,7 @@ export interface IBls {
};
sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array;
aggregatePubkeys(publicKeys: Uint8Array[]): Uint8Array;
aggregatePublicKeys(publicKeys: Uint8Array[]): Uint8Array;
aggregateSignatures(signatures: Uint8Array[]): Uint8Array;
verify(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): boolean;
verifyAggregate(publicKeys: Uint8Array[], message: Uint8Array, signature: Uint8Array): boolean;
@ -28,7 +28,7 @@ export interface IBls {
export interface IKeypair {
publicKey: IPublicKey;
privateKey: IPrivateKey;
secretKey: ISecretKey;
}
export interface IPublicKey {
@ -44,7 +44,7 @@ export interface ISignature {
verifyMultiple(publicKeys: IPublicKey[], messages: Uint8Array[]): boolean;
}
export interface IPrivateKey {
export interface ISecretKey {
toPublicKey(): IPublicKey;
sign(message: Uint8Array): ISignature;
toBytes(): Uint8Array;

View File

@ -13,7 +13,7 @@ runForAllImplementations((bls, implementation) => {
prepareTest: () => {
const msg = randomMsg();
const sk = bls.PrivateKey.fromKeygen();
const sk = bls.SecretKey.fromKeygen();
const pk = sk.toPublicKey();
const sig = sk.sign(msg);
return {
@ -34,7 +34,7 @@ runForAllImplementations((bls, implementation) => {
prepareTest: () => {
const msg = randomMsg();
const dataArr = range(aggCount).map(() => {
const sk = bls.PrivateKey.fromKeygen();
const sk = bls.SecretKey.fromKeygen();
const pk = sk.toPublicKey();
const sig = sk.sign(msg);
return {pk, sig};
@ -60,7 +60,7 @@ runForAllImplementations((bls, implementation) => {
prepareTest: () => {
return {
input: range(aggCount).map(() => bls.PrivateKey.fromKeygen().toPublicKey()),
input: range(aggCount).map(() => bls.SecretKey.fromKeygen().toPublicKey()),
};
},
testRunner: (pks) => {
@ -76,7 +76,7 @@ runForAllImplementations((bls, implementation) => {
prepareTest: () => {
const msg = randomMsg();
const sigs = range(aggCount).map(() => {
const sk = bls.PrivateKey.fromKeygen();
const sk = bls.SecretKey.fromKeygen();
return sk.sign(msg);
});
return {

View File

@ -3,7 +3,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-tes
import {bytesToHex, hexToBytes} from "../../src/helpers";
import {SPEC_TESTS_DIR} from "../params";
import {describeForAllImplementations} from "../switch";
import {ZeroPrivateKeyError} from "../../src/errors";
import {ZeroSecretKeyError} from "../../src/errors";
interface ISignMessageTestCase {
data: {
@ -25,7 +25,7 @@ describeForAllImplementations((bls) => {
const signature = bls.sign(hexToBytes(privkey), hexToBytes(message));
return bytesToHex(signature);
} catch (e) {
if (e instanceof ZeroPrivateKeyError) return null;
if (e instanceof ZeroSecretKeyError) return null;
else throw e;
}
},

View File

@ -1,11 +1,11 @@
import {expect} from "chai";
import {PrivateKey, PublicKey, Signature, init} from "../../src";
import {SecretKey, PublicKey, Signature, init} from "../../src";
describe("index named exports", () => {
it("Classes and methods should be defined", async () => {
await init("herumi");
const sk = PrivateKey.fromKeygen();
const sk = SecretKey.fromKeygen();
const msg = new Uint8Array(32);
const sig = sk.sign(msg);
const pk = sk.toPublicKey();

View File

@ -5,7 +5,7 @@ import {getN, randomMessage} from "../util";
export function runIndexTests(bls: IBls): void {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function getRandomData() {
const sk = bls.PrivateKey.fromKeygen();
const sk = bls.SecretKey.fromKeygen();
const pk = sk.toPublicKey();
const msg = randomMessage();
const sig = sk.sign(msg);
@ -47,15 +47,15 @@ export function runIndexTests(bls: IBls): void {
describe("verify multiple", () => {
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 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 aggPubkeys = [
bls.aggregatePubkeys([pks[0], pks[1]].map((pk) => pk.toBytes())),
bls.aggregatePubkeys([pks[2], pks[3]].map((pk) => pk.toBytes())),
bls.aggregatePublicKeys([pks[0], pks[1]].map((pk) => pk.toBytes())),
bls.aggregatePublicKeys([pks[2], pks[3]].map((pk) => pk.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", () => {
const n = 4;
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 sigs = sks.map((sk) => sk.sign(msg));

View File

@ -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();
});
});
}

View File

@ -14,8 +14,8 @@ export function runPublicKeyTests(bls: IBls): void {
expect(bls.PublicKey.fromHex(publicKey).toHex()).to.be.equal(publicKey);
});
it("from private key", () => {
bls.PrivateKey.fromKeygen().toPublicKey();
it("from secret key", () => {
bls.SecretKey.fromKeygen().toPublicKey();
});
});
}

View File

@ -1,4 +1,4 @@
import {runPrivateKeyTests} from "./privateKey.test";
import {runSecretKeyTests} from "./secretKey.test";
import {runPublicKeyTests} from "./publicKey.test";
// import {runKeypairTests} from "./keypair.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
describeForAllImplementations((bls) => {
runPrivateKeyTests(bls);
runSecretKeyTests(bls);
runPublicKeyTests(bls);
// runKeypairTests(bls);
runIndexTests(bls);

View File

@ -1,5 +1,5 @@
import herumi from "../../src/herumi";
import {runPrivateKeyTests} from "./privateKey.test";
import {runSecretKeyTests} from "./secretKey.test";
import {runPublicKeyTests} from "./publicKey.test";
// import {runKeypairTests} from "./keypair.test";
import {runIndexTests} from "./index.test";
@ -14,7 +14,7 @@ describe("herumi", () => {
await herumi.init();
});
runPrivateKeyTests(herumi);
runSecretKeyTests(herumi);
runPublicKeyTests(herumi);
// runKeypairTests(bls);
runIndexTests(herumi);

View File

@ -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();
});
});
}