NethermindEth / cairo-vm-go

A virtual machine for Cairo written in Go
MIT License
70 stars 43 forks source link

`ChainedEcOpRandomEcPoint` hint (ecop builtin (STARK curve) related hint) #489

Open TAdev0 opened 1 week ago

TAdev0 commented 1 week ago
    %{
        from starkware.crypto.signature.signature import ALPHA, BETA, FIELD_PRIME
        from starkware.python.math_utils import random_ec_point
        from starkware.python.utils import to_bytes

        n_elms = ids.len
        assert isinstance(n_elms, int) and n_elms >= 0, \
            f'Invalid value for len. Got: {n_elms}.'
        if '__chained_ec_op_max_len' in globals():
            assert n_elms <= __chained_ec_op_max_len, \
                f'chained_ec_op() can only be used with len<={__chained_ec_op_max_len}. ' \
                f'Got: n_elms={n_elms}.'

        # Define a seed for random_ec_point that's dependent on all the input, so that:
        #   (1) The added point s is deterministic.
        #   (2) It's hard to choose inputs for which the builtin will fail.
        seed = b"".join(
            map(
                to_bytes,
                [
                    ids.p.x,
                    ids.p.y,
                    *memory.get_range(ids.m, n_elms),
                    *memory.get_range(ids.q.address_, 2 * n_elms),
                ],
            )
        )
        ids.s.x, ids.s.y = random_ec_point(FIELD_PRIME, ALPHA, BETA, seed)
    %}

https://github.com/starkware-libs/cairo-lang/blob/efa9648f57568aad8f8a13fbf027d2de7c63c2c0/src/starkware/cairo/common/ec.cairo#L186