def test_random(self): # Randomized tests for _ in range(0, 10): alpha = [FQ.random() for _ in range(0, 4)] points = [(FQ(i), shamirs_poly(FQ(i), 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)
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]
def test_static(self): # Verify against static test vectors alpha = [ 6808181831819141657160280673506432691407806061837762993142662373500430825792, 4138536697521448323155976179625860582331141320072618244300034508091478437877, 20259243729221075783953642258755031830946498253783650311586175820530608751936, 11227115470523445882235139084890542822660569362938710556861479160600812964997 ] points = [(i, shamirs_poly(i, alpha)) for i in range(0, len(alpha))] test_points = [ (0, 6808181831819141657160280673506432691407806061837762993142662373500430825792 ), (1, 20544834857245836424258632451520592838797650598216707762192147676147522484985 ), (2, 10833210933219706719196668784844423052753721417299010433393634464005858464330 ), (3, 1259517139202877390892412692306630092142705895884865660519589327528699562575 ) ] assert points == test_points assert alpha[0] == lagrange(points, 0)