This is an automated email from the ASF dual-hosted git repository.

kmccusker pushed a commit to branch issue7
in repository 
https://gitbox.apache.org/repos/asf/incubator-milagro-crypto-js.git


The following commit(s) were added to refs/heads/issue7 by this push:
     new adc1453  add BLS test vectors
adc1453 is described below

commit adc1453f218c6facd34f822891dd9540a7344c57
Author: Kealan McCusker <[email protected]>
AuthorDate: Wed Jun 26 11:49:49 2019 +0100

    add BLS test vectors
---
 examples/browser/example_BLS.html                  |  10 +-
 ...example_BLS_BLS383.js => example_BLS_BLS381.js} |  44 ++++-
 test/test_BLS.js                                   | 178 +++++++++++++++++++++
 3 files changed, 223 insertions(+), 9 deletions(-)

diff --git a/examples/browser/example_BLS.html 
b/examples/browser/example_BLS.html
index 5c19a78..67418e3 100644
--- a/examples/browser/example_BLS.html
+++ b/examples/browser/example_BLS.html
@@ -46,7 +46,7 @@
 
 
 <p><a id="myLink4" href="#" onclick="location.reload(false);bn254();">BN254 
254-bit k=12 Pairing-Friendly BN Curve Boneh-Lynn-Shacham</a></p>
-<p><a id="myLink5" href="#" onclick="location.reload(false);bls383();">BLS383 
383-bit k=12 Pairing-Friendly BLS Curve Boneh-Lynn-Shacham</a></p>
+<p><a id="myLink5" href="#" onclick="location.reload(false);bls383();">BLS381 
381-bit k=12 Pairing-Friendly BLS Curve Boneh-Lynn-Shacham</a></p>
 <p><a id="myLink6" href="#" onclick="location.reload(false);bls24();">BLS24 
479-bit k=24 Pairing-Friendly BLS Curve Boneh-Lynn-Shacham</a></p>
 <p><a id="myLink7" href="#" onclick="location.reload(false);bls48();">BLS48 
556-bit k=48 Pairing-Friendly BLS Curve Boneh-Lynn-Shacham</a></p>
 
@@ -103,12 +103,12 @@ function bn254() {
 
 }
 
