def test_sub_int(self): first = 100 second = 337 bg1 = BigNum(first) self.assertEqual(bg1 - second, BigNum(first - second)) self.assertEqual(second - bg1, BigNum(second - first))
def test_mul_int(self): first = 10 second = 20 bg = BigNum(first) self.assertEqual(bg * second, BigNum(first * second))
def generate_pis(self, chi, n): if chi <= n + 1: raise ValueError( "chi should be greater than n + 1, chi=%s n+1=%s" % (chi, n + 1)) pis = [] prod = BigNum(1) # prod = (x - w_1) (x - w_2) ... (x - w_{n+1}) for j in range(1, n + 2): prod = prod * (chi - j) # denoms[0] = 1 / (w_1 - w_2) (w_1 - w_3) ... (w_1 - w_{n + 1}) # denoms[1] = 1 / (w_2 - w_1) (w_2 - w_3) ... (w_2 - w_{n + 1}) # denoms[n] = 1 / (w_{n+1}- w_1) (w_{n+1} - w_2) ... (w_{n+1} - w_n) denoms = self.compute_denominators(n + 1) missing_factor = chi - (n + 1) ln_plus1 = prod * missing_factor.mod_inverse() ln_plus1 = ln_plus1 * denoms[n].mod_inverse() # P_0 is special pis.append(ln_plus1 - BigNum(1)) two = BigNum(2) for i in range(1, n + 1): missing_factor = chi - i l_i = prod * missing_factor.mod_inverse() l_i = l_i * denoms[i - 1].mod_inverse() pis.append(two * l_i + ln_plus1) return pis
def test_add(self): first = 100 second = 337 bg1 = BigNum(first) bg2 = BigNum(second) self.assertEqual(bg1 + bg2, BigNum(first + second))
def test_mul(self): first = 10 second = 20 bg1 = BigNum(first) bg2 = BigNum(second) self.assertEqual(bg1 * bg2, BigNum(first * second))
def test_eq(self): first = 100 second = 337 bg1 = BigNum(first) bg2 = BigNum(second) self.assertEqual(bg1, BigNum(first)) self.assertNotEqual(bg1, bg2)
def test_sub(self): first = 100 second = 337 bg1 = BigNum(first) bg2 = BigNum(second) self.assertEqual(bg1 - bg2, BigNum(first - second)) self.assertEqual(bg2 - bg1, BigNum(second - first))
def compute_denominators(self, k): denominators = [] temp = BigNum(1) for i in range(1, k + 1): if i == 1: for j in range(2, k + 1): elem = i - j temp = temp * elem elif i == k: elem = 1 - k temp = temp * elem else: inverse = BigNum(i - 1 - k) inverse = inverse.mod_inverse() elem = i - 1 temp = temp * elem temp = temp * inverse denominators.append(temp) return denominators
def mul_test(self, g1): # create N - 1 BigNum bg = [BigNum() for _ in xrange(MAX - 1)] # get the sum of the BigNums s = reduce((lambda x, y: x + y), bg) # Nth element of bg is order + 1 - s, so sum(bg) = order + 1 # so g1 * bg[0] + g1 * bg[1] + ... + g1 * bg[N] == g1 * sum(bg) == # == g1 * (order + 1) == g1 bg.append(bg[0].getOrder() + 1 - s) res = [g1 * e for e in bg] s = reduce((lambda x, y: x + y), res) self.assertEqual(s, g1)
def test_neg(self): num = 10 bg = BigNum(num) self.assertEqual(-bg, BigNum(-num))
def test_mod_inverse(self): bg = BigNum() self.assertEqual(bg * bg.mod_inverse(), BigNum(1))
def test_pow(self): num = 2 p = 2 bg = BigNum(num)**p self.assertEqual(bg, BigNum(num**p))
def test_add_int(self): first = 100 second = 337 bg1 = BigNum(first) self.assertEqual(bg1 + second, BigNum(first + second))
def test_get_order(self): order = BigNum(num=0) self.assertEqual(BigNum.getOrder(), order)
import sys import datetime from libffpy import LibffPy, BigNum n = int(sys.argv[1]) l = LibffPy(n) g2 = l.gen2() g1 = l.gen1() start = datetime.datetime.now() bg = [BigNum() for _ in xrange(n - 1)] end = datetime.datetime.now() print "BigNum creation ellapsed: %s" % (end - start) s = reduce((lambda x,y: x + y), bg) bg.append(bg[0].getOrder() + 1 - s) start = datetime.datetime.now() res = [g2 * e for e in bg] end = datetime.datetime.now() print "G2 Multiplication ellapsed: %s" % (end - start) s = reduce((lambda x,y: x+ y), res) print "Test passed: %s" % (s == g2) start = datetime.datetime.now()
def test_pow(self): gt = self.libff.pair(self.g1, self.g2) bg = BigNum(3) self.assertEqual(gt**bg, gt * gt * gt)