From 5db052ac6193f6168fd271a514c7f49c7471044b Mon Sep 17 00:00:00 2001 From: manel1874 Date: Tue, 5 Dec 2023 13:48:14 +0000 Subject: [PATCH] Project update: encryption included --- .github/workflows/lint-test-cover-docs.yml | 2 +- README.rst | 36 +-- demos/intro_demo.ipynb | 323 ++++++++++----------- docs/_source/{tecdsa.rst => tinysig.rst} | 12 +- docs/index.rst | 2 +- docs/toc.rst | 2 +- pyproject.toml | 8 +- src/tecdsa/__init__.py | 4 - src/tinysig/__init__.py | 4 + src/{tecdsa => tinysig}/network.py | 0 src/{tecdsa => tinysig}/setup.py | 0 src/{tecdsa => tinysig}/tecdsa.py | 228 ++++++++++----- src/{tecdsa => tinysig}/utils.py | 0 tests/{test_tbecdsa.py => test_tecdsa.py} | 6 +- 14 files changed, 340 insertions(+), 287 deletions(-) rename docs/_source/{tecdsa.rst => tinysig.rst} (59%) delete mode 100644 src/tecdsa/__init__.py create mode 100644 src/tinysig/__init__.py rename src/{tecdsa => tinysig}/network.py (100%) rename src/{tecdsa => tinysig}/setup.py (100%) rename src/{tecdsa => tinysig}/tecdsa.py (77%) rename src/{tecdsa => tinysig}/utils.py (100%) rename tests/{test_tbecdsa.py => test_tecdsa.py} (98%) diff --git a/.github/workflows/lint-test-cover-docs.yml b/.github/workflows/lint-test-cover-docs.yml index a635b11..eadbd6c 100644 --- a/.github/workflows/lint-test-cover-docs.yml +++ b/.github/workflows/lint-test-cover-docs.yml @@ -18,7 +18,7 @@ jobs: - name: Lint and test module. run: | pip install -U .[lint,test] - # python -m pylint tecdsa # Check against linting rules. + # python -m pylint tinysig # Check against linting rules. python -m pytest # Run tests. - name: Publish coverage results. run: | diff --git a/README.rst b/README.rst index efc4e09..50e8017 100644 --- a/README.rst +++ b/README.rst @@ -1,42 +1,38 @@ ======= -tecdsa +tinysig ======= -Pure-Python implementation of a `threshold ecdsa signature scheme `__ based on a secure multi-party computation (MPC) `protocol for evaluating arithmetic sum-of-products expressions `__ via a non-interactive computation phase. +Pure-Python implementation of a `threshold ecdsa signature scheme `__ based on a secure multi-party computation (MPC) `protocol for evaluating arithmetic sum-of-products expressions `__ via a non-interactive computation phase. |pypi| |readthedocs| |actions| |coveralls| -.. |pypi| image:: https://badge.fury.io/py/tecdsa.svg - :target: https://badge.fury.io/py/tecdsa +.. |pypi| image:: https://badge.fury.io/py/tinysig.svg + :target: https://badge.fury.io/py/tinysig :alt: PyPI version and link. -.. |readthedocs| image:: https://readthedocs.org/projects/tecdsa/badge/?version=latest - :target: https://tecdsa.readthedocs.io/en/latest/?badge=latest +.. |readthedocs| image:: https://readthedocs.org/projects/tinysig/badge/?version=latest + :target: https://tinysig.readthedocs.io/en/latest/?badge=latest :alt: Read the Docs documentation status. -.. |actions| image:: https://github.com/nillion-oss/tecdsa/workflows/lint-test-cover-docs/badge.svg - :target: https://github.com/nillion-oss/tecdsa/actions/workflows/lint-test-cover-docs.yml +.. |actions| image:: https://github.com/nillion-oss/tinysig/workflows/lint-test-cover-docs/badge.svg + :target: https://github.com/nillion-oss/tinysig/actions/workflows/lint-test-cover-docs.yml :alt: GitHub Actions status. -.. |coveralls| image:: https://coveralls.io/repos/github/nillion-oss/tecdsa/badge.svg?branch=main - :target: https://coveralls.io/github/nillion-oss/tecdsa?branch=main - :alt: Coveralls test coverage summary. - Installation and Usage ---------------------- -This library is available as a `package on PyPI `__: +This library is available as a `package on PyPI `__: .. code-block:: bash - python -m pip install tecdsa + python -m pip install tinysig The library can be imported in the usual way: .. code-block:: python - import tecdsa - from tecdsa import * + import tinysig + from tinysig import * Basic Example ^^^^^^^^^^^^^ @@ -72,7 +68,7 @@ After defining a message we can sign it as follows: .. code-block:: python - >>> message = "Let me tell you a great secret about Nillion." + >>> message = "Let me tell you a secret about Nillion." >>> ecnet.ts_online_protocol(message, client_id) We run the following to print the signature owned by the client (ID=1): @@ -115,11 +111,11 @@ Style conventions are enforced using `Pylint `__: .. code-block:: bash python -m pip install .[lint] - python -m pylint src/tecdsa + python -m pylint src/tinysig Contributions ^^^^^^^^^^^^^ -In order to contribute to the source code, open an issue or submit a pull request on the `GitHub page `__ for this library. +In order to contribute to the source code, open an issue or submit a pull request on the `GitHub page `__ for this library. Versioning ^^^^^^^^^^ @@ -127,7 +123,7 @@ The version number format for this library and the changes to the library associ Publishing ^^^^^^^^^^ -This library can be published as a `package on PyPI `__ by a package maintainer. First, install the dependencies required for packaging and publishing: +This library can be published as a `package on PyPI `__ by a package maintainer. First, install the dependencies required for packaging and publishing: .. code-block:: bash diff --git a/demos/intro_demo.ipynb b/demos/intro_demo.ipynb index 9df20e8..f2aa44c 100644 --- a/demos/intro_demo.ipynb +++ b/demos/intro_demo.ipynb @@ -7,9 +7,9 @@ "metadata": {}, "outputs": [], "source": [ - "from tecdsa.utils import verify_ecdsa_signature, verify_dsa_signature\n", - "from tecdsa.setup import DSASetup, ECDSASetup\n", - "from tecdsa.tecdsa import ThresholdSignature" + "from tinysig.utils import verify_ecdsa_signature, verify_dsa_signature\n", + "from tinysig.setup import DSASetup, ECDSASetup\n", + "from tinysig.tecdsa import ThresholdSignature" ] }, { @@ -45,22 +45,22 @@ ] }, { - "cell_type": "code", - "execution_count": 3, - "id": "ebd53f46-0823-43ac-8678-7a536e7a2131", + "cell_type": "markdown", + "id": "3b131b8a-ac50-4c31-a919-df9da22df805", "metadata": {}, - "outputs": [], "source": [ - "# Option 1\n", - "fnil = ThresholdSignature(N, C)" + "The first option takes longer as the `generate_dsa_setup()` function has to find a primitive root for the `q` prime of the DSA setup." ] }, { - "cell_type": "markdown", - "id": "3b131b8a-ac50-4c31-a919-df9da22df805", + "cell_type": "code", + "execution_count": null, + "id": "78fb7116-1b22-4b9a-8e30-632e294bfdfe", "metadata": {}, + "outputs": [], "source": [ - "This option takes longer as the `generate_dsa_setup()` function has to find a primitive root for the `q` prime of the DSA setup." + "# Option 1\n", + "fnil = ThresholdSignature(N, C)" ] }, { @@ -232,7 +232,7 @@ "metadata": {}, "outputs": [], "source": [ - "message = \"Let me tell you a great secret about Nillion.\"" + "message = \"Let me tell you a secret about Nillion.\"" ] }, { @@ -265,9 +265,9 @@ "output_type": "stream", "text": [ " Client(id=1,\n", - " r=6202005962638382719598591827040686943912736840956149356327459726601,\n", - " s=18131665991815852492200116613897714960534471085827748460942091666925,\n", - " m=Let me tell you a great secret about Nillion.,\n", + " r=866456810140799270992901093853994083574513660692530393694283169735,\n", + " s=17230127798322948297359259017038343422389044881055246558700591255771,\n", + " m=Let me tell you a secret about Nillion.,\n", " )\n" ] } @@ -288,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 14, "id": "cae841ea-ea6e-420d-891f-c34a6f8b55df", "metadata": {}, "outputs": [], @@ -316,7 +316,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 15, "id": "03d48d0b-1b19-4e87-9423-e7ff35df7133", "metadata": {}, "outputs": [], @@ -327,7 +327,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 16, "id": "8d206ddb-8670-49fa-aca0-1440b5d6f8f3", "metadata": {}, "outputs": [], @@ -338,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 17, "id": "23b19bd5-9ceb-46b0-a24c-ed5ec88bf005", "metadata": {}, "outputs": [], @@ -348,7 +348,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 18, "id": "6330a904-5111-4d22-a8ce-e05cb709408e", "metadata": {}, "outputs": [], @@ -359,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 19, "id": "28cb83d7-a931-4818-bb27-1701fd6a5f1f", "metadata": {}, "outputs": [], @@ -384,7 +384,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 20, "id": "4f69041c-19b0-41d4-83ad-87f8118eda24", "metadata": {}, "outputs": [ @@ -395,8 +395,8 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mVerifySignatureError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[28], line 9\u001b[0m\n\u001b[1;32m 7\u001b[0m G \u001b[38;5;241m=\u001b[39m ecnil\u001b[38;5;241m.\u001b[39mecdsa\u001b[38;5;241m.\u001b[39mG\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Verify\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m \u001b[43mverify_ecdsa_signature\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother_message\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mY\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mq\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mG\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.pyenv/versions/3.10.10/envs/ecdsa/lib/python3.10/site-packages/tbecdsa/utils.py:190\u001b[0m, in \u001b[0;36mverify_ecdsa_signature\u001b[0;34m(message, r, s, Y, q, G)\u001b[0m\n\u001b[1;32m 188\u001b[0m v \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(V\u001b[38;5;241m.\u001b[39mx)\n\u001b[1;32m 189\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m v \u001b[38;5;241m!=\u001b[39m r:\n\u001b[0;32m--> 190\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m VerifySignatureError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSignature mismatch. Abort.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "Cell \u001b[0;32mIn[20], line 9\u001b[0m\n\u001b[1;32m 7\u001b[0m G \u001b[38;5;241m=\u001b[39m ecnil\u001b[38;5;241m.\u001b[39mecdsa\u001b[38;5;241m.\u001b[39mG\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Verify\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m \u001b[43mverify_ecdsa_signature\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother_message\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mY\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mq\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mG\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/.pyenv/versions/3.10.10/envs/ecdsa/lib/python3.10/site-packages/tinysig/utils.py:190\u001b[0m, in \u001b[0;36mverify_ecdsa_signature\u001b[0;34m(message, r, s, Y, q, G)\u001b[0m\n\u001b[1;32m 188\u001b[0m v \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(V\u001b[38;5;241m.\u001b[39mx)\n\u001b[1;32m 189\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m v \u001b[38;5;241m!=\u001b[39m r:\n\u001b[0;32m--> 190\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m VerifySignatureError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSignature mismatch. Abort.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mVerifySignatureError\u001b[0m: Signature verification failed. Signature mismatch. Abort." ] } @@ -425,7 +425,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 21, "id": "39569617-19f9-4427-9db9-0c56e4f772b8", "metadata": {}, "outputs": [ @@ -437,53 +437,53 @@ " nodes=[\n", " Node(id=1,\n", " shares_db={\n", - " 1th_client_x_enc_sh_exp: ,\n", - " 1th_client_m_lambda_exp_sh_exp: 18805008961205655853974338859205055368831044864697545644490465153571438711799,\n", - " 1th_client_signature_sh_base: 91779520132695752504598595459510400413067395296456213887423493859572385422122,\n", + " 1th_client_x_enc_sh_exp: ,\n", + " 1th_client_lambda_1_enc_sh_base: ,\n", + " 1th_client_lambda_2_enc_sh_base: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", " open_db={\n", - " 1th_client_x_sk: 102469784307885151344434448408108083163496555990607993030079296257768877177482,\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_r: 31753913312844769919847511464927299861472164513026404815794344435752523133091,\n", - " 1th_client_k_inv_sk: 99007062913722544630459242921033930766972195770224818402003123704021659134276,\n", - " 1th_client_gap_particle_m: 3105980911072955919053240646545559247084242804202022487995108928842220696094,\n", + " 1th_client_x_sk: 28366821186158931449365894762575778610930595231666123227518790537448233657765,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_r: 57171912513127058861305913686040155518728941802280615925843447425966440139137,\n", + " 1th_client_k_inv_sk: 8073490050704617529204062524429733350901213971996330479316418658370454712661,\n", + " 1th_client_gap_particle_m: 106579965961397005283454587061856305910257559613153554636714343161318176254853,\n", " }\n", " )\n", " Node(id=2,\n", " shares_db={\n", - " 1th_client_x_enc_sh_exp: ,\n", - " 1th_client_m_lambda_exp_sh_exp: 108606047286522208847578453720076266696012634649496781964928173924361506779525,\n", - " 1th_client_signature_sh_base: 14097041990847478370465638810235809055897373211739469599149103775486801032157,\n", + " 1th_client_x_enc_sh_exp: ,\n", + " 1th_client_lambda_1_enc_sh_base: ,\n", + " 1th_client_lambda_2_enc_sh_base: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", " open_db={\n", - " 1th_client_x_sk: 102469784307885151344434448408108083163496555990607993030079296257768877177482,\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_r: 31753913312844769919847511464927299861472164513026404815794344435752523133091,\n", - " 1th_client_k_inv_sk: 99007062913722544630459242921033930766972195770224818402003123704021659134276,\n", - " 1th_client_gap_particle_m: 3105980911072955919053240646545559247084242804202022487995108928842220696094,\n", + " 1th_client_x_sk: 28366821186158931449365894762575778610930595231666123227518790537448233657765,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_r: 57171912513127058861305913686040155518728941802280615925843447425966440139137,\n", + " 1th_client_k_inv_sk: 8073490050704617529204062524429733350901213971996330479316418658370454712661,\n", + " 1th_client_gap_particle_m: 106579965961397005283454587061856305910257559613153554636714343161318176254853,\n", " }\n", " )\n", " Node(id=3,\n", " shares_db={\n", - " 1th_client_x_enc_sh_exp: ,\n", - " 1th_client_m_lambda_exp_sh_exp: 79008080502481446358856101831037787726509978488153796354999416201748522695236,\n", - " 1th_client_signature_sh_base: 57889872377523500056536556248862903975761972917443548592733728878154084316939,\n", + " 1th_client_x_enc_sh_exp: ,\n", + " 1th_client_lambda_1_enc_sh_base: ,\n", + " 1th_client_lambda_2_enc_sh_base: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", " open_db={\n", - " 1th_client_x_sk: 102469784307885151344434448408108083163496555990607993030079296257768877177482,\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_r: 31753913312844769919847511464927299861472164513026404815794344435752523133091,\n", - " 1th_client_k_inv_sk: 99007062913722544630459242921033930766972195770224818402003123704021659134276,\n", - " 1th_client_gap_particle_m: 3105980911072955919053240646545559247084242804202022487995108928842220696094,\n", + " 1th_client_x_sk: 28366821186158931449365894762575778610930595231666123227518790537448233657765,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_r: 57171912513127058861305913686040155518728941802280615925843447425966440139137,\n", + " 1th_client_k_inv_sk: 8073490050704617529204062524429733350901213971996330479316418658370454712661,\n", + " 1th_client_gap_particle_m: 106579965961397005283454587061856305910257559613153554636714343161318176254853,\n", " }\n", " )\n", " ]\n", @@ -491,24 +491,20 @@ " clients=[\n", " Client(id=1,\n", " shares_db={\n", - " 1th_client_m_lambda_exp_sh_exp_node_1: 18805008961205655853974338859205055368831044864697545644490465153571438711799,\n", - " 1th_client_m_lambda_exp_sh_exp_node_2: 108606047286522208847578453720076266696012634649496781964928173924361506779525,\n", - " 1th_client_m_lambda_exp_sh_exp_node_3: 79008080502481446358856101831037787726509978488153796354999416201748522695236,\n", - " m_lambda_exp: 90627047539853062297711447460911536261356702778212363621995796218612956142192,\n", - " gap_lambda_exp: 91551342479687100116633344204445557531301978216457851867025998830604586230946,\n", - " 1th_client_signature_sh_base_node_1: 91779520132695752504598595459510400413067395296456213887423493859572385422122,\n", - " 1th_client_signature_sh_base_node_2: 14097041990847478370465638810235809055897373211739469599149103775486801032157,\n", - " 1th_client_signature_sh_base_node_3: 57889872377523500056536556248862903975761972917443548592733728878154084316939,\n", - " sig_gap: 47974345290710482168903343569201539914729786201503471736884067452144758726849,\n", + " gap_lambda_exp: 78480600859901013658977298041246219230152361570082221929571955851195324294816,\n", + " m_lambda_exp: 114912707192229153265222546030179129497023690142525895211846450187987474696967,\n", + " 1th_client_enc_signature_sh_base_node_1: ,\n", + " 1th_client_enc_signature_sh_base_node_2: ,\n", + " 1th_client_enc_signature_sh_base_node_3: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", - " private_keys=>,\n", + " private_keys=>,\n", " open_db={\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_r: 31753913312844769919847511464927299861472164513026404815794344435752523133091,\n", - " 1th_client_s: 45641033648832070136841209516832737108002048145377673994477584593492361851821,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_r: 57171912513127058861305913686040155518728941802280615925843447425966440139137,\n", + " 1th_client_s: 92042169691787731500333922131011982711433537751444369619584811954794492387646,\n", " 1th_client_message: This is one of many releases we are going to have in the next few months.,\n", " }\n", " )\n", @@ -531,7 +527,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 22, "id": "29dcc5c5-2263-414f-a69e-07d492b62695", "metadata": {}, "outputs": [], @@ -542,7 +538,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 23, "id": "c87dc2a5-ac98-4160-81cf-fcc64df10b49", "metadata": {}, "outputs": [], @@ -553,7 +549,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 24, "id": "1277a72f-8618-4e53-809f-1ee1f108df19", "metadata": {}, "outputs": [], @@ -563,7 +559,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 25, "id": "9c52fc18-1be2-424a-af1e-9cb8a2548e7a", "metadata": {}, "outputs": [], @@ -574,7 +570,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 26, "id": "b6c23905-4bcf-47ef-b707-431900d818de", "metadata": {}, "outputs": [ @@ -586,113 +582,89 @@ " nodes=[\n", " Node(id=1,\n", " shares_db={\n", - " randomsh_node_1: 26769440234656992175755532478958052042591858765515471507714033651589557161275,\n", - " randomsh_node_2: 101114896275779667892858842630820945984339520389178341454003745582001833565950,\n", - " randomsh_node_3: 65295368212334320364822732577293115580823338745394808319313401203985502047244,\n", - " random_minus_1th_client_x_sh_exp: 21285557804254400517499316140863911282953027731008870766837031342101421986995,\n", - " 1th_client_x_enc_sh_exp: ,\n", - " 1th_client_k_lambda_sh_exp: 64808101058331855648098966258074768495522068627855889258210392032045767117789,\n", - " 1th_client_k_lambda_sh_base: 45914160945743234463835765639491558729542954125290225663180923306814251949178,\n", - " 1th_client_lambda_1_lambda_sh_exp: 110261921268401980199777795756244310703391358903969707276878833519586775656461,\n", - " 1th_client_lambda_1_lambda_sh_base: 108264603285536304734821164728289517992500267790392063330713261312757662392280,\n", - " 1th_client_lambda_2_lambda_sh_exp: 28987819239560065081237892375136555651251977493306094401896942309427202983421,\n", - " 1th_client_lambda_2_lambda_sh_base: 393993427082182993888611869452796442059787351768810467093084945818453065664,\n", - " 1th_client_k_inv_lambda_sh_exp: 50983988152024393114598480691332805034474886596279871084211867029022744926579,\n", - " 1th_client_m_lambda_exp_sh_exp: 59277933116377587085179315064911505668916472307689836192666966490564030729882,\n", - " 1th_client_k_inv_minus_lambda_2_sh_exp: 21996168912464328033360588316196249383222909102973776682314924719595541943158,\n", - " 1th_client_signature_sh_base: 58525477711371260240334972186282972087472544951891672017409127407100153430357,\n", + " randomsh_node_1: 29627232355725091013240817289865842167963979835036529475876283255307412359686,\n", + " randomsh_node_2: 25963599236838826166953673698485233994041097844090884826392497191055486845126,\n", + " randomsh_node_3: 5085385825717084993488103744108010930455719344509659269077663467598091870118,\n", + " random_minus_1th_client_x_sh_exp: 104102972143424312637592319339960633531467443508956898548018721560739758910052,\n", + " 1th_client_x_enc_sh_exp: ,\n", + " 1th_client_lambda_1_enc_sh_base: ,\n", + " 1th_client_lambda_2_enc_sh_base: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", " open_db={\n", - " pow_share_node_1: 72378868255854391565760175650872293318597189536572400527922097502433058411878,\n", - " pow_share_node_2: 45872515933530664101602257524600538219508489031264254994253819321752776775816,\n", - " pow_share_node_3: 29014508172579319062670438431715762317330725523345422883913336857780815595948,\n", - " 1th_client_x_sk: 64217921291331773899722626677277272882064119440032325106726282285652751432437,\n", - " ec_pow_share_node_1: ,\n", - " ec_pow_share_node_2: ,\n", - " ec_pow_share_node_3: ,\n", - " 1th_client_x_pre_pk: ,\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_sk: 106690866310991489943153000702872351126038800689406262426052359671490366155073,\n", - " 1th_client_k_r: 63311173584842841245925798104795648111620391687666830997321134344588083074523,\n", - " 1th_client_k_inv_sk: 107906334498891160288150417749665075810783183991392731713096401950578281812997,\n", - " 1th_client_gap_particle_m: 69322805609917834763653403789850653945489715737259872208282176384775167087218,\n", + " pow_share_node_1: 77585459698290511619005581560983933643482531684361384405850574333446729581121,\n", + " pow_share_node_2: 101170248072912801351305038035809645741125059248060226800088651742369495341193,\n", + " pow_share_node_3: 29638726145587148445851518207237880426392353141993386953028445324801784688348,\n", + " 1th_client_x_sk: 93617153115056104555293226862786799656560420200542199530470755445485628767811,\n", + " ec_pow_share_node_1: ,\n", + " ec_pow_share_node_2: ,\n", + " ec_pow_share_node_3: ,\n", + " 1th_client_x_pre_pk: ,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_sk: 73120756094594951066353272382782475815440099440127161796865621427483707130128,\n", + " 1th_client_k_r: 103282898328945964813915082336989977450474770613981052602078378837302399497399,\n", + " 1th_client_k_inv_sk: 23363410526937945844982742486249051082268605006910294362766598752158597335879,\n", + " 1th_client_gap_particle_m: 28184468823471858655666031129961895000551310994776069004306812339344684524140,\n", " }\n", " )\n", " Node(id=2,\n", " shares_db={\n", - " randomsh_node_1: 72655457485939389274262646238908281476957104058738201607336845534626284859858,\n", - " randomsh_node_2: 55421664265852990662402318574735773655011024051024860751083583489548130800173,\n", - " randomsh_node_3: 44198230652869082014345749838424845600089020503892065861360713945075170694589,\n", - " random_minus_1th_client_x_sh_exp: 17978783689643860262869736622490840897246670992032037432728437473306693866471,\n", - " 1th_client_x_enc_sh_exp: ,\n", - " 1th_client_k_lambda_sh_exp: 109046234982584279344320108345538172606301505649245143407617166312984970462065,\n", - " 1th_client_k_lambda_sh_base: 16041270280715525560014858200313600029015059473186017401036961446635721672877,\n", - " 1th_client_lambda_1_lambda_sh_exp: 14637587812657205465089060314861794398045935656251157915659263317920692221790,\n", - " 1th_client_lambda_1_lambda_sh_base: 26927290265932762621234385455146593833467909324594318258475853624783121420370,\n", - " 1th_client_lambda_2_lambda_sh_exp: 111387604221397619557478295762098362007647046471814770560600604854995628920443,\n", - " 1th_client_lambda_2_lambda_sh_base: 15648644956979592409131495285480833237606061740472442534334164023606449679144,\n", - " 1th_client_k_inv_lambda_sh_exp: 6745854227771969418377338603869400923695449574890616934805092748083541582303,\n", - " 1th_client_m_lambda_exp_sh_exp: 7891733584885236046711721710992393474350486081360540980854170569837150639487,\n", - " 1th_client_k_inv_minus_lambda_2_sh_exp: 11150339216730598623596489791178612446045358327211606716626746954156424706228,\n", - " 1th_client_signature_sh_base: 99130481358755645570913642446332077523645332139473716675869609686163579526448,\n", + " randomsh_node_1: 45025512887139467219637953762932119848907940969897063212583108250102595505289,\n", + " randomsh_node_2: 17822889700333326442607337121277267050028888070229418523978276217489410148502,\n", + " randomsh_node_3: 88114287146652674684842859158045476745526523375406662758498562324354068723128,\n", + " random_minus_1th_client_x_sh_exp: 52721409119209839595237348673705191710742408784963187712638462330064998595094,\n", + " 1th_client_x_enc_sh_exp: ,\n", + " 1th_client_lambda_1_enc_sh_base: ,\n", + " 1th_client_lambda_2_enc_sh_base: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", " open_db={\n", - " pow_share_node_1: 72378868255854391565760175650872293318597189536572400527922097502433058411878,\n", - " pow_share_node_2: 45872515933530664101602257524600538219508489031264254994253819321752776775816,\n", - " pow_share_node_3: 29014508172579319062670438431715762317330725523345422883913336857780815595948,\n", - " 1th_client_x_sk: 64217921291331773899722626677277272882064119440032325106726282285652751432437,\n", - " ec_pow_share_node_1: ,\n", - " ec_pow_share_node_2: ,\n", - " ec_pow_share_node_3: ,\n", - " 1th_client_x_pre_pk: ,\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_sk: 106690866310991489943153000702872351126038800689406262426052359671490366155073,\n", - " 1th_client_k_r: 63311173584842841245925798104795648111620391687666830997321134344588083074523,\n", - " 1th_client_k_inv_sk: 107906334498891160288150417749665075810783183991392731713096401950578281812997,\n", - " 1th_client_gap_particle_m: 69322805609917834763653403789850653945489715737259872208282176384775167087218,\n", + " pow_share_node_1: 77585459698290511619005581560983933643482531684361384405850574333446729581121,\n", + " pow_share_node_2: 101170248072912801351305038035809645741125059248060226800088651742369495341193,\n", + " pow_share_node_3: 29638726145587148445851518207237880426392353141993386953028445324801784688348,\n", + " 1th_client_x_sk: 93617153115056104555293226862786799656560420200542199530470755445485628767811,\n", + " ec_pow_share_node_1: ,\n", + " ec_pow_share_node_2: ,\n", + " ec_pow_share_node_3: ,\n", + " 1th_client_x_pre_pk: ,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_sk: 73120756094594951066353272382782475815440099440127161796865621427483707130128,\n", + " 1th_client_k_r: 103282898328945964813915082336989977450474770613981052602078378837302399497399,\n", + " 1th_client_k_inv_sk: 23363410526937945844982742486249051082268605006910294362766598752158597335879,\n", + " 1th_client_gap_particle_m: 28184468823471858655666031129961895000551310994776069004306812339344684524140,\n", " }\n", " )\n", " Node(id=3,\n", " shares_db={\n", - " randomsh_node_1: 80467104596292784995039830657604396254463789914559555668663681347644787618471,\n", - " randomsh_node_2: 21701464042755607759296886007989738374567587485349163154590663265294001864960,\n", - " randomsh_node_3: 100601572627545979661556637006394789708817365091180024552239177876511754079724,\n", - " random_minus_1th_client_x_sh_exp: 16317407426751689361152032901068957344284849244363366938662721251946077534328,\n", - " 1th_client_x_enc_sh_exp: ,\n", - " 1th_client_k_lambda_sh_exp: 67687894616425587027423848566794623807840017597405412684236998307161151652932,\n", - " 1th_client_k_lambda_sh_base: 27833039705320897040551638431985464923603433345228226403929657914049433624966,\n", - " 1th_client_lambda_1_lambda_sh_exp: 17981540287082509631932088256043972474997713697789597119888467700607069813447,\n", - " 1th_client_lambda_1_lambda_sh_base: 2949600072862649594898126130838370818644756422341793042150301207517336252843,\n", - " 1th_client_lambda_2_lambda_sh_exp: 89970861924037096172137585573787705605377182569231545460797204963324757607552,\n", - " 1th_client_lambda_2_lambda_sh_base: 114532674433452481534518996941820426545049999443331494478889058485967324359656,\n", - " 1th_client_k_inv_lambda_sh_exp: 48104194593930661735273598382612949722156937626730347658185260753907360391436,\n", - " 1th_client_m_lambda_exp_sh_exp: 85669434903508096659355936822838596282837731295195009804125466007768221466379,\n", - " 1th_client_k_inv_minus_lambda_2_sh_exp: 73925421880249814325833459758232817646776710281634562539810314851651114828252,\n", - " 1th_client_signature_sh_base: 26294689748181801575956195524248545264537826091618411177415420384036597551421,\n", + " randomsh_node_1: 11002458067623961891708012362705630845854089850274478403487421455626144153790,\n", + " randomsh_node_2: 85905062359468959738546139800876687133719954224100546087657978100707335060025,\n", + " randomsh_node_3: 80280527474464777048832861873582211493060656065772265219202101717863878170642,\n", + " random_minus_1th_client_x_sh_exp: 68820594591173525847168043888633453541228471884288345699675537773317943214174,\n", + " 1th_client_x_enc_sh_exp: ,\n", + " 1th_client_lambda_1_enc_sh_base: ,\n", + " 1th_client_lambda_2_enc_sh_base: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", " open_db={\n", - " pow_share_node_1: 72378868255854391565760175650872293318597189536572400527922097502433058411878,\n", - " pow_share_node_2: 45872515933530664101602257524600538219508489031264254994253819321752776775816,\n", - " pow_share_node_3: 29014508172579319062670438431715762317330725523345422883913336857780815595948,\n", - " 1th_client_x_sk: 64217921291331773899722626677277272882064119440032325106726282285652751432437,\n", - " ec_pow_share_node_1: ,\n", - " ec_pow_share_node_2: ,\n", - " ec_pow_share_node_3: ,\n", - " 1th_client_x_pre_pk: ,\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_sk: 106690866310991489943153000702872351126038800689406262426052359671490366155073,\n", - " 1th_client_k_r: 63311173584842841245925798104795648111620391687666830997321134344588083074523,\n", - " 1th_client_k_inv_sk: 107906334498891160288150417749665075810783183991392731713096401950578281812997,\n", - " 1th_client_gap_particle_m: 69322805609917834763653403789850653945489715737259872208282176384775167087218,\n", + " pow_share_node_1: 77585459698290511619005581560983933643482531684361384405850574333446729581121,\n", + " pow_share_node_2: 101170248072912801351305038035809645741125059248060226800088651742369495341193,\n", + " pow_share_node_3: 29638726145587148445851518207237880426392353141993386953028445324801784688348,\n", + " 1th_client_x_sk: 93617153115056104555293226862786799656560420200542199530470755445485628767811,\n", + " ec_pow_share_node_1: ,\n", + " ec_pow_share_node_2: ,\n", + " ec_pow_share_node_3: ,\n", + " 1th_client_x_pre_pk: ,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_sk: 73120756094594951066353272382782475815440099440127161796865621427483707130128,\n", + " 1th_client_k_r: 103282898328945964813915082336989977450474770613981052602078378837302399497399,\n", + " 1th_client_k_inv_sk: 23363410526937945844982742486249051082268605006910294362766598752158597335879,\n", + " 1th_client_gap_particle_m: 28184468823471858655666031129961895000551310994776069004306812339344684524140,\n", " }\n", " )\n", " ]\n", @@ -700,27 +672,26 @@ " clients=[\n", " Client(id=1,\n", " shares_db={\n", - " 1th_client_m_lambda_exp_sh_exp_node_1: 59277933116377587085179315064911505668916472307689836192666966490564030729882,\n", - " 1th_client_m_lambda_exp_sh_exp_node_2: 7891733584885236046711721710992393474350486081360540980854170569837150639487,\n", - " 1th_client_m_lambda_exp_sh_exp_node_3: 85669434903508096659355936822838596282837731295195009804125466007768221466379,\n", - " m_lambda_exp: 37047012394414671028549526649334921896107734460109626635224344007100890791380,\n", - " 1th_client_gap_lambda_enc_sh_exp_node_1: ,\n", - " 1th_client_gap_lambda_enc_sh_exp_node_2: ,\n", - " 1th_client_gap_lambda_enc_sh_exp_node_3: ,\n", - " gap_lambda_exp: 14593568459178450201196623351708437273742502624114583187528081246402839799107,\n", - " 1th_client_signature_sh_base_node_1: 58525477711371260240334972186282972087472544951891672017409127407100153430357,\n", - " 1th_client_signature_sh_base_node_2: 99130481358755645570913642446332077523645332139473716675869609686163579526448,\n", - " 1th_client_signature_sh_base_node_3: 26294689748181801575956195524248545264537826091618411177415420384036597551421,\n", - " sig_gap: 68158559607952458624507363207456021345658747958848039528271898416231818463857,\n", + " 1th_client_gap_lambda_enc_sh_exp_node_1: ,\n", + " 1th_client_gap_lambda_enc_sh_exp_node_2: ,\n", + " 1th_client_gap_lambda_enc_sh_exp_node_3: ,\n", + " 1th_client_m_lambda_enc_sh_exp_node_1: ,\n", + " 1th_client_m_lambda_enc_sh_exp_node_2: ,\n", + " 1th_client_m_lambda_enc_sh_exp_node_3: ,\n", + " gap_lambda_exp: 38525050769264943183414988907017306198319648651950028815882719172642913819527,\n", + " m_lambda_exp: 109320655523612717441723315123948469295660792864445628424954220295616298713073,\n", + " 1th_client_enc_signature_sh_base_node_1: ,\n", + " 1th_client_enc_signature_sh_base_node_2: ,\n", + " 1th_client_enc_signature_sh_base_node_3: ,\n", " },\n", " public_keys={\n", - " 0: ,\n", + " 0: ,\n", " },\n", - " private_keys=>,\n", + " private_keys=>,\n", " open_db={\n", - " 1th_client_x_pk: ,\n", - " 1th_client_k_r: 63311173584842841245925798104795648111620391687666830997321134344588083074523,\n", - " 1th_client_s: 63833539223658105135053461039650923566058911866233596880424355717202875082925,\n", + " 1th_client_x_pk: ,\n", + " 1th_client_k_r: 103282898328945964813915082336989977450474770613981052602078378837302399497399,\n", + " 1th_client_s: 93165829290385224446235210611087967890935211267183012775935192826938233151942,\n", " 1th_client_message: Stay tunned. Join the Telegram chat. Join the Discord channel. Be happy.,\n", " }\n", " )\n", diff --git a/docs/_source/tecdsa.rst b/docs/_source/tinysig.rst similarity index 59% rename from docs/_source/tecdsa.rst rename to docs/_source/tinysig.rst index ef57dce..746cdd2 100644 --- a/docs/_source/tecdsa.rst +++ b/docs/_source/tinysig.rst @@ -1,26 +1,26 @@ -tecdsa module -============= +tinysig module +============== -.. automodule:: tecdsa.network +.. automodule:: tinysig.network :members: :undoc-members: :show-inheritance: -.. automodule:: tecdsa.setup +.. automodule:: tinysig.setup :members: :undoc-members: :show-inheritance: -.. automodule:: tecdsa.tecdsa +.. automodule:: tinysig.tecdsa :members: :undoc-members: :show-inheritance: -.. automodule:: tecdsa.utils +.. automodule:: tinysig.utils :members: :undoc-members: :show-inheritance: diff --git a/docs/index.rst b/docs/index.rst index 725c23d..453eb51 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,4 +1,4 @@ -.. tecdsa documentation master file, created by +.. tinysig documentation master file, created by sphinx-quickstart on Sat Nov 4 13:05:19 2023. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. diff --git a/docs/toc.rst b/docs/toc.rst index 56030bd..3942776 100644 --- a/docs/toc.rst +++ b/docs/toc.rst @@ -2,4 +2,4 @@ :maxdepth: 4 :caption: Contents: - _source/tecdsa \ No newline at end of file + _source/tinysig \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index bf20ff9..e322856 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [project] -name = "tecdsa" +name = "tinysig" version = "0.1.0" description = """\ Pure-Python implementation of a threshold ecdsa signature scheme \ @@ -26,8 +26,8 @@ dependencies = [ ] [project.urls] -Repository = "https://github.com/nillion-oss/tecdsa" -Documentation = "https://tecdsa.readthedocs.io" +Repository = "https://github.com/nillion-oss/tinysig" +Documentation = "https://tinysig.readthedocs.io" [project.optional-dependencies] docs = [ @@ -58,4 +58,4 @@ requires = [ build-backend = "setuptools.build_meta" [tool.pytest.ini_options] -addopts = "--doctest-modules --ignore=docs --cov=tecdsa --cov-report term-missing" \ No newline at end of file +addopts = "--doctest-modules --ignore=docs --cov=tinysig --cov-report term-missing" \ No newline at end of file diff --git a/src/tecdsa/__init__.py b/src/tecdsa/__init__.py deleted file mode 100644 index 04eb6b9..0000000 --- a/src/tecdsa/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -"""Allow the users to access the class and functions directly""" -from tecdsa.tecdsa import ThresholdSignature -from tecdsa.setup import DSASetup, ECDSASetup -from tecdsa.utils import verify_ecdsa_signature, verify_dsa_signature diff --git a/src/tinysig/__init__.py b/src/tinysig/__init__.py new file mode 100644 index 0000000..e4487aa --- /dev/null +++ b/src/tinysig/__init__.py @@ -0,0 +1,4 @@ +"""Allow the users to access the class and functions directly""" +from tinysig.tecdsa import ThresholdSignature +from tinysig.setup import DSASetup, ECDSASetup +from tinysig.utils import verify_ecdsa_signature, verify_dsa_signature diff --git a/src/tecdsa/network.py b/src/tinysig/network.py similarity index 100% rename from src/tecdsa/network.py rename to src/tinysig/network.py diff --git a/src/tecdsa/setup.py b/src/tinysig/setup.py similarity index 100% rename from src/tecdsa/setup.py rename to src/tinysig/setup.py diff --git a/src/tecdsa/tecdsa.py b/src/tinysig/tecdsa.py similarity index 77% rename from src/tecdsa/tecdsa.py rename to src/tinysig/tecdsa.py index e939ce8..4ffa484 100644 --- a/src/tecdsa/tecdsa.py +++ b/src/tinysig/tecdsa.py @@ -432,38 +432,78 @@ def invert_masked_factor_local(self, label) -> None: sh_inv_label = label+"_inv_lambda_sh_exp" node.set_share(inv_share, sh_inv_label) - def encrypt_and_add_to_sk_local( - self, - label: str, - save_label: str, - client_id: int, - delete=True + def step_4_encrypt_elements( + self, + label_lambda_1: str, + label_lambda_2: str, + labdel_lambda_k_inv: str, + save_label_m: str, + save_label_gap: str, + save_label_lambda_1: str, + save_label_lambda_2: str, + client_id: int ) -> None: """ - Encrypt share and add ecrypted value to the encrypted share secret key blinding exponent. + Step 4 of the Threshold Signing protocol. Parameters: - label (str): The label of the share to be encrypted. - save_label (str): The label used to save the result of the encrypted addition. - client_id (int): The unique identifier for the client. + label (str): The label of the masked factor to be inverted. + label_lambda_1 (str): The label of lambda 1. + label_lambda_2 (str): The label of lambda 2. + labdel_lambda_k_inv (str): The label of :math:`k^{-1}`. + save_label_m (str): The label to save encrypted m. + save_label_gap (str): The label to save :math:`\lambda_{\text{gap}}`. + save_label_lambda_1 (str): The label to save lambda 1. + save_label_lambda_2 (str): The label to save lambda 2. + client_id: int Returns: None """ - + + q_minus_one = self.q - 1 for node in self.nodes: # DB management - clear_share = node.get_share(label+"_sh_exp") + sh_lambda_1_exp = node.get_share(label_lambda_1 +"_sh_exp") + sh_lambda_2_exp = node.get_share(label_lambda_2 +"_sh_exp") + sh_lambda_k_inv = node.get_share(labdel_lambda_k_inv +"_sh_exp") + sh_lambda_1_base = node.get_share(label_lambda_1 +"_sh_base") + sh_lambda_2_base = node.get_share(label_lambda_2 +"_sh_base") enc_lambda_sk = node.get_share(str(client_id)+"th_client_x_enc_sh_exp") # Local operation - ## Encrypt value from label - encrypted_share_value = node.he_public_keys[client_id - 1].encrypt(clear_share) - ## Add encrypted values - enc_gap_value = encrypted_share_value + enc_lambda_sk + ## 4(a) + sh_m = (sh_lambda_1_exp - sh_lambda_k_inv) % q_minus_one + enc_sh_m = node.he_public_keys[client_id - 1].encrypt(sh_m) + ## 4(b) + sh_int_gap = (sh_lambda_k_inv - sh_lambda_2_exp) % q_minus_one + enc_sh_int_gap = node.he_public_keys[client_id - 1].encrypt(sh_int_gap) + enc_sh_gap = enc_sh_int_gap + enc_lambda_sk + ## 4(c) + enc_sh_lambda_1_base = node.he_public_keys[client_id - 1].encrypt(sh_lambda_1_base) + enc_sh_lambda_2_base= node.he_public_keys[client_id - 1].encrypt(sh_lambda_2_base) # DB management - enc_gap_label = str(client_id)+"th_client_"+save_label+"_enc_sh_exp" - node.set_share(enc_gap_value, enc_gap_label) - node.delete_share(label+"_sh_exp") if delete else None + node.set_share(enc_sh_m, save_label_m+"_sh_exp") + node.set_share(enc_sh_gap, save_label_gap+"_sh_exp") + node.set_share(enc_sh_lambda_1_base, save_label_lambda_1+"_sh_base") + node.set_share(enc_sh_lambda_2_base, save_label_lambda_2+"_sh_base") + + + def delete_shares(self, list: List) -> None: + """ + Delete a set of shares. + + Parameters: + list (List): List of shares to delete. + + Returns: + None + """ + + for node in self.nodes: + for element in list: + node.delete_share(element) + + def decrypt_and_reconstruct_local( self, @@ -491,7 +531,7 @@ def decrypt_and_reconstruct_local( dec_sh_per_node = [client.he_private_key.decrypt(enc_sh) for enc_sh in enc_sh_per_node] q_minus_one = self.q - 1 ## Reconstruct and take the symmetric value - dec_val = -add(dec_sh_per_node, q_minus_one) % q_minus_one + dec_val = add(dec_sh_per_node, q_minus_one) # DB management dec_label = save_label + "_exp" client.set_share(dec_val, dec_label) @@ -526,7 +566,7 @@ def ts_prep_protocol(self, client_id): except KeyError: print(f"Public key triple (, y, Enc([\lambda_x])) from DKG is not complete for client {client_id}. Generate it first using 'distributed_key_generation_protocol({client_id})'") - # Client independent preprocessing + # Signers preprocessing # Step 1 label_k = str(client_id)+"th_client_k" label_lambda_1 = str(client_id)+"th_client_lambda_1" @@ -534,32 +574,47 @@ def ts_prep_protocol(self, client_id): self.get_lambda([label_k, label_lambda_1, label_lambda_2]) # Step 2 self.key_agreement_protocol(label_k) - # Step 3 + # Step 3(a): set r self.compute_r_local(label_k, client) - # Step 4: invert k + # Step 3(b): invert k self.invert_masked_factor_local(label_k) - # Step 5: compute m share - self.subtract_exp_shares_local(label_lambda_1 + "_lambda", label_k + "_inv_lambda", str(client_id)+"th_client_m_lambda_exp") - - # Client dependent preprocessing - # Step 6: reveal to client - get_label = str(client_id)+"th_client_m_lambda_exp" - save_label_m = "m_lambda_exp" - type_share = "exp" - self.reveal(type_share, get_label, save_label_m, client) - # Step 7: encrypt and share to client - ## Compute difference lambda_inv_k - lambda_2 - self.subtract_exp_shares_local(label_k + "_inv_lambda", label_lambda_2 + "_lambda", str(client_id)+"th_client_k_inv_minus_lambda_2") - ## Encrypt the difference and add to the encrypted secret key lambda + # Step 4: encrypt + self.step_4_encrypt_elements( + label_lambda_1 + "_lambda", + label_lambda_2 + "_lambda", + label_k + "_inv_lambda", + str(client_id)+"th_client_m_lambda_enc", + str(client_id)+"th_client_gap_lambda_enc", + str(client_id)+"th_client_lambda_1_enc" , + str(client_id)+"th_client_lambda_2_enc" , + client_id) + # Step 5: delete + self.delete_shares([ + str(client_id)+"th_client_k_lambda_sh_exp", + str(client_id)+"th_client_k_lambda_sh_base", + str(client_id)+"th_client_lambda_1_lambda_sh_exp", + str(client_id)+"th_client_lambda_1_lambda_sh_base", + str(client_id)+"th_client_lambda_2_lambda_sh_exp", + str(client_id)+"th_client_lambda_2_lambda_sh_base", + str(client_id)+"th_client_k_inv_lambda_sh_exp", + ]) + + # Client preprocessing + + # Step 6: send encryption label_gap = "gap_lambda" - delete = not self.debug - self.encrypt_and_add_to_sk_local(str(client_id)+"th_client_k_inv_minus_lambda_2", label_gap, client_id, delete=delete) - ## Send it to the client - label_send_gap = str(client_id)+"th_client_"+label_gap+"_enc" + label_send_gap = str(client_id)+"th_client_"+ label_gap +"_enc" + label_m = "m_lambda" + label_send_m = str(client_id)+"th_client_"+ label_m +"_enc" type_share = "exp" self.send(type_share, label_send_gap, client, delete=True) - # Step 8: client decrypts and reconstructs + self.send(type_share, label_send_m, client, delete=True) + # Step 7: client decrypts and reconstructs self.decrypt_and_reconstruct_local(label_send_gap, label_gap, client) + self.decrypt_and_reconstruct_local(label_send_m, label_m, client) + + + def broadcast_masked_message_digest(self, message: str, client: Client) -> None: """ @@ -581,8 +636,8 @@ def broadcast_masked_message_digest(self, message: str, client: Client) -> None: message_digest = SHA256.new(data=message.encode("utf-8")) m = int(message_digest.hexdigest(), 16) % self.q ## Compute gap particle - particle = m * pow(self.h, -m_lambda_exp, self.q) % self.q - gap_particle = particle * pow(self.h, gap_lambda_exp, self.q) % self.q + minus_m_plus_gap = (-(m_lambda_exp + gap_lambda_exp)) % (self.q - 1) + gap_particle = (m * pow(self.h, minus_m_plus_gap, self.q)) % self.q # Broadcast self.broadcast(gap_particle, str(client.id)+"th_client_gap_particle_m") @@ -601,29 +656,19 @@ def sign_local(self, client_id: int, delete=True): for node in self.nodes: # DB management - sh_lambda_1 = node.get_share(str(client_id)+"th_client_lambda_1_lambda_sh_base") - sh_lambda_2 = node.get_share(str(client_id)+"th_client_lambda_2_lambda_sh_base") + enc_sh_lambda_1 = node.get_share(str(client_id)+"th_client_lambda_1_enc_sh_base") + enc_sh_lambda_2 = node.get_share(str(client_id)+"th_client_lambda_2_enc_sh_base") p_k_inv = node.get_open(str(client_id)+"th_client_k_inv_sk") p_x = node.get_open(str(client_id)+"th_client_x_sk") p_r = node.get_open(str(client_id)+"th_client_k_r") p_gap_m = node.get_open(str(client_id)+"th_client_gap_particle_m") # Local operation - s_h_gap_left = (sh_lambda_1 * p_k_inv) % q - s_h_gap_left = (s_h_gap_left * p_gap_m) % q - s_h_gap_right = (sh_lambda_2 * p_k_inv) % q - s_h_gap_right = (s_h_gap_right * p_r) % q - s_h_gap_right = (s_h_gap_right * p_x) % q - s_h_gap = (s_h_gap_left + s_h_gap_right) % q + scalar_k_m = (p_k_inv * p_gap_m) % q + scalar_k_r_x = (((p_k_inv * p_r) % q) * p_x) % q + enc_sh_s_gap = enc_sh_lambda_1 * scalar_k_m + enc_sh_lambda_2 * scalar_k_r_x # DB management - node.set_share(s_h_gap, str(client_id)+"th_client_signature_sh_base") + node.set_share(enc_sh_s_gap, str(client_id)+"th_client_enc_signature_sh_base") if delete: - node.delete_share(str(client_id)+"th_client_lambda_1_lambda_sh_base") - node.delete_share(str(client_id)+"th_client_lambda_1_lambda_sh_exp") - node.delete_share(str(client_id)+"th_client_lambda_2_lambda_sh_base") - node.delete_share(str(client_id)+"th_client_lambda_2_lambda_sh_exp") - node.delete_share(str(client_id)+"th_client_k_lambda_sh_exp") - node.delete_share(str(client_id)+"th_client_k_lambda_sh_base") - node.delete_share(str(client_id)+"th_client_k_inv_lambda_sh_exp") node.delete_open(str(client_id)+"th_client_k_sk") def reconstruct_and_verify_sig(self, message: str, get_label: str, client: Client, delete=True): @@ -652,7 +697,50 @@ def reconstruct_and_verify_sig(self, message: str, get_label: str, client: Clien r = client.get_open(str(client.id)+"th_client_k_r") s_h_gap = client.get_share(get_label) # Compute signature - s = (s_h_gap * pow(self.h, -gap_lambda_exp, self.q)) % self.q + s = (s_h_gap * pow(self.h, gap_lambda_exp, self.q)) % self.q + # Verify signature + verify_dsa_signature(message, r, s, y, p, q, g) if self.setup == DSASetup else verify_ecdsa_signature(message, r, s, y, q, G) + # DB management + signature_label = str(client.id)+"th_client_s" + client.set_open(s, signature_label) + message_label = str(client.id)+"th_client_message" + client.set_open(message, message_label) + + def decrypt_reconstruct_unmask_verify_sig_local(self, message: str, get_label: str, client: Client, delete=True): + """ + Reconstructs and verifies a client's digital signature for a given message. + + Parameters: + message (str): The input message for which the signature is to be reconstructed and verified. + get_label (str): The label of the shares to be dencrypted and reconstructed. + client (Client): An instance of the client for which the signature is reconstructed and verified. + delete (bool, optional): A flag indicating whether to delete intermediate shares after verification (default is True). + + Returns: + None: This function doesn't return a value; it verifies the signature and potentially deletes intermediate shares. + """ + q = self.q + if self.setup == DSASetup: + p = self.dsa.p + g = self.dsa.g + else: + G = self.ecdsa.G + + + # DB management + enc_sh_per_node = [client.get_share(str(client.id)+"th_client_"+get_label+"_sh_base_node_"+str(node.id)) for node in self.nodes] + gap_lambda_exp = client.get_share("gap_lambda_exp") + y = client.get_open(str(client.id)+"th_client_x_pk") + r = client.get_open(str(client.id)+"th_client_k_r") + + # Local operation + ## Decrypt + dec_sh_per_node = [client.he_private_key.decrypt(enc_sh) for enc_sh in enc_sh_per_node] + q_minus_one = self.q - 1 + ## Reconstruct + s_h_gap = add(dec_sh_per_node, q) + ## Unmask + s = (s_h_gap * pow(self.h, gap_lambda_exp, q)) % q # Verify signature verify_dsa_signature(message, r, s, y, p, q, g) if self.setup == DSASetup else verify_ecdsa_signature(message, r, s, y, q, G) # DB management @@ -690,22 +778,20 @@ def ts_online_protocol(self, message: str, client_id: int) -> None: print(f"The preprocessing phase was not run for client {client_id}.") - # Step 11: compute digest, mask it, include gap and broadcast the result to all nodes + # Step 8: compute digest, mask it, include gap and broadcast the result to all nodes self.broadcast_masked_message_digest(message, client) - - # Step 12a: all nodes compute locally the shares corresponding to clients + + # Step 9a: all nodes compute locally the shares corresponding to clients delete = not self.debug self.sign_local(client_id, delete=delete) - # Step 12b: reveal to client - get_label = str(client_id)+"th_client_signature" - save_label_m = "sig_gap" + # Step 9b: send encryption + label_enc_sig = "enc_signature" + label_send_enc_sig = str(client_id)+"th_client_" + label_enc_sig type_share = "base" - self.reveal(type_share, get_label, save_label_m, client) - - # Step 13 verify - get_label = "sig_gap" - self.reconstruct_and_verify_sig(message, get_label, client) + self.send(type_share, label_send_enc_sig, client, delete=True) + # Step 10: client decrypts, reconstructs, unmasks and verifies signature + self.decrypt_reconstruct_unmask_verify_sig_local(message, label_enc_sig, client) def print_signature(self, client_id: int) -> None: diff --git a/src/tecdsa/utils.py b/src/tinysig/utils.py similarity index 100% rename from src/tecdsa/utils.py rename to src/tinysig/utils.py diff --git a/tests/test_tbecdsa.py b/tests/test_tecdsa.py similarity index 98% rename from tests/test_tbecdsa.py rename to tests/test_tecdsa.py index d3536af..2fa0673 100644 --- a/tests/test_tbecdsa.py +++ b/tests/test_tecdsa.py @@ -2,9 +2,9 @@ from random import seed import time -from tecdsa.utils import add, verify_ecdsa_signature -from tecdsa.setup import DSASetup, ECDSASetup -from tecdsa.tecdsa import ThresholdSignature +from tinysig.utils import add, verify_ecdsa_signature +from tinysig.setup import DSASetup, ECDSASetup +from tinysig.tecdsa import ThresholdSignature import unittest