You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by ms...@apache.org on 2020/06/25 01:05:43 UTC

[incubator-teaclave] branch master updated: [docs] Add tutorial of writing functions in Python (#370)

This is an automated email from the ASF dual-hosted git repository.

mssun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git


The following commit(s) were added to refs/heads/master by this push:
     new 70f0255  [docs] Add tutorial of writing functions in Python (#370)
70f0255 is described below

commit 70f02555f58ad3f58640f75f4aadab3307230a0b
Author: Mingshen Sun <bo...@mssun.me>
AuthorDate: Wed Jun 24 18:05:37 2020 -0700

    [docs] Add tutorial of writing functions in Python (#370)
---
 README.md                   |  1 +
 docs/README.md              |  3 ++-
 docs/functions-in-python.md | 63 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 2589a90..09fa0d4 100644
--- a/README.md
+++ b/README.md
@@ -35,6 +35,7 @@ platform, making computation on privacy-sensitive data safe and simple.
 ### Try Teaclave
 
 - [My First Function](docs/my-first-function.md)
+- [Write Functions in Python](docs/functions-in-python.md)
 - [How to Add Built-in Functions](docs/builtin-functions.md)
 
 ### Design
diff --git a/docs/README.md b/docs/README.md
index 992837d..5c68d5b 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -6,8 +6,9 @@ permalink: /docs/
 
 ## Try
 
-- [How to Add Built-in Functions](builtin-functions.md)
 - [My First Function](my-first-function.md)
+- [Function in Python](functions-in-python.md)
+- [How to Add Built-in Functions](builtin-functions.md)
 
 ## Design
 
diff --git a/docs/functions-in-python.md b/docs/functions-in-python.md
new file mode 100644
index 0000000..5549a20
--- /dev/null
+++ b/docs/functions-in-python.md
@@ -0,0 +1,63 @@
+---
+permalink: /docs/functions-in-python
+---
+
+# Write Functions in Python
+
+The Teaclave platform provides a convenient way to register a customized
+function written in Python, and the function is interpreted at runtime in an
+isolated trusted execution environment (i.e., Intel SGX).
+
+## Entrypoint
+
+Here is an simple example of an echo function:
+
+```python
+def entrypoint(argv):
+    assert argv[0] == 'message'
+    assert argv[1] is not None
+    return argv[1]
+```
+
+The `entrypoint` function defined above is the "entrypoint" to executing the
+function. It takes one argument which is a list of arguments of this echo
+function. The return value of the `entrypoint` function will be passed back to
+the client.
+
+::: tip NOTE
+Note that the function arguments in key-value format passed from the platform
+are flattened into a list. For example, the `{"message": "Hello, Teaclave!"}`
+arguments will become `"message"` (`argv[0]`) and `"Hello, Teaclave!"`
+(`argv[1]`).
+:::
+
+## Modules
+
+Current Python executor (i.e., MesaPy) already supports many modules of the
+original Python standard library such as `marshal`, `math`, `binascii`,
+`itertools`, `micronumpy`. You can find a full list of available modules in the
+[document of MesaPy for SGX](https://github.com/mesalock-linux/mesapy/blob/sgx/sgx/README.md).
+
+Besides these modules for general computation, you may curious about doing file
+I/O in customized Python function. We provides APIs to integrated with the
+executor runtime to read/write files registered along with the task. You can
+either open a file through the `teaclave_open` function or with the `teaclave`
+module like this:
+
+```python
+# open input via built-in teaclave_open
+with teaclave_open("input_file", "rb") as f:
+    line = f.readline()
+
+# open input via teaclave module
+from teaclave import open
+with open("output_file", "wb") as f:
+    f.write("This message is from Mesapy!")
+```
+
+Either function will give an `file` object in Python, you can use it to read
+lines or write data. And the first argument is the key of the registered
+input/output files.
+
+You can learn more about advanced usages in the example of
+[logistic regression in Python](https://github.com/apache/incubator-teaclave/tree/master/examples/python).


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org