Fix w new vector types

This commit is contained in:
Cayman 2020-02-04 16:30:10 -06:00
parent 825726b5cc
commit c5cecec09b
4 changed files with 29 additions and 25 deletions

View File

@ -23,7 +23,7 @@ export function generateKeyPair(): Keypair {
*/
export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
assert(secretKey, "secretKey is null or undefined");
const keypair = new Keypair(PrivateKey.fromBytes(secretKey));
const keypair = new Keypair(PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array)));
return keypair.publicKey.toBytesCompressed();
}
@ -37,8 +37,11 @@ export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Doma
assert(secretKey, "secretKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(domain, "domain is null or undefined");
const privateKey = PrivateKey.fromBytes(secretKey);
return privateKey.signMessage(messageHash, domain).toBytesCompressed();
const privateKey = PrivateKey.fromBytes(Buffer.from(secretKey as Uint8Array));
return privateKey.signMessage(
Buffer.from(messageHash as Uint8Array),
Buffer.from(domain as Uint8Array)
).toBytesCompressed();
}
/**
@ -48,7 +51,7 @@ export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Doma
export function aggregateSignatures(signatures: BLSSignature[]): BLSSignature {
assert(signatures, "signatures is null or undefined");
return signatures.map((signature): Signature => {
return Signature.fromCompressedBytes(signature);
return Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array));
}).reduce((previousValue, currentValue): Signature => {
return previousValue.add(currentValue);
}).toBytesCompressed();
@ -63,7 +66,7 @@ export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
if(publicKeys.length === 0) {
return Buffer.alloc(PUBLIC_KEY_LENGTH);
}
return publicKeys.map(PublicKey.fromBytes).reduce((agg, pubKey) => {
return publicKeys.map((p) => PublicKey.fromBytes(Buffer.from(p as Uint8Array))).reduce((agg, pubKey) => {
if(agg) {
return agg.add(pubKey);
} else {
@ -87,8 +90,12 @@ export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BL
assert(domain, "domain is null or undefined");
try {
return PublicKey
.fromBytes(publicKey)
.verifyMessage(Signature.fromCompressedBytes(signature), messageHash, domain);
.fromBytes(Buffer.from(publicKey as Uint8Array))
.verifyMessage(
Signature.fromCompressedBytes(Buffer.from(signature as Uint8Array)),
Buffer.from(messageHash as Uint8Array),
Buffer.from(domain as Uint8Array)
);
} catch (e) {
return false;
}
@ -117,11 +124,11 @@ export function verifyMultiple(
}
try {
return Signature
.fromCompressedBytes(signature)
.fromCompressedBytes(Buffer.from(signature as Uint8Array))
.verifyMultiple(
publicKeys.map((key) => PublicKey.fromBytes(key)),
messageHashes,
domain
publicKeys.map((key) => PublicKey.fromBytes(Buffer.from(key as Uint8Array))),
messageHashes.map((m) => Buffer.from(m as Uint8Array)),
Buffer.from(domain as Uint8Array),
);
} catch (e) {
return false;

View File

@ -1,6 +1,5 @@
import {SECRET_KEY_LENGTH} from "./constants";
import assert from "assert";
import {BLSSecretKey, Bytes32, Domain} from "@chainsafe/eth2.0-types";
import {SecretKeyType} from "@chainsafe/eth2-bls-wasm";
import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
import {getContext} from "./context";
@ -51,7 +50,7 @@ export class PrivateKey {
// return Signature.fromValue(this.value.sign(message));
// }
public signMessage(message: Bytes32, domain: Domain): Signature {
public signMessage(message: Uint8Array, domain: Uint8Array): Signature {
domain = padLeft(domain, 8);
return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain])));
}
@ -60,7 +59,7 @@ export class PrivateKey {
return PublicKey.fromPublicKeyType(this.value.getPublicKey());
}
public toBytes(): BLSSecretKey {
public toBytes(): Buffer {
return Buffer.from(this.value.serialize());
}

View File

@ -1,5 +1,4 @@
import {PrivateKey} from "./privateKey";
import {BLSPubkey, Bytes32, Domain} from "@chainsafe/eth2.0-types";
import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
import {getContext} from "./context";
import {PUBLIC_KEY_LENGTH} from "./constants";
@ -14,12 +13,12 @@ export class PublicKey {
protected constructor(value: PublicKeyType) {
this.value = value;
}
public static fromPrivateKey(privateKey: PrivateKey): PublicKey {
return privateKey.toPublicKey();
}
public static fromBytes(bytes: BLSPubkey): PublicKey {
public static fromBytes(bytes: Uint8Array): PublicKey {
const context = getContext();
const publicKey = new context.PublicKey();
if(!EMPTY_PUBLIC_KEY.equals(bytes)) {
@ -49,11 +48,11 @@ export class PublicKey {
return agg;
}
public verifyMessage(signature: Signature, messageHash: Bytes32, domain: Domain): boolean {
public verifyMessage(signature: Signature, messageHash: Uint8Array, domain: Uint8Array): boolean {
return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain]));
}
public toBytesCompressed(): BLSPubkey {
public toBytesCompressed(): Buffer {
return Buffer.from(this.value.serialize());
}

View File

@ -1,6 +1,5 @@
import assert from "assert";
import {FP_POINT_LENGTH} from "./constants";
import {BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types";
import {SignatureType} from "@chainsafe/eth2-bls-wasm";
import {getContext} from "./context";
import {PublicKey} from "./publicKey";
@ -14,7 +13,7 @@ export class Signature {
this.value = value;
}
public static fromCompressedBytes(value: BLSSignature): Signature {
public static fromCompressedBytes(value: Uint8Array): Signature {
assert(
value.length === 2 * FP_POINT_LENGTH,
`Signature must have ${2 * FP_POINT_LENGTH} bytes`
@ -43,20 +42,20 @@ export class Signature {
return this.value;
}
public verify(publicKey: PublicKey, message: Bytes32, domain: Domain): boolean {
public verify(publicKey: PublicKey, message: Uint8Array, domain: Uint8Array): boolean {
domain = padLeft(domain, 8);
return publicKey.verifyMessage(this, message, domain);
}
public verifyMultiple(publicKeys: PublicKey[], messages: Bytes32[], domain: Domain): boolean {
public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[], domain: Uint8Array): boolean {
domain = padLeft(domain, 8);
return this.value.verifyAggregatedHashWithDomain(
publicKeys.map((key) => key.getValue()),
messages.map((message) => Buffer.concat([message, domain]))
messages.map((message) => Buffer.concat([Buffer.from(message as Uint8Array), domain as Uint8Array]))
);
}
public toBytesCompressed(): BLSSignature {
public toBytesCompressed(): Buffer {
return Buffer.from(this.value.serialize());
}