update bls methods and wasm usage
This commit is contained in:
parent
c1a6c41dd4
commit
dcf6551372
33
src/index.ts
33
src/index.ts
|
@ -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
|
||||||
};
|
};
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue