def compute(data, results, correct_hash): # Compute commitment hashed_data = poseidon_hash(flatten(data)) [x.assert_eq(y) for (x,y) in zip(hashed_data, correct_hash)] # Compute graduation rates by category output = {} for category in data: arr2011 = [x == 1 for x in data[category]] arr2013 = [x == 11 for x in data[category]] graduated2011 = LinCombFxp(sum(arr2011)) graduated2013 = LinCombFxp(sum(arr2013)) length2011 = PrivVal(len(arr2011)) length2013 = PrivVal(len(arr2013)) gr2011 = graduated2011 * 100 / (length2011 + (length2011 == 0)) gr2013 = graduated2013 * 100 / (length2013 + (length2013 == 0)) output[category] = { "Began in 2011": gr2011, "Began in 2013": gr2013 } # # Assert results are correct for category in results: output[category]["Began in 2011"].assert_eq(results[category]["Began in 2011"]) output[category]["Began in 2013"].assert_eq(results[category]["Began in 2013"])
def test_mixed_branch(self): assert if_then_else(PrivValBool(0), PrivVal(1), PrivValFxp(2.5)).val() == 2.5 assert if_then_else(PrivValBool(1), PrivValFxp(1.5), PrivValBool(1)).val() == 1.5 assert if_then_else(PrivValBool(1), PrivValBool(1), PrivVal(2)).val() == 1
def test_add(self): assert (PrivValFxp(1.0) + 2).val() == 3.0 assert (PrivValFxp(1.0) + 2.0).val() == 3.0 assert (2 + PrivValFxp(1.0)).val() == 3.0 assert (2.0 + PrivValFxp(1.0)).val() == 3.0 assert (PrivValFxp(1.0) + PrivValFxp(2.0)).val() == 3.0 assert (PrivValFxp(1.0) + PrivVal(2)).val() == 3.0 assert (PrivVal(2) + PrivValFxp(1.0)).val() == 3.0
def test_mul(self): assert (PrivValFxp(2.0) * 2).val() == 4 assert (PrivValFxp(2.0) * 2.0).val() == 4 assert (2 * PrivValFxp(2.0)).val() == 4 assert (2.0 * PrivValFxp(2.0)).val() == 4 assert (PrivValFxp(2.0) * PrivValFxp(2.0)).val() == 4 assert (PrivValFxp(2.0) * PrivVal(2)).val() == 4 assert (PrivVal(2) * PrivValFxp(2.0)).val() == 4
def test_sub(self): assert (PrivValFxp(2.0) - 1).val() == 1.0 assert (PrivValFxp(2.0) - 1.0).val() == 1.0 assert (2 - PrivValFxp(1.0)).val() == 1.0 assert (2.0 - PrivValFxp(1.0)).val() == 1.0 assert (PrivValFxp(2.0) - PrivValFxp(1.0)).val() == 1.0 assert (PrivValFxp(2.0) - PrivVal(1)).val() == 1.0 assert (PrivVal(2) - PrivValFxp(1.0)).val() == 1.0
def test_div_integer(self): assert (PrivValFxp(4.0) / 2).val() == 2.0 assert (PrivValFxp(4.0) / 2.0).val() == 2.0 assert (4 / PrivValFxp(2.0)).val() == 2.0 assert (4.0 / PrivValFxp(2.0)).val() == 2.0 assert (PrivValFxp(4.0) / PrivValFxp(2.0)).val() == 2.0 assert (PrivValFxp(4.0) / PrivVal(2)).val() == 2.0 assert (PrivVal(4) / PrivValFxp(2.0)).val() == 2.0
def test_div_fraction(self): assert (PrivValFxp(5.0) / 2).val() == 2.5 assert (PrivValFxp(5.0) / 2.0).val() == 2.5 assert (5 / PrivValFxp(2.0)).val() == 2.5 assert (5.0 / PrivValFxp(2.0)).val() == 2.5 assert (PrivValFxp(5.0) / PrivValFxp(2.0)).val() == 2.5 assert (PrivValFxp(5.0) / PrivVal(2)).val() == 2.5 assert (PrivVal(5) / PrivValFxp(2.0)).val() == 2.5
def test_floor_div(self): assert (PrivValFxp(5.0) // 2).val() == 2.0 assert (PrivValFxp(5.0) // 2.0).val() == 2.0 assert (5 // PrivValFxp(2.0)).val() == 2.0 assert (5.0 // PrivValFxp(2.0)).val() == 2.0 assert (PrivValFxp(5.0) // PrivValFxp(2.0)).val() == 2.0 assert (PrivValFxp(5.0) // PrivVal(2)).val() == 2.0 assert (PrivVal(5) // PrivValFxp(2.0)).val() == 2.0
def test_rshift(self): runtime.bitlength = 5 assert (PrivVal(2) << 2).val() == 8 assert (2 << PrivVal(2)).val() == 8 assert (PrivVal(2) << PrivVal(2)).val() == 8 runtime.bitlength = 16
def test_lshift(self): runtime.bitlength = 5 assert (PrivVal(8) >> 2).val() == 2 assert (8 >> PrivVal(2)).val() == 2 assert (PrivVal(8) >> PrivVal(2)).val() == 2 runtime.bitlength = 16
def test_zkifbulletproofs_permutation(self): from pysnark.poseidon_hash import permute output = permute([PrivVal(0), PrivVal(1), PrivVal(2), PrivVal(3), PrivVal(4)]) result = [0x2a918b9c9f9bd7bb509331c81e297b5707f6fc7393dcee1b13901a0b22202e18, \ 0x65ebf8671739eeb11fb217f2d5c5bf4a0c3f210e3f3cd3b08b5db75675d797f7, \ 0x2cc176fc26bc70737a696a9dfd1b636ce360ee76926d182390cdb7459cf585ce, \ 0x4dc4e29d283afd2a491fe6aef122b9a968e74eff05341f3cc23fda1781dcb566, \ 0x03ff622da276830b9451b88b85e6184fd6ae15c8ab3ee25a5667be8592cce3b1] assert all([x.val() == y for (x,y) in zip(output, result)])
def test_mul(self): assert (PrivValBool(1) * PrivValBool(0)).val() == 0 assert (PrivVal(2) * PrivValBool(0)).val() == 0 assert (PrivValBool(0) * PrivVal(2)).val() == 0 assert (PrivVal(2) * PrivValBool(1)).val() == 2 assert (PrivValBool(1) * PrivVal(2)).val() == 2 assert (2 * PrivValBool(1)).val() == 2 assert (PrivValBool(1) * 2).val() == 2
def test_lt(self): assert (3 < PrivVal(5)).val() == 1 assert (PrivVal(3) < 5).val() == 1 assert (PrivVal(3) < PrivVal(5)).val() == 1 assert isinstance(PrivVal(3) < 5, LinCombBool) assert (3 < PrivVal(1)).val() == 0 assert (PrivVal(3) < 1).val() == 0 assert (PrivVal(3) < PrivVal(1)).val() == 0 assert isinstance(PrivVal(3) < 1, LinCombBool)
def test_gt(self): assert (5 > PrivVal(3)).val() == 1 assert (PrivVal(5) > 3).val() == 1 assert (PrivVal(5) > PrivVal(3)).val() == 1 assert isinstance(PrivVal(5) > 3, LinCombBool) assert (1 > PrivVal(3)).val() == 0 assert (PrivVal(1) > 3).val() == 0 assert (PrivVal(1) > PrivVal(3)).val() == 0 assert isinstance(PrivVal(1) > 3, LinCombBool)
def test_zkinterface_permutation(self): from pysnark.poseidon_hash import permute output = permute([PrivVal(0), PrivVal(1), PrivVal(2), PrivVal(3), PrivVal(4)]) result = [0x299c867db6c1fdd79dcefa40e4510b9837e60ebb1ce0663dbaa525df65250465, \ 0x1148aaef609aa338b27dafd89bb98862d8bb2b429aceac47d86206154ffe053d, \ 0x24febb87fed7462e23f6665ff9a0111f4044c38ee1672c1ac6b0637d34f24907, \ 0x0eb08f6d809668a981c186beaf6110060707059576406b248e5d9cf6e78b3d3e, \ 0x07748bc6877c9b82c8b98666ee9d0626ec7f5be4205f79ee8528ef1c4a376fc7] print(output) assert all([x.val() == y for (x,y) in zip(output, result)])
def benchmark_factorial(nm, fn): numc = 0 def callback(num): nonlocal numc numc = num ret = benchmark(callback)(fn)(PrivVal(FAC_TEST)) ret2 = fn(PrivVal(FAC_MAX)) print("%-30s: output=%d, %d, constraints=%d" % (nm, ret.value, ret2.value, numc))
def test_assert_le(self): PrivVal(3).assert_le(3) PrivVal(3).assert_le(5) PrivVal(3).assert_le(PrivVal(3)) PrivVal(3).assert_le(PrivVal(5)) with pytest.raises(AssertionError): PrivVal(3).assert_le(1) with pytest.raises(AssertionError): PrivVal(3).assert_le(PrivVal(1))
def test_branch_condition(self): with pytest.raises(RuntimeError): if_then_else(PrivVal(True), PrivVal(1), PrivVal(2)) with pytest.raises(RuntimeError): if_then_else(PrivVal(1), PrivVal(1), PrivVal(2)) with pytest.raises(RuntimeError): if_then_else(PrivValFxp(1), PrivVal(1), PrivVal(2))
def test_mod(self): assert (PrivVal(5) % 2).val() == 1 assert (5 % PrivVal(2)).val() == 1 assert (PrivVal(5) % PrivVal(2)).val() == 1 assert (PrivVal(5) % 1).val() == 0 assert (5 % PrivVal(1)).val() == 0 assert (PrivVal(5) % PrivVal(1)).val() == 0
def test_floor_div(self): assert (PrivVal(4) // 2).val() == 2 assert (4 // PrivVal(2)).val() == 2 assert (PrivVal(4) // PrivVal(2)).val() == 2 assert (PrivVal(5) // 2).val() == 2 assert (5 // PrivVal(2)).val() == 2 assert (PrivVal(5) // PrivVal(2)).val() == 2
def test_neq(self): assert ((PrivVal(2) != 1).val() == 1) assert ((2 != PrivVal(1)).val() == 1) assert ((PrivVal(2) != PrivVal(1)).val() == 1) assert ((PrivVal(2) != 2).val() == 0) assert ((2 != PrivVal(2)).val() == 0) assert ((PrivVal(2) != PrivVal(2)).val() == 0)
def test_eq(self): assert ((PrivVal(2) == 2).val() == 1) assert ((2 == PrivVal(2)).val() == 1) assert ((PrivVal(2) == PrivVal(2)).val() == 1) assert ((PrivVal(2) == 1).val() == 0) assert ((2 == PrivVal(1)).val() == 0) assert ((PrivVal(2) == PrivVal(1)).val() == 0)
def test_assert_gt(self): PrivVal(5).assert_gt(3) PrivVal(5).assert_gt(PrivVal(3)) with pytest.raises(AssertionError): PrivVal(1).assert_gt(1) with pytest.raises(AssertionError): PrivVal(1).assert_gt(PrivVal(1)) with pytest.raises(AssertionError): PrivVal(1).assert_gt(3) with pytest.raises(AssertionError): PrivVal(1).assert_gt(PrivVal(3))
def test_mod(self): assert (PrivValFxp(5) % 2).val() == 1.0 assert (PrivValFxp(5) % 2.0).val() == 1.0 assert (5 % PrivValFxp(2)).val() == 1.0 assert (5.0 % PrivValFxp(2)).val() == 1.0 assert (PrivValFxp(5) % PrivValFxp(2)).val() == 1.0 assert (PrivValFxp(5) % PrivVal(2)).val() == 1.0 assert (PrivVal(5) % PrivValFxp(2)).val() == 1.0 assert (PrivValFxp(5) % 1).val() == 0.0 assert (PrivValFxp(5) % 1.0).val() == 0.0 assert (5 % PrivValFxp(1)).val() == 0.0 assert (5.0 % PrivValFxp(1)).val() == 0.0 assert (PrivValFxp(5) % PrivValFxp(1)).val() == 0.0 assert (PrivValFxp(5) % PrivVal(1)).val() == 0.0 assert (PrivVal(5) % PrivValFxp(1)).val() == 0.0
def test3(): done = 0 w=0 while done!=PrivVal(5) and done!=10: w = done done += 1 if done==3: break
def test4(): k = PrivVal(9) sum = 0 for i in range(min(k,9)): if i==3: break sum = i return sum
def test_assert_ne(self): PrivVal(3).assert_ne(2) PrivVal(3).assert_ne(PrivVal(2)) PrivVal(2).assert_ne(PrivVal(3)) with pytest.raises(AssertionError): PrivVal(3).assert_ne(3) with pytest.raises(AssertionError): PrivVal(3).assert_ne(PrivVal(3))
def test_assert_eq(self): PrivVal(3).assert_eq(3) PrivVal(3).assert_eq(PrivVal(3)) with pytest.raises(AssertionError): PrivVal(3).assert_eq(2) with pytest.raises(AssertionError): PrivVal(3).assert_eq(PrivVal(2)) with pytest.raises(AssertionError): PrivVal(2).assert_eq(PrivVal(3))
def test_lincombfxp_arithmetic(self): assert assert_constant_constraints(lambda: LinCombFxp(PrivVal(1))) == 0 assert assert_constant_constraints(lambda: PrivValFxp(0) + 0) == 0 assert assert_constant_constraints(lambda: PrivValFxp(0) - 0) == 0 assert assert_constant_constraints(lambda: PrivValFxp(0) * 0) == 0 assert assert_linear_constraints(lambda: PrivValFxp(1) / 1) == (2,4) assert assert_linear_constraints(lambda: PrivValFxp(1) // 1) == (2,4) assert assert_linear_constraints(lambda: PrivValFxp(1) % 1) == (2,4) assert assert_linear_constraints(lambda: PrivValFxp(1) ** 5) == (8,20) assert assert_constant_constraints(lambda: PrivValFxp(0) + PrivVal(0)) == 0 assert assert_constant_constraints(lambda: PrivValFxp(0) - PrivVal(0)) == 0 assert assert_constant_constraints(lambda: PrivValFxp(0) * PrivVal(0)) == 1 assert assert_linear_constraints(lambda: PrivValFxp(1) / PrivVal(1)) == (2,4) assert assert_linear_constraints(lambda: PrivValFxp(1) // PrivVal(1)) == (2,4) assert assert_linear_constraints(lambda: PrivValFxp(1) % PrivVal(1)) == (2,4) # assert assert_linear_constraints(lambda: PrivValFxp(1) ** PrivVal(0)) == 41 # assert assert_linear_constraints(lambda: PrivValFxp(1) ** PrivVal(1)) == 41 assert assert_constant_constraints(lambda: PrivValFxp(0) + PrivValFxp(0)) == 0 assert assert_constant_constraints(lambda: PrivValFxp(0) - PrivValFxp(0)) == 0 assert assert_linear_constraints(lambda: PrivValFxp(0) * PrivValFxp(0)) == (2,5) assert assert_linear_constraints(lambda: PrivValFxp(1) / PrivValFxp(1)) == (2,4) assert assert_linear_constraints(lambda: PrivValFxp(1) // PrivValFxp(1)) == (2,4) assert assert_linear_constraints(lambda: PrivValFxp(1) % PrivValFxp(1)) == (2,4)
def test(): z = 40 if PrivVal(1): z = 100 else: z = 200 return z