Swap 2 Secrets via Homomorphic Properties of Shamir Secret Sharing

The Description of the problem

I have 2 secrets denoted as s 1 , s 2 s_1, s_2 s1,s2 and they are in different vector with same dimensions. Now all the vector are encrypted by the Shamir Secret Sharing. What is I really want is to swap the s 1 , s 2 s_1, s_2 s1,s2 in their shares format via the homomorphic properties of Secret Sharing. I need you give me a python implementation.
To accomplish the task of swapping the secrets s 1 s_1 s1 and s 2 s_2 s2 without revealing them, you can use the additive homomorphic properties of Shamir’s Secret Sharing. I’ll provide a Python implementation using the secretsharing library:

The Solution from GPT-4

  1. Install the secretsharing library if you haven’t already:
pip install secret-sharing
  1. Here’s a Python implementation that demonstrates swapping the secrets s 1 s_1 s1 and s 2 s_2 s2 in their shares format:
import random
from secretsharing import PlaintextToHexSecretSharer as Sharer

def generate_shares(secret, n_shares, threshold):
    shares = Sharer.split_secret(secret, threshold, n_shares)
    return shares

def swap_shares(shares_s1, shares_s2):
    swapped_shares_s1 = []
    swapped_shares_s2 = []

    for share_s1, share_s2 in zip(shares_s1, shares_s2):
        int_share_s1 = int(share_s1.split('-')[1], 16)
        int_share_s2 = int(share_s2.split('-')[1], 16)

        # Swap the secrets using additive homomorphism
        swapped_int_share_s1 = (int_share_s1 - int_share_s2) % Sharer.prime
        swapped_int_share_s2 = (int_share_s2 + int_share_s1) % Sharer.prime
        
        swapped_share_s1 = share_s1.split('-')[0] + '-' + format(swapped_int_share_s1, 'x')
        swapped_share_s2 = share_s2.split('-')[0] + '-' + format(swapped_int_share_s2, 'x')

        swapped_shares_s1.append(swapped_share_s1)
        swapped_shares_s2.append(swapped_share_s2)

    return swapped_shares_s1, swapped_shares_s2

def recover_secret(shares):
    secret = Sharer.recover_secret(shares)
    return secret

def main():
    s1 = hex(random.randint(1, 1_000_000))[2:]  # Example secret 1
    s2 = hex(random.randint(1, 1_000_000))[2:]  # Example secret 2
    n_shares = 5
    threshold = 3

    shares_s1 = generate_shares(s1, n_shares, threshold)
    shares_s2 = generate_shares(s2, n_shares, threshold)

    print(f"Original shares:\nSecret 1: {shares_s1}\nSecret 2: {shares_s2}")

    swapped_shares_s1, swapped_shares_s2 = swap_shares(shares_s1, shares_s2)

    print(f"\nSwapped shares:\nSecret 1: {swapped_shares_s1}\nSecret 2: {swapped_shares_s2}")

    recovered_s2 = recover_secret(swapped_shares_s1[:threshold])
    recovered_s1 = recover_secret(swapped_shares_s2[:threshold])

    print(f"\nRecovered secrets after swapping:\nSecret 1: {recovered_s1}\nSecret 2: {recovered_s2}")

if __name__ == "__main__":
    main()

This implementation generates shares for two secrets, s 1 s_1 s1 and s 2 s_2 s2, and then swaps them using homomorphic properties without revealing the secrets. Finally, it recovers the secrets from the swapped shares. Note that this implementation assumes the secrets are in hexadecimal format.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值