Opentss核心函数使用

主要学习核心函数的使用

核心函数

主要是密钥生成,签名和通信,采用(t,n)的门限,使用的是secp256k1曲线。

消息传递

消息结构

如下,是一个枚举结构,由process_beginmsg_handler返回

pub enum SendingMessages {
    NormalMessage(String, Vec<u8>),       // 
    P2pMessage(HashMap<String, Vec<u8>>), // p2p
    SubsetMessage(Vec<u8>),               // 传递给参与方集合
    BroadcastMessage(Vec<u8>),            // 广播给所有人
    EmptyMsg,
    KeyGenSuccessWithResult(String),
    SignOfflineSuccessWithResult(String),
    SignOnlineSuccessWithResult(String),
}
两个函数

进程启动函数process_begin

pub fn process_begin(&mut self) -> Result<SendingMessages, anyhow::Error>;

消息处理函数msg_handler

pub fn msg_handler(
    &mut self,
    index: String,
    recv_msg: &Vec<u8>,
) -> Result<SendingMessages, anyhow::Error>;

密钥生成

方法定义
impl KeyGenPhase {
    pub fn new(
        partyid: String, // 参与方id
        params: Parameters,// 门限的(t,n)
        party_ids: &Option<Vec<String>>,// 多方参与者,顺序随意
    ) -> Result<Self, anyhow::Error>;
}

pub struct Parameters {
    pub threshold: usize,   // t
    pub share_count: usize, // n
}
使用方法

此为密钥生成阶段的完整使用方法,大致分为三步

  1. 定义参数,调用一个KeyGenPhase方法
let partyid = "1".to_string();//参与方id
let params = Parameters {//定义门限(t,n)
    threshold: 1,
    share_count: 3,
};
let party_ids = vec!["1".to_string(), "2".to_string(), "3".to_string()];//定义参与方集合
let mut keygen = KeyGenPhase::new(partyid, params, &Some(party_ids)).unwrap();
  1. 调用process_begin函数,启动该阶段,返回一个消息,并传递
let sending_msg: SendingMessages = keygen.process_begin().unwrap();

根据消息类型,选择将它传递给哪些参与方

match sending_msg {
    SendingMessages::BroadcastMessage(msg) => {
        // broadcast the msg to all(including self).
    }
    SendingMessages::P2pMessage(msg) => {
        // send according to the k,v in the msg. k is the index which v will to be sent to.
    }
    SendingMessages::SubsetMessage(msg) => {
        // send according to the k in the party_ids or subset(used in sign phase). k is the index which msg will to be sent to.
    }
    _ => {}
}
  1. 消息接收到后使用msg_handler进行消息处理,

它返回下一轮要用的信息(有很多轮),或者结果,这里显示的是最后一轮,也就是返回结果

loop {
    let recv_from = "".to_string();//消息来源
    let recv_msg = vec![0u8];//接收的消息
    let sending_msg = keygen.msg_handler(recv_from, &recv_msg).unwrap();
    match sending_msg {
        SendingMessages::KeyGenSuccessWithResult(msg) => {
            // got the keygen result
            //这里就代表成功了
            break;
        }
        _ => {
            // other sending messages, ref Step 2.
        }
    }
}

收到匹配到SendingMessages::KeyGenSuccessWithResult,就代表完成