-// BLS383 context
-function bls383() {
-       var ctx = new CTX('BLS383');
+// BLS381 context
+function bls381() {
+       var ctx = new CTX('BLS381');
        mywindow=window.open();
 
-       mywindow.document.write("<br> BLS383 Pairing-Friendly Curve "+  "<br>");
+       mywindow.document.write("<br> BLS381 Pairing-Friendly Curve "+  "<br>");
 
        var i,res;
        var result;
diff --git a/examples/node/example_BLS_BLS383.js 
b/examples/node/example_BLS_BLS381.js
similarity index 65%
rename from examples/node/example_BLS_BLS383.js
rename to examples/node/example_BLS_BLS381.js
index 83e1d2c..fc28aa2 100644
--- a/examples/node/example_BLS_BLS383.js
+++ b/examples/node/example_BLS_BLS381.js
@@ -1,11 +1,10 @@
 /* Test BLS - test driver and function exerciser for BLS API Functions */
 
 var CTX = require("../../index");
-var ctx = new CTX('BLS383');
+var ctx = new CTX('BLS381');
 
 var i,res;
 
-var BGS=ctx.BLS.BGS;
 var BFS=ctx.BLS.BFS;
 
 /* Group 1 Size */
@@ -60,16 +59,41 @@ if (res==0)
 else
     console.log("Error User 2 invalid Signature");
 
+// User 3
+var sk3=[];
+var pk3=[];
+var sig3=[];
+
+ctx.BLS.KeyPairGenerate(rng,sk3,pk3);
+console.log("Private key user 3: 0x"+ctx.BLS.bytestostring(sk3));
+console.log("Public key user 3: 0x"+ctx.BLS.bytestostring(pk3));
+
+console.log("Message : "+message);
+ctx.BLS.sign(sig3,message,sk3);
+console.log("Signature user 3: 0x"+ctx.BLS.bytestostring(sig3));
+
+var res=ctx.BLS.verify(sig3,message,pk3);
+if (res==0)
+    console.log("Success User 3 valid Signature");
+else
+    console.log("Error User 3 invalid Signature");
+
 // Combined
+var pk12=[];
+var sig12=[];
 var pk=[];
 var sig=[];
 
 // Add signatures
-ctx.BLS.add_G1(sig1,sig2,sig);
+ctx.BLS.add_G1(sig1,sig2,sig12);
+console.log("Signature combined: 0x"+ctx.BLS.bytestostring(sig12));
+ctx.BLS.add_G1(sig3,sig12,sig);
 console.log("Signature combined: 0x"+ctx.BLS.bytestostring(sig));
 
 // Add public keys
-ctx.BLS.add_G2(pk1,pk2,pk);
+ctx.BLS.add_G2(pk1,pk2,pk12);
+console.log("Public key combined: 0x"+ctx.BLS.bytestostring(pk12));
+ctx.BLS.add_G2(pk3,pk12,pk);
 console.log("Public key combined: 0x"+ctx.BLS.bytestostring(pk));
 
 var res=ctx.BLS.verify(sig,message,pk);
@@ -78,6 +102,18 @@ if (res==0)
 else
     console.log("Error combined invalid Signature");
 
+var res=ctx.BLS.verify(sig12,message,pk);
+if (res==0)
+    console.log("Success combined valid Signature");
+else
+    console.log("Error combined invalid Signature");
+
+var res=ctx.BLS.verify(sig,message,pk12);
+if (res==0)
+    console.log("Success combined valid Signature");
+else
+    console.log("Error combined invalid Signature");
+
 // Test corrupted signature
 sig1[0] = 1;
 var res=ctx.BLS.verify(sig1,message,pk1);
diff --git a/test/test_BLS.js b/test/test_BLS.js
new file mode 100644
index 0000000..9b8d2bb
--- /dev/null
+++ b/test/test_BLS.js
@@ -0,0 +1,178 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+// Test BLS
+
+var CTX = require("../index");
+
+var chai = require('chai');
+
+var expect = chai.expect;
+
+// Curves for test
+var pf_curves = ['BN254', 'BN254CX', 'BLS381', 'BLS383', 'BLS461', 'FP256BN', 
'FP512BN', 'BLS24', 'BLS48'];
+
+pf_curves.forEach(function(curve) {
+
+    describe('TEST BLS' + curve, function() {
+
+        var ctx = new CTX(curve),
+            rng = new ctx.RAND(),
+            sk1 = [],
+            pk1 = [],
+            sig1 = [],
+            sk2 = [],
+            pk2 = [],
+            sig2 = [],
+            sk3 = [],
+            pk3 = [],
+            sig3 = [],
+           message="test message";
+
+        if (ctx.ECP.CURVE_PAIRING_TYPE === 1 | ctx.ECP.CURVE_PAIRING_TYPE === 
2) {
+            BLS = ctx.BLS;
+        } else if (ctx.ECP.CURVE_PAIRING_TYPE === 3) {
+            BLS = ctx.BLS192;
+        } else if (ctx.ECP.CURVE_PAIRING_TYPE === 4) {
+            BLS = ctx.BLS256;
+        }
+
+        before(function(done) {
+            this.timeout(0);
+
+            var RAW = [];
+            rng.clean();
+            for (var j = 0; j < 100; j++) RAW[j] = j;
+            rng.seed(100, RAW);
+
+           // Key pairs
+           BLS.KeyPairGenerate(rng,sk1,pk1);
+           BLS.KeyPairGenerate(rng,sk2,pk2);
+           BLS.KeyPairGenerate(rng,sk3,pk3);       
+
+           // Sign message
+           BLS.sign(sig1,message,sk1);
+           BLS.sign(sig2,message,sk2);
+           BLS.sign(sig3,message,sk3);     
+
+            done();
+        });
+
+       it('test verification', function(done) {
+            this.timeout(0);
+
+           var rc=BLS.verify(sig1,message,pk1);
+
+            expect(rc).to.be.equal(BLS.BLS_OK);
+
+           rc=BLS.verify(sig2,message,pk2);
+
+            expect(rc).to.be.equal(BLS.BLS_OK);
+
+           rc=BLS.verify(sig3,message,pk3);
+
+            expect(rc).to.be.equal(BLS.BLS_OK);
+
+            done();
+        });
+
+       it('test corrupted message', function(done) {
+            this.timeout(0);
+
+           var message2="bad message";
+           
+           var rc=BLS.verify(sig1,message2,pk1);
+
+            expect(rc).to.be.equal(BLS.BLS_FAIL);
+           
+            done();
+        });
+       
+       it('test corrupted signature', function(done) {
+            this.timeout(0);
+
+           var tmp = sig1[0]
+           sig1[0]=1;
+           
+           var rc=BLS.verify(sig1,message,pk1);
+           sig1[0] = tmp
+
+            expect(rc).to.be.equal(BLS.BLS_FAIL);
+           
+            done();
+        });
+
+       it('test combined correct signature / correct public key', 
function(done) {
+            this.timeout(0);
+
+           var sig12=[];
+           var sigAll=[];          
+           BLS.add_G1(sig1,sig2,sig12);
+           BLS.add_G1(sig12,sig3,sigAll);          
+
+           var pk12=[];
+           var pkAll=[];           
+           BLS.add_G2(pk1,pk2,pk12);
+           BLS.add_G2(pk12,pk3,pkAll); 
+           
+           var rc=BLS.verify(sigAll,message,pkAll);
+
+            expect(rc).to.be.equal(BLS.BLS_OK);
+           
+            done();
+        });
+
+       it('test combined incorrect signature / correct public key', 
function(done) {
+            this.timeout(0);
+
+           var sig12=[];
+           BLS.add_G1(sig1,sig2,sig12);
+
+           var pk12=[];
+           var pkAll=[];           
+           BLS.add_G2(pk1,pk2,pk12);
+           BLS.add_G2(pk12,pk3,pkAll); 
+           
+           var rc=BLS.verify(sig12,message,pkAll);
+
+            expect(rc).to.be.equal(BLS.BLS_FAIL);
+           
+            done();
+        });
+
+       it('test combined correct signature / incorrect public key', 
function(done) {
+            this.timeout(0);
+
+           var sig12=[];
+           var sigAll=[];          
+           BLS.add_G1(sig1,sig2,sig12);
+           BLS.add_G1(sig12,sig3,sigAll);          
+
+           var pk12=[];
+           BLS.add_G2(pk1,pk2,pk12);
+           
+           var rc=BLS.verify(sigAll,message,pk12);
+
+            expect(rc).to.be.equal(BLS.BLS_FAIL);
+           
+            done();
+        });
+       
+    });
+});

Reply via email to