You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@milagro.apache.org by sa...@apache.org on 2020/02/18 14:37:34 UTC

[incubator-milagro-MPC] 04/06: Refactor examples

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

sandreoli pushed a commit to branch issue7-schnorr-python-wrapper
in repository https://gitbox.apache.org/repos/asf/incubator-milagro-MPC.git

commit 92df5e8fbc662e78873b3123066e604f738a3821
Author: Samuele Andreoli <sa...@yahoo.it>
AuthorDate: Tue Feb 18 14:03:42 2020 +0000

    Refactor examples
---
 python/CMakeLists.txt                      |  1 +
 python/{ => examples}/CMakeLists.txt       |  8 +---
 python/examples/context.py                 | 25 ++++++++++
 python/{ => examples}/example_dump_keys.py |  2 +-
 python/{ => examples}/example_ecdsa.py     |  2 +-
 python/{ => examples}/example_mta.py       |  2 +-
 python/{ => examples}/example_r.py         |  2 +-
 python/{ => examples}/example_s.py         |  2 +-
 python/examples/example_schnorr.py         | 74 ++++++++++++++++++++++++++++++
 9 files changed, 107 insertions(+), 11 deletions(-)

diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index 55b719c..d7ed21c 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -19,3 +19,4 @@ include(PythonSiteDirs)
 
 add_subdirectory(amcl)
 add_subdirectory(test)
+add_subdirectory(examples)
diff --git a/python/CMakeLists.txt b/python/examples/CMakeLists.txt
similarity index 86%
copy from python/CMakeLists.txt
copy to python/examples/CMakeLists.txt
index 55b719c..1d768ba 100644
--- a/python/CMakeLists.txt
+++ b/python/examples/CMakeLists.txt
@@ -13,9 +13,5 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
-
-include(PythonSiteDirs)
-
-add_subdirectory(amcl)
-add_subdirectory(test)
+file(GLOB EXAMPLES *.py)
+file(COPY ${EXAMPLES} DESTINATION "${PROJECT_BINARY_DIR}/python/examples")
diff --git a/python/examples/context.py b/python/examples/context.py
new file mode 100644
index 0000000..c7a9ac2
--- /dev/null
+++ b/python/examples/context.py
@@ -0,0 +1,25 @@
+"""
+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.
+"""
+
+import os
+import sys
+
+sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
+
+from amcl import mpc, schnorr
diff --git a/python/example_dump_keys.py b/python/examples/example_dump_keys.py
similarity index 99%
rename from python/example_dump_keys.py
rename to python/examples/example_dump_keys.py
index 8fe5665..5e6ac46 100755
--- a/python/example_dump_keys.py
+++ b/python/examples/example_dump_keys.py
@@ -19,7 +19,7 @@ specific language governing permissions and limitations
 under the License.
 """
 
-from amcl import mpc
+from context import mpc
 
 P_hex = "94f689d07ba20cf7c7ca7ccbed22ae6b40c426db74eaee4ce0ced2b6f52a5e136663f5f1ef379cdbb0c4fdd6e4074d6cff21082d4803d43d89e42fd8dfa82b135aa31a8844ffea25f255f956cbc1b9d8631d01baf1010d028a190b94ce40f3b72897e8196df19edf1ff62e6556f2701d52cef1442e3301db7608ecbdcca703db"
 Q_hex = "9a9ad73f246df853e129c589925fdad9df05606a61081e62e72be4fb33f6e5ec492cc734f28bfb71fbe2ba9a11e4c02e2c0d103a5cbb0a9d6402c07de63b1b995dd72ac8f29825d66923a088b421fb4d52b0b855d2f5dde2be9b0ca0cee6f7a94e5566735fe6cff1fcad3199602f88528d19aa8d0263adff8f5053c38254a2a3"
diff --git a/python/example_ecdsa.py b/python/examples/example_ecdsa.py
similarity index 99%
rename from python/example_ecdsa.py
rename to python/examples/example_ecdsa.py
index 39c5607..8b0c9ea 100755
--- a/python/example_ecdsa.py
+++ b/python/examples/example_ecdsa.py
@@ -19,7 +19,7 @@ specific language governing permissions and limitations
 under the License.
 """
 
