{"id":24701946,"url":"https://github.com/natharyan/secret-sharing-arithmetic-functions","last_synced_at":"2025-03-22T04:13:45.503Z","repository":{"id":270253324,"uuid":"909768695","full_name":"natharyan/Secret-Sharing-Arithmetic-Functions","owner":"natharyan","description":"Shamir's secret sharing with beaver's multiplication to generate shares of an arbitrary arithmetic function given by the user with two secret values as the variables","archived":false,"fork":false,"pushed_at":"2025-01-21T18:27:25.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-21T18:43:03.509Z","etag":null,"topics":["cryptography","cryptography-project","secret-sharing","shamir-secret-sharing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/natharyan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-29T18:08:05.000Z","updated_at":"2025-01-21T18:27:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"82f7566c-e1f2-43ba-a9e1-14cd43ea50f3","html_url":"https://github.com/natharyan/Secret-Sharing-Arithmetic-Functions","commit_stats":null,"previous_names":["natharyan/secret-sharing-arbitrary-numeric-functions","natharyan/secret-sharing-arbitrary-arithmetic-functions","natharyan/secret-sharing-arithmetic-functions"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natharyan%2FSecret-Sharing-Arithmetic-Functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natharyan%2FSecret-Sharing-Arithmetic-Functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natharyan%2FSecret-Sharing-Arithmetic-Functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natharyan%2FSecret-Sharing-Arithmetic-Functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/natharyan","download_url":"https://codeload.github.com/natharyan/Secret-Sharing-Arithmetic-Functions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244902918,"owners_count":20529115,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","cryptography-project","secret-sharing","shamir-secret-sharing"],"created_at":"2025-01-27T05:30:14.301Z","updated_at":"2025-03-22T04:13:45.485Z","avatar_url":"https://github.com/natharyan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shamir's Secret Sharing for Arbitrary Arithmetic Functions\n\nThis project implements Shamir's Secret Sharing and integrates Beaver's Multiplication to generate and compute shares for an arbitrary arithmetic function defined by the user. It is designed to explore the theoretical foundations of secret sharing and multi-party computation.\n\n\u003e **Note:** This implementation is intended for theoretical exploration and is not fully secure for real-world applications. It does not use client-server architectures, secure communication protocols, or other measures necessary for production-level security.\n\n\u003cimg width=\"1236\" alt=\"output\" src=\"https://github.com/user-attachments/assets/62a022de-2846-4efd-a4f8-8320b6fcfe7f\" /\u003e\n\n---\n\n## Introduction\n\nShamir's Secret Sharing is a cryptographic technique that divides a secret into multiple shares, requiring a threshold number of shares to reconstruct the original secret. This project extends Shamir's scheme by incorporating Beaver's Multiplication, enabling the computation of shares for arbitrary arithmetic expressions involving two secret values.\n\nThe primary goals of this project are:\n- To explore the mathematics behind secret sharing schemes.\n- To implement theoretical concepts like Beaver's Multiplication and Lagrange interpolation in Python.\n\n---\n\n## File Descriptions\n\n### main.py\n\nThe main file orchestrates the program by:\n- Accepting user inputs for two secrets (`x` and `y`), their thresholds, and the number of shares.\n- Parsing and evaluating an arithmetic expression involving the secrets.\n- Displaying the computed result as shares and recovering the secret using Lagrange interpolation.\n\nKey functions:\n- `shunting_yard_postfix(infix)`: Converts an infix arithmetic expression to postfix notation.\n- `build_parsetree(expression)`: Constructs a binary parse tree for the arithmetic expression.\n- `evaluate_tree(node)`: Recursively evaluates the parse tree using secret sharing operations.\n\n### secret_sharing.py\n\nThis file contains:\n\n#### **Shamir Class**\n\nImplements Shamir's Secret Sharing:\n- **`split_secret(secret)`**: Splits a secret into multiple shares based on a randomly generated polynomial.\n  \n  **Mathematics:**\n  ```math\n  f(x) = a_0 + a_1x + a_2x^2 + \\ldots + a_{t-1}x^{t-1} \\mod p\n  \nwhere $$\\(a_0\\)$$ is the secret. Shares are points $$(x_i, f(x_i))$$.\n\n- **`recover_secret(shares, threshold)`**: Recovers the secret using Lagrange interpolation.\n  \n  **Mathematics:**\n  \n  Lagrange basis polynomial for \\(x\\):\n  ```math\n  L_i(x) = \\prod_{j \\neq i} \\frac{x - x_j}{x_i - x_j} \\mod p\n  ```\n  Secret (constant term):\n  ```math\n  f(0) = \\sum_{i=0}^{t-1} y_i \\cdot L_i(0) \\mod p\n  ```\n\n#### **Operations Class**\n\nProvides methods for arithmetic operations on shares:\n- **Addition**: Adds two shares or a share with a public value.\n- **Multiplication**: Implements Beaver's Multiplication for secure multiplication of two secrets without revealing them.\n\n### utils.py\n\nContains utility functions for mathematical operations:\n- **`_lagrange_interpolation_evaluate_at_x(threshold, x_eval, x_s, y_s, prime)`**:\n  Efficiently evaluates the Lagrange polynomial at \\(x = x_{\\text{eval}}\\).\n\n- **`_lagrange_interpolation(threshold, x_s, y_s, prime)`**:\n  Constructs the Lagrange polynomial as a symbolic expression.\n\n- **`_divmod(num, denom, prime)`**:\n  Computes modular division using the extended Euclidean algorithm.\n\n---\n\n## Theory\n\n### Shamir's Secret Sharing\n\nShamir's Secret Sharing is a \\((t, n)\\)-threshold scheme where:\n- \\(t\\): Minimum number of shares required to reconstruct the secret.\n- \\(n\\): Total number of shares distributed.\n\nA polynomial of degree \\(t-1\\) is used to encode the secret, ensuring that any \\(t\\) shares can recover the secret but fewer than \\(t\\) shares cannot.\n\n### Beaver's Multiplication\n\nBeaver's Multiplication enables the secure multiplication of two secrets \\(x\\) and \\(y\\) without revealing them. It uses a precomputed random triple \\(([a], [b], [ab])\\), where \\([ab] = a \\cdot b \\mod p\\). The multiplication works as follows:\n\n1. Compute masked values:\n   ```math\n   x' = x + a \\quad \\text{and} \\quad y' = y + b\n   ```\n\n2. The masked values \\(x'\\) and \\(y'\\) are revealed to all parties. These reveal no information about \\(x\\) or \\(y\\), as \\(a\\) and \\(b\\) are random and independent.\n\n3. After revealing \\(x'\\) and \\(y'\\), the parties compute:\n   ```math\n   [xy] = (x' \\cdot y') - (x' \\cdot [b]) - (y' \\cdot [a]) + [ab]\n   ```\n\n4. Substituting the values:\n   ```math\n   [xy] = [(x + a) \\cdot (y + b)] - [(x + a) \\cdot b] - [(y + b) \\cdot a] + ab\n   ```\n\n   Expanding and simplifying:\n   ```math\n   [xy] = x \\cdot y + a \\cdot y + b \\cdot x + ab - (x \\cdot b + a \\cdot b + b \\cdot a + ab) + ab\n   ```\n\n   ```math\n   [xy] = x \\cdot y\n   ```\n\nThis computation is affine-linear with respect to the secret values, meaning it can be securely implemented within a linear secret-sharing scheme.\n\n### Parse Tree Construction and Evaluation\n\nArithmetic expressions are parsed into a binary tree:\n- Leaf nodes: Operands (public values or shares).\n- Internal nodes: Operators (+, -, *, ^).\n\nEvaluation combines operations on shares, preserving the security of secrets.\n\n---\n\n## Usage\n\n1. Clone the repository.\n2. Run `python src`.\n3. Enter secrets, thresholds, and an arithmetic expression.\n4. View the computed result as shares and the recovered secret.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatharyan%2Fsecret-sharing-arithmetic-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnatharyan%2Fsecret-sharing-arithmetic-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatharyan%2Fsecret-sharing-arithmetic-functions/lists"}