2020-11-05 20:55:25 +00:00
|
|
|
import bls, {aggregatePubkeys, aggregateSignatures, initBLS, Keypair, verify, verifyMultiple} from "../../src";
|
2020-02-20 20:26:26 +00:00
|
|
|
import SHA256 from "@chainsafe/as-sha256";
|
2020-11-05 20:55:25 +00:00
|
|
|
import {expect} from "chai";
|
|
|
|
import {destroy} from "../../src/context";
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
describe("test bls", function () {
|
2020-11-04 17:40:36 +00:00
|
|
|
before(async function () {
|
2019-12-02 12:44:45 +00:00
|
|
|
await initBLS();
|
2019-11-27 20:58:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
destroy();
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
describe("aggregate pubkey", function () {
|
|
|
|
it("should aggregate empty array", function () {
|
2019-08-05 15:48:26 +00:00
|
|
|
expect(bls.aggregatePubkeys([])).to.not.throw;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-04 17:40:36 +00:00
|
|
|
describe("verify", function () {
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should verify signature", () => {
|
2019-08-05 15:48:26 +00:00
|
|
|
const keypair = Keypair.generate();
|
2020-02-20 20:26:26 +00:00
|
|
|
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test")));
|
2020-11-04 17:40:36 +00:00
|
|
|
const signature = keypair.privateKey.signMessage(messageHash);
|
2020-11-19 00:23:34 +00:00
|
|
|
const result = verify(keypair.publicKey.toBytes(), messageHash, signature.toBytes());
|
2019-08-05 15:48:26 +00:00
|
|
|
expect(result).to.be.true;
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should not modify original pubkey when verifying", () => {
|
2019-08-05 15:48:26 +00:00
|
|
|
const keypair = Keypair.generate();
|
2020-02-20 20:26:26 +00:00
|
|
|
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test")));
|
2020-11-04 17:40:36 +00:00
|
|
|
const signature = keypair.privateKey.signMessage(messageHash);
|
2020-11-19 00:23:34 +00:00
|
|
|
const pubKey = keypair.publicKey.toBytes();
|
|
|
|
verify(pubKey, messageHash, signature.toBytes());
|
|
|
|
expect("0x" + pubKey.toString("hex")).to.be.equal(keypair.publicKey.toHex());
|
2019-08-05 15:48:26 +00:00
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should fail verify empty signature", () => {
|
2019-08-05 15:48:26 +00:00
|
|
|
const keypair = Keypair.generate();
|
2020-11-05 20:55:25 +00:00
|
|
|
const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
|
2019-08-05 15:48:26 +00:00
|
|
|
const signature = Buffer.alloc(96);
|
2020-11-19 00:23:34 +00:00
|
|
|
const result = verify(keypair.publicKey.toBytes(), messageHash2, signature);
|
2019-08-05 15:48:26 +00:00
|
|
|
expect(result).to.be.false;
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should fail verify signature of different message", () => {
|
2019-08-05 15:48:26 +00:00
|
|
|
const keypair = Keypair.generate();
|
2020-11-05 20:55:25 +00:00
|
|
|
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
|
|
|
|
const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
|
2020-11-04 17:40:36 +00:00
|
|
|
const signature = keypair.privateKey.signMessage(messageHash);
|
2020-11-19 00:23:34 +00:00
|
|
|
const result = verify(keypair.publicKey.toBytes(), messageHash2, signature.toBytes());
|
2019-08-05 15:48:26 +00:00
|
|
|
expect(result).to.be.false;
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should fail verify signature signed by different key", () => {
|
2019-08-05 15:48:26 +00:00
|
|
|
const keypair = Keypair.generate();
|
|
|
|
const keypair2 = Keypair.generate();
|
2020-11-05 20:55:25 +00:00
|
|
|
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
|
2020-11-04 17:40:36 +00:00
|
|
|
const signature = keypair.privateKey.signMessage(messageHash);
|
2020-11-19 00:23:34 +00:00
|
|
|
const result = verify(keypair2.publicKey.toBytes(), messageHash, signature.toBytes());
|
2019-08-05 15:48:26 +00:00
|
|
|
expect(result).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-04 17:40:36 +00:00
|
|
|
describe("verify multiple", function () {
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should verify aggregated signatures", function () {
|
2019-08-28 13:57:26 +00:00
|
|
|
this.timeout(5000);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
|
|
|
const keypair1 = Keypair.generate();
|
|
|
|
const keypair2 = Keypair.generate();
|
|
|
|
const keypair3 = Keypair.generate();
|
|
|
|
const keypair4 = Keypair.generate();
|
|
|
|
|
2020-02-20 20:26:26 +00:00
|
|
|
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
|
|
|
|
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-02-24 22:46:13 +00:00
|
|
|
const signature1 = keypair1.privateKey.signMessage(message1);
|
|
|
|
const signature2 = keypair2.privateKey.signMessage(message1);
|
|
|
|
const signature3 = keypair3.privateKey.signMessage(message2);
|
|
|
|
const signature4 = keypair4.privateKey.signMessage(message2);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-11-19 00:23:34 +00:00
|
|
|
const aggregatePubKey12 = aggregatePubkeys([keypair1.publicKey.toBytes(), keypair2.publicKey.toBytes()]);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-11-19 00:23:34 +00:00
|
|
|
const aggregatePubKey34 = aggregatePubkeys([keypair3.publicKey.toBytes(), keypair4.publicKey.toBytes()]);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2019-11-27 20:58:41 +00:00
|
|
|
const aggregateSignature = aggregateSignatures([
|
2020-11-19 00:23:34 +00:00
|
|
|
signature1.toBytes(),
|
|
|
|
signature2.toBytes(),
|
|
|
|
signature3.toBytes(),
|
|
|
|
signature4.toBytes(),
|
2019-08-05 15:48:26 +00:00
|
|
|
]);
|
|
|
|
|
2020-11-05 20:55:25 +00:00
|
|
|
const result = verifyMultiple([aggregatePubKey12, aggregatePubKey34], [message1, message2], aggregateSignature);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
|
|
|
expect(result).to.be.true;
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should verify aggregated signatures - same message", function () {
|
2019-08-28 13:57:26 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
|
|
|
|
const keypair1 = Keypair.generate();
|
|
|
|
const keypair2 = Keypair.generate();
|
|
|
|
const keypair3 = Keypair.generate();
|
|
|
|
const keypair4 = Keypair.generate();
|
|
|
|
|
2020-02-20 20:26:26 +00:00
|
|
|
const message = Buffer.from(SHA256.digest(Buffer.from("Test1")));
|
2019-08-28 13:57:26 +00:00
|
|
|
|
2020-02-24 22:46:13 +00:00
|
|
|
const signature1 = keypair1.privateKey.signMessage(message);
|
|
|
|
const signature2 = keypair2.privateKey.signMessage(message);
|
|
|
|
const signature3 = keypair3.privateKey.signMessage(message);
|
|
|
|
const signature4 = keypair4.privateKey.signMessage(message);
|
2019-08-28 13:57:26 +00:00
|
|
|
|
2019-11-27 20:58:41 +00:00
|
|
|
const aggregateSignature = aggregateSignatures([
|
2020-11-19 00:23:34 +00:00
|
|
|
signature1.toBytes(),
|
|
|
|
signature2.toBytes(),
|
|
|
|
signature3.toBytes(),
|
|
|
|
signature4.toBytes(),
|
2019-08-28 13:57:26 +00:00
|
|
|
]);
|
|
|
|
|
2019-11-27 20:58:41 +00:00
|
|
|
const result = verifyMultiple(
|
2019-08-28 13:57:26 +00:00
|
|
|
[
|
2020-11-19 00:23:34 +00:00
|
|
|
keypair1.publicKey.toBytes(),
|
|
|
|
keypair2.publicKey.toBytes(),
|
|
|
|
keypair3.publicKey.toBytes(),
|
|
|
|
keypair4.publicKey.toBytes(),
|
2019-08-28 13:57:26 +00:00
|
|
|
],
|
|
|
|
[message, message, message, message],
|
2020-11-19 00:23:34 +00:00
|
|
|
aggregateSignature
|
2019-08-28 13:57:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(result).to.be.true;
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should fail to verify aggregated signatures - swapped messages", function () {
|
2019-08-28 13:57:26 +00:00
|
|
|
this.timeout(5000);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
|
|
|
const keypair1 = Keypair.generate();
|
|
|
|
const keypair2 = Keypair.generate();
|
|
|
|
const keypair3 = Keypair.generate();
|
|
|
|
const keypair4 = Keypair.generate();
|
|
|
|
|
2020-02-20 20:26:26 +00:00
|
|
|
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
|
|
|
|
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-02-24 22:46:13 +00:00
|
|
|
const signature1 = keypair1.privateKey.signMessage(message1);
|
|
|
|
const signature2 = keypair2.privateKey.signMessage(message1);
|
|
|
|
const signature3 = keypair3.privateKey.signMessage(message2);
|
|
|
|
const signature4 = keypair4.privateKey.signMessage(message2);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-11-19 00:23:34 +00:00
|
|
|
const aggregatePubKey12 = bls.aggregatePubkeys([keypair1.publicKey.toBytes(), keypair2.publicKey.toBytes()]);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-11-19 00:23:34 +00:00
|
|
|
const aggregatePubKey34 = bls.aggregatePubkeys([keypair3.publicKey.toBytes(), keypair4.publicKey.toBytes()]);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
|
|
|
const aggregateSignature = bls.aggregateSignatures([
|
2020-11-19 00:23:34 +00:00
|
|
|
signature1.toBytes(),
|
|
|
|
signature2.toBytes(),
|
|
|
|
signature3.toBytes(),
|
|
|
|
signature4.toBytes(),
|
2019-08-05 15:48:26 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
const result = bls.verifyMultiple(
|
|
|
|
[aggregatePubKey12, aggregatePubKey34],
|
|
|
|
[message2, message1],
|
2020-11-04 17:40:36 +00:00
|
|
|
aggregateSignature
|
2019-08-05 15:48:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(result).to.be.false;
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should fail to verify aggregated signatures - different pubkeys and messsages", () => {
|
2019-08-05 15:48:26 +00:00
|
|
|
const keypair1 = Keypair.generate();
|
|
|
|
const keypair2 = Keypair.generate();
|
|
|
|
const keypair3 = Keypair.generate();
|
|
|
|
const keypair4 = Keypair.generate();
|
|
|
|
|
2020-02-20 20:26:26 +00:00
|
|
|
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
|
|
|
|
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-02-24 22:46:13 +00:00
|
|
|
const signature1 = keypair1.privateKey.signMessage(message1);
|
|
|
|
const signature2 = keypair2.privateKey.signMessage(message1);
|
|
|
|
const signature3 = keypair3.privateKey.signMessage(message2);
|
|
|
|
const signature4 = keypair4.privateKey.signMessage(message2);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-11-19 00:23:34 +00:00
|
|
|
const aggregatePubKey12 = bls.aggregatePubkeys([keypair1.publicKey.toBytes(), keypair2.publicKey.toBytes()]);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
|
|
|
const aggregateSignature = bls.aggregateSignatures([
|
2020-11-19 00:23:34 +00:00
|
|
|
signature1.toBytes(),
|
|
|
|
signature2.toBytes(),
|
|
|
|
signature3.toBytes(),
|
|
|
|
signature4.toBytes(),
|
2019-08-05 15:48:26 +00:00
|
|
|
]);
|
|
|
|
|
2020-11-05 20:55:25 +00:00
|
|
|
const result = bls.verifyMultiple([aggregatePubKey12], [message2, message1], aggregateSignature);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
|
|
|
expect(result).to.be.false;
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:03:24 +00:00
|
|
|
it("should fail to verify aggregated signatures - no public keys", () => {
|
2019-08-05 15:48:26 +00:00
|
|
|
const signature = Buffer.alloc(96);
|
|
|
|
|
2020-02-20 20:26:26 +00:00
|
|
|
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
|
|
|
|
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
|
2019-08-05 15:48:26 +00:00
|
|
|
|
2020-11-04 17:40:36 +00:00
|
|
|
const result = bls.verifyMultiple([], [message2, message1], signature);
|
2019-08-05 15:48:26 +00:00
|
|
|
|
|
|
|
expect(result).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|