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

kmccusker pushed a commit to branch fix-bls-wrapper
in repository https://gitbox.apache.org/repos/asf/incubator-milagro-crypto-c.git

commit 19d1e7351646470af040c456ac495e23d12fcb28
Author: Kealan McCusker <[email protected]>
AuthorDate: Tue Jan 21 17:19:59 2020 +0000

    fixed BLS Python wrapper
---
 python/CMakeLists.txt    |   1 +
 python/bls_ZZZ.py.in     |   4 +-
 python/run_bls_ZZZ.py.in | 152 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 155 insertions(+), 2 deletions(-)

diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index cd41aa1..38745ac 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -53,6 +53,7 @@ foreach(curve ${AMCL_CURVE})
 
   if(TARGET amcl_bls_${TC})
     amcl_configure_file_curve(bls_ZZZ.py.in bls_${TC}.py ${curve} 
amcl_bls_${TC}_GEN_PYS)
+    amcl_configure_file_curve(run_bls_ZZZ.py.in run_bls_${TC}.py ${curve} 
amcl_bls_${TC}_GEN_PYS)    
     install(FILES "${amcl_bls_${TC}_GEN_PYS}" DESTINATION 
${PYTHON3_SITE_PACKAGES})
   endif()
   
diff --git a/python/bls_ZZZ.py.in b/python/bls_ZZZ.py.in
index cf5e6d7..1074ddb 100755
--- a/python/bls_ZZZ.py.in
+++ b/python/bls_ZZZ.py.in
@@ -54,8 +54,8 @@ extern void KILL_CSPRNG(csprng *R);
 extern void OCT_clear(octet *O);
 
 extern int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W);
-extern int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S);
-extern int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W);
+extern int BLS_ZZZ_SIGN(octet *SIG,octet *m,octet *S);
+extern int BLS_ZZZ_VERIFY(octet *SIG,octet *m,octet *W);
 extern int BLS_ZZZ_ADD_G1(octet *R1,octet *R2,octet *R);
 extern int BLS_ZZZ_ADD_G2(octet *W1,octet *W2,octet *W);
 
diff --git a/python/run_bls_ZZZ.py.in b/python/run_bls_ZZZ.py.in
new file mode 100755
index 0000000..f19a764
--- /dev/null
+++ b/python/run_bls_ZZZ.py.in
@@ -0,0 +1,152 @@
+#!/usr/bin/env python3
+
+"""
+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.
+"""
+
+
+"""
+example for using bls
+
+"""
+import cffi
+import platform
+import os
+import bls_ZZZ
+
+if __name__ == "__main__":
+    # Print hex values
+    DEBUG = False
+
+    # Seed
+    seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30"
+    seed = bytes.fromhex(seed_hex)
+
+    # Message
+    message = b"test message"
+
+    # random number generator
+    rng = bls_ZZZ.create_csprng(seed)
+
+    # Generate key pairs
+    rtn, sk1, pktmp = bls_ZZZ.key_pair_generate(rng)
+    if rtn != 0:
+        print("Error: key_pair_generate {}".format(rtn))
+        raise SystemExit(0)
+    print("sk1: {}".format(sk1.hex()))
+    print("pktmp: {}".format(pktmp.hex()))
+
+    rtn, sk1, pk1 = bls_ZZZ.key_pair_generate(rng, sk1)
+    if rtn != 0:
+        print("Error: key_pair_generate {}".format(rtn))
+        raise SystemExit(0)
+    print("sk1: {}".format(sk1.hex()))
+    print("pk1: {}".format(pk1.hex()))
+
+    rtn, sk2, pk2 = bls_ZZZ.key_pair_generate(rng)
+    if rtn != 0:
+        print("Error: key_pair_generate {}".format(rtn))
+        raise SystemExit(0)
+    print("sk2: {}".format(sk2.hex()))
+    print("pk2: {}".format(pk2.hex()))
+
+    rtn, sk3, pk3 = bls_ZZZ.key_pair_generate(rng)
+    if rtn != 0:
+        print("Error: key_pair_generate {}".format(rtn))
+        raise SystemExit(0)
+    print("sk3: {}".format(sk3.hex()))
+    print("pk3: {}".format(pk3.hex()))
+
+    # Sign and verify
+    rtn, sig1 = bls_ZZZ.sign(message, sk1)
+    if rtn != 0:
+        print("Error: sign {}".format(rtn))
+        raise SystemExit(0)
+    print("sig1: {}".format(sig1.hex()))
+
+    rtn = bls_ZZZ.verify(sig1, message, pk1)
+    if rtn != 0:
+        print("Error: Invalid signature {}".format(rtn))
+        raise SystemExit(0)
+    print("Success: Signature is valid")
+
+    rtn, sig2 = bls_ZZZ.sign(message, sk2)
+    if rtn != 0:
+        print("Error: sign {}".format(rtn))
+        raise SystemExit(0)
+    print("sig2: {}".format(sig2.hex()))
+
+    rtn = bls_ZZZ.verify(sig2, message, pk2)
+    if rtn != 0:
+        print("Error: Invalid signature {}".format(rtn))
+        raise SystemExit(0)
+    print("Success: Signature is valid")
+
+    rtn, sig3 = bls_ZZZ.sign(message, sk3)
+    if rtn != 0:
+        print("Error: sign {}".format(rtn))
+        raise SystemExit(0)
+    print("sig3: {}".format(sig3.hex()))
+
+    rtn = bls_ZZZ.verify(sig3, message, pk3)
+    if rtn != 0:
+        print("Error: Invalid signature {}".format(rtn))
+        raise SystemExit(0)
+    print("Success: Signature is valid")
+
+    # Add Signatures
+    rtn, sig12 = bls_ZZZ.add_G1(sig1, sig2)
+    if rtn != 0:
+        print("Error: add_G1 {}".format(rtn))
+        raise SystemExit(0)
+    print("sig12: {}".format(sig12.hex()))
+
+    rtn, sig123 = bls_ZZZ.add_G1(sig12, sig3)
+    if rtn != 0:
+        print("Error: add_G1 {}".format(rtn))
+        raise SystemExit(0)
+    print("sig123: {}".format(sig123.hex()))
+
+    # Add Public keys
+    rtn, pk12 = bls_ZZZ.add_G2(pk1, pk2)
+    if rtn != 0:
+        print("Error: add_G2 {}".format(rtn))
+        raise SystemExit(0)
+    print("pk12: {}".format(pk12.hex()))
+
+    rtn, pk123 = bls_ZZZ.add_G2(pk12, pk3)
+    if rtn != 0:
+        print("Error: add_G2 {}".format(rtn))
+        raise SystemExit(0)
+    print("pk123: {}".format(pk123.hex()))
+
+    # Verify aggretated values
+    rtn = bls_ZZZ.verify(sig123, message, pk123)
+    if rtn != 0:
+        print("Error: Invalid aggregated signature {}".format(rtn))
+        raise SystemExit(0)
+    print("Success: Aggregated signature is valid")
+
+    # Clear memory
+    bls_ZZZ.kill_csprng(rng)
+    del sk1
+    del pk1
+    del sk2
+    del pk2
+    del sk3
+    del pk3

Reply via email to