密钥结构
{
    // 多方参与者的标识
    "index": ...,// 序号
    "participants": ...,// 总集合
// 公钥
	"pubkey": {// 
    	"pk": ... ,
    	"share_pks": ... 
	},
// 私钥
	"privkey": {
    	"cl_sk":...,
    	"ec_sk":...,
    	"share_sk":...
    }
}
示例
key1 = {"index":"1","participants":["1","2","3"],"pubkey":{"pk":["fbc70884258bec54cc35b3baaf0d1bfb67a0fa60d568f9ec356cfc25d2133156","f5f938eb41bb1d7aec0ad7cb49daea968ba2dde3428a066f5ad6f5cf1830a940"],"share_pks":{"2":["d6713a24da44a15300e81bdd87420566f3fea044a94671c9fbfddd9005c2457","43364acc9489f436da9442ff0002ff9b2b4f62bf27df05acf574528cac4562db"],"3":["c94682ce7ca96cab0b16d93c0fa235d6d6be86642a5a8774ae3844c9df94ea4c","81b1209df84d50ae42ec4750e7e937b054532ab9d94110c7ded6cd401661511f"],"1":["b0f31d64197c28949412c11cf3bfb3ada4fb714ee9c1cedff5d70c03cc055ad0","416fdfc3d301d40091cc0915b8bc8ef012401b7119d0d7dfd4004b983ee7c11f"]}},"privkey":{"cl_sk":"1e8256813d593a82e5dc619a62d773c9bb12d01d6e03e5c8cf064e822df6e82cde95aa41b1c694a36e97aa953d539a0e9653c55432877bf3fd50e78583bfe7035d5a38c5c28a7aff3432de7cb1f2cc86a8fe0add737950d8f4fab37f23d76f0cd703f7cfb7ba658a4b7de5eda286893466902fe4f4dafefebe099e07702c4bf4c412d820c7bd52321c7af94a6","ec_sk":"9d120fafb69cdbc27a7cc080c18b3d754113325245b2c2ccc9a389828ad0c86d","share_sk":"a35664a07e3760508172569490e40db53fb99a0c612075d90f077ba574e77ab0"}}
key3 = {"index":"3","participants":["1","2","3"],"pubkey":{"pk":["fbc70884258bec54cc35b3baaf0d1bfb67a0fa60d568f9ec356cfc25d2133156","f5f938eb41bb1d7aec0ad7cb49daea968ba2dde3428a066f5ad6f5cf1830a940"],"share_pks":{"3":["c94682ce7ca96cab0b16d93c0fa235d6d6be86642a5a8774ae3844c9df94ea4c","81b1209df84d50ae42ec4750e7e937b054532ab9d94110c7ded6cd401661511f"],"2":["d6713a24da44a15300e81bdd87420566f3fea044a94671c9fbfddd9005c2457","43364acc9489f436da9442ff0002ff9b2b4f62bf27df05acf574528cac4562db"],"1":["b0f31d64197c28949412c11cf3bfb3ada4fb714ee9c1cedff5d70c03cc055ad0","416fdfc3d301d40091cc0915b8bc8ef012401b7119d0d7dfd4004b983ee7c11f"]}},"privkey":{"cl_sk":"36a65fb545efc423443771dc106cbcf5b0b30176c35e1a60f55541f51bd780b53b83ded1f4d99a5da9c16c929978a7423d6824baee94dad55ec7666ac914e932f9467fe0b4ad86b66e04a1cd4845911b06f27e523b058cb13c494f796b5ff27e09bed42b48a2b78b6adb1ae41dfa44db945037a1030405e386db6b824ab8d3e91d72e75fce4f85f3f4afbacf9","ec_sk":"45fdef4382488ccd4d25088ed9a94f9b3a3f7b3c68e6f84889b604df2a48d772","share_sk":"cda98e6bcf026a0c4c22c9c5e706e0aabb847865caf8bc885d4aeca8c38aff56"}}
key2 = {"index":"2","participants":["1","2","3"],"pubkey":{"pk":["fbc70884258bec54cc35b3baaf0d1bfb67a0fa60d568f9ec356cfc25d2133156","f5f938eb41bb1d7aec0ad7cb49daea968ba2dde3428a066f5ad6f5cf1830a940"],"share_pks":{"2":["d6713a24da44a15300e81bdd87420566f3fea044a94671c9fbfddd9005c2457","43364acc9489f436da9442ff0002ff9b2b4f62bf27df05acf574528cac4562db"],"3":["c94682ce7ca96cab0b16d93c0fa235d6d6be86642a5a8774ae3844c9df94ea4c","81b1209df84d50ae42ec4750e7e937b054532ab9d94110c7ded6cd401661511f"],"1":["b0f31d64197c28949412c11cf3bfb3ada4fb714ee9c1cedff5d70c03cc055ad0","416fdfc3d301d40091cc0915b8bc8ef012401b7119d0d7dfd4004b983ee7c11f"]}},"privkey":{"cl_sk":"174235859b6cda9aad27b195ca2bda39ad39aa3f7172dcb59a66b46893d18c6fbe43b0e9b52455d263eeb114d2fca2ab454e74c0344ecee19e9069eb9470c149c603bf875faa5377d8a3b1459110bc498df39aee9f7bb893ec8ac2d09825df75dbcb067a08d805dd312f28766a7d8973e1cfffd762eea02c9c96709461420dbff24a7c7ef5652f0e7e717a756","ec_sk":"62b7063f694a9d7a038a966ea61eafc2ae325c3613ffa56a34150218d2164e5f","share_sk":"b87ff986269ce52e66ca902d3bf5772ffd9f0939160c9930b62934271c393d03"}}

离线签名

方法定义
impl SignPhase {
    pub fn new(
        partyid: String,
        params: Parameters,
        subset: &Vec<String>,// 签名的参与方集合
        keys: &String,//密钥生成阶段产出的密钥
    ) -> Result<Self, anyhow::Error>;
}
使用方法

和密钥生成类似,都是新建对象,调用process_begin启动,接着调用msg_handler对通信消息进行处理。收到SendingMessages::SignOfflineSuccessWithResult代表该阶段完成。

在线签名

方法定义
impl SignPhaseOnline {
    pub fn new(
        offline_result: &String, //离线签名的结果
        message_bytes: Vec<u8>//要签名的消息哈希值
    ) -> Result<Self, anyhow::Error>;
}
使用方法

和离线的一样,值得注意的是离线签名的结果只能用一次

签名结构

如下,所有参与方都会收到这个签名

{
  "s": "14af6f72d8bd26faccd75ff092544d15a3dce5d97e897773b515cd70ab0453e7",
  "r": "3687024517eb44de2cfaa6166866c9bd2587090317a4d12521b571c7509319b4",
  "recid": 0
}
示例
signature3 = {"s":"2842356d6eaaa84ad8d2eb47369b1f49c12068485ba9bd3e130e6043fe5414ee","r":"ba86d2297a05e77357906a2a7b52d13eedf52d78f1cfa50543059f41bf86be7b","recid":0}
signature2 = {"s":"2842356d6eaaa84ad8d2eb47369b1f49c12068485ba9bd3e130e6043fe5414ee","r":"ba86d2297a05e77357906a2a7b52d13eedf52d78f1cfa50543059f41bf86be7b","recid":0}
signature1 = {"s":"2842356d6eaaa84ad8d2eb47369b1f49c12068485ba9bd3e130e6043fe5414ee","r":"ba86d2297a05e77357906a2a7b52d13eedf52d78f1cfa50543059f41bf86be7b","recid":0}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值