Ejemplo n.º 1
0
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"])
Ejemplo n.º 2
0
 def step(di1, ni1, di2, ni2):
     frc = LinCombFxp.fromvar(di1 + di2, True) / LinCombFxp.fromvar(
         ni1 + ni2, True)
     ecur = frc * ni1
     v1n = LinCombFxp.fromvar(
         ni1 * ni2 * (di1 + di2) * (ni1 + ni2 - di1 - di2), True)
     v1d = LinCombFxp.fromvar((ni1 + ni2) * (ni1 + ni2) * (ni1 + ni2 - 1),
                              True)
     v1 = v1n / v1d
     return di1, ecur, v1
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
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 outputs
    output = {}
    for category in data:
        num_pell = sum([i >= 10 for i in data[category]])
        num_non_pell = sum([i < 10 for i in data[category]])
        num_all = num_pell + num_non_pell

        received_bachelors_pell = LinCombFxp(
            sum([i == 11 for i in data[category]]))
        different_institution_pell = LinCombFxp(
            sum([i == 12 for i in data[category]]))
        same_institution_pell = LinCombFxp(
            sum([i == 13 for i in data[category]]))

        received_bachelors_non_pell = LinCombFxp(
            sum([i == 1 for i in data[category]]))
        different_institution_non_pell = LinCombFxp(
            sum([i == 2 for i in data[category]]))
        same_institution_non_pell = LinCombFxp(
            sum([i == 3 for i in data[category]]))

        output[category] = {
            "Pell": {
                "Received Bachelor's":
                received_bachelors_pell / num_pell,
                "Enrolled at same institution":
                same_institution_pell / num_pell,
                "Enrolled at different insitution":
                different_institution_pell / num_pell
            },
            "No Pell": {
                "Received Bachelor's":
                received_bachelors_non_pell / num_non_pell,
                "Enrolled at same institution":
                same_institution_non_pell / num_non_pell,
                "Enrolled at different insitution":
                different_institution_non_pell / num_non_pell
            },
            "All Students": {
                "Received Bachelor's":
                (received_bachelors_pell + received_bachelors_non_pell) /
                num_all,
                "Enrolled at same institution":
                (same_institution_pell + same_institution_non_pell) / num_all,
                "Enrolled at different insitution":
                (different_institution_pell + different_institution_non_pell) /
                num_all
            }
        }

    for category in results:
        for student_type in results[category]:
            for outcome in results[category][student_type]:
                output[category][student_type][outcome].assert_eq(
                    results[category][student_type][outcome])
Ejemplo n.º 5
0
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 percentage per category
    total_cases = len(data)

    data = {
        "0-19": LinCombFxp(sum([(0 <= x) & (x <= 19) for x in data])),
        "20-29": LinCombFxp(sum([(20 <= x) & (x <= 29) for x in data])),
        "30-39": LinCombFxp(sum([(30 <= x) & (x <= 39) for x in data])),
        "40-49": LinCombFxp(sum([(40 <= x) & (x <= 49) for x in data])),
        "50-59": LinCombFxp(sum([(50 <= x) & (x <= 59) for x in data])),
        "60-69": LinCombFxp(sum([(60 <= x) & (x <= 69) for x in data])),
        "70-79": LinCombFxp(sum([(70 <= x) & (x <= 79) for x in data])),
        "80+": LinCombFxp(sum([80 <= x for x in data]))
    }

    percentage_per_category = {x: data[x] / total_cases for x in data}

    # Check output
    for x in data:
        percentage_per_category[x].assert_eq(results[x])
Ejemplo n.º 6
0
def if_then_else(cond, truev, falsev):
    if truev is falsev:
        return truev

    if isinstance(cond, int):
        if cond != 0 and cond != 1:
            raise ValueError("if_then_else can only take Boolean values")
        return truev if cond else falsev

    if not isinstance(cond, LinCombBool):
        raise RuntimeError("Wrong type for if_then_else condition")

    if callable(truev): truev = guarded(cond)(truev)()
    if callable(falsev): falsev = guarded(-cond)(falsev)()

    if isinstance(truev, list):
        return [
            if_then_else(cond, truevi, falsevi)
            for (truevi, falsevi) in zip(truev, falsev)
        ]

    if isinstance(truev, LinCombFxp):
        falsev = LinCombFxp._ensurefxp(falsev)
    return falsev + cond * (truev - falsev)
Ejemplo n.º 7
0
def compute(data, lfa_data, results, correct_hash):
    # Compute commitment
    hashed_data = poseidon_hash(flatten(data + lfa_data))
    [x.assert_eq(y) for (x, y) in zip(hashed_data, correct_hash)]

    # Compute pre-LFA data
    output = {"Pre-COVID": {}, "Post-COVID": {}}

    num_students = PrivVal(len(data))

    only_distance = LinCombFxp(sum([i == 1 for i in data]))
    some_distance = LinCombFxp(sum([i == 2 for i in data]))
    no_distance = LinCombFxp(sum([i == 3 for i in data]))

    output["Pre-COVID"] = {
        "Only Distance Education": only_distance / num_students,
        "Some Distance": some_distance / num_students,
        "No Distance": no_distance / num_students
    }

    num_lfa_students = PrivVal(len(data))

    lfa_only_distance = LinCombFxp(sum([i == 1 for i in lfa_data]))
    lfa_some_distance = LinCombFxp(sum([i == 2 for i in lfa_data]))
    lfa_no_distance = LinCombFxp(sum([i == 3 for i in lfa_data]))

    output["Post-COVID"] = {
        "Only Distance Education": lfa_only_distance / num_lfa_students,
        "Some Distance": lfa_some_distance / num_lfa_students,
        "No Distance": lfa_no_distance / num_lfa_students
    }

    for time_period in results:
        for distance_status in results[time_period]:
            output[time_period][distance_status].assert_eq(
                results[time_period][distance_status])
Ejemplo n.º 8
0
         [0, 9, 0, 1], [0, 9, 0, 1], [0, 9, 0, 1], [0, 8, 0, 1], [0, 7, 0, 1],
         [0, 6, 0, 1], [0, 5, 0, 1], [0, 5, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1],
         [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1],
         [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1],
         [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1], [0, 4, 0, 1],
         [0, 4, 0, 1], [0, 4, 0, 1]])

    pysnark.runtime.bitlength = 32

    def step(di1, ni1, di2, ni2):
        frc = LinCombFxp.fromvar(di1 + di2, True) / LinCombFxp.fromvar(
            ni1 + ni2, True)
        ecur = frc * ni1
        v1n = LinCombFxp.fromvar(
            ni1 * ni2 * (di1 + di2) * (ni1 + ni2 - di1 - di2), True)
        v1d = LinCombFxp.fromvar((ni1 + ni2) * (ni1 + ni2) * (ni1 + ni2 - 1),
                                 True)
        v1 = v1n / v1d
        return di1, ecur, v1

    steps = map(lambda x: step(*x), kmdata)
    (dtot, etot, vtot) = map(sum, zip(*steps))

    dtot = LinCombFxp.fromvar(dtot, True)
    chi0 = (dtot - etot)
    chi = chi0 * chi0 / vtot
    chi = chi.val()

    print("Final result: chi statistic=", chi, ", p-value=",
          1 - chi2.cdf(chi, 1))