Merge pull request #90 from ChainSafe/dapplion/keyValidate

Add validate key option to PublicKey.fromBytes()
This commit is contained in:
Cayman 2021-04-05 15:45:47 -05:00 committed by GitHub
commit 30eea7095e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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},