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_pow(self): assert (PrivValFxp(2) ** 2).val() == 4.0 # assert (PrivValFxp(2) ** 2.0).val() == 4.0 # assert (2 ** PrivValFxp(2)).val() == 4.0 # assert (2.0 ** PrivValFxp(2)).val() == 4.0 # assert (PrivValFxp(2) ** PrivValFxp(2)).val() == 4.0 # assert (PrivValFxp(2) ** PrivVal(2)).val() == 4.0 # assert (PrivVal(2) ** PrivValFxp(2)).val() == 4.0 assert (PrivValFxp(2) ** 0).val() == 1.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_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_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_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_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_lincombfxp_assertion(self): assert assert_constant_constraints(lambda: PrivValFxp(0).assert_zero()) == 1 assert assert_constant_constraints(lambda: PrivValFxp(1).assert_nonzero()) == 1 assert assert_linear_constraints(lambda: PrivValFxp(1).assert_positive()) == (1,1) assert assert_linear_constraints(lambda: PrivValFxp(1).assert_range(PrivVal(0), PrivVal(2))) == (2, 2) assert assert_constant_constraints(lambda: PrivValFxp(0).assert_eq(PrivVal(0))) == 1 assert assert_constant_constraints(lambda: PrivValFxp(0).assert_ne(PrivVal(1))) == 1 assert assert_linear_constraints(lambda: PrivValFxp(0).assert_lt(PrivVal(1))) == (1,1) assert assert_linear_constraints(lambda: PrivValFxp(0).assert_le(PrivVal(1))) == (1,1) assert assert_linear_constraints(lambda: PrivValFxp(1).assert_gt(PrivVal(0))) == (1,1) assert assert_linear_constraints(lambda: PrivValFxp(0).assert_ge(PrivVal(0))) == (1,1)
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_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_lincombfxp_comparison(self): assert assert_constant_constraints(lambda: PrivValFxp(0) == 0) == 2 assert assert_constant_constraints(lambda: PrivValFxp(0) != 0) == 2 assert assert_constant_constraints(lambda: PrivValFxp(0) == PrivVal(0)) == 2 assert assert_constant_constraints(lambda: PrivValFxp(0) != PrivVal(1)) == 2 assert assert_linear_constraints(lambda: PrivValFxp(0) < PrivVal(0)) == (1,2) assert assert_linear_constraints(lambda: PrivValFxp(0) <= PrivVal(0)) == (1,2) assert assert_linear_constraints(lambda: PrivValFxp(0) > PrivVal(0)) == (1,2) assert assert_linear_constraints(lambda: PrivValFxp(0) >= PrivVal(0)) == (1,2)
def test_fixed_point(self): assert PrivValFxp(1).lc.value == 1 << fixedpoint.resolution assert PrivValFxp(1.0).lc.value == 1 << fixedpoint.resolution
def test_lincombfxp_branch(self): assert if_then_else(PrivValBool(0), PrivValFxp(1.5), PrivValFxp(2.5)).val() == 2.5 assert if_then_else(PrivValBool(1), PrivValFxp(1.5), PrivValFxp(2.5)).val() == 1.5
def test_lincombfxp_check(self): assert assert_constant_constraints(lambda: PrivValFxp(1).check_zero()) == 2 assert assert_constant_constraints(lambda: PrivValFxp(1).check_nonzero()) == 2 assert assert_linear_constraints(lambda: PrivValFxp(1).check_positive()) == (1,2)
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_priv_val(self): assert PrivValFxp(3).val() == 3 assert PrivValFxp(3.125).val() == 3.125
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
@snark 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 GPA total = sum(data) num_students = PrivVal(len(data)) gpa = total / num_students # Assert GPAs match gpa.assert_eq(results) if __name__ == "__main__": data = [3.0, 3.4, 2.3, 4.0, 3.8, 2.6] data = [PrivValFxp(x) for x in data] results = 3.1796875 correct_hash = [ 14881611281449098881494411741949295589753503020288873600598525691590595578845, 3435532317483150868219412711120858263178735851271792327085056805091609162997, 6678028067310553066516917272138683945237950974535046093126817351999384698723, 1190464966308886353495781666839595039502310509909317614817781318509062364533 ] compute(data, results, correct_hash) # count_ops(lambda x,y,z: compute(x,y,z), (data, results, correct_hash)) print("Successfully verified GPAs!")