forked from unboundsecurity/blockchain-crypto-mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpc_crypto_eddsa.h
198 lines (163 loc) · 7.47 KB
/
mpc_crypto_eddsa.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* NOTICE
*
* The blockchain-crypto-mpc software is licensed under a proprietary license or the GPL v.3.
* If you choose to receive it under the GPL v.3 license, the following applies:
* Blockchain-crypto-mpc is a Multiparty Computation (MPC)-based cryptographic library for securing blockchain wallets and applications.
*
* Copyright (C) 2018, Unbound Tech Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "mpc_eddsa.h"
#include "mpc_crypto_context.h"
#include "mpc_core.h"
class mpc_eddsa_share_t : public mpc_crypto_share_t
{
public:
static const uint64_t CODE_TYPE = 0xd68c71663a9a4dcc;
mpc_eddsa_share_t();
virtual void convert(ub::converter_t& converter) override;
virtual mpc_crypto_key_e get_type() const override { return mpc_eddsa; }
void copy_pub_key(byte_ptr out) const { core.Q_full.encode(out); }
virtual mpc_crypto_context_t* create_refresh_oper();
virtual uint64_t calc_uid() const override
{
byte_t pub_key[32];
core.Q_full.encode(pub_key);
return crypto::sha256_truncated_uint64(mem_t(pub_key, 32));
}
mpc::eddsa_share_t core;
};
static ub::convertable_t::factory_t::register_t<mpc_eddsa_share_t, mpc_eddsa_share_t::CODE_TYPE> g_register_mpc_eddsa_share_t;
class mpc_eddsa_refresh_t : public mpc_crypto_context_t
{
public:
static const uint64_t CODE_TYPE = 0x81b6d0d69a7f4f48;
mpc_eddsa_refresh_t() : agree_random(64+1) {}
virtual uint64_t get_type() const override { return CODE_TYPE; }
virtual void convert(ub::converter_t& converter) override;
virtual error_t step(const mpc_crypto_message_t& in, mpc_crypto_message_t& out, unsigned& flags) override
{
return protocol_step(*this, in, out, flags);
}
virtual mpc_crypto_share_t* create_share() const override { return new mpc_eddsa_share_t; }
virtual mpc_crypto_key_e get_share_type() const override { return mpc_eddsa; }
virtual void set_share_core(const mpc_crypto_share_t& src) override { share = ((const mpc_eddsa_share_t&)src).core;}
virtual void get_share_core(mpc_crypto_share_t& dst) const { ((mpc_eddsa_share_t&)dst).core = share; }
virtual int get_messages_count() const override { return 3; }
virtual bool changes_share() const override { return true; }
typedef mpc::agree_random_t::message1_t message1_t;
typedef mpc::agree_random_t::message2_t message2_t;
typedef mpc::agree_random_t::message3_t message3_t;
error_t party1_step1(message1_t& out);
error_t party2_step1(const message1_t& in, message2_t& out);
error_t party1_step2(const message2_t& in, message3_t& out);
error_t party2_step2(const message3_t& in, none_message_t& out);
private:
mpc::eddsa_share_t share;
mpc::agree_random_t agree_random;
};
static ub::convertable_t::factory_t::register_t<mpc_eddsa_refresh_t, mpc_eddsa_refresh_t::CODE_TYPE> g_register_mpc_eddsa_refresh_t;
class mpc_eddsa_gen_t : public mpc_crypto_context_t
{
public:
mpc_eddsa_gen_t();
static const uint64_t CODE_TYPE = 0xf8e9471543464fc2;
virtual uint64_t get_type() const override { return CODE_TYPE; }
virtual mpc_crypto_key_e get_share_type() const override { return mpc_eddsa; }
virtual void convert(ub::converter_t& converter) override;
virtual error_t step(const mpc_crypto_message_t& in, mpc_crypto_message_t& out, unsigned& flags) override
{
return protocol_step(*this, in, out, flags);
}
virtual mpc_crypto_share_t* create_share() const override { return new mpc_eddsa_share_t; }
virtual void set_share_core(const mpc_crypto_share_t& src) override { assert(false); }
virtual void get_share_core(mpc_crypto_share_t& dst) const { ((mpc_eddsa_share_t&)dst).core = share; }
virtual int get_messages_count() const override { return 5; }
virtual bool changes_share() const override { return true; }
typedef mpc::agree_random_t::message1_t message1_t;
typedef mpc::agree_random_t::message2_t message2_t;
struct message3_t
{
mpc::agree_random_t::message3_t agree_msg3;
mpc::eddsa_gen_t::message1_t gen_msg1;
void convert(ub::converter_t& converter)
{
converter.convert(agree_msg3);
converter.convert(gen_msg1);
}
};
typedef mpc::eddsa_gen_t::message2_t message4_t;
typedef mpc::eddsa_gen_t::message3_t message5_t;
error_t party1_step1(message1_t& out);
error_t party2_step1(const message1_t& in, message2_t& out);
error_t party1_step2(const message2_t& in, message3_t& out);
error_t party2_step2(const message3_t& in, message4_t& out);
error_t party1_step3(const message4_t& in, message5_t& out);
error_t party2_step3(const message5_t& in, none_message_t& out);
private:
mpc::eddsa_share_t share;
mpc::eddsa_gen_t ctx;
mpc::agree_random_t agree_random;
};
static ub::convertable_t::factory_t::register_t<mpc_eddsa_gen_t, mpc_eddsa_gen_t::CODE_TYPE> g_register_mpc_eddsa_gen_t;
class mpc_eddsa_sign_t : public mpc_crypto_context_t
{
public:
static const uint64_t CODE_TYPE = 0xa6987b06a1664ccd;
virtual uint64_t get_type() const { return CODE_TYPE; }
mpc_eddsa_sign_t();
virtual void convert(ub::converter_t& converter) override;
virtual error_t step(const mpc_crypto_message_t& in, mpc_crypto_message_t& out, unsigned& flags) override
{
return protocol_step(*this, in, out, flags);
}
virtual mpc_crypto_share_t* create_share() const override { return new mpc_eddsa_share_t; }
virtual void set_share_core(const mpc_crypto_share_t& src) override { share = ((const mpc_eddsa_share_t&)src).core;}
virtual void get_share_core(mpc_crypto_share_t& dst) const { ((mpc_eddsa_share_t&)dst).core = share; }
void copy_result(byte_ptr out) const { memmove(out, result, 64); }
void set_data_to_sign(mem_t data_to_sign) { ctx.data_to_sign = data_to_sign; }
void set_refresh(bool refresh) { this->refresh = refresh; }
virtual mpc_crypto_key_e get_share_type() const override { return mpc_eddsa; }
virtual int get_messages_count() const override { return 4; }
virtual bool changes_share() const override { return refresh; }
struct message1_t
{
mpc::eddsa_sign_t::message1_t sign_msg1;
bool refresh;
buf_t data_to_sign;
void convert(ub::converter_t& converter)
{
converter.convert(sign_msg1);
converter.convert(refresh);
converter.convert(data_to_sign);
}
};
typedef mpc::eddsa_sign_t::message2_t message2_t;
typedef mpc::eddsa_sign_t::message3_t message3_t;
typedef mpc::eddsa_sign_t::message4_t message4_t;
error_t party1_step1(message1_t& out);
error_t party2_step1(const message1_t& in, message2_t& out);
error_t party1_step2(const message2_t& in, message3_t& out);
error_t party2_step2(const message3_t& in, message4_t& out);
error_t party1_step3(const message4_t& in, none_message_t& out);
private:
bool refresh;
mpc::eddsa_sign_t ctx;
byte_t result[64];
mpc::eddsa_share_t share;
};
static ub::convertable_t::factory_t::register_t<mpc_eddsa_sign_t, mpc_eddsa_sign_t::CODE_TYPE> g_register_mpc_eddsa_sign_t;