Exemplo n.º 1
0
def test_backward_compatibility_py_evm():
    from py_ecc import optimized_bn128 as bn128
    from py_ecc.optimized_bn128 import (
        FQP,
        FQ2,
    )

    FQ = bn128.FQ
    p1 = (FQ(0), FQ(0), FQ(1))
    bn128.is_on_curve(p1, bn128.b)
Exemplo n.º 2
0
def polyhash(key, nonce, message, round_consts=None):
    r"""
    This construction uses two `n`-bit keys, `k` and `k'`
    it operates on message of bitlength `L = t * n`.
    It starts by splitting the input message `x` into `t` blocks
    of bitlength `n` denoted `x_i`.
    The `x_i`, `k` and `k'` are represented as elements of `GF(p)`
    The authentication function `g_{k,k'}(x)` is then defined as:

        g_{k,k'}(x) = k' + \sum_{i=0}^t (x_i+c_i) * k^i

    Where:

     * `x` is the message
     * `x_i` is a segment of the message
     * `c_i` is an optional round constant
     * `k'` is the nonce
     * `k` is the key

    """
    if isinstance(message, (str, bytes)):
        if isinstance(message, str):
            message = message.encode('utf-8')
        message = bytes_to_field_elements(message)

    if round_consts and len(message) != round_consts:
        raise RuntimeError("Round consts must be the same length as message")

    k = None
    res = FQ(nonce)

    for i, x_i in enumerate(message):
        rhs = FQ(x_i)

        if round_consts:
            rhs += round_consts[i]

        if k is not None:
            rhs *= k
            k *= k
        else:
            k = FQ(key)

        res += rhs

    return res
Exemplo n.º 3
0
def _load_g1_point(point):
    """Unserialize a G1 point, from Ethereum hex encoded 0x..."""
    if len(point) != 2:
        raise RuntimeError("Invalid G1 point - not 2 vals", point)

    out = tuple(FQ(_filter_int(_)) for _ in point)

    if not bn128.is_on_curve(out, bn128.b):
        raise ValueError("Invalid G1 point - not on curve", out)

    return out
Exemplo n.º 4
0
def ecAdd(p1: Point, p2: Point) -> Point:
    p1 = FQ(p1[0]), FQ(p1[1])
    p2 = FQ(p2[0]), FQ(p2[1])
    return fq2point(add(p1, p2))
Exemplo n.º 5
0
def ecMul(p: Point, x: int) -> Point:
    pt = FQ(p[0]), FQ(p[1])
    return fq2point(multiply(pt, x))
Exemplo n.º 6
0
 def get_monitor_parameters(self, address):
   parameters = self.contract.functions.getMonitorParameters(address).call()
   target = parameters[:2]
   shuffle = parameters[2:4]
   index = parameters[4]
   return (FQ(target[0]), FQ(target[1])), (FQ(shuffle[0]), FQ(shuffle[1])), index