Add validate key option to PublicKey.fromBytes()

This commit is contained in:
dapplion 2021-04-05 22:38:12 +02:00
parent 12d64eeb56
commit adf89c8360
3 changed files with 13 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import * as blst from "@chainsafe/blst"; import * as blst from "@chainsafe/blst";
import {EmptyAggregateError, ZeroPublicKeyError} from "../errors"; import {EmptyAggregateError} from "../errors";
import {bytesToHex, hexToBytes} from "../helpers"; import {bytesToHex, hexToBytes} from "../helpers";
import {PointFormat, PublicKey as IPublicKey} from "../interface"; import {PointFormat, PublicKey as IPublicKey} from "../interface";
@ -9,12 +9,9 @@ export class PublicKey extends blst.PublicKey implements IPublicKey {
} }
/** @param type Defaults to `CoordType.jacobian` */ /** @param type Defaults to `CoordType.jacobian` */
static fromBytes(bytes: Uint8Array, type?: blst.CoordType): PublicKey { static fromBytes(bytes: Uint8Array, type?: blst.CoordType, validate?: boolean): PublicKey {
const pk = blst.PublicKey.fromBytes(bytes, type); const pk = blst.PublicKey.fromBytes(bytes, type);
if (pk.value.is_inf()) { if (validate) pk.keyValidate();
throw new ZeroPublicKeyError();
}
return new PublicKey(pk.value); return new PublicKey(pk.value);
} }

View File

@ -29,7 +29,7 @@ export declare class SecretKey {
export declare class PublicKey { export declare class PublicKey {
/** @param type Only for impl `blst-native`. Defaults to `CoordType.jacobian` */ /** @param type Only for impl `blst-native`. Defaults to `CoordType.jacobian` */
static fromBytes(bytes: Uint8Array, type?: CoordType): PublicKey; static fromBytes(bytes: Uint8Array, type?: CoordType, validate?: boolean): PublicKey;
static fromHex(hex: string): PublicKey; static fromHex(hex: string): PublicKey;
static aggregate(publicKeys: PublicKey[]): PublicKey; static aggregate(publicKeys: PublicKey[]): PublicKey;
/** @param format Defaults to `PointFormat.compressed` */ /** @param format Defaults to `PointFormat.compressed` */

View File

@ -3,6 +3,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-tes
import {hexToBytes} from "../../src/helpers"; import {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 {CoordType} from "@chainsafe/blst";
interface IAggregateSigsVerifyTestCase { interface IAggregateSigsVerifyTestCase {
data: { data: {
@ -21,7 +22,14 @@ describeForAllImplementations((bls) => {
path.join(SPEC_TESTS_DIR, "tests/general/phase0/bls/fast_aggregate_verify/small"), path.join(SPEC_TESTS_DIR, "tests/general/phase0/bls/fast_aggregate_verify/small"),
(testCase) => { (testCase) => {
const {pubkeys, message, signature} = testCase.data.input; const {pubkeys, message, signature} = testCase.data.input;
return bls.verifyAggregate(pubkeys.map(hexToBytes), hexToBytes(message), hexToBytes(signature)); try {
return bls.Signature.fromBytes(hexToBytes(signature)).verifyAggregate(
pubkeys.map((hex) => bls.PublicKey.fromBytes(hexToBytes(hex), CoordType.jacobian, true)),
hexToBytes(message)
);
} catch (e) {
return false;
}
}, },
{ {
inputTypes: {data: InputType.YAML}, inputTypes: {data: InputType.YAML},