Exemple #1
0
    def testBenchMark(self):
        num_variables = 12
        num_terms = 2**11
        P = randomPrime(41)
        m = makeMVLinearConstructor(num_variables, P)

        d: Dict[int, int] = dict()
        for _ in range(num_terms):
            d[random.randint(0,
                             2**num_variables - 1)] = random.randint(0, P - 1)

        p = m(d)
        pv = InteractiveLinearProver(p)
        t0 = time.time() * 1000
        A, asum = pv.calculateTable()
        t1 = time.time() * 1000
        t = t1 - t0
        v = InteractiveVerifier(random.randint(0, 0xFFFFFFFF), p, asum)
        t0 = time.time() * 1000
        vT = pv.attemptProve(A, v)
        t1 = time.time() * 1000
        t += t1 - t0

        self.assertTrue(v.convinced, "Verifier is not convinced.")
        print("Prover takes {0}ms, verifier takes {1}ms".format(t - vT, vT))
    def testFunctionality(self):
        m = makeMVLinearConstructor(4, 47)
        x0 = m({1: 1})
        x1 = m({1 << 1: 1})
        x2 = m({1 << 2: 1})
        x3 = m({1 << 3: 1})

        p = random.randint(0, 46) * x0 + random.randint(0, 46) * x1 + random.randint(0, 46) * x2 + \
            random.randint(0, 46) * x3
        print("Testing Polynomial: " + str(p))

        pv = InteractiveNaiveProver(p)
        s = pv.calculateSum([])
        print("Asserted sum: {}".format(s))

        v = InteractiveVerifier(random.randint(0, 0xFFFFFFFF), p, s)
        result, vT = pv.attemptProve(v)
        self.assertTrue(result, "Fail to prove the sum. ")
        self.assertTrue(v.convinced, "Verifier not convinced. ")
Exemple #3
0
    def testFunctionality(self):
        P = randomPrime(37)
        m = makeMVLinearConstructor(4, P)
        x0 = m({1: 1})
        x1 = m({1 << 1: 1})
        x2 = m({1 << 2: 1})
        x3 = m({1 << 3: 1})

        p = random.randint(0, P-1) * x0 + random.randint(0, P-1) * x1 + random.randint(0, P-1) * x2 + \
            random.randint(0, P-1) * x3
        print("Testing Polynomial: " + str(p))

        pv = InteractiveLinearProver(p)
        A, s = pv.calculateTable()
        print("Asserted sum: {}".format(s))

        v = InteractiveVerifier(random.randint(0, 0xFFFFFFFF), p, s)
        pv.attemptProve(A, v, showDialog=True)
        self.assertTrue(v.convinced, "Verifier not convinced. ")
    def testBenchMark(self):
        num_variables = 12
        num_terms = 1500
        m = makeMVLinearConstructor(num_variables, 199)

        d: Dict[int, int] = dict()
        for _ in range(num_terms):
            d[random.randint(0, 2**num_variables - 1)] = random.randint(0, 198)

        p = m(d)
        pv = InteractiveNaiveProver(p)
        asum = pv.calculateSum([])
        v = InteractiveVerifier(random.randint(0, 0xFFFFFFFF), p, asum)

        t0 = time.time() * 1000
        _, vT = pv.attemptProve(v)
        t1 = time.time() * 1000

        self.assertTrue(v.convinced, "Verifier is not convinced.")
        print("Prover takes {0}ms, verifier takes {1}ms".format(
            t1 - t0 - vT, vT))
Exemple #5
0
def extend(data: List[int], fieldSize: int) -> MVLinear:
    """
    Convert an array to a polynomial where the argument is the binary form of index.
    :param data: Array of size 2^l. If the size of array is not power of 2, out-of-range part will be arbitrary.
    :param fieldSize: The size of finite field that the array value belongs.
    :return: The result MVLinear P(x1, x2, ..., xl) = Arr[0bxl...x1]
    """
    l: int = math.ceil(math.log(len(data), 2))
    p = fieldSize
    gen = makeMVLinearConstructor(l, p)
    x = [gen({1 << i: 1}) for i in range(l)]  # predefine x_1, ..., x_l

    poly_terms = {i: 0 for i in range(2**l)}

    for b in range(len(data)):
        sub_poly = gen({b: data[b]})
        xi0 = [x[i] for i in range(l) if b >> i & 1 == 0]
        sub_poly *= _product1mx(xi0, 0, len(xi0)-1)
        for t, v in sub_poly.terms.items():
            poly_terms[t] += v

    return gen(poly_terms)
Exemple #6
0
def extend_sparse(data: Dict[int, int], num_var: int, fieldSize: int) -> MVLinear:
    """
    Convert an sparse map to a polynomial where the argument is the binary form of index.
    :param data: sparse map if index<2^L. If the size of array is not power of 2, out-of-range part will be arbitrary.
    :param num_var: number of variables
    :param fieldSize: The size of finite field that the array value belongs.
    :return: The result MVLinear P(x1, x2, ..., xl) = Arr[0bxl...x1]
    """
    l: int = num_var
    p = fieldSize
    gen = makeMVLinearConstructor(l, p)
    x = [gen({1 << i: 1}) for i in range(l)]  # predefine x_1, ..., x_l

    poly_terms = {i: 0 for i in range(2**l)}

    for b, vb in data.items():
        sub_poly = gen({b: vb})
        xi0 = [x[i] for i in range(l) if b >> i & 1 == 0]
        sub_poly *= _product1mx(xi0, 0, len(xi0)-1)
        for t, v in sub_poly.terms.items():
            poly_terms[t] += v

    return gen(poly_terms)