Beispiel #1
0
    def check_positive(self, bits=None):
        """
        Checks if a LinComb is a positive bitlength-bit integer
        Costs bitlength + 2 constraints
        """
        from pysnark.boolean import PrivValBool

        if bits is None:
            bits = bitlength

        if is_guard() and self.value.bit_length() <= bits:
            ret = PrivValBool(1 if self.value >= 0 else 0)
            abs = self.value if self.value >= 0 else -self.value

            bits = [PrivValBool((abs & (1 << ix)) >> ix) for ix in range(bits)]
        elif ignore_errors():
            ret = PrivValBool(0)
            bits = [PrivValBool(0) for _ in range(bits)]
        else:
            raise ValueError(
                str(self.value) + " is not a " + str(bits) + "-bit integer")

        # If ret == 1, then requires that 2 * self = self + sum, so sum = self
        # If ret == 0, this requires that 0 = self + sum, so sum = -self
        add_constraint(2 * ret, self, self + LinComb.from_bits(bits))

        return ret
Beispiel #2
0
 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
Beispiel #3
0
    def to_bits(self, bits=None):
        """
        Splits an integer LinComb into an bitlength-length array of LinCombBool bits
        Raises AssertionError if LinComb cannot be represented with bitlength bits
        Costs bitlength + 1 constraints to split a LinComb into bits
        """
        from pysnark.boolean import PrivValBool

        if bits is None:
            bits = bitlength

        if not ignore_errors():
            if self.value < 0 or self.value.bit_length() > bits:
                raise AssertionError(
                    str(self.value) + " is not a " + str(bits) +
                    "-bit positive integer")

        # Construct bits
        bits = [
            PrivValBool((self.value & (1 << ix)) >> ix) for ix in range(bits)
        ]

        # Check that bits are equal to self
        (self - LinComb.from_bits(bits)).assert_zero()

        return bits
Beispiel #4
0
    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
Beispiel #5
0
 def test_sub(self):
     assert (PrivValBool(1) - PrivValBool(1)).val() == 0
     assert (PrivVal(1) - PrivValBool(1)).val() == 0
     assert (PrivValBool(1) - PrivVal(1)).val() == 0
     assert (1 - PrivValBool(1)).val() == 0
     assert (PrivValBool(1) - 1).val() == 0
Beispiel #6
0
 def test_add(self):
     assert (PrivValBool(1) + PrivValBool(1)).val() == 2
     assert (PrivVal(1) + PrivValBool(1)).val() == 2
     assert (PrivValBool(1) + PrivVal(1)).val() == 2
     assert (1 + PrivValBool(1)).val() == 2
     assert (PrivValBool(1) + 1).val() == 2
Beispiel #7
0
 def test_priv_val(self):
     assert PrivValBool(0).val() == 0
     assert PrivValBool(1).val() == 1
Beispiel #8
0
 def test_lincombbool(self):
     from pysnark.poseidon_hash import poseidon_hash
     poseidon_hash([PrivValBool(0), PrivValBool(1), PrivValBool(0), PrivValBool(1), PrivValBool(0)])
Beispiel #9
0
 def test_lincomb_branch(self):
     assert if_then_else(PrivValBool(0), PrivVal(1), PrivVal(2)).val() == 2
     assert if_then_else(PrivValBool(1), PrivVal(1), PrivVal(2)).val() == 1
Beispiel #10
0
 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