ezkl_codec - Infernet ML
Description: Infernet ML Documentation

Skip to content

# Module: `ezkl_codec`

Utilities for encoding and decoding ezkl proof requests. These are meant to be used in the context of solidity contracts. These utilities are used in the `ezkl_proof_service` service.

## `encode_processed_fields_hex(field_elements)`

Helper function to encode a processed field element array

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`field_elements` | `Optional[list[int]]` | list of field elements | required

Returns:

Type | Description
--- | ---
`Optional[HexStr]` | Optional[HexStr]: field elements encoded as an int256[] hex string or None if field_elements is None

Source code in `src/infernet_ml/utils/codec/ezkl_codec.py`

    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    def encode_processed_fields_hex(
        field_elements: Optional[list[int]],
    ) -> Optional[HexStr]:
        """
        Helper function to encode a processed field element array

        Args:
            field_elements (Optional[list[int]]): list of field elements

        Returns:
            Optional[HexStr]: field elements encoded as an int256[] hex string
                or None if field_elements is None
        """
        if not field_elements:
            return None

        return encode(
            ["int256[]"],
            [
                [
                    # convert field elements to int
                    int(felt_to_big_endian(x), 0)
                    for x in field_elements
                ]
            ],
        ).hex()

## `encode_proof_request(vk_addr, input_vector_bytes, output_vector_bytes)`

Helper function to encode an EZKL Proof Request. Note that vectors should be encoded using the `infernet_ml.utils.codec.vector.encode_vector` function.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`vk_addr` | `Optional[HexStr]` | the verifying key address | required
`input_vector_bytes` | `Optional[bytes]` | the encoded input vector | required
`output_vector_bytes` | `Optional[bytes]` | the encoded output bector | required

Returns:

Name | Type | Description
--- | --- | ---
`bytes` | `bytes` | encoded proof request bytes

Source code in `src/infernet_ml/utils/codec/ezkl_codec.py`

    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    def encode_proof_request(
        vk_addr: Optional[HexStr],
        input_vector_bytes: Optional[bytes],
        output_vector_bytes: Optional[bytes],
    ) -> bytes:
        """
        Helper function to encode an EZKL Proof Request. Note that vectors should
        be encoded using the `infernet_ml.utils.codec.vector.encode_vector`
        function.

        Args:
            vk_addr (Optional[HexStr]): the verifying key address
            input_vector_bytes (Optional[bytes]): the encoded input vector
            output_vector_bytes (Optional[bytes]): the encoded output bector

        Returns:
            bytes: encoded proof request bytes
        """
        types = ["bool", "bool", "bool"]
        data: list[Any] = []

        data.append(vk_addr is not None)
        data.append(input_vector_bytes is not None)
        data.append(output_vector_bytes is not None)

        if vk_addr:
            types.append("address")
            data.append(vk_addr)

        if input_vector_bytes:
            types.append("bytes")
            data.append(input_vector_bytes)

        if output_vector_bytes:
            types.append("bytes")
            data.append(output_vector_bytes)

        return encode(types, data)

## `extract_io_from_proof_execution(settings, witness)`

Helper to extract processed i/o from a witness.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`settings` | `str | Path` | path to the settings file | required
`witness` | `dict[str, Any]` | the generated witness dict | required

Returns:

Type | Description
--- | ---
`Optional[list[int]]` | tuple[Optional[list[int]], Optional[list[int]]]: processed input list,
`Optional[list[int]]` | processed output list

Source code in `src/infernet_ml/utils/codec/ezkl_codec.py`

    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    def extract_io_from_proof_execution(
        settings: str | Path, witness: dict[str, Any]
    ) -> tuple[Optional[list[int]], Optional[list[int]]]:
        """
        Helper to extract processed i/o from a witness.

        Args:
            settings (str | Path): path to the settings file
            witness (dict[str, Any]): the generated witness dict

        Returns:
            tuple[Optional[list[int]], Optional[list[int]]]: processed input list,
            processed output list
        """

        input_visibility, output_visibility, _ = extract_visibilities_from_settings(
            settings
        )

        ip = op = None
        match input_visibility.lower():
            case "hashed":
                ip = witness["processed_inputs"]["poseidon_hash"]
            case "encrypted":
                ip = witness["processed_inputs"]["ciphertexts"]

        match output_visibility.lower():
            case "hashed":
                op = witness["processed_outputs"]["poseidon_hash"]
            case "encrypted":
                op = witness["processed_outputs"]["ciphertexts"]

        return ip, op

## `extract_proof_request(infernet_input)`

Helper function to extract a ProofRequest from an EZKL Service InfernetInput payload.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`infernet_input` | `InfernetInput` | input to extract a ProofRequest from | required

Raises:

Type | Description
--- | ---
`ValueError` | thrown if an Unsupported source is provided

Returns:

Name | Type | Description
--- | --- | ---
`EZKLGenerateProofRequest` | `EZKLGenerateProofRequest` | the EZKLGenerateProofRequest, either decoded from the onchain bytes, or extracted directly.

Source code in `src/infernet_ml/utils/codec/ezkl_codec.py`

     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    def extract_proof_request(infernet_input: InfernetInput) -> EZKLGenerateProofRequest:
        """
        Helper function to extract a ProofRequest from an EZKL Service
        InfernetInput payload.

        Args:
            infernet_input (InfernetInput): input to extract a ProofRequest from

        Raises:
            ValueError: thrown if an Unsupported source is provided

        Returns:
            EZKLGenerateProofRequest: the EZKLGenerateProofRequest, either decoded from
                the onchain bytes, or extracted directly.
        """
        match infernet_input.source:
            case JobLocation.ONCHAIN:
                return EZKLGenerateProofRequest.from_web3(infernet_input.onchain_data)
            case JobLocation.OFFCHAIN:
                return EZKLGenerateProofRequest(**infernet_input.offchain_data)
            case _:
                raise ValueError(
                    f"Source must either be {JobLocation.ONCHAIN} or {JobLocation.OFFCHAIN}. Got {infernet_input.source}"  # noqa: E501
                )

## `extract_visibilities_from_settings(settings)`

Helper function to extract visibilities from a generated settings file.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`settings` | `str | Path | dict[str, Any]` | path to the settings file or the settings dict | required

Returns:

Type | Description
--- | ---
`str` | tuple[str, str, str]: input visibility, output visibility,
`str` | and param visibility

Source code in `src/infernet_ml/utils/codec/ezkl_codec.py`

    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    def extract_visibilities_from_settings(
        settings: str | Path | dict[str, Any],
    ) -> tuple[str, str, str]:
        """
        Helper function to extract visibilities from a generated settings
        file.

        Args:
            settings (str | Path | dict[str, Any]): path to the settings file
                or the settings dict

        Returns:
            tuple[str, str, str]: input visibility, output visibility,
            and param visibility
        """
        if isinstance(settings, (str, Path)):
            settings_dict = json.load(open(settings))
        else:
            settings_dict = settings

        input_v = (
            "Hashed"
            if "Hashed" in settings_dict["run_args"]["input_visibility"]
            else settings_dict["run_args"]["input_visibility"]
        )

        output_v = (
            "Hashed"
            if "Hashed" in settings_dict["run_args"]["output_visibility"]
            else settings_dict["run_args"]["output_visibility"]
        )

        param_v = (
            "Hashed"
            if "Hashed" in settings_dict["run_args"]["param_visibility"]
            else settings_dict["run_args"]["param_visibility"]
        )

        logging.info(
            f"input_visibility: {input_v} "
            f"output_visibility: {output_v} "
            f"param_visibility: {param_v}"
        )
        return input_v, output_v, param_v