pythoncryptographyintegersecret-keylargenumber

How to solve decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>] occurred due to large input number?


I am having an issue with handling large integers. I am trying to implement threshold secret sharing scheme by following the article in the link. I have modified the driver code to the following so that our secret key is practically large number instead of some pass phrase.

t, n = 3, 5
secret = 5238440126074724526414965738603764307730944402353374454036086258591065307597
print(f'Original Secret: {secret}')
shares = generate_shares(n, t, secret)
print(f'Shares: {", ".join(str(share) for share in shares)}')

pool = random.sample(shares, t)
print(f'Combining shares: {", ".join(str(share) for share in pool)}')
print(f'Reconstructed secret: {reconstruct_secret(pool) }')

I am getting the following error at due to line return int(round(Decimal(sums), 0)) from reconstruct_secret function :

decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

But it works just fine when I shorten my secret to 5238440126074724526414965. I have no idea how I can modify the reconstruct_secret function to work according to my requirement. Any suggestions would be a great help.


Solution

  • Thanks to @Barmar, my problem was solved by increasing the precision of decimal module as follows:

    from decimal import *
    getcontext().prec = 100
    

    Now I am able to reconstruct secret keys as per my requirement.