Skip to content

Add Shamir secret sharing helpers for scalar polynomials#214

Merged
LLFourn merged 1 commit intomasterfrom
add-shamir-secret-sharing
Jun 18, 2025
Merged

Add Shamir secret sharing helpers for scalar polynomials#214
LLFourn merged 1 commit intomasterfrom
add-shamir-secret-sharing

Conversation

@LLFourn
Copy link
Owner

@LLFourn LLFourn commented Jun 18, 2025

Summary

This PR makes creating and restoring secret backups easier by adding Shamir secret sharing functionality to the polynomial module.

Changes

  • Add generate_shamir_sharing_poly to create polynomial with secret at x=0
  • Add trusted_dealer_shamir_sharing that returns an iterator of shares
  • Update interpolate_and_eval_poly_at_0 docs to clarify it reconstructs secrets
  • Add comprehensive tests including proptest for random testing

Example Usage

use secp256kfun::{poly, prelude::*};
use rand::seq::SliceRandom;

let my_secret = s\!(42);
let shares: Vec<_> = poly::scalar::trusted_dealer_shamir_sharing(my_secret, 3, 5, &mut rand::thread_rng()).collect();

// Sample 3 random shares (threshold amount) to reconstruct the secret
let mut rng = rand::thread_rng();
let random_shares: Vec<_> = shares.choose_multiple(&mut rng, 3).cloned().collect();

let recovered_secret = poly::scalar::interpolate_and_eval_poly_at_0(&random_shares);
assert_eq\!(recovered_secret, my_secret);

Abridged conversation transcript

Human: Ok can you review the currently staged changes.

Assistant: [Reviews staged changes showing new Shamir secret sharing functions: generate_shamir_sharing_poly, trusted_dealer_shamir_sharing, and updated docs for interpolate_and_eval_poly_at_0]

Human: I think from trusted_dealer_shamir_sharing we can just return an impl Iterator

Assistant: You're right! [Modifies function to return iterator instead of Vec]

Human: cool before making a PR to fix that can you make a quick PR to fix that format warning. about format_code_in_doc_comments.

Assistant: [Creates PR #213 to remove format_code_in_doc_comments from rustfmt.toml]

Human: Before we do that can you make a proptest that tests doing trusted_dealer_shamir_sharing and then reconstruction.

Assistant: [Creates comprehensive proptest in tests/poly.rs that tests reconstruction with random secrets, various thresholds, and different numbers of shares]

Human: oh I wonder what the type parameter for trusted dealer sharing is actually there for.

Assistant: [Examines code and finds unused type parameter Z: ZeroChoice that was declared but never used in the function]

Human: I think remove it

Assistant: [Removes unused type parameter from function signature and updates tests]

Human: cool. Make a PR. Attach a summary of this conversation to it. Note at the beginning that the purpose was to make creating and restoring secret backups easier to work on.

🤖 Generated with Claude Code

This PR makes creating and restoring secret backups easier to work on
by adding convenient Shamir secret sharing functionality.

Changes:
- Add `generate_shamir_sharing_poly` to create polynomial with secret at x=0
- Add `trusted_dealer_shamir_sharing` that returns an iterator of shares
- Update `interpolate_and_eval_poly_at_0` docs to clarify it reconstructs secrets
- Add comprehensive tests including proptest for random testing

The trusted_dealer_shamir_sharing function splits a secret into n shares
with a threshold, returning (share_index, share_value) pairs as an iterator
for memory efficiency.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@LLFourn LLFourn merged commit 5730669 into master Jun 18, 2025
15 checks passed
@LLFourn LLFourn deleted the add-shamir-secret-sharing branch June 18, 2025 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant