Fidelius开发教程

来自典枢
跳到导航 跳到搜索

介绍

简介

Fidelius 提供了自定义算法的能力。此处的算法用于分析数据,并将分析结果交付给用户。

Fidelius 的算法实现基于 C/C++1。一个算法是指一个运行在可信执行环境(TEE)中的代码,由于目前主要的支持硬件为 Intel SGX,因此编程方式符合 Intel SGX 的编程规范。

本文介绍的算法开发基于 Fidelius 的最新版本,请检查自己的版本。

Hello World

需要 git clone example,然后软链接到 example 目录下,需要解释编译的步骤,release 版本签名的步骤,执行的步骤需要解释各个文件的作用和意义 下面来看一个完整的 hello world 程序。

  1. include ”corecommon/ crypto / stdeth . h”
  1. include ” stbox / tsgx / log . h”
  1. include ”ypc_t/ a n a l y z e r / algo_wrapper . h”
  1. include ”ypc_t/ a n a l y z e r /macro . h”

class hello_world {

public :

i n l i n e stbox : : bytes do_parse ( const stbox : : bytes &param ) {

LOG(INFO) << ” h e l l o ␣ world ” ;

return param ;

}

};

ypc : : algo_wrapper<ypc : : crypto : : eth_sgx_crypto ,

ypc : : noinput_data_stream ,

hello_world , ypc : : l o c a l _ r e s u l t >

pw ;

YPC_PARSER_IMPL(pw ) ;

algo_wrapper

template<typename Crypto , typename DataSession , typename ParserT ,

typename Result , typename ModelT = void ,

template <typename> c l a s s DataAllowancePolicy = ignore_data_allowance ,

template <typename> c l a s s ModelAllowancePolicy = ignore_model_allowance>

class algo_warpper ;

代码来源core/include/ypc_t/analyzer/algo_wrapper.h

Crypto:密码协议簇,目前支持 ypc::crypto::eth_sgx_crypto,兼容以太坊。

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<...>。

DataAllowancePolicy,表示数据源的许可验证策略,支持 ignore_data_allowance,check_data_allowance。

ModelAllowancePolicy,表示模型的许可验证策略,支持 ignore_model_allowance,check_model_allowance。

通过对上述参数的选择和组合,可以支持不同的场景。

HPDA

算法的主体通常使用 HPDA(High Performance Data Analysis)完成。HPDA 算法由输入(input)、处理单元(processor)、输出(output)三种不同的功能单元 组成,其中输入、处理有一个或多个,输出仅有一个。

一个 HPDA 算法可以表示一个如下的有向图,其中节点表示功能单元,即输入、处理单元或输出,边表示数据。自然地,输入只有出边,输出只有入边, 处理单元有一个或多个入边,且有一个或多个出边。

算法的执行过程一般分为连个阶段:1)构造 HPDA 的图;2)运行算法。算法一旦开始运行,则会开始读取输入、产生输出,直到所有的输入读取完成。

Iris 为例

c l a s s enclave_iris_means_parser {

public :

enclave_iris_means_parser ( ypc : : data_source ∗ source )

m_source ( source ){};

i n l i n e stbox : : bytes do_parse ( const stbox : : bytes &param ) {

ypc : : to_type<extra_nt_t> c o n v e r t e r ( m_source ) ;

transform_format t r a n s (& c o n v e r t e r ) ;

typedef hpda : : algorithm : : kmeans : : kmeans_processor<

hpda : : ntobject ,

i r i s _ d a t a , double , i i d >

kmeans_t ;

kmeans_t km(&trans , 3 , 0 . 0 0 1 ) ;

hpda : : output : : memory_output

mo(km. means_stream ( ) ) ;

mo. get_engine()−>run ( ) ;

. . .

}

protected :

ypc : : data_source ∗m_source ;

};

Y

该算法可以表示为如下的图

Iris例图1.png

图中的边—数据类型

在 HPDA 所表示的有向图中,一条边可以表示为 (A, B),我们称 A 是 B 的输入源,A 的输出数据类型和 B 的输入数据类型必须一致,否则会产生编译错误。我们将 A 的输出数据类型(或 B 的输入数据类型)称为边 (A, B) 上的数据类型。

边的数据类型为

template<typename . . . ARGS>

f f : : u t i l : : n t o b j e c t ;

300PX