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.
Files changed (191) hide show
  1. mplang/__init__.py +21 -45
  2. mplang/py.typed +13 -0
  3. mplang/v1/__init__.py +157 -0
  4. mplang/v1/_device.py +602 -0
  5. mplang/{analysis → v1/analysis}/__init__.py +1 -1
  6. mplang/{analysis → v1/analysis}/diagram.py +5 -7
  7. mplang/v1/core/__init__.py +157 -0
  8. mplang/{core → v1/core}/cluster.py +30 -14
  9. mplang/{core → v1/core}/comm.py +5 -1
  10. mplang/{core → v1/core}/context_mgr.py +1 -1
  11. mplang/{core/dtype.py → v1/core/dtypes.py} +44 -2
  12. mplang/{core → v1/core}/expr/__init__.py +7 -7
  13. mplang/{core → v1/core}/expr/ast.py +13 -14
  14. mplang/{core → v1/core}/expr/evaluator.py +65 -24
  15. mplang/{core → v1/core}/expr/printer.py +24 -18
  16. mplang/{core → v1/core}/expr/transformer.py +3 -3
  17. mplang/{core → v1/core}/expr/utils.py +2 -2
  18. mplang/{core → v1/core}/expr/visitor.py +1 -1
  19. mplang/{core → v1/core}/expr/walk.py +1 -1
  20. mplang/{core → v1/core}/interp.py +6 -6
  21. mplang/{core → v1/core}/mpir.py +23 -16
  22. mplang/{core → v1/core}/mpobject.py +6 -6
  23. mplang/{core → v1/core}/mptype.py +13 -10
  24. mplang/{core → v1/core}/pfunc.py +4 -4
  25. mplang/{core → v1/core}/primitive.py +106 -201
  26. mplang/{core → v1/core}/table.py +36 -8
  27. mplang/{core → v1/core}/tensor.py +1 -1
  28. mplang/{core → v1/core}/tracer.py +9 -9
  29. mplang/{api.py → v1/host.py} +38 -6
  30. mplang/v1/kernels/__init__.py +41 -0
  31. mplang/{kernels → v1/kernels}/base.py +1 -1
  32. mplang/v1/kernels/basic.py +240 -0
  33. mplang/{kernels → v1/kernels}/context.py +42 -27
  34. mplang/{kernels → v1/kernels}/crypto.py +44 -37
  35. mplang/v1/kernels/fhe.py +858 -0
  36. mplang/{kernels → v1/kernels}/mock_tee.py +12 -13
  37. mplang/{kernels → v1/kernels}/phe.py +263 -57
  38. mplang/{kernels → v1/kernels}/spu.py +137 -48
  39. mplang/{kernels → v1/kernels}/sql_duckdb.py +12 -15
  40. mplang/{kernels → v1/kernels}/stablehlo.py +30 -23
  41. mplang/v1/kernels/value.py +626 -0
  42. mplang/{ops → v1/ops}/__init__.py +5 -16
  43. mplang/{ops → v1/ops}/base.py +2 -5
  44. mplang/{ops/builtin.py → v1/ops/basic.py} +34 -26
  45. mplang/v1/ops/crypto.py +262 -0
  46. mplang/v1/ops/fhe.py +272 -0
  47. mplang/{ops → v1/ops}/jax_cc.py +33 -68
  48. mplang/v1/ops/nnx_cc.py +168 -0
  49. mplang/{ops → v1/ops}/phe.py +16 -4
  50. mplang/{ops → v1/ops}/spu.py +3 -5
  51. mplang/v1/ops/sql_cc.py +303 -0
  52. mplang/{ops → v1/ops}/tee.py +9 -24
  53. mplang/{protos → v1/protos}/v1alpha1/mpir_pb2.pyi +71 -21
  54. mplang/v1/protos/v1alpha1/value_pb2.py +34 -0
  55. mplang/v1/protos/v1alpha1/value_pb2.pyi +169 -0
  56. mplang/{runtime → v1/runtime}/__init__.py +2 -2
  57. mplang/v1/runtime/channel.py +230 -0
  58. mplang/{runtime → v1/runtime}/cli.py +35 -20
  59. mplang/{runtime → v1/runtime}/client.py +19 -8
  60. mplang/{runtime → v1/runtime}/communicator.py +59 -15
  61. mplang/{runtime → v1/runtime}/data_providers.py +80 -19
  62. mplang/{runtime → v1/runtime}/driver.py +30 -12
  63. mplang/v1/runtime/link_comm.py +196 -0
  64. mplang/{runtime → v1/runtime}/server.py +58 -42
  65. mplang/{runtime → v1/runtime}/session.py +57 -71
  66. mplang/{runtime → v1/runtime}/simulation.py +55 -28
  67. mplang/v1/simp/api.py +353 -0
  68. mplang/{simp → v1/simp}/mpi.py +8 -9
  69. mplang/{simp/__init__.py → v1/simp/party.py} +19 -145
  70. mplang/{simp → v1/simp}/random.py +21 -22
  71. mplang/v1/simp/smpc.py +238 -0
  72. mplang/v1/utils/table_utils.py +185 -0
  73. mplang/v2/__init__.py +424 -0
  74. mplang/v2/backends/__init__.py +57 -0
  75. mplang/v2/backends/bfv_impl.py +705 -0
  76. mplang/v2/backends/channel.py +217 -0
  77. mplang/v2/backends/crypto_impl.py +723 -0
  78. mplang/v2/backends/field_impl.py +454 -0
  79. mplang/v2/backends/func_impl.py +107 -0
  80. mplang/v2/backends/phe_impl.py +148 -0
  81. mplang/v2/backends/simp_design.md +136 -0
  82. mplang/v2/backends/simp_driver/__init__.py +41 -0
  83. mplang/v2/backends/simp_driver/http.py +168 -0
  84. mplang/v2/backends/simp_driver/mem.py +280 -0
  85. mplang/v2/backends/simp_driver/ops.py +135 -0
  86. mplang/v2/backends/simp_driver/state.py +60 -0
  87. mplang/v2/backends/simp_driver/values.py +52 -0
  88. mplang/v2/backends/simp_worker/__init__.py +29 -0
  89. mplang/v2/backends/simp_worker/http.py +354 -0
  90. mplang/v2/backends/simp_worker/mem.py +102 -0
  91. mplang/v2/backends/simp_worker/ops.py +167 -0
  92. mplang/v2/backends/simp_worker/state.py +49 -0
  93. mplang/v2/backends/spu_impl.py +275 -0
  94. mplang/v2/backends/spu_state.py +187 -0
  95. mplang/v2/backends/store_impl.py +62 -0
  96. mplang/v2/backends/table_impl.py +838 -0
  97. mplang/v2/backends/tee_impl.py +215 -0
  98. mplang/v2/backends/tensor_impl.py +519 -0
  99. mplang/v2/cli.py +603 -0
  100. mplang/v2/cli_guide.md +122 -0
  101. mplang/v2/dialects/__init__.py +36 -0
  102. mplang/v2/dialects/bfv.py +665 -0
  103. mplang/v2/dialects/crypto.py +689 -0
  104. mplang/v2/dialects/dtypes.py +378 -0
  105. mplang/v2/dialects/field.py +210 -0
  106. mplang/v2/dialects/func.py +135 -0
  107. mplang/v2/dialects/phe.py +723 -0
  108. mplang/v2/dialects/simp.py +944 -0
  109. mplang/v2/dialects/spu.py +349 -0
  110. mplang/v2/dialects/store.py +63 -0
  111. mplang/v2/dialects/table.py +407 -0
  112. mplang/v2/dialects/tee.py +346 -0
  113. mplang/v2/dialects/tensor.py +1175 -0
  114. mplang/v2/edsl/README.md +279 -0
  115. mplang/v2/edsl/__init__.py +99 -0
  116. mplang/v2/edsl/context.py +311 -0
  117. mplang/v2/edsl/graph.py +463 -0
  118. mplang/v2/edsl/jit.py +62 -0
  119. mplang/v2/edsl/object.py +53 -0
  120. mplang/v2/edsl/primitive.py +284 -0
  121. mplang/v2/edsl/printer.py +119 -0
  122. mplang/v2/edsl/registry.py +207 -0
  123. mplang/v2/edsl/serde.py +375 -0
  124. mplang/v2/edsl/tracer.py +614 -0
  125. mplang/v2/edsl/typing.py +816 -0
  126. mplang/v2/kernels/Makefile +30 -0
  127. mplang/v2/kernels/__init__.py +23 -0
  128. mplang/v2/kernels/gf128.cpp +148 -0
  129. mplang/v2/kernels/ldpc.cpp +82 -0
  130. mplang/v2/kernels/okvs.cpp +283 -0
  131. mplang/v2/kernels/okvs_opt.cpp +291 -0
  132. mplang/v2/kernels/py_kernels.py +398 -0
  133. mplang/v2/libs/collective.py +330 -0
  134. mplang/v2/libs/device/__init__.py +51 -0
  135. mplang/v2/libs/device/api.py +813 -0
  136. mplang/v2/libs/device/cluster.py +352 -0
  137. mplang/v2/libs/ml/__init__.py +23 -0
  138. mplang/v2/libs/ml/sgb.py +1861 -0
  139. mplang/v2/libs/mpc/__init__.py +41 -0
  140. mplang/v2/libs/mpc/_utils.py +99 -0
  141. mplang/v2/libs/mpc/analytics/__init__.py +35 -0
  142. mplang/v2/libs/mpc/analytics/aggregation.py +372 -0
  143. mplang/v2/libs/mpc/analytics/groupby.md +99 -0
  144. mplang/v2/libs/mpc/analytics/groupby.py +331 -0
  145. mplang/v2/libs/mpc/analytics/permutation.py +386 -0
  146. mplang/v2/libs/mpc/common/constants.py +39 -0
  147. mplang/v2/libs/mpc/ot/__init__.py +32 -0
  148. mplang/v2/libs/mpc/ot/base.py +222 -0
  149. mplang/v2/libs/mpc/ot/extension.py +477 -0
  150. mplang/v2/libs/mpc/ot/silent.py +217 -0
  151. mplang/v2/libs/mpc/psi/__init__.py +40 -0
  152. mplang/v2/libs/mpc/psi/cuckoo.py +228 -0
  153. mplang/v2/libs/mpc/psi/okvs.py +49 -0
  154. mplang/v2/libs/mpc/psi/okvs_gct.py +79 -0
  155. mplang/v2/libs/mpc/psi/oprf.py +310 -0
  156. mplang/v2/libs/mpc/psi/rr22.py +344 -0
  157. mplang/v2/libs/mpc/psi/unbalanced.py +200 -0
  158. mplang/v2/libs/mpc/vole/__init__.py +31 -0
  159. mplang/v2/libs/mpc/vole/gilboa.py +327 -0
  160. mplang/v2/libs/mpc/vole/ldpc.py +383 -0
  161. mplang/v2/libs/mpc/vole/silver.py +336 -0
  162. mplang/v2/runtime/__init__.py +15 -0
  163. mplang/v2/runtime/dialect_state.py +41 -0
  164. mplang/v2/runtime/interpreter.py +871 -0
  165. mplang/v2/runtime/object_store.py +194 -0
  166. mplang/v2/runtime/value.py +141 -0
  167. {mplang_nightly-0.1.dev158.dist-info → mplang_nightly-0.1.dev268.dist-info}/METADATA +24 -17
  168. mplang_nightly-0.1.dev268.dist-info/RECORD +180 -0
  169. {mplang_nightly-0.1.dev158.dist-info → mplang_nightly-0.1.dev268.dist-info}/WHEEL +1 -1
  170. mplang/core/__init__.py +0 -92
  171. mplang/device.py +0 -340
  172. mplang/kernels/builtin.py +0 -207
  173. mplang/ops/crypto.py +0 -109
  174. mplang/ops/ibis_cc.py +0 -139
  175. mplang/ops/sql.py +0 -61
  176. mplang/protos/v1alpha1/mpir_pb2_grpc.py +0 -3
  177. mplang/runtime/link_comm.py +0 -131
  178. mplang/simp/smpc.py +0 -201
  179. mplang/utils/table_utils.py +0 -73
  180. mplang_nightly-0.1.dev158.dist-info/RECORD +0 -77
  181. /mplang/{core → v1/core}/mask.py +0 -0
  182. /mplang/{protos → v1/protos}/v1alpha1/mpir_pb2.py +0 -0
  183. /mplang/{runtime → v1/runtime}/exceptions.py +0 -0
  184. /mplang/{runtime → v1/runtime}/http_api.md +0 -0
  185. /mplang/{kernels → v1/simp}/__init__.py +0 -0
  186. /mplang/{utils → v1/utils}/__init__.py +0 -0
  187. /mplang/{utils → v1/utils}/crypto.py +0 -0
  188. /mplang/{utils → v1/utils}/func_utils.py +0 -0
  189. /mplang/{utils → v1/utils}/spu_utils.py +0 -0
  190. {mplang_nightly-0.1.dev158.dist-info → mplang_nightly-0.1.dev268.dist-info}/entry_points.txt +0 -0
  191. {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
+ ]