Exemplo n.º 1
0
    def precommit(self):
        """
        Commit to the bit-decomposition of the value.
        """
        actual_value = ensure_bn(self.x.value)
        value_as_bits = decompose_into_n_bits(actual_value, self.num_bits)

        # Set true value to computed secrets
        for rand in self.randomizers:
            rand.value = self.order.random()

        precommitment = {}
        precommitment["Cs"] = [
            b * self.g + r.value * self.h
            for b, r in zip(value_as_bits, self.randomizers)
        ]

        # Compute revealed randomizer
        rand = Bn(0)
        power = Bn(1)
        for r in self.randomizers:
            rand = rand.mod_add(r.value * power, self.order)
            power *= 2
        rand = rand.mod_sub(self.randomizer.value, self.order)
        precommitment["rand"] = rand

        return precommitment
Exemplo n.º 2
0
def point_double(a, b, p, x, y):
    """Define "doubling" an EC point.
     A special case, when a point needs to be added to itself.

     Reminder:
        lam = (3 * xp ^ 2 + a) * (2 * yp) ^ -1 (mod p)
        xr  = lam ^ 2 - 2 * xp
        yr  = lam * (xp - xr) - yp (mod p)

    Returns the point representing the double of the input (x, y).
    """

    # ADD YOUR CODE BELOW
    if x is None and y is None:
        return None, None

    xsq = x.mod_mul(x, p)
    xsq3 = Bn(3).mod_mul(xsq, p)
    num = xsq3.mod_add(a, p)
    y2 = Bn(2).mod_mul(y, p)
    y2inv = y2.mod_inverse(m=p)
    lam = num.mod_mul(y2inv, p)

    xr = lam.mod_mul(lam, p)
    xr = xr.mod_sub(x, p)
    xr = xr.mod_sub(x, p)

    yr = lam.mod_mul(x.mod_sub(xr, p), p)
    yr = yr.mod_sub(y, p)

    return (xr, yr)

    xr, yr = None, None

    return xr, yr
Exemplo n.º 3
0
    def simulate_precommit(self):
        randomizers = [self.order.random() for _ in range(self.num_bits)]
        precommitment = {}
        precommitment["Cs"] = [r * self.h for r in randomizers]
        precommitment["Cs"][0] += self.com

        # Compute revealed randomizer
        rand = Bn(0)
        power = Bn(1)
        for r in randomizers:
            rand = rand.mod_add(r * power, self.order)
            power *= 2
        precommitment["rand"] = rand

        return precommitment
Exemplo n.º 4
0
def sum_bn_array(arr, modulus):
    """
    Sum an array of big numbers under a modulus.

    >>> a = [Bn(5), Bn(7)]
    >>> m = 10
    >>> sum_bn_array(a, m)
    2
    """
    if not isinstance(modulus, Bn):
        modulus = Bn(modulus)
    res = Bn(0)
    for elem in arr:
        if not isinstance(elem, Bn):
            elem = Bn(elem)
        res = res.mod_add(elem, modulus)
    return res