function to add new shares to a set of existing ones#2
function to add new shares to a set of existing ones#2gryphius wants to merge 1 commit intoSSSaaS:masterfrom
Conversation
|
|
||
| # extended_gcd, divmod, lagrange_interpolate are copied from https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing | ||
|
|
||
| def extended_gcd(self, a, b): |
There was a problem hiding this comment.
Could we replace gcd with an iterative version and remove this? My 2c.
There was a problem hiding this comment.
Not sure why I didn't do this when I first wrote the code... it has been a while :-)
| To explain what this means, the return value will be such that | ||
| the following is true: den * divmod(num, den, p) % p == num | ||
| """ | ||
| inv, _ = self.extended_gcd(den, p) |
There was a problem hiding this comment.
We already have a mod_inverse function that could've been used here.
| den = PI(dens) | ||
| num = sum([self.divmod(nums[i] * den * y_s[i] % p, dens[i], p) | ||
| for i in range(k)]) | ||
| return (self.divmod(num, den, p) + p) % p No newline at end of file |
There was a problem hiding this comment.
I'd keep the newline at the end of the file. :-)
| the following is true: den * divmod(num, den, p) % p == num | ||
| """ | ||
| inv, _ = self.extended_gcd(den, p) | ||
| return num * inv |
There was a problem hiding this comment.
Shouldn't this be well, divmod? :-) num*inv isn't guaranteed to be in [0, p)... You just need to wrap it in another modulus statement: (num * inv) % p.
| return num * inv | ||
|
|
||
| def lagrange_interpolate(self, x, x_s, y_s, p): | ||
| """ |
There was a problem hiding this comment.
I'm fine with this function but we're already doing most of the work in combine...
Lines 57 to 69 in f987712
Seems like we should use what's in sssa.py already and extract it into utils.
| # The "raw" secret is padded with \x00 and split into 32 char (64 hex) chunks: for each part there will be a polynomial. | ||
| # The result of 'create' still has "shares" entries, but each is a \sum_i^parts x_i+y_i. | ||
| # Reconstruct points on the polynomial(s) from the shares | ||
| x,y=self.util.shares_to_x_y(shares) |
There was a problem hiding this comment.
x, y -- add a space please. :)
| new_share += self.util.to_base64(value) | ||
| new_share += self.util.to_base64(y_interpolated) | ||
|
|
||
| shares.append(new_share) |
There was a problem hiding this comment.
Why not return just the new_share? I think that makes more sense than adding the new share at the end of the list of shares... My 2c.
implemented #1 with a little help and lots of copy-pasting from https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing .
So I'm not sure if you'd want to merge this as-is or adapt it to maybe use the existing functions of the utils class more. I did not feel comfortable to mess with them, it's been a while since my last math class...