-from amcl import mpc
+from context import mpc
 
 seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30"
 
diff --git a/python/example_mta.py b/python/examples/example_mta.py
similarity index 99%
rename from python/example_mta.py
rename to python/examples/example_mta.py
index 1491120..e22b920 100755
--- a/python/example_mta.py
+++ b/python/examples/example_mta.py
@@ -19,7 +19,7 @@ specific language governing permissions and limitations
 under the License.
 """
 
-from amcl import mpc
+from context import mpc
 
 seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30"
 
diff --git a/python/example_r.py b/python/examples/example_r.py
similarity index 99%
rename from python/example_r.py
rename to python/examples/example_r.py
index 7f55ce0..5fca2a1 100755
--- a/python/example_r.py
+++ b/python/examples/example_r.py
@@ -19,7 +19,7 @@ specific language governing permissions and limitations
 under the License.
 """
 
-from amcl import mpc
+from context import mpc
 
 seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30"
 
diff --git a/python/example_s.py b/python/examples/example_s.py
similarity index 99%
rename from python/example_s.py
rename to python/examples/example_s.py
index 011a437..122a433 100755
--- a/python/example_s.py
+++ b/python/examples/example_s.py
@@ -19,7 +19,7 @@ specific language governing permissions and limitations
 under the License.
 """
 
-from amcl import mpc
+from context import mpc
 
 seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30"
 
diff --git a/python/examples/example_schnorr.py b/python/examples/example_schnorr.py
new file mode 100755
index 0000000..d906d5f
--- /dev/null
+++ b/python/examples/example_schnorr.py
@@ -0,0 +1,74 @@
+#!/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.
+"""
+
+from context import schnorr
+
+seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30"
+
+x_hex = "fab4ce512dff74bd9c71c89a14de5b877af45dca0329ee3fcb72611c0784fef3"
+V_hex = "032cf4b348c9d00718f01ed98923e164df53b5e8bc4c2250662ed2df784e1784f4"
+
+if __name__ == "__main__":
+    seed = bytes.fromhex(seed_hex)
+
+    x = bytes.fromhex(x_hex)
+    V = bytes.fromhex(V_hex)
+
+    # random number generator
+    rng = schnorr.create_csprng(seed)
+
+    print("Example Schnorr Protocol")
+    print("DLOG: V = x.G")
+    print(f"\tx = {x_hex}")
+    print(f"\tV = {V_hex}")
+
+    # Generate commitment C = r.G, r random in [0, ..., q]
+    r, C = schnorr.commit(rng)
+
+    print("\n[Prover] Commitment C = r.G")
+    print(f"\tr = {r.hex()}")
+    print(f"\tC = {C.hex()}")
+
+    # Generate deterministic challenge e = H(G, V, C)
+    e = schnorr.challenge(V, C)
+
+    print("\n[Prover] Deterministic Challenge e = H(G, V, C)")
+    print(f"\te = {e.hex()}")
+
+    # Generate proof p = r - ex mod q
+    p = schnorr.prove(r, e, x)
+
+    print("\n[Prover] Generate proof p = r - ex")
+    print(f"\tp = {p.hex()}")
+
+    # Verifier regenerates deterministic challenge
+    e = schnorr.challenge(V, C)
+    print("\n[Verifier] Deterministic Challenge e = H(G, V, C)")
+    print(f"\te = {e.hex()}")
+
+    # Verify
+    rc = schnorr.verify(V, C, e, p)
+
+    print("\n[Verifier] Verify proof p")
+    if rc == schnorr.OK:
+        print("\tSuccess")
+    else:
+        print("\tFailure")