def test_completeness_sanity(self):
        print(f"Test Completeness of GKR protocol (test individual functions)")
        L = 7
        p = randomPrime(256)
        gkr = randomGKR(L, p)
        g = [random.randint(0, p - 1) for _ in range(L)]

        A_hg, G = initialize_PhaseOne(gkr.f1, L, p, gkr.f3, g)
        s = sumOfGKR(A_hg, gkr.f2, p)
        v = GKRVerifier(gkr, g, s)
        self.assertEqual(v.state, GKRVerifierState.PHASE_ONE_LISTENING,
                         "wrong verifier state")

        u, f2u = talkToVerifierPhase1(A_hg, gkr, v)
        self.assertEqual(len(u), L, "wrong randomness size")
        self.assertEqual(v.state, GKRVerifierState.PHASE_TWO_LISTENING,
                         "verifier should be in phase two")
        self.assertEqual(
            f2u % p,
            evaluate(gkr.f2, u, p) % p,
            "f2(u) returned by talkToVerifierPhase1 is incorrect")

        print(
            f"initialize_PhaseOne, sumOfGKR, talkToVerifierPhase1 looks good. "
        )
        A_f1 = initialize_PhaseTwo(gkr.f1, G, u, p)
        talk_to_verifier_phase2(A_f1, gkr, f2u, v)
        self.assertEqual(v.state, GKRVerifierState.ACCEPT,
                         "Verifier does not accept this proof. ")
        print(f"initialize_PhaseTwo, talk_to_verifier_phase2 looks good. ")
        print(f"Completeness test PASS!")
示例#2
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 test_evaluate(self):
     for t in range(10):
         p = randomPrime(64)
         L = 8
         arr = [random.randint(0, p - 1) for _ in range(1 << L)]
         poly = extend(arr, p)
         args = [random.randint(0, p - 1) for _ in range(L)]
         self.assertEqual(poly.eval(args), evaluate(arr, args, p))
 def test_extend(self):
     for t in range(10):
         p = randomPrime(64)
         arr = [random.randint(0, p - 1) for _ in range(1024)]
         poly = extend(arr, p)
         for _ in range(poly.num_variables**2):
             i = random.randint(0, len(arr) - 1)
             self.assertEqual(arr[i], poly.eval_bin(i))
         print(f"Test #{t} finished. ")
示例#5
0
    def testCompleteness(self):
        for _ in range(100):
            P = randomPrime(221)
            p = PMF([randomMVLinear(7, prime=P) for _ in range(5)])
            pv = InteractivePMFProver(p)
            As, s = pv.calculateAllBookKeepingTables()
            v = InteractivePMFVerifier(p, s)
            pv.attemptProve(As, v)

            self.assertTrue(v.convinced)
 def test_evaluate_sparse(self):
     for t in range(10):
         p = randomPrime(64)
         L = 9
         data = {
             random.randint(0, (1 << L) - 1): random.randint(0, p - 1)
             for _ in range(1 << round(L**0.5))
         }
         poly = extend_sparse(data, L, p)
         args = [random.randint(0, p - 1) for _ in range(L)]
         self.assertEqual(poly.eval(args), evaluate_sparse(data, args, p))
 def test_extend_sparse(self):
     for t in range(10):
         p = randomPrime(64)
         L = 10
         data = {
             random.randint(0, (1 << L) - 1): random.randint(0, p - 1)
             for _ in range(256)
         }
         poly = extend_sparse(data, L, p)
         for k in range((1 << L) - 1):
             expected = data[k] if k in data else 0
             actual = poly.eval_bin(k)
             self.assertEqual(expected, actual)
         print(f"Test #{t} finished. ")
    def test_initialize_phase_one_two(self):
        L = 5
        p = randomPrime(64)
        print(
            f"Testing GKR Prover Bookkeeping table generator functions... Use L = {L}, p = {p}"
        )
        # generate random sparse f1, random f3, g
        D_f1 = generateRandomF1(L, p)
        f3 = randomMVLinear(L, prime=p)
        g = [random.randint(0, p - 1) for _ in range(L)]
        # get bookkeeping table for f3
        A_f3, _ = calculateBookKeepingTable(f3)
        # get poly form for f1 (only for test checking)
        A_f1 = [0] * (1 << (3 * L))
        for k, v in D_f1.items():
            A_f1[k] = v
        f1 = extend_sparse(D_f1, 3 * L, p)

        f1_fix_g = f1.eval_part(g)
        self.assertEqual(f1_fix_g.num_variables, 2 * L)

        A_hg_expected = [0] * (1 << L)
        for i in range(1 << (2 * L)):
            x = i & ((1 << L) - 1)
            y = (i & (((1 << L) - 1) << L)) >> L
            A_hg_expected[x] = (A_hg_expected[x] +
                                f1_fix_g.eval_bin(i) * A_f3[y]) % p

        A_hg_actual, G = initialize_PhaseOne(D_f1, L, p, A_f3, g)
        for i in range(1 << L):
            self.assertEqual(A_hg_expected[i] % p, A_hg_actual[i] % p)
        print("PASS: initialize_PhaseOne")
        # phase 2
        u = [random.randint(0, p - 1) for _ in range(L)]
        f1_fix_gu = f1_fix_g.eval_part(u)
        self.assertEqual(f1_fix_gu.num_variables, L)

        A_f1_expected = [0] * (1 << L)
        for i in range(1 << L):
            y = i & ((1 << L) - 1)
            A_f1_expected[y] = f1_fix_gu.eval_bin(y)

        A_f1_actual = initialize_PhaseTwo(D_f1, G, u, p)
        for i in range(1 << L):
            self.assertEqual(A_f1_expected[i] % p, A_f1_actual[i] % p)
        print("PASS: initialize_PhaseTwo")
示例#9
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 test_protocol_comprehensive(self):
        NUM_TESTS = 10
        Ls = list(range(10, 13))
        print(f"Performing GKR interactive protocol comprehensive test...")
        for i in range(NUM_TESTS):
            p = randomPrime(256)
            L = Ls[i % len(Ls)]
            print(f"test #{i+1}: |g|=|x|=|y| = {L}: TESTING",
                  end="",
                  flush=True)
            gkr = randomGKR(L, p)
            g = [random.randint(0, p - 1) for _ in range(L)]

            pv = GKRProver(gkr)
            A_hg, G, s = pv.initializeAndGetSum(g)

            v = GKRVerifier(gkr, g, s)
            assert v.state == GKRVerifierState.PHASE_ONE_LISTENING, "Verifier sanity check failed"
            pv.proveToVerifier(A_hg, G, s, v)

            self.assertEqual(v.state, GKRVerifierState.ACCEPT)
            print(f"\b\b\b\b\b\b\bPASS")
        print(f"All GKR interactive protocol comprehensive tests passed! ")
 def testCompleteness(self):
     for _ in range(100):
         P = randomPrime(224)
         p = PMF([randomMVLinear(7, prime=P) for _ in range(5)])
         theorem, proof, _ = generateTheoremAndProof(p)
         self.assertTrue(verifyProof(theorem, proof))