Update bls
This commit is contained in:
parent
e950c172f4
commit
825726b5cc
|
@ -7,12 +7,12 @@ import {PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH} from "../constants";
|
||||||
* @param source
|
* @param source
|
||||||
* @param length
|
* @param length
|
||||||
*/
|
*/
|
||||||
export function padLeft(source: Buffer, length: number): Buffer {
|
export function padLeft(source: Uint8Array, length: number): Buffer {
|
||||||
assert(source.length <= length, "Given array must be smaller or equal to desired array size");
|
assert(source.length <= length, "Given array must be smaller or equal to desired array size");
|
||||||
const result = Buffer.alloc(length, 0);
|
const result = Buffer.alloc(length, 0);
|
||||||
source.copy(result, length - source.length);
|
result.set(source, length - source.length);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const EMPTY_PUBLIC_KEY = Buffer.alloc(PUBLIC_KEY_LENGTH);
|
export const EMPTY_PUBLIC_KEY = Buffer.alloc(PUBLIC_KEY_LENGTH);
|
||||||
export const EMPTY_SIGNATURE = Buffer.alloc(SIGNATURE_LENGTH);
|
export const EMPTY_SIGNATURE = Buffer.alloc(SIGNATURE_LENGTH);
|
||||||
|
|
|
@ -2,7 +2,7 @@ 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";
|
||||||
import {BLSPubkey, BLSSecretKey, BLSSignature, Domain, bytes32} from "@chainsafe/eth2.0-types";
|
import {BLSPubkey, BLSSecretKey, BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
||||||
import {PUBLIC_KEY_LENGTH} from "./constants";
|
import {PUBLIC_KEY_LENGTH} from "./constants";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
|
||||||
* @param messageHash
|
* @param messageHash
|
||||||
* @param domain
|
* @param domain
|
||||||
*/
|
*/
|
||||||
export function sign(secretKey: BLSSecretKey, messageHash: bytes32, domain: Domain): BLSSignature {
|
export function sign(secretKey: BLSSecretKey, messageHash: Bytes32, domain: Domain): BLSSignature {
|
||||||
assert(secretKey, "secretKey is null or undefined");
|
assert(secretKey, "secretKey is null or undefined");
|
||||||
assert(messageHash, "messageHash is null or undefined");
|
assert(messageHash, "messageHash is null or undefined");
|
||||||
assert(domain, "domain is null or undefined");
|
assert(domain, "domain is null or undefined");
|
||||||
|
@ -80,7 +80,7 @@ export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
|
||||||
* @param signature
|
* @param signature
|
||||||
* @param domain
|
* @param domain
|
||||||
*/
|
*/
|
||||||
export function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BLSSignature, domain: Domain): boolean {
|
export function verify(publicKey: BLSPubkey, messageHash: Bytes32, signature: BLSSignature, domain: Domain): boolean {
|
||||||
assert(publicKey, "publicKey is null or undefined");
|
assert(publicKey, "publicKey is null or undefined");
|
||||||
assert(messageHash, "messageHash is null or undefined");
|
assert(messageHash, "messageHash is null or undefined");
|
||||||
assert(signature, "signature is null or undefined");
|
assert(signature, "signature is null or undefined");
|
||||||
|
@ -103,7 +103,7 @@ export function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BL
|
||||||
*/
|
*/
|
||||||
export function verifyMultiple(
|
export function verifyMultiple(
|
||||||
publicKeys: BLSPubkey[],
|
publicKeys: BLSPubkey[],
|
||||||
messageHashes: bytes32[],
|
messageHashes: Bytes32[],
|
||||||
signature: BLSSignature,
|
signature: BLSSignature,
|
||||||
domain: Domain
|
domain: Domain
|
||||||
): boolean {
|
): boolean {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import {SECRET_KEY_LENGTH} from "./constants";
|
import {SECRET_KEY_LENGTH} from "./constants";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import {BLSSecretKey, Domain, bytes32} from "@chainsafe/eth2.0-types";
|
import {BLSSecretKey, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
||||||
import {SecretKeyType} from "@chainsafe/eth2-bls-wasm";
|
import {SecretKeyType} from "@chainsafe/eth2-bls-wasm";
|
||||||
import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
|
import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
|
@ -51,7 +51,7 @@ export class PrivateKey {
|
||||||
// return Signature.fromValue(this.value.sign(message));
|
// return Signature.fromValue(this.value.sign(message));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public signMessage(message: bytes32, domain: Domain): Signature {
|
public signMessage(message: Bytes32, domain: Domain): Signature {
|
||||||
domain = padLeft(domain, 8);
|
domain = padLeft(domain, 8);
|
||||||
return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain])));
|
return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain])));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import {PrivateKey} from "./privateKey";
|
import {PrivateKey} from "./privateKey";
|
||||||
import {BLSPubkey, Domain, bytes32} from "@chainsafe/eth2.0-types";
|
import {BLSPubkey, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
||||||
import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
|
import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
import {PUBLIC_KEY_LENGTH} from "./constants";
|
import {PUBLIC_KEY_LENGTH} from "./constants";
|
||||||
|
@ -22,7 +22,7 @@ export class PublicKey {
|
||||||
public static fromBytes(bytes: BLSPubkey): PublicKey {
|
public static fromBytes(bytes: BLSPubkey): PublicKey {
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
const publicKey = new context.PublicKey();
|
const publicKey = new context.PublicKey();
|
||||||
if(!bytes.equals(EMPTY_PUBLIC_KEY)) {
|
if(!EMPTY_PUBLIC_KEY.equals(bytes)) {
|
||||||
publicKey.deserialize(bytes);
|
publicKey.deserialize(bytes);
|
||||||
}
|
}
|
||||||
return new PublicKey(
|
return new PublicKey(
|
||||||
|
@ -49,7 +49,7 @@ export class PublicKey {
|
||||||
return agg;
|
return agg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public verifyMessage(signature: Signature, messageHash: bytes32, domain: Domain): boolean {
|
public verifyMessage(signature: Signature, messageHash: Bytes32, domain: Domain): boolean {
|
||||||
return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain]));
|
return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ export class PublicKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
public toHexString(): string {
|
public toHexString(): string {
|
||||||
return `0x${this.toBytesCompressed().toString("hex")}`;
|
return `0x${Buffer.from(this.toBytesCompressed()).toString("hex")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getValue(): PublicKeyType {
|
public getValue(): PublicKeyType {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import {FP_POINT_LENGTH} from "./constants";
|
import {FP_POINT_LENGTH} from "./constants";
|
||||||
import {BLSSignature, Domain, bytes32} from "@chainsafe/eth2.0-types";
|
import {BLSSignature, Bytes32, Domain} from "@chainsafe/eth2.0-types";
|
||||||
import {SignatureType} from "@chainsafe/eth2-bls-wasm";
|
import {SignatureType} from "@chainsafe/eth2-bls-wasm";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
|
@ -21,7 +21,7 @@ export class Signature {
|
||||||
);
|
);
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
const signature = new context.Signature();
|
const signature = new context.Signature();
|
||||||
if(!value.equals(EMPTY_SIGNATURE)) {
|
if(!EMPTY_SIGNATURE.equals(value)) {
|
||||||
signature.deserialize(value);
|
signature.deserialize(value);
|
||||||
}
|
}
|
||||||
return new Signature(signature);
|
return new Signature(signature);
|
||||||
|
@ -43,12 +43,12 @@ export class Signature {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public verify(publicKey: PublicKey, message: bytes32, domain: Domain): boolean {
|
public verify(publicKey: PublicKey, message: Bytes32, domain: Domain): boolean {
|
||||||
domain = padLeft(domain, 8);
|
domain = padLeft(domain, 8);
|
||||||
return publicKey.verifyMessage(this, message, domain);
|
return publicKey.verifyMessage(this, message, domain);
|
||||||
}
|
}
|
||||||
|
|
||||||
public verifyMultiple(publicKeys: PublicKey[], messages: bytes32[], domain: Domain): boolean {
|
public verifyMultiple(publicKeys: PublicKey[], messages: Bytes32[], domain: Domain): boolean {
|
||||||
domain = padLeft(domain, 8);
|
domain = padLeft(domain, 8);
|
||||||
return this.value.verifyAggregatedHashWithDomain(
|
return this.value.verifyAggregatedHashWithDomain(
|
||||||
publicKeys.map((key) => key.getValue()),
|
publicKeys.map((key) => key.getValue()),
|
||||||
|
|
Reference in New Issue