Skip to content

Commit

Permalink
add developer doc
Browse files Browse the repository at this point in the history
  • Loading branch information
chmwang authored and chmwang committed Oct 15, 2024
1 parent e1c730a commit b7745ae
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 55 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ In the figure, the trusted third party can choose to be a blockchain network. As
## Quick Start
Fidelius runs on Intel SGX and requires confirmation that the hardware environment is equipped with a supported central processing unit (CPU), BIOS settings, and the installation of drivers and related software. Even without the required hardware environment, the debug version can still run after installing the Intel SGX SDK.

If you have the relevant hardware, please refer to the [documentation](./doc/Release_EN.md) and run the Fidelius release version.

If you do not have the relevant hardware, please refer to the [documentation](./doc/Debug_EN.md) and run the Fidelius debug version.
Please refer to the [documentation](./doc/Release_EN.md) to compile and run Fidelius.

## Advanced Use
- [developer documentation](./doc/Developer_ZH.md)
- [doxygen documentation](https://doc-fidelius.yeez.tech/index.html)
- [wiki documentation](https://doc-dianshu.yeez.tech/index.php/Fidelius)

Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ compile_project() {

cmake -DCMAKE_INSTALL_PREFIX=$HOME -DCMAKE_BUILD_TYPE=$build_type -DCMAKE_DEBUG_POSTFIX=$suffix -B $build_path
cmake --build $build_path -j
#cmake --install $build_path
cmake --install $build_path

if [[ "$build_type" == "Release" ]]; then
hash_dir=${ypc_home}/hash_hex
Expand Down
Empty file added doc/Developer_EN.md
Empty file.
140 changes: 140 additions & 0 deletions doc/Developer_ZH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
开发 Fidelius 应用
------------------------
### 安装 Fidelius SDK
开发应用需安装 Fidelius 开发环境,即 Fidelius SDK,编译安装请参考[文档](./Release_ZH.md)

Fidelius 的默认安装目录为 `$HOME`,其目录结构如下:
```tree
.
├── include
├── lib
├── bin
```
- `include` 为 Fidelius 提供的头文件,`lib` 为 Fidelius 提供的动态、静态链接库。
- `bin` 为 Fidelius 提供的工具,如密码协议工具(yterminus)、数据加密工具(data_provider)、数据分析工具(fid_analyzer)等。


### 应用项目
一个典型的应用项目目录结构如下:
```tree
src
├── CMakeLists.txt
├── enclave
│ ├── CMakeLists.txt
│ ├── enclave.config.xml
│ ├── enclave_debug.lds
│ ├── enclave.lds
│ ├── enclave_private.pem
│ ├── enclave_parser.h
│ └── eparser.cpp
└── plugin
├── CMakeLists.txt
├── my_reader.cpp
└── my_reader.h
```
目录 `enclave` 包含了数据分析的核心算法,目录 `plugin` 定义了输入数据的读取方式。

#### `enclave` 目录说明

- `enclave.config.xml` 为 enclave 的配置文件,定义了产品号、安全版本号,为运行在可信执行环境中的程序分配了栈空间大小、堆空间大小等。
- `enclave_debug.lds``enclave.lds` 保留原始内容不做修改。
- `enclave_private.pem` 为签名 enclave 的私钥。

##### `enclave_parser.h` 说明
`enclave_parser.h` 中,需定义一个数据分析的类,例如 `my_enclave_parser`,并且需要在该类中实现数据分析的函数 `inline stbox::bytes do_parse(const stbox::bytes &param)`,以下是一个简单的实现方式。

```c++
// enclave_parser.h
class my_enclave_parser {
public:
my_enclave_parser(ypc::data_source<stbox::bytes> *source)
: m_source(source){};
inline stbox::bytes do_parse(const stbox::bytes &param) {
return stbox::bytes("Hello!");
}

protected:
ypc::data_source<stbox::bytes> *m_source;
};
```
除此之外,Fidelius 还提供了 [高性能计算框架 HPDA](./pdf/hpda.pdf) 帮助开发者实现 `do_parse` 函数、完成数据分析算法的开发,HPDA 中包括计算引擎、输入/输出、算子等。
##### `eparser.cpp` 说明
一个典型的 `eparser.cpp` 内容如下:
```c++
// eparser.cpp
#include "enclave_parser.h"
#include "ypc/core_t/analyzer/algo_wrapper.h"
#include "ypc/core_t/analyzer/macro.h"
#include "ypc/corecommon/crypto/stdeth.h"
using Crypto = ypc::crypto::eth_sgx_crypto;
ypc::algo_wrapper<Crypto, ypc::sealed_data_stream,
my_enclave_parser,
ypc::onchain_result<Crypto>> pw;
YPC_PARSER_IMPL(pw);
```

`eparser.cpp` 是 Fidelius 中算法模板 `ypc::algo_wrapper` 的实例化,算法模板的包括七个参数,可对参数进行组合以完成具体的需求场景(例如:[数据托管场景](./pdf/delegation.pdf)[链下交付场景](./pdf/offchain.pdf) 等)。

算法模板的定义以及参数的说明如下:
```c++
template<typename Crypto,
typename DataSession,
typename ParserT,
typename Result,
typename ModelT = void,
template <typename> class DataAllowancePolicy = ignore_data_allowance,
template <typename> class ModelAllowancePolicy = ignore_model_allowance
> class algo_wrapper;
```

- `Crypto`:密码算法簇,目前支持以下两种加密算法:
- `ypc::crypto::eth_sgx_crypto`:椭圆曲线为 Secp256k1,哈希函数为 sha3-256,对称加密算法为 Rijndael128GCM,兼容以太坊。
- `ypc::crypto::gmssl_sgx_crypto`:椭圆曲线为 SM2,哈希函数为 SM3,对称加密算法为 SM4。
- `DataSession`:数据源方式,支持:
- 无数据源(`noinput_data_stream`
- 单一未加密数据源(`raw_data_stream`
- 单一加密数据源(`sealed_data_stream`
- 多数据源(`multi_data_stream`)且其中每一个数据源都是加密数据。
- `ParserT`:表示自定义的算法类,由开发者自行开发。
- `Result`:表示结果的类型,支持
- 本地结果(`local_result`):结果不加密
- 链上结果(`onchain_result`):将结果发送到区块链中交付,要求数据大小能够上链
- 链下结果交付(`offchain_result`):结果不发送到区块链中
- 结果加密转发(`forward_result`):将结果转发给下一个计算任务使用
- `ModelT`: 表示模型参数的类型,是 `ff::util::ntobject<...>`,由开发者指定,默认为 `void`,即没有模型参数。
- `DataAllowancePolicy`: 表示数据源的许可验证策略:
- 支持不检查数据源的许可(`ignore_data_allowance`
- 检查数据源的许可(`check_data_allowance`
- `ModelAllowancePolicy`: 表示模型的许可验证策略:
- 支持不检查模型的许可(`ignore_model_allowance`
- 检查模型参数的许可(`check_model_allowance`

#### `plugin` 目录说明
数据分析中的输入数据是任意的,因此开发者提供开发算法的同时,需要提供读取数据的方式,通过实现以下接口定义读取数据的方式。
```c++
// 打开文件,创建文件指针
void *create_item_reader(const char *file_path, int len);

// 重置文件指针
int reset_for_read(void *handle);

// 从当前文件指针处获取一个 item,并存入 buf
int read_item_data(void *handle, char *buf, int *len);

// 关闭文件,重置文件指针
int close_item_reader(void *handle);

// 获取 item 个数
uint64_t get_item_number(void *handle);
```
### 参考文档
- [Fidelius 编程](./pdf/programming.pdf)
- [Fidelius 高性能计算框架(HPDA)](./pdf/hpda.pdf)
- [Fidelius 数据托管场景](./pdf/delegation.pdf)
- [Fidelius 链下交付场景](./pdf/offchain.pdf)
- [Fidelius 多数据融合场景](./pdf/multi_stream.pdf)
- [Fidelius 复杂任务场景](./pdf/task_graph.pdf)
5 changes: 2 additions & 3 deletions doc/README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ Fidelius 基于“数据可用不可见”思想,推出了面向数据合作
## 快速开始
Fidelius 基于 Intel SGX 运行,需确认硬件环境配备了支持的中央处理器(CPU),对 BIOS 进行设置,并安装驱动和相关软件。即使不具备该硬件环境,仍能在安装了 Intel SGX SDK 之后运行 Debug 版本。

具备相关硬件,请参考[文档](./Release_ZH.md),运行 Fidelius Release 版本。

不具备相关硬件,请参考[文档](./Debug_ZH.md),运行 Fidelius Debug 版本。
编译运行 Fidelius 请参考[文档](./Release_ZH.md)

## 进阶使用
- Fidelius 的 [开发者文档](./Developer_ZH.md)
- Fidelius 的 [doxygen 文档](https://doc-fidelius.yeez.tech/index.html)
- Fidelius 的 [wiki 文档](https://doc-dianshu.yeez.tech/index.php/Fidelius)
<!--### 测试-->
Expand Down
33 changes: 9 additions & 24 deletions doc/Release_EN.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Install Fidelius
Compile and Install Fidelius
------------------------
### Dependency
- Make sure you have the following operating system:
* Ubuntu 20.04 LTS Server 64bits

- Ensure that SGX is enabled in BIOS and CPU. Follow the [README](https://github.com/ayeks/SGX-hardware/blob/master/README.md) in [SGX-hardware](https://github.com/ayeks/SGX-hardware) or use the following method to confirm directly.
```
$ git clone https://github.com/ayeks/SGX-hardware.git
$ git clone --depth 1 --branch ayeks-patch-1 https://github.com/ayeks/SGX-hardware.git
$ cd SGX-hardware
$ gcc test-sgx.c -o test-sgx
$ ./test-sgx
Expand Down Expand Up @@ -34,44 +34,29 @@ $ sudo apt install libssl-dev libcurl4-openssl-dev protobuf-compiler libprotobuf
$ sudo apt install mysql-server libgoogle-glog-dev libboost-all-dev libmysqlcppconn-dev
```

### Install Fidelius
- Download the public key of YeeZTech's APT repository:
### Compile and Install Fidelius
```
$ wget -O yeez-pkey.pem https://repo.yeez.tech/public-key-yeez.txt
```

- Add the downloaded repository public key to the system:
```
$ sudo apt-key add yeez-pkey.pem
```

- Add the APT repository to the system software sources:
```
$ echo -e "deb https://repo.yeez.tech/ release main" | sudo tee -a /etc/apt/sources.list
```

- Update the system software sources and install the Fidelius component:
```
$ sudo apt update
$ sudo apt install ypc fflib
$ git clone --depth 1 https://github.com/YeeZTech/YeeZ-Privacy-Computing.git
$ cd YeeZ-Privacy-Computing && ./build.sh compile-project $COMPILE_MODE
```
**Note**: Set `$COMPILE_MODE` to "debug" for "sgx available" is 0, or set `$COMPILE_MODE` to "prerelease" for "sgx available" is 1.

Run Fidelius
------------------------
### Example (Use the Iris dataset as the data source to run the K-Means algorithm)
The K-Means clustering based on the Iris dataset is a very classic learning example in machine learning.
- Download example code:
```
$ git clone --depth 1 --branch v0.5.1 https://github.com/YeeZTech/YPC-algo-example.git
$ git clone --depth 1 https://github.com/YeeZTech/YPC-algo-example.git
```

- Compile example code:
```
$ cd YPC-algo-example && mkdir -p build && cd build
$ cmake -DCMAKE_PREFIX_PATH=$YPC_INSTALL_DIR/lib/cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
$ cmake -DCMAKE_PREFIX_PATH=$YPC_INSTALL_DIR/lib/cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE ..
$ make -j8
```
**Note**:`$YPC_INSTALL_DIR` is the installation path, which by default is `/opt/ypc`.
**Note**: `$YPC_INSTALL_DIR` is the installation path, which by default is `$HOME`. Set `$BUILD_TYPE` to "Debug" for debug mode, or set `$BUILD_TYPE` to "RelWithDebInfo" for prerelease/release mode.

- Run example code:
```
Expand Down
33 changes: 9 additions & 24 deletions doc/Release_ZH.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
安装 Fidelius
编译安装 Fidelius
------------------------
### 环境依赖

Expand All @@ -8,7 +8,7 @@

- 确保 BIOS 和 CPU 启用 SGX,请遵循 [SGX-hardware](https://github.com/ayeks/SGX-hardware) 中的 [README](https://github.com/ayeks/SGX-hardware/blob/master/README.md) 。或直接使用如下方式进行确认:
```
$ git clone https://github.com/ayeks/SGX-hardware.git
$ git clone --depth 1 --branch ayeks-patch-1 https://github.com/ayeks/SGX-hardware.git
$ cd SGX-hardware
$ gcc test-sgx.c -o test-sgx
$ ./test-sgx
Expand Down Expand Up @@ -36,27 +36,12 @@ $ sudo apt install libssl-dev libcurl4-openssl-dev protobuf-compiler libprotobuf
$ sudo apt install mysql-server libgoogle-glog-dev libboost-all-dev libmysqlcppconn-dev
```

### 安装 Fidelius 组件
- 下载熠智科技 apt 仓库的公钥:
### 编译安装 Fidelius
```
$ wget -O yeez-pkey.pem https://repo.yeez.tech/public-key-yeez.txt
```

- 将下载的仓库公钥添加进系统:
```
$ sudo apt-key add yeez-pkey.pem
```

- 将熠智科技 apt 仓库添加进系统软件源:
```
$ echo -e "deb https://repo.yeez.tech/ release main" | sudo tee -a /etc/apt/sources.list
```

- 更新系统软件源并安装 Fidelius 组件:
```
$ sudo apt update
$ sudo apt install ypc fflib
$ git clone --depth 1 https://github.com/YeeZTech/YeeZ-Privacy-Computing.git
$ cd YeeZ-Privacy-Computing && ./build.sh compile-project $COMPILE_MODE
```
**注意**:若 "sgx available" 的值为0,将 `$COMPILE_MODE` 设置为 "debug";否则,将 `$COMPILE_MODE` 设置为 "prerelease"。


运行 Fidelius
Expand All @@ -65,16 +50,16 @@ $ sudo apt install ypc fflib
基于 Iris 数据集的 K-Means 聚类是机器学习中一个非常经典的学习示例。
- 下载示例代码:
```
$ git clone --depth 1 --branch v0.5.1 https://github.com/YeeZTech/YPC-algo-example.git
$ git clone --depth 1 https://github.com/YeeZTech/YPC-algo-example.git
```

- 编译示例代码:
```
$ cd YPC-algo-example && mkdir -p build && cd build
$ cmake -DCMAKE_PREFIX_PATH=$YPC_INSTALL_DIR/lib/cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
$ cmake -DCMAKE_PREFIX_PATH=$YPC_INSTALL_DIR/lib/cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE ..
$ make -j8
```
**注意**:`$YPC_INSTALL_DIR`为 Fidelius 组件安装路径,默认情况下为系统路径`/opt/ypc`
**注意**`$YPC_INSTALL_DIR` 为 Fidelius 组件安装路径,默认情况下的路径为 `$HOME`。若以 debug 模式编译,将 `$BUILD_TYPE` 设置为 "Debug";否则,将 `$BUILD_TYPE` 设置为 "RelWithDebInfo"

- 运行示例代码:
```
Expand Down
Binary file added doc/pdf/delegation.pdf
Binary file not shown.
Binary file added doc/pdf/hpda.pdf
Binary file not shown.
Binary file added doc/pdf/multi_stream.pdf
Binary file not shown.
Binary file added doc/pdf/offchain.pdf
Binary file not shown.
Binary file added doc/pdf/programming.pdf
Binary file not shown.
Binary file added doc/pdf/shukey.pdf
Binary file not shown.
Binary file added doc/pdf/task_graph.pdf
Binary file not shown.

0 comments on commit b7745ae

Please sign in to comment.