Update unit tests

This commit is contained in:
Cayman 2020-02-24 16:46:13 -06:00
parent 5f19822a85
commit 0a068484b3
No known key found for this signature in database
GPG Key ID: 54B21AEC3C53E1F5
2 changed files with 21 additions and 104 deletions

View File

@ -24,16 +24,13 @@ describe("test bls", function () {
it("should verify signature", () => { it("should verify signature", () => {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test"))); const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test")));
const domain = Buffer.alloc(8, 1);
const signature = keypair.privateKey.signMessage( const signature = keypair.privateKey.signMessage(
messageHash, messageHash,
domain
); );
const result = verify( const result = verify(
keypair.publicKey.toBytesCompressed(), keypair.publicKey.toBytesCompressed(),
messageHash, messageHash,
signature.toBytesCompressed(), signature.toBytesCompressed(),
domain
); );
expect(result).to.be.true; expect(result).to.be.true;
}); });
@ -42,17 +39,14 @@ describe("test bls", function () {
it("should not modify original pubkey when verifying", () => { it("should not modify original pubkey when verifying", () => {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test"))); const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test")));
const domain = Buffer.alloc(8, 1);
const signature = keypair.privateKey.signMessage( const signature = keypair.privateKey.signMessage(
messageHash, messageHash,
domain
); );
const pubKey = keypair.publicKey.toBytesCompressed(); const pubKey = keypair.publicKey.toBytesCompressed();
verify( verify(
pubKey, pubKey,
messageHash, messageHash,
signature.toBytesCompressed(), signature.toBytesCompressed(),
domain
); );
expect("0x" + pubKey.toString("hex")).to.be.equal(keypair.publicKey.toHexString()); expect("0x" + pubKey.toString("hex")).to.be.equal(keypair.publicKey.toHexString());
}); });
@ -61,13 +55,11 @@ describe("test bls", function () {
it("should fail verify empty signature", () => { it("should fail verify empty signature", () => {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2"))); const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
const domain = Buffer.from("01", "hex");
const signature = Buffer.alloc(96); const signature = Buffer.alloc(96);
const result = verify( const result = verify(
keypair.publicKey.toBytesCompressed(), keypair.publicKey.toBytesCompressed(),
messageHash2, messageHash2,
signature, signature,
domain
); );
expect(result).to.be.false; expect(result).to.be.false;
}); });
@ -76,34 +68,13 @@ describe("test bls", function () {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message"))); const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2"))); const messageHash2 = Buffer.from(SHA256.digest(Buffer.from("Test message2")));
const domain = padLeft(Buffer.from("01", "hex"), 8);
const signature = keypair.privateKey.signMessage( const signature = keypair.privateKey.signMessage(
messageHash, messageHash,
domain
); );
const result = verify( const result = verify(
keypair.publicKey.toBytesCompressed(), keypair.publicKey.toBytesCompressed(),
messageHash2, messageHash2,
signature.toBytesCompressed(), signature.toBytesCompressed(),
domain
);
expect(result).to.be.false;
});
it("should fail verify signature of different domain", () => {
const keypair = Keypair.generate();
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
const domain = padLeft(Buffer.from("01", "hex"), 8);
const domain2 = padLeft(Buffer.from("02", "hex"), 8);
const signature = keypair.privateKey.signMessage(
messageHash,
domain
);
const result = verify(
keypair.publicKey.toBytesCompressed(),
messageHash,
signature.toBytesCompressed(),
domain2
); );
expect(result).to.be.false; expect(result).to.be.false;
}); });
@ -112,16 +83,13 @@ describe("test bls", function () {
const keypair = Keypair.generate(); const keypair = Keypair.generate();
const keypair2 = Keypair.generate(); const keypair2 = Keypair.generate();
const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message"))); const messageHash = Buffer.from(SHA256.digest(Buffer.from("Test message")));
const domain = Buffer.from("01", "hex");
const signature = keypair.privateKey.signMessage( const signature = keypair.privateKey.signMessage(
messageHash, messageHash,
domain
); );
const result = verify( const result = verify(
keypair2.publicKey.toBytesCompressed(), keypair2.publicKey.toBytesCompressed(),
messageHash, messageHash,
signature.toBytesCompressed(), signature.toBytesCompressed(),
domain
); );
expect(result).to.be.false; expect(result).to.be.false;
}); });
@ -133,7 +101,6 @@ describe("test bls", function () {
this.timeout(5000); this.timeout(5000);
const domain = Buffer.alloc(8, 0);
const keypair1 = Keypair.generate(); const keypair1 = Keypair.generate();
const keypair2 = Keypair.generate(); const keypair2 = Keypair.generate();
@ -143,10 +110,10 @@ describe("test bls", function () {
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1"))); const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2"))); const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
const signature1 = keypair1.privateKey.signMessage(message1, domain); const signature1 = keypair1.privateKey.signMessage(message1);
const signature2 = keypair2.privateKey.signMessage(message1, domain); const signature2 = keypair2.privateKey.signMessage(message1);
const signature3 = keypair3.privateKey.signMessage(message2, domain); const signature3 = keypair3.privateKey.signMessage(message2);
const signature4 = keypair4.privateKey.signMessage(message2, domain); const signature4 = keypair4.privateKey.signMessage(message2);
const aggregatePubKey12 = aggregatePubkeys([ const aggregatePubKey12 = aggregatePubkeys([
keypair1.publicKey.toBytesCompressed(), keypair1.publicKey.toBytesCompressed(),
@ -169,7 +136,6 @@ describe("test bls", function () {
[aggregatePubKey12, aggregatePubKey34], [aggregatePubKey12, aggregatePubKey34],
[message1, message2], [message1, message2],
aggregateSignature, aggregateSignature,
domain
); );
expect(result).to.be.true; expect(result).to.be.true;
@ -179,7 +145,6 @@ describe("test bls", function () {
this.timeout(5000); this.timeout(5000);
const domain = Buffer.alloc(8, 0);
const keypair1 = Keypair.generate(); const keypair1 = Keypair.generate();
const keypair2 = Keypair.generate(); const keypair2 = Keypair.generate();
@ -188,10 +153,10 @@ describe("test bls", function () {
const message = Buffer.from(SHA256.digest(Buffer.from("Test1"))); const message = Buffer.from(SHA256.digest(Buffer.from("Test1")));
const signature1 = keypair1.privateKey.signMessage(message, domain); const signature1 = keypair1.privateKey.signMessage(message);
const signature2 = keypair2.privateKey.signMessage(message, domain); const signature2 = keypair2.privateKey.signMessage(message);
const signature3 = keypair3.privateKey.signMessage(message, domain); const signature3 = keypair3.privateKey.signMessage(message);
const signature4 = keypair4.privateKey.signMessage(message, domain); const signature4 = keypair4.privateKey.signMessage(message);
const aggregateSignature = aggregateSignatures([ const aggregateSignature = aggregateSignatures([
signature1.toBytesCompressed(), signature1.toBytesCompressed(),
@ -209,7 +174,7 @@ describe("test bls", function () {
], ],
[message, message, message, message], [message, message, message, message],
aggregateSignature, aggregateSignature,
domain true,
); );
expect(result).to.be.true; expect(result).to.be.true;
@ -218,7 +183,6 @@ describe("test bls", function () {
it("should fail to verify aggregated signatures - swapped messages", function () { it("should fail to verify aggregated signatures - swapped messages", function () {
this.timeout(5000); this.timeout(5000);
const domain = Buffer.alloc(8, 0);
const keypair1 = Keypair.generate(); const keypair1 = Keypair.generate();
const keypair2 = Keypair.generate(); const keypair2 = Keypair.generate();
@ -228,10 +192,10 @@ describe("test bls", function () {
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1"))); const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2"))); const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
const signature1 = keypair1.privateKey.signMessage(message1, domain); const signature1 = keypair1.privateKey.signMessage(message1);
const signature2 = keypair2.privateKey.signMessage(message1, domain); const signature2 = keypair2.privateKey.signMessage(message1);
const signature3 = keypair3.privateKey.signMessage(message2, domain); const signature3 = keypair3.privateKey.signMessage(message2);
const signature4 = keypair4.privateKey.signMessage(message2, domain); const signature4 = keypair4.privateKey.signMessage(message2);
const aggregatePubKey12 = bls.aggregatePubkeys([ const aggregatePubKey12 = bls.aggregatePubkeys([
keypair1.publicKey.toBytesCompressed(), keypair1.publicKey.toBytesCompressed(),
@ -254,7 +218,6 @@ describe("test bls", function () {
[aggregatePubKey12, aggregatePubKey34], [aggregatePubKey12, aggregatePubKey34],
[message2, message1], [message2, message1],
aggregateSignature, aggregateSignature,
domain
); );
expect(result).to.be.false; expect(result).to.be.false;
@ -262,7 +225,6 @@ describe("test bls", function () {
it("should fail to verify aggregated signatures - different pubkeys and messsages", () => { it("should fail to verify aggregated signatures - different pubkeys and messsages", () => {
const domain = Buffer.alloc(8, 0);
const keypair1 = Keypair.generate(); const keypair1 = Keypair.generate();
const keypair2 = Keypair.generate(); const keypair2 = Keypair.generate();
@ -272,10 +234,10 @@ describe("test bls", function () {
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1"))); const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2"))); const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
const signature1 = keypair1.privateKey.signMessage(message1, domain); const signature1 = keypair1.privateKey.signMessage(message1);
const signature2 = keypair2.privateKey.signMessage(message1, domain); const signature2 = keypair2.privateKey.signMessage(message1);
const signature3 = keypair3.privateKey.signMessage(message2, domain); const signature3 = keypair3.privateKey.signMessage(message2);
const signature4 = keypair4.privateKey.signMessage(message2, domain); const signature4 = keypair4.privateKey.signMessage(message2);
const aggregatePubKey12 = bls.aggregatePubkeys([ const aggregatePubKey12 = bls.aggregatePubkeys([
keypair1.publicKey.toBytesCompressed(), keypair1.publicKey.toBytesCompressed(),
@ -294,57 +256,13 @@ describe("test bls", function () {
[aggregatePubKey12], [aggregatePubKey12],
[message2, message1], [message2, message1],
aggregateSignature, aggregateSignature,
domain
); );
expect(result).to.be.false; expect(result).to.be.false;
}); });
it("should fail to verify aggregated signatures - different domain", () => {
const domain = Buffer.alloc(8, 0);
const domain2 = Buffer.alloc(8, 1);
const keypair1 = Keypair.generate();
const keypair2 = Keypair.generate();
const keypair3 = Keypair.generate();
const keypair4 = Keypair.generate();
const message1 = Buffer.from(SHA256.digest(Buffer.from("Test1")));
const message2 = Buffer.from(SHA256.digest(Buffer.from("Test2")));
const signature1 = keypair1.privateKey.signMessage(message1, domain);
const signature2 = keypair2.privateKey.signMessage(message1, domain);
const signature3 = keypair3.privateKey.signMessage(message2, domain2);
const signature4 = keypair4.privateKey.signMessage(message2, domain2);
const aggregatePubKey12 = bls.aggregatePubkeys([
keypair1.publicKey.toBytesCompressed(),
keypair2.publicKey.toBytesCompressed(),
]);
const aggregateSignature = bls.aggregateSignatures([
signature1.toBytesCompressed(),
signature2.toBytesCompressed(),
signature3.toBytesCompressed(),
signature4.toBytesCompressed(),
]);
const result = bls.verifyMultiple(
[aggregatePubKey12],
[message2, message1],
aggregateSignature,
domain
);
expect(result).to.be.false;
});
it("should fail to verify aggregated signatures - no public keys", () => { it("should fail to verify aggregated signatures - no public keys", () => {
const domain = Buffer.alloc(8, 0);
const signature = Buffer.alloc(96); const signature = Buffer.alloc(96);
@ -355,7 +273,6 @@ describe("test bls", function () {
[], [],
[message2, message1], [message2, message1],
signature, signature,
domain
); );
expect(result).to.be.false; expect(result).to.be.false;

View File

@ -820,10 +820,10 @@
bip39 "^3.0.2" bip39 "^3.0.2"
buffer "^5.4.3" buffer "^5.4.3"
"@chainsafe/eth2-bls-wasm@^0.2.1": "@chainsafe/eth2-bls-wasm@^0.4.0":
version "0.2.1" version "0.4.0"
resolved "https://registry.yarnpkg.com/@chainsafe/eth2-bls-wasm/-/eth2-bls-wasm-0.2.1.tgz#82710c4b81676eb8a2940394efffc0e59216e731" resolved "https://registry.yarnpkg.com/@chainsafe/eth2-bls-wasm/-/eth2-bls-wasm-0.4.0.tgz#94e89e4e53ec6cdcc07178dbfeb0b80215550d2f"
integrity sha512-optSPu5Ez8tT0iEeCsLhjhMJYyXAZRyPFT2k4q+WrQxr82or5+IVQx4jHdy/uQwMvGB0rlvFYlwsyBrkioTCVw== integrity sha512-4BGktOKmUyyYJNCio5Zn1LdxaGWLhPqdUoZXokrfCLW3SKhw4E6MlnpAHwPtvnDuGSkmdF/GLhYeyjQYTtovqg==
dependencies: dependencies:
buffer "^5.4.3" buffer "^5.4.3"