Fix blst tests

This commit is contained in:
dapplion 2020-11-30 00:01:48 +00:00
parent 32266dae20
commit 60d795c694
3 changed files with 18 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import * as blst from "@chainsafe/blst"; import * as blst from "@chainsafe/blst";
import {ZeroPublicKeyError} from "../errors";
import {bytesToHex, hexToBytes} from "../helpers"; import {bytesToHex, hexToBytes} from "../helpers";
import {IPublicKey} from "../interface"; import {IPublicKey} from "../interface";
@ -13,6 +14,10 @@ export class PublicKey implements IPublicKey {
static fromBytes(bytes: Uint8Array): PublicKey { static fromBytes(bytes: Uint8Array): PublicKey {
const affine = blst.PublicKey.fromBytes(bytes); const affine = blst.PublicKey.fromBytes(bytes);
if (affine.value.is_inf()) {
throw new ZeroPublicKeyError();
}
const jacobian = blst.AggregatePublicKey.fromPublicKey(affine); const jacobian = blst.AggregatePublicKey.fromPublicKey(affine);
return new PublicKey(affine, jacobian); return new PublicKey(affine, jacobian);
} }

View File

@ -2,6 +2,7 @@ import * as blst from "@chainsafe/blst";
import {bytesToHex, hexToBytes} from "../helpers"; import {bytesToHex, hexToBytes} from "../helpers";
import {ISignature} from "../interface"; import {ISignature} from "../interface";
import {PublicKey} from "./publicKey"; import {PublicKey} from "./publicKey";
import {ZeroSignatureError} from "../errors";
export class Signature implements ISignature { export class Signature implements ISignature {
readonly affine: blst.Signature; readonly affine: blst.Signature;
@ -11,7 +12,12 @@ export class Signature implements ISignature {
} }
static fromBytes(bytes: Uint8Array): Signature { static fromBytes(bytes: Uint8Array): Signature {
return new Signature(blst.Signature.fromBytes(bytes)); const affine = blst.Signature.fromBytes(bytes);
if (affine.value.is_inf()) {
throw new ZeroSignatureError();
}
return new Signature(affine);
} }
static fromHex(hex: string): Signature { static fromHex(hex: string): Signature {

View File

@ -15,3 +15,9 @@ export class ZeroPublicKeyError extends Error {
super("PUBLIC_KEY_IS_ZERO"); super("PUBLIC_KEY_IS_ZERO");
} }
} }
export class ZeroSignatureError extends Error {
constructor() {
super("SIGNATURE_IS_ZERO");
}
}