This commit is contained in:
Marin Petrunić 2020-11-23 14:42:52 +01:00
parent 135625ccc0
commit c153de6e24
No known key found for this signature in database
GPG Key ID: 834D07135E110DA5
8 changed files with 121 additions and 125 deletions

View File

@ -1,21 +1,23 @@
import herumi from "bls-eth-wasm"; import {herumiToIBls} from "./herumi";
import { herumiToIBls } from "./herumi"; import {IBls} from "./interface";
import { IBls } from "./interface";
export type Backing = "herumi" | "blst-native" | "blst-wasm"; export type Backing = "herumi" | "blst-native" | "blst-wasm";
let contextBacking: Backing|undefined = undefined; let contextBacking: Backing | undefined = undefined;
let context: IBls|undefined = undefined; let context: IBls | undefined = undefined;
//to maintain api compatible, add all backing context to return type //to maintain api compatible, add all backing context to return type
export async function init(backing: Backing = "herumi"): Promise<IBls> { export async function init(backing: Backing = "herumi"): Promise<IBls> {
if (!context) { if (!context) {
switch(backing) { switch (backing) {
case "herumi": { case "herumi":
context = await herumiToIBls(); {
contextBacking = backing; context = await herumiToIBls();
} break; contextBacking = backing;
default: throw new Error(`Unsupported backing - ${backing}`) }
break;
default:
throw new Error(`Unsupported backing - ${backing}`);
} }
} }
await context.init(); await context.init();
@ -23,9 +25,9 @@ export async function init(backing: Backing = "herumi"): Promise<IBls> {
} }
export function destroy(): void { export function destroy(): void {
if(context) { if (context) {
context.destroy(); context.destroy();
} }
context = undefined; context = undefined;
contextBacking = undefined; contextBacking = undefined;
} }
@ -37,7 +39,6 @@ export function getContext(): IBls {
return context; return context;
} }
export function getContextBacking(): Backing { export function getContextBacking(): Backing {
if (!context) { if (!context) {
throw new Error("BLS not initialized"); throw new Error("BLS not initialized");

View File

@ -1,33 +1,33 @@
import herumi from "bls-eth-wasm"; import herumi from "bls-eth-wasm";
import { init as initHerumi, destroy, getContext } from "./context"; import {init as initHerumi, destroy, getContext} from "./context";
import { IBls, IPrivateKeyValue, IPublicKeyValue, ISignatureValue } from '../interface'; import {IBls, IPrivateKeyValue, IPublicKeyValue, ISignatureValue} from "../interface";
export async function herumiToIBls(): Promise<IBls> { export async function herumiToIBls(): Promise<IBls> {
await initHerumi(); await initHerumi();
const context: IBls = { const context: IBls = {
SecretKey: getContext().SecretKey as IPrivateKeyValue, SecretKey: getContext().SecretKey as IPrivateKeyValue,
PublicKey: getContext().PublicKey as IPublicKeyValue, PublicKey: getContext().PublicKey as IPublicKeyValue,
Signature: getContext().Signature as ISignatureValue, Signature: getContext().Signature as ISignatureValue,
toHex: herumi.toHex, toHex: herumi.toHex,
toHexStr: herumi.toHexStr, toHexStr: herumi.toHexStr,
fromHexStr: herumi.fromHexStr, fromHexStr: herumi.fromHexStr,
getCurveOrder: herumi.getCurveOrder, getCurveOrder: herumi.getCurveOrder,
getFieldOrder: herumi.getFieldOrder, getFieldOrder: herumi.getFieldOrder,
verifySignatureOrder: herumi.verifySignatureOrder, verifySignatureOrder: herumi.verifySignatureOrder,
verifyPublicKeyOrder: herumi.verifyPublicKeyOrder, verifyPublicKeyOrder: herumi.verifyPublicKeyOrder,
areAllMsgDifferent: herumi.areAllMsgDifferent, areAllMsgDifferent: herumi.areAllMsgDifferent,
shouldVerifyBlsSignatureOrder: herumi.shouldVerifyBlsSignatureOrder, shouldVerifyBlsSignatureOrder: herumi.shouldVerifyBlsSignatureOrder,
shouldVerifyBlsPublicKeyOrder: herumi.shouldVerifyBlsPublicKeyOrder, shouldVerifyBlsPublicKeyOrder: herumi.shouldVerifyBlsPublicKeyOrder,
deserializeHexStrToSecretKey: herumi.deserializeHexStrToSecretKey as IBls["deserializeHexStrToSecretKey"], deserializeHexStrToSecretKey: herumi.deserializeHexStrToSecretKey as IBls["deserializeHexStrToSecretKey"],
deserializeHexStrToPublicKey: herumi.deserializeHexStrToPublicKey as IBls["deserializeHexStrToPublicKey"], deserializeHexStrToPublicKey: herumi.deserializeHexStrToPublicKey as IBls["deserializeHexStrToPublicKey"],
deserializeHexStrToSignature: herumi.deserializeHexStrToSignature as IBls["deserializeHexStrToSignature"], deserializeHexStrToSignature: herumi.deserializeHexStrToSignature as IBls["deserializeHexStrToSignature"],
init: async () => { init: async () => {
return context; return context;
}, },
destroy: () => { destroy: () => {
destroy(); destroy();
} },
} };
return context; return context;
} }

View File

@ -1 +1 @@
export {herumiToIBls} from "./adapter"; export {herumiToIBls} from "./adapter";

View File

@ -1,9 +1,9 @@
import {PUBLIC_KEY_LENGTH} from "./constants"; import {PUBLIC_KEY_LENGTH} from "./constants";
import assert from "assert"; import assert from "assert";
import { Keypair } from "./keypair"; import {Keypair} from "./keypair";
import { PrivateKey } from "./privateKey"; import {PrivateKey} from "./privateKey";
import { PublicKey } from "./publicKey"; import {PublicKey} from "./publicKey";
import { Signature } from "./signature"; import {Signature} from "./signature";
export {Keypair, PrivateKey, PublicKey, Signature}; export {Keypair, PrivateKey, PublicKey, Signature};

View File

@ -1,90 +1,85 @@
interface Common { interface ICommon {
new (): this;
new(): this; deserializeHexStr(s: string): void;
deserializeHexStr(s: string): void; serializeToHexStr(): string;
serializeToHexStr(): string; isEqual(rhs: this): boolean;
isEqual(rhs: this): boolean deserialize(v: Uint8Array): void;
deserialize(v: Uint8Array): void; serialize(): Uint8Array;
serialize(): Uint8Array; add(rhs: this): void;
add(rhs: this): void; dump(msg?: string): string;
dump(msg?: string): string; clear(): void;
clear(): void;
clone(): this; clone(): this;
} }
export interface IPrivateKeyValue extends Common { export interface IPrivateKeyValue extends ICommon {
setInt(x: number): void;
setInt(x: number): void; setHashOf(a: Uint8Array): void;
setHashOf(a: Uint8Array): void; setLittleEndian(a: Uint8Array): void;
setLittleEndian(a: Uint8Array): void; setByCSPRNG(): void;
setByCSPRNG(): void; getPublicKey(): IPublicKeyValue;
getPublicKey(): IPublicKeyValue;
sign(m: string | Uint8Array): ISignatureValue;
sign(m: string | Uint8Array): ISignatureValue;
} }
export interface IPublicKeyValue extends Common { export interface IPublicKeyValue extends ICommon {
verify(signature: ISignatureValue, m: Uint8Array | string): boolean;
verify(signature: ISignatureValue, m: Uint8Array | string): boolean; isValidOrder(): boolean;
isValidOrder(): boolean; deserializeUncompressed(s: Uint8Array): void;
deserializeUncompressed (s: Uint8Array): void; serializeUncompressed(): Uint8Array;
serializeUncompressed (): Uint8Array; deserializeUncompressedHexStr(s: string): void;
deserializeUncompressedHexStr (s:string): void; serializeUncompressedToHexStr(): string;
serializeUncompressedToHexStr(): string;
} }
export interface ISignatureValue extends Common { export interface ISignatureValue extends ICommon {
deserializeUncompressed(s: Uint8Array): void;
deserializeUncompressed (s: Uint8Array): void; serializeUncompressed(): Uint8Array;
serializeUncompressed (): Uint8Array; deserializeUncompressedHexStr(s: string): void;
deserializeUncompressedHexStr (s:string): void; serializeUncompressedToHexStr(): string;
serializeUncompressedToHexStr(): string; isValidOrder(): boolean;
isValidOrder(): boolean; aggregate(others: ISignatureValue[]): boolean;
aggregate(others: ISignatureValue[]): boolean; aggregateVerifyNoCheck(publicKeys: IPublicKeyValue[], messages: Uint8Array): boolean;
aggregateVerifyNoCheck(publicKeys: IPublicKeyValue[], messages: Uint8Array): boolean; fastAggregateVerify(publicKeys: IPublicKeyValue[], message: Uint8Array): boolean;
fastAggregateVerify(publicKeys: IPublicKeyValue[], message: Uint8Array): boolean;
} }
export interface IBls { export interface IBls {
//property names are like that for api compatibility //property names are like that for api compatibility
SecretKey: InstanceType<IPrivateKeyValue>; SecretKey: InstanceType<IPrivateKeyValue>;
PublicKey: InstanceType<IPublicKeyValue>; PublicKey: InstanceType<IPublicKeyValue>;
Signature: InstanceType<ISignatureValue>; Signature: InstanceType<ISignatureValue>;
toHex(a: Uint8Array, start: number, length: number): string; toHex(a: Uint8Array, start: number, length: number): string;
toHexStr(a: Uint8Array): string; toHexStr(a: Uint8Array): string;
fromHexStr(s: string): Uint8Array; fromHexStr(s: string): Uint8Array;
getCurveOrder(): string; getCurveOrder(): string;
getFieldOrder(): string; getFieldOrder(): string;
verifySignatureOrder(doVerify: boolean): void; verifySignatureOrder(doVerify: boolean): void;
verifyPublicKeyOrder(doVerify: boolean): void; verifyPublicKeyOrder(doVerify: boolean): void;
/**
*
* @param msgs single array with concatenated messages
* @param msgSize defaults to MSG_SIZE
*/
areAllMsgDifferent(msgs: Uint8Array, msgSize?: number): boolean;
shouldVerifyBlsSignatureOrder(b: string): void;
shouldVerifyBlsPublicKeyOrder(b: string): void;
deserializeHexStrToSecretKey(s: string): IPrivateKeyValue;
deserializeHexStrToPublicKey(s: string): IPublicKeyValue;
deserializeHexStrToSignature(s: string): ISignatureValue;
init(): Promise<this>; /**
destroy(): void; *
} * @param msgs single array with concatenated messages
* @param msgSize defaults to MSG_SIZE
*/
areAllMsgDifferent(msgs: Uint8Array, msgSize?: number): boolean;
shouldVerifyBlsSignatureOrder(b: string): void;
shouldVerifyBlsPublicKeyOrder(b: string): void;
deserializeHexStrToSecretKey(s: string): IPrivateKeyValue;
deserializeHexStrToPublicKey(s: string): IPublicKeyValue;
deserializeHexStrToSignature(s: string): ISignatureValue;
init(): Promise<this>;
destroy(): void;
}

View File

@ -1,9 +1,9 @@
import {SECRET_KEY_LENGTH} from "./constants"; import {SECRET_KEY_LENGTH} from "./constants";
import assert from "assert"; import assert from "assert";
import {generateRandomSecretKey} from "@chainsafe/bls-keygen"; import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
import { IPrivateKeyValue } from "./interface"; import {IPrivateKeyValue} from "./interface";
import { getContext } from "./context"; import {getContext} from "./context";
import { PublicKey, Signature } from "."; import {PublicKey, Signature} from ".";
export class PrivateKey { export class PrivateKey {
private value: IPrivateKeyValue; private value: IPrivateKeyValue;

View File

@ -3,8 +3,8 @@ import {PUBLIC_KEY_LENGTH} from "./constants";
import assert from "assert"; import assert from "assert";
import {Signature} from "./signature"; import {Signature} from "./signature";
import {EMPTY_PUBLIC_KEY} from "./helpers/utils"; import {EMPTY_PUBLIC_KEY} from "./helpers/utils";
import { IPublicKeyValue } from './interface'; import {IPublicKeyValue} from "./interface";
import { getContext } from "./context"; import {getContext} from "./context";
export class PublicKey { export class PublicKey {
private value: IPublicKeyValue; private value: IPublicKeyValue;
@ -31,7 +31,7 @@ export class PublicKey {
assert(value.length === PUBLIC_KEY_LENGTH * 2); assert(value.length === PUBLIC_KEY_LENGTH * 2);
const context = getContext(); const context = getContext();
const pubkeyValue = new context.PublicKey(); const pubkeyValue = new context.PublicKey();
pubkeyValue.deserializeHexStr(value) pubkeyValue.deserializeHexStr(value);
return new PublicKey(pubkeyValue); return new PublicKey(pubkeyValue);
} }

View File

@ -1,9 +1,9 @@
import assert from "assert"; import assert from "assert";
import { FP_POINT_LENGTH } from "./constants"; import {FP_POINT_LENGTH} from "./constants";
import { getContext } from "./context"; import {getContext} from "./context";
import { EMPTY_SIGNATURE } from "./helpers/utils"; import {EMPTY_SIGNATURE} from "./helpers/utils";
import { ISignatureValue } from './interface'; import {ISignatureValue} from "./interface";
import { PublicKey } from "./publicKey"; import {PublicKey} from "./publicKey";
export class Signature { export class Signature {
private value: ISignatureValue; private value: ISignatureValue;