update bls methods and wasm usage

This commit is contained in:
Marin Petrunić 2020-02-07 10:13:12 +01:00 committed by Cayman
parent c1a6c41dd4
commit dcf6551372
No known key found for this signature in database
GPG Key ID: 54B21AEC3C53E1F5
4 changed files with 52 additions and 12 deletions

View File

@ -48,11 +48,11 @@ export function sign(secretKey: Uint8Array, messageHash: Uint8Array): Buffer {
*/ */
export function aggregateSignatures(signatures: Uint8Array[]): Buffer { export function aggregateSignatures(signatures: Uint8Array[]): Buffer {
assert(signatures, "signatures is null or undefined"); assert(signatures, "signatures is null or undefined");
return signatures.map((signature): Signature => { return Signature.aggregate(
return Signature.fromCompressedBytes(toBuffer(signature)); signatures.map((signature): Signature => {
}).reduce((previousValue, currentValue): Signature => { return Signature.fromCompressedBytes(signature);
return previousValue.add(currentValue); })
}).toBytesCompressed(); ).toBytesCompressed();
} }
/** /**
@ -89,16 +89,37 @@ export function verify(publicKey: Uint8Array, messageHash: Uint8Array, signature
} }
} }
/**
* Verifies if aggregated signature is same message signed with given public keys.
* @param publicKeys
* @param messageHash
* @param signature
*/
export function verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Array, signature: Uint8Array): boolean {
assert(publicKeys, "publicKey is null or undefined");
assert(messageHash, "messageHash is null or undefined");
assert(signature, "signature is null or undefined");
try {
return Signature
.fromCompressedBytes(signature)
.verifyAggregate(publicKeys, messageHash);
} catch (e) {
return false;
}
}
/** /**
* Verifies if signature is list of message signed with corresponding public key. * Verifies if signature is list of message signed with corresponding public key.
* @param publicKeys * @param publicKeys
* @param messageHashes * @param messageHashes
* @param signature * @param signature
* @param fast Check if all messages are different
*/ */
export function verifyMultiple( export function verifyMultiple(
publicKeys: Uint8Array[], publicKeys: Uint8Array[],
messageHashes: Uint8Array[], messageHashes: Uint8Array[],
signature: Uint8Array, signature: Uint8Array,
fast = false
): 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");
@ -113,6 +134,7 @@ 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)),
fast
); );
} catch (e) { } catch (e) {
return false; return false;
@ -126,5 +148,6 @@ export default {
aggregateSignatures, aggregateSignatures,
aggregatePubkeys, aggregatePubkeys,
verify, verify,
verifyAggregate,
verifyMultiple verifyMultiple
}; };

View File

@ -50,7 +50,7 @@ export class PrivateKey {
// } // }
public signMessage(message: Uint8Array): Signature { public signMessage(message: Uint8Array): Signature {
return Signature.fromValue(this.value.signHashWithDomain(message)); return Signature.fromValue(this.value.sign(message));
} }
public toPublicKey(): PublicKey { public toPublicKey(): PublicKey {

View File

@ -49,7 +49,7 @@ export class PublicKey {
} }
public verifyMessage(signature: Signature, messageHash: Uint8Array): boolean { public verifyMessage(signature: Signature, messageHash: Uint8Array): boolean {
return this.value.verifyHashWithDomain(signature.getValue(), messageHash); return this.value.verify(signature.getValue(), messageHash);
} }
public toBytesCompressed(): Buffer { public toBytesCompressed(): Buffer {

View File

@ -11,6 +11,7 @@ export class Signature {
protected constructor(value: SignatureType) { protected constructor(value: SignatureType) {
this.value = value; this.value = value;
assert(this.value.isValidOrder());
} }
public static fromCompressedBytes(value: Uint8Array): Signature { public static fromCompressedBytes(value: Uint8Array): Signature {
@ -30,6 +31,15 @@ export class Signature {
return new Signature(signature); return new Signature(signature);
} }
public static aggregate(signatures: Signature[]): Signature {
const context = getContext();
const signature = new context.Signature();
signature.aggregate(signatures.map((sig) => sig.getValue()));
return new Signature(
signature
);
}
public add(other: Signature): Signature { public add(other: Signature): Signature {
const agg = this.value.clone(); const agg = this.value.clone();
agg.add(other.value); agg.add(other.value);
@ -42,14 +52,21 @@ export class Signature {
return this.value; return this.value;
} }
public verify(publicKey: PublicKey, message: Uint8Array): boolean { public verifyAggregate(publicKey: Uint8Array[], message: Uint8Array): boolean {
return publicKey.verifyMessage(this, message); return this.value.fastAggregateVerify(
publicKey.map((bytes) => PublicKey.fromBytes(bytes).getValue()),
message
);
} }
public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[]): boolean { public verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[], fast = false): boolean {
return this.value.verifyAggregatedHashWithDomain( const msgs = Buffer.concat(messages);
if(!fast && !getContext().areAllMsgDifferent(msgs)) {
return false;
}
return this.value.aggregateVerifyNoCheck(
publicKeys.map((key) => key.getValue()), publicKeys.map((key) => key.getValue()),
messages msgs
); );
} }