remove domain param from every method in bls

This commit is contained in:
Marin Petrunić 2020-02-04 17:17:11 +01:00 committed by Cayman
parent ee3ce5d951
commit c1a6c41dd4
No known key found for this signature in database
GPG Key ID: 54B21AEC3C53E1F5
4 changed files with 14 additions and 38 deletions

View File

@ -34,17 +34,12 @@ export function generatePublicKey(secretKey: Uint8Array): Buffer {
* Signs given message using secret key. * Signs given message using secret key.
* @param secretKey * @param secretKey
* @param messageHash * @param messageHash
* @param domain
*/ */
export function sign(secretKey: Uint8Array, messageHash: Uint8Array, domain: Uint8Array): Buffer { export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
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");
const privateKey = PrivateKey.fromBytes(toBuffer(secretKey)); const privateKey = PrivateKey.fromBytes(toBuffer(secretKey));
return privateKey.signMessage( return privateKey.signMessage(toBuffer(messageHash)).toBytesCompressed();
toBuffer(messageHash),
toBuffer(domain),
).toBytesCompressed();
} }
/** /**
@ -80,26 +75,15 @@ export function aggregatePubkeys(publicKeys: Uint8Array[]): Buffer {
* @param publicKey * @param publicKey
* @param messageHash * @param messageHash
* @param signature * @param signature
* @param domain
*/ */
export function verify( export function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature: Uint8Array): boolean {
publicKey: Uint8Array,
messageHash: Uint8Array,
signature: Uint8Array,
domain: Uint8Array
): 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");
assert(domain, "domain is null or undefined");
try { try {
return PublicKey return PublicKey
.fromBytes(toBuffer(publicKey)) .fromBytes(publicKey)
.verifyMessage( .verifyMessage(Signature.fromCompressedBytes(toBuffer(signature)), toBuffer(messageHash));
Signature.fromCompressedBytes(toBuffer(signature)),
toBuffer(messageHash),
toBuffer(domain)
);
} catch (e) { } catch (e) {
return false; return false;
} }
@ -110,18 +94,15 @@ export function verify(
* @param publicKeys * @param publicKeys
* @param messageHashes * @param messageHashes
* @param signature * @param signature
* @param domain
*/ */
export function verifyMultiple( export function verifyMultiple(
publicKeys: Uint8Array[], publicKeys: Uint8Array[],
messageHashes: Uint8Array[], messageHashes: Uint8Array[],
signature: Uint8Array, signature: Uint8Array,
domain: Uint8Array
): boolean { ): boolean {
assert(publicKeys, "publicKey is null or undefined"); assert(publicKeys, "publicKey is null or undefined");
assert(messageHashes, "messageHash is null or undefined"); assert(messageHashes, "messageHash is null or undefined");
assert(signature, "signature is null or undefined"); assert(signature, "signature is null or undefined");
assert(domain, "domain is null or undefined");
if(publicKeys.length === 0 || publicKeys.length != messageHashes.length) { if(publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false; return false;
@ -132,7 +113,6 @@ export function verifyMultiple(
.verifyMultiple( .verifyMultiple(
publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))), publicKeys.map((key) => PublicKey.fromBytes(toBuffer(key))),
messageHashes.map((m) => toBuffer(m)), messageHashes.map((m) => toBuffer(m)),
toBuffer(domain),
); );
} catch (e) { } catch (e) {
return false; return false;

View File

@ -5,7 +5,6 @@ import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
import {getContext} from "./context"; import {getContext} from "./context";
import {PublicKey} from "./publicKey"; import {PublicKey} from "./publicKey";
import {Signature} from "./signature"; import {Signature} from "./signature";
import {padLeft} from "./helpers/utils";
export class PrivateKey { export class PrivateKey {
@ -50,9 +49,8 @@ export class PrivateKey {
// return Signature.fromValue(this.value.sign(message)); // return Signature.fromValue(this.value.sign(message));
// } // }
public signMessage(message: Uint8Array, domain: Uint8Array): Signature { public signMessage(message: Uint8Array): Signature {
domain = padLeft(domain, 8); return Signature.fromValue(this.value.signHashWithDomain(message));
return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain])));
} }
public toPublicKey(): PublicKey { public toPublicKey(): PublicKey {

View File

@ -48,8 +48,8 @@ export class PublicKey {
return agg; return agg;
} }
public verifyMessage(signature: Signature, messageHash: Uint8Array, domain: Uint8Array): boolean { public verifyMessage(signature: Signature, messageHash: Uint8Array): boolean {
return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain])); return this.value.verifyHashWithDomain(signature.getValue(), messageHash);
} }
public toBytesCompressed(): Buffer { public toBytesCompressed(): Buffer {

View File

@ -3,7 +3,7 @@ import {FP_POINT_LENGTH} from "./constants";
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";
import {EMPTY_SIGNATURE, padLeft} from "./helpers/utils"; import {EMPTY_SIGNATURE} from "./helpers/utils";
export class Signature { export class Signature {
@ -42,16 +42,14 @@ export class Signature {
return this.value; return this.value;
} }
public verify(publicKey: PublicKey, message: Uint8Array, domain: Uint8Array): boolean { public verify(publicKey: PublicKey, message: Uint8Array): boolean {
domain = padLeft(domain, 8); return publicKey.verifyMessage(this, message);
return publicKey.verifyMessage(this, message, domain);
} }
public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[], domain: Uint8Array): boolean { public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[]): boolean {
domain = padLeft(domain, 8);
return this.value.verifyAggregatedHashWithDomain( return this.value.verifyAggregatedHashWithDomain(
publicKeys.map((key) => key.getValue()), publicKeys.map((key) => key.getValue()),
messages.map((message) => Buffer.concat([message, domain])) messages
); );
} }