mplang-nightly 0.1.dev158__py3-none-any.whl → 0.1.dev268__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mplang/__init__.py +21 -45
- mplang/py.typed +13 -0
- mplang/v1/__init__.py +157 -0
- mplang/v1/_device.py +602 -0
- mplang/{analysis → v1/analysis}/__init__.py +1 -1
- mplang/{analysis → v1/analysis}/diagram.py +5 -7
- mplang/v1/core/__init__.py +157 -0
- mplang/{core → v1/core}/cluster.py +30 -14
- mplang/{core → v1/core}/comm.py +5 -1
- mplang/{core → v1/core}/context_mgr.py +1 -1
- mplang/{core/dtype.py → v1/core/dtypes.py} +44 -2
- mplang/{core → v1/core}/expr/__init__.py +7 -7
- mplang/{core → v1/core}/expr/ast.py +13 -14
- mplang/{core → v1/core}/expr/evaluator.py +65 -24
- mplang/{core → v1/core}/expr/printer.py +24 -18
- mplang/{core → v1/core}/expr/transformer.py +3 -3
- mplang/{core → v1/core}/expr/utils.py +2 -2
- mplang/{core → v1/core}/expr/visitor.py +1 -1
- mplang/{core → v1/core}/expr/walk.py +1 -1
- mplang/{core → v1/core}/interp.py +6 -6
- mplang/{core → v1/core}/mpir.py +23 -16
- mplang/{core → v1/core}/mpobject.py +6 -6
- mplang/{core → v1/core}/mptype.py +13 -10
- mplang/{core → v1/core}/pfunc.py +4 -4
- mplang/{core → v1/core}/primitive.py +106 -201
- mplang/{core → v1/core}/table.py +36 -8
- mplang/{core → v1/core}/tensor.py +1 -1
- mplang/{core → v1/core}/tracer.py +9 -9
- mplang/{api.py → v1/host.py} +38 -6
- mplang/v1/kernels/__init__.py +41 -0
- mplang/{kernels → v1/kernels}/base.py +1 -1
- mplang/v1/kernels/basic.py +240 -0
- mplang/{kernels → v1/kernels}/context.py +42 -27
- mplang/{kernels → v1/kernels}/crypto.py +44 -37
- mplang/v1/kernels/fhe.py +858 -0
- mplang/{kernels → v1/kernels}/mock_tee.py +12 -13
- mplang/{kernels → v1/kernels}/phe.py +263 -57
- mplang/{kernels → v1/kernels}/spu.py +137 -48
- mplang/{kernels → v1/kernels}/sql_duckdb.py +12 -15
- mplang/{kernels → v1/kernels}/stablehlo.py +30 -23
- mplang/v1/kernels/value.py +626 -0
- mplang/{ops → v1/ops}/__init__.py +5 -16
- mplang/{ops → v1/ops}/base.py +2 -5
- mplang/{ops/builtin.py → v1/ops/basic.py} +34 -26
- mplang/v1/ops/crypto.py +262 -0
- mplang/v1/ops/fhe.py +272 -0
- mplang/{ops → v1/ops}/jax_cc.py +33 -68
- mplang/v1/ops/nnx_cc.py +168 -0
- mplang/{ops → v1/ops}/phe.py +16 -4
- mplang/{ops → v1/ops}/spu.py +3 -5
- mplang/v1/ops/sql_cc.py +303 -0
- mplang/{ops → v1/ops}/tee.py +9 -24
- mplang/{protos → v1/protos}/v1alpha1/mpir_pb2.pyi +71 -21
- mplang/v1/protos/v1alpha1/value_pb2.py +34 -0
- mplang/v1/protos/v1alpha1/value_pb2.pyi +169 -0
- mplang/{runtime → v1/runtime}/__init__.py +2 -2
- mplang/v1/runtime/channel.py +230 -0
- mplang/{runtime → v1/runtime}/cli.py +35 -20
- mplang/{runtime → v1/runtime}/client.py +19 -8
- mplang/{runtime → v1/runtime}/communicator.py +59 -15
- mplang/{runtime → v1/runtime}/data_providers.py +80 -19
- mplang/{runtime → v1/runtime}/driver.py +30 -12
- mplang/v1/runtime/link_comm.py +196 -0
- mplang/{runtime → v1/runtime}/server.py +58 -42
- mplang/{runtime → v1/runtime}/session.py +57 -71
- mplang/{runtime → v1/runtime}/simulation.py +55 -28
- mplang/v1/simp/api.py +353 -0
- mplang/{simp → v1/simp}/mpi.py +8 -9
- mplang/{simp/__init__.py → v1/simp/party.py} +19 -145
- mplang/{simp → v1/simp}/random.py +21 -22
- mplang/v1/simp/smpc.py +238 -0
- mplang/v1/utils/table_utils.py +185 -0
- mplang/v2/__init__.py +424 -0
- mplang/v2/backends/__init__.py +57 -0
- mplang/v2/backends/bfv_impl.py +705 -0
- mplang/v2/backends/channel.py +217 -0
- mplang/v2/backends/crypto_impl.py +723 -0
- mplang/v2/backends/field_impl.py +454 -0
- mplang/v2/backends/func_impl.py +107 -0
- mplang/v2/backends/phe_impl.py +148 -0
- mplang/v2/backends/simp_design.md +136 -0
- mplang/v2/backends/simp_driver/__init__.py +41 -0
- mplang/v2/backends/simp_driver/http.py +168 -0
- mplang/v2/backends/simp_driver/mem.py +280 -0
- mplang/v2/backends/simp_driver/ops.py +135 -0
- mplang/v2/backends/simp_driver/state.py +60 -0
- mplang/v2/backends/simp_driver/values.py +52 -0
- mplang/v2/backends/simp_worker/__init__.py +29 -0
- mplang/v2/backends/simp_worker/http.py +354 -0
- mplang/v2/backends/simp_worker/mem.py +102 -0
- mplang/v2/backends/simp_worker/ops.py +167 -0
- mplang/v2/backends/simp_worker/state.py +49 -0
- mplang/v2/backends/spu_impl.py +275 -0
- mplang/v2/backends/spu_state.py +187 -0
- mplang/v2/backends/store_impl.py +62 -0
- mplang/v2/backends/table_impl.py +838 -0
- mplang/v2/backends/tee_impl.py +215 -0
- mplang/v2/backends/tensor_impl.py +519 -0
- mplang/v2/cli.py +603 -0
- mplang/v2/cli_guide.md +122 -0
- mplang/v2/dialects/__init__.py +36 -0
- mplang/v2/dialects/bfv.py +665 -0
- mplang/v2/dialects/crypto.py +689 -0
- mplang/v2/dialects/dtypes.py +378 -0
- mplang/v2/dialects/field.py +210 -0
- mplang/v2/dialects/func.py +135 -0
- mplang/v2/dialects/phe.py +723 -0
- mplang/v2/dialects/simp.py +944 -0
- mplang/v2/dialects/spu.py +349 -0
- mplang/v2/dialects/store.py +63 -0
- mplang/v2/dialects/table.py +407 -0
- mplang/v2/dialects/tee.py +346 -0
- mplang/v2/dialects/tensor.py +1175 -0
- mplang/v2/edsl/README.md +279 -0
- mplang/v2/edsl/__init__.py +99 -0
- mplang/v2/edsl/context.py +311 -0
- mplang/v2/edsl/graph.py +463 -0
- mplang/v2/edsl/jit.py +62 -0
- mplang/v2/edsl/object.py +53 -0
- mplang/v2/edsl/primitive.py +284 -0
- mplang/v2/edsl/printer.py +119 -0
- mplang/v2/edsl/registry.py +207 -0
- mplang/v2/edsl/serde.py +375 -0
- mplang/v2/edsl/tracer.py +614 -0
- mplang/v2/edsl/typing.py +816 -0
- mplang/v2/kernels/Makefile +30 -0
- mplang/v2/kernels/__init__.py +23 -0
- mplang/v2/kernels/gf128.cpp +148 -0
- mplang/v2/kernels/ldpc.cpp +82 -0
- mplang/v2/kernels/okvs.cpp +283 -0
- mplang/v2/kernels/okvs_opt.cpp +291 -0
- mplang/v2/kernels/py_kernels.py +398 -0
- mplang/v2/libs/collective.py +330 -0
- mplang/v2/libs/device/__init__.py +51 -0
- mplang/v2/libs/device/api.py +813 -0
- mplang/v2/libs/device/cluster.py +352 -0
- mplang/v2/libs/ml/__init__.py +23 -0
- mplang/v2/libs/ml/sgb.py +1861 -0
- mplang/v2/libs/mpc/__init__.py +41 -0
- mplang/v2/libs/mpc/_utils.py +99 -0
- mplang/v2/libs/mpc/analytics/__init__.py +35 -0
- mplang/v2/libs/mpc/analytics/aggregation.py +372 -0
- mplang/v2/libs/mpc/analytics/groupby.md +99 -0
- mplang/v2/libs/mpc/analytics/groupby.py +331 -0
- mplang/v2/libs/mpc/analytics/permutation.py +386 -0
- mplang/v2/libs/mpc/common/constants.py +39 -0
- mplang/v2/libs/mpc/ot/__init__.py +32 -0
- mplang/v2/libs/mpc/ot/base.py +222 -0
- mplang/v2/libs/mpc/ot/extension.py +477 -0
- mplang/v2/libs/mpc/ot/silent.py +217 -0
- mplang/v2/libs/mpc/psi/__init__.py +40 -0
- mplang/v2/libs/mpc/psi/cuckoo.py +228 -0
- mplang/v2/libs/mpc/psi/okvs.py +49 -0
- mplang/v2/libs/mpc/psi/okvs_gct.py +79 -0
- mplang/v2/libs/mpc/psi/oprf.py +310 -0
- mplang/v2/libs/mpc/psi/rr22.py +344 -0
- mplang/v2/libs/mpc/psi/unbalanced.py +200 -0
- mplang/v2/libs/mpc/vole/__init__.py +31 -0
- mplang/v2/libs/mpc/vole/gilboa.py +327 -0
- mplang/v2/libs/mpc/vole/ldpc.py +383 -0
- mplang/v2/libs/mpc/vole/silver.py +336 -0
- mplang/v2/runtime/__init__.py +15 -0
- mplang/v2/runtime/dialect_state.py +41 -0
- mplang/v2/runtime/interpreter.py +871 -0
- mplang/v2/runtime/object_store.py +194 -0
- mplang/v2/runtime/value.py +141 -0
- {mplang_nightly-0.1.dev158.dist-info → mplang_nightly-0.1.dev268.dist-info}/METADATA +24 -17
- mplang_nightly-0.1.dev268.dist-info/RECORD +180 -0
- {mplang_nightly-0.1.dev158.dist-info → mplang_nightly-0.1.dev268.dist-info}/WHEEL +1 -1
- mplang/core/__init__.py +0 -92
- mplang/device.py +0 -340
- mplang/kernels/builtin.py +0 -207
- mplang/ops/crypto.py +0 -109
- mplang/ops/ibis_cc.py +0 -139
- mplang/ops/sql.py +0 -61
- mplang/protos/v1alpha1/mpir_pb2_grpc.py +0 -3
- mplang/runtime/link_comm.py +0 -131
- mplang/simp/smpc.py +0 -201
- mplang/utils/table_utils.py +0 -73
- mplang_nightly-0.1.dev158.dist-info/RECORD +0 -77
- /mplang/{core → v1/core}/mask.py +0 -0
- /mplang/{protos → v1/protos}/v1alpha1/mpir_pb2.py +0 -0
- /mplang/{runtime → v1/runtime}/exceptions.py +0 -0
- /mplang/{runtime → v1/runtime}/http_api.md +0 -0
- /mplang/{kernels → v1/simp}/__init__.py +0 -0
- /mplang/{utils → v1/utils}/__init__.py +0 -0
- /mplang/{utils → v1/utils}/crypto.py +0 -0
- /mplang/{utils → v1/utils}/func_utils.py +0 -0
- /mplang/{utils → v1/utils}/spu_utils.py +0 -0
- {mplang_nightly-0.1.dev158.dist-info → mplang_nightly-0.1.dev268.dist-info}/entry_points.txt +0 -0
- {mplang_nightly-0.1.dev158.dist-info → mplang_nightly-0.1.dev268.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# Copyright 2025 Ant Group Co., Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Mock TEE backend implementation for local testing.
|
|
16
|
+
|
|
17
|
+
WARNING: This implementation is NOT SECURE and should only be used for
|
|
18
|
+
development and testing purposes. It simulates TEE attestation without
|
|
19
|
+
any actual hardware security guarantees.
|
|
20
|
+
|
|
21
|
+
For production deployments, use real TEE backends that integrate with
|
|
22
|
+
hardware attestation (Intel SGX DCAP, AMD SEV-SNP, etc.).
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import base64
|
|
28
|
+
import warnings
|
|
29
|
+
from dataclasses import dataclass
|
|
30
|
+
from typing import TYPE_CHECKING, Any, ClassVar
|
|
31
|
+
|
|
32
|
+
import numpy as np
|
|
33
|
+
|
|
34
|
+
from mplang.v2.backends.crypto_impl import BytesValue, PublicKeyValue
|
|
35
|
+
from mplang.v2.dialects import tee
|
|
36
|
+
from mplang.v2.edsl import serde
|
|
37
|
+
from mplang.v2.runtime.value import Value
|
|
38
|
+
|
|
39
|
+
if TYPE_CHECKING:
|
|
40
|
+
from mplang.v2.edsl.graph import Operation
|
|
41
|
+
from mplang.v2.runtime.interpreter import Interpreter
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# ==============================================================================
|
|
45
|
+
# --- Mock Data Structures
|
|
46
|
+
# ==============================================================================
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@serde.register_class
|
|
50
|
+
@dataclass
|
|
51
|
+
class MockQuoteValue(Value):
|
|
52
|
+
"""Mock TEE quote structure.
|
|
53
|
+
|
|
54
|
+
In production, this would contain:
|
|
55
|
+
- Platform-specific attestation data
|
|
56
|
+
- Signature from TEE hardware
|
|
57
|
+
- Measurement (MRENCLAVE/MRTD)
|
|
58
|
+
- User-provided report_data (bound public key hash)
|
|
59
|
+
|
|
60
|
+
For mock purposes, we store the bound public key directly.
|
|
61
|
+
The quote is the only mock-specific structure needed because it represents
|
|
62
|
+
hardware-generated attestation data that doesn't exist outside a real TEE.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
_serde_kind: ClassVar[str] = "tee_impl.MockQuoteValue"
|
|
66
|
+
|
|
67
|
+
platform: str
|
|
68
|
+
bound_pk: bytes # The public key bytes bound in this quote (32 bytes for x25519)
|
|
69
|
+
suite: str # The KEM suite (e.g., "x25519")
|
|
70
|
+
|
|
71
|
+
def to_json(self) -> dict[str, Any]:
|
|
72
|
+
return {
|
|
73
|
+
"platform": self.platform,
|
|
74
|
+
"bound_pk": base64.b64encode(self.bound_pk).decode("ascii"),
|
|
75
|
+
"suite": self.suite,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@classmethod
|
|
79
|
+
def from_json(cls, data: dict[str, Any]) -> MockQuoteValue:
|
|
80
|
+
return cls(
|
|
81
|
+
platform=data["platform"],
|
|
82
|
+
bound_pk=base64.b64decode(data["bound_pk"]),
|
|
83
|
+
suite=data["suite"],
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
def to_bytes(self) -> bytes:
|
|
87
|
+
"""Serialize quote for transmission."""
|
|
88
|
+
# Format: [platform_len:1][platform][suite_len:1][suite][pk]
|
|
89
|
+
platform_bytes = self.platform.encode("utf-8")
|
|
90
|
+
suite_bytes = self.suite.encode("utf-8")
|
|
91
|
+
return (
|
|
92
|
+
bytes([len(platform_bytes)])
|
|
93
|
+
+ platform_bytes
|
|
94
|
+
+ bytes([len(suite_bytes)])
|
|
95
|
+
+ suite_bytes
|
|
96
|
+
+ self.bound_pk
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
@classmethod
|
|
100
|
+
def from_bytes(cls, data: bytes) -> MockQuoteValue:
|
|
101
|
+
"""Deserialize quote."""
|
|
102
|
+
platform_len = data[0]
|
|
103
|
+
platform = data[1 : 1 + platform_len].decode("utf-8")
|
|
104
|
+
suite_start = 1 + platform_len
|
|
105
|
+
suite_len = data[suite_start]
|
|
106
|
+
suite = data[suite_start + 1 : suite_start + 1 + suite_len].decode("utf-8")
|
|
107
|
+
pk_start = suite_start + 1 + suite_len
|
|
108
|
+
bound_pk = data[pk_start : pk_start + 32]
|
|
109
|
+
return cls(platform=platform, bound_pk=bound_pk, suite=suite)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# ==============================================================================
|
|
113
|
+
# --- Implementation Functions
|
|
114
|
+
# ==============================================================================
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _emit_mock_warning(operation: str) -> None:
|
|
118
|
+
"""Emit a warning that mock TEE is being used."""
|
|
119
|
+
warnings.warn(
|
|
120
|
+
f"Insecure mock TEE operation '{operation}' in use. "
|
|
121
|
+
"NOT secure; for local testing only.",
|
|
122
|
+
UserWarning,
|
|
123
|
+
stacklevel=4,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@tee.quote_gen_p.def_impl
|
|
128
|
+
def _quote_gen_impl(
|
|
129
|
+
interpreter: Interpreter,
|
|
130
|
+
op: Operation,
|
|
131
|
+
pk: PublicKeyValue,
|
|
132
|
+
) -> MockQuoteValue:
|
|
133
|
+
"""Generate a mock TEE quote binding the provided public key.
|
|
134
|
+
|
|
135
|
+
In a real TEE, this would:
|
|
136
|
+
1. Hash the public key into report_data
|
|
137
|
+
2. Generate a hardware-signed attestation report
|
|
138
|
+
3. Package everything into a quote structure
|
|
139
|
+
|
|
140
|
+
For mock, we just wrap the public key in a MockQuoteValue.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
interpreter: The interpreter context
|
|
144
|
+
op: The operation being executed
|
|
145
|
+
pk: Public key to bind (from crypto.kem_keygen)
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
MockQuoteValue containing the bound public key
|
|
149
|
+
"""
|
|
150
|
+
_emit_mock_warning("tee.quote_gen")
|
|
151
|
+
|
|
152
|
+
if not isinstance(pk, PublicKeyValue):
|
|
153
|
+
raise TypeError(
|
|
154
|
+
f"quote_gen expects PublicKeyValue from crypto.kem_keygen, "
|
|
155
|
+
f"got {type(pk).__name__}"
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
# In a real implementation, the platform would be detected from the environment
|
|
159
|
+
platform = "mock"
|
|
160
|
+
|
|
161
|
+
return MockQuoteValue(
|
|
162
|
+
platform=platform,
|
|
163
|
+
bound_pk=pk.key_bytes,
|
|
164
|
+
suite=pk.suite,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@tee.attest_p.def_impl
|
|
169
|
+
def _attest_impl(
|
|
170
|
+
interpreter: Interpreter,
|
|
171
|
+
op: Operation,
|
|
172
|
+
quote: MockQuoteValue | BytesValue,
|
|
173
|
+
) -> PublicKeyValue:
|
|
174
|
+
"""Verify a mock quote and extract the attested public key.
|
|
175
|
+
|
|
176
|
+
In a real implementation, this would:
|
|
177
|
+
1. Verify the quote signature against TEE vendor root certificates
|
|
178
|
+
2. Check the measurement matches expected code hash
|
|
179
|
+
3. Extract and return the verified public key
|
|
180
|
+
|
|
181
|
+
For mock, we just extract the public key directly (no real verification).
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
interpreter: The interpreter context
|
|
185
|
+
op: The operation being executed
|
|
186
|
+
quote: The quote to verify (MockQuoteValue or BytesValue for serialized quotes)
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
PublicKeyValue - the verified public key, ready for use with kem_derive
|
|
190
|
+
"""
|
|
191
|
+
_emit_mock_warning("tee.attest")
|
|
192
|
+
|
|
193
|
+
# Handle different quote formats
|
|
194
|
+
if isinstance(quote, MockQuoteValue):
|
|
195
|
+
mock_quote = quote
|
|
196
|
+
elif isinstance(quote, BytesValue):
|
|
197
|
+
mock_quote = MockQuoteValue.from_bytes(quote.unwrap())
|
|
198
|
+
elif isinstance(quote, (bytes, bytearray, np.ndarray)):
|
|
199
|
+
# Fallback for raw bytes (backwards compatibility / real TEE interop)
|
|
200
|
+
if isinstance(quote, np.ndarray):
|
|
201
|
+
quote = bytes(quote)
|
|
202
|
+
mock_quote = MockQuoteValue.from_bytes(bytes(quote))
|
|
203
|
+
else:
|
|
204
|
+
raise TypeError(f"Expected MockQuoteValue or BytesValue, got {type(quote)}")
|
|
205
|
+
|
|
206
|
+
# Return a real PublicKeyValue that can be used directly with kem_derive
|
|
207
|
+
return PublicKeyValue(
|
|
208
|
+
suite=mock_quote.suite,
|
|
209
|
+
key_bytes=mock_quote.bound_pk,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
__all__ = [
|
|
214
|
+
"MockQuoteValue",
|
|
215
|
+
]
|