Example #1
0
    def test_random_small(self):
        q = 100003
        for _ in range(0, 10):
            alpha = [FQ.random(q) for _ in range(0, 4)]
            points = [(FQ(i, q), shamirs_poly(FQ(i, q), alpha))
                      for i in range(0, len(alpha))]
            assert alpha[0] == lagrange(points, 0)
            assert alpha[0] != lagrange(points[1:], 0)
            assert alpha[0] != lagrange(points[2:], 0)

            # XXX: scipy's lagrange has floating point precision for large numbers
            points_x, points_y = unzip(points)
            interpolation = scipy_lagrange([_.n for _ in points_x], [_.n for _ in points_y])
            assert int(interpolation.c[-1]) == alpha[0]
Example #2
0
 def test_fromdocs2(self):
     p = 100003
     k = 4
     a = [randn(p) for _ in range(0, k)]  # [6257, 85026, 44499, 14701]
     F = lambda i, x: a[i] * (x**i)
     X = lambda x: a[0] + F(1, x) + F(2, x) + F(3, x)
     # Create the shares
     Sx = range(1, 5)
     Sy = [X(_) for _ in Sx]
     for x, y in zip(Sx, Sy):
         z = shamirs_poly_n(x, a, p)
         assert z == y % p
     # Then recover secret
     assert a[0] == int(scipy_lagrange(Sx, Sy).c[-1])
Example #3
0
 def test_fromdocs2(self):
     p = 100003
     k = 4
     a = [FQ.random(p) for _ in range(0, k)] # [6257, 85026, 44499, 14701]
     F = lambda i, x: a[i] * (x**i)
     X = lambda x: a[0] + F(1, x) + F(2, x) + F(3, x)
     # Create the shares
     Sx = range(1, 5)
     Sy = [X(_) for _ in Sx]
     for x, y in zip(Sx, Sy):
         z = shamirs_poly(FQ(x, p), a)
         assert z == y
     # Then recover secret
     result = int(scipy_lagrange(Sx, [_.n for _ in Sy]).c[-1]) % p
     assert a[0] == result