forked from coinbase/cb-mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtdh2.h
More file actions
136 lines (111 loc) · 3.79 KB
/
tdh2.h
File metadata and controls
136 lines (111 loc) · 3.79 KB
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
#pragma once
#include <cbmpc/crypto/base.h>
#include <cbmpc/crypto/lagrange.h>
#include <cbmpc/crypto/secret_sharing.h>
namespace coinbase::crypto::tdh2 {
typedef std::vector<ecc_point_t> pub_shares_t;
static const int iv_size = 16;
class public_key_t;
struct ciphertext_t {
buf_t c;
buf_t iv;
ecc_point_t R1, R2;
bn_t e, f;
buf_t L;
void convert(coinbase::converter_t& converter) { converter.convert(c, R1, R2, e, f, iv); }
/**
* @specs:
* - tdh2-spec | tdh2-combine-1P
* @notes:
* - This is a helper function used in the last step of tdh2-combine-1P such that given `V`, it performs the aes-gcm
* decryption
*/
error_t decrypt(const ecc_point_t& V, buf_t& dec, mem_t label) const;
/**
* @specs:
* - tdh2-spec | tdh2-verify-1P
*/
error_t verify(const public_key_t& pub_key, mem_t label) const;
};
template <class T>
T& update_state(T& state, const ciphertext_t& v) {
update_state(state, v.c);
update_state(state, v.R1);
update_state(state, v.R2);
update_state(state, v.e);
update_state(state, v.f);
return state;
}
struct public_key_t {
ecc_point_t Q, Gamma;
public_key_t() {}
public_key_t(const ecc_point_t& _Q) : Q(_Q) { Gamma = ro::hash_curve(mem_t("TDH2-Gamma"), Q).curve(Q.get_curve()); }
/**
* @specs:
* - tdh2-spec | tdh2-encrypt-1P
* @notes:
* - This function generates random r, s, iv and calls encrypt(plain, label, r, s, iv, curve)
*/
ciphertext_t encrypt(mem_t plain, mem_t label) const;
/**
* @specs:
* - tdh2-spec | tdh2-encrypt-1P
*/
ciphertext_t encrypt(mem_t plain, mem_t label, const bn_t& r, const bn_t& s, mem_t iv) const;
bool valid() const { return Q.valid(); }
void convert(coinbase::converter_t& converter) { converter.convert(Q, Gamma); }
buf_t to_bin() const { return coinbase::convert(*this); }
error_t from_bin(mem_t bin) { return coinbase::convert(*this, bin); }
bool operator==(const public_key_t& other) const { return Q == other.Q && Gamma == other.Gamma; }
bool operator!=(const public_key_t& other) const { return Q != other.Q || Gamma != other.Gamma; }
};
struct private_key_t {
bn_t x;
public_key_t pub_key;
void convert(coinbase::converter_t& c) { c.convert(x, pub_key); }
public_key_t pub() const { return pub_key; }
bool valid() const { return pub_key.Q.valid(); }
};
struct partial_decryption_t {
int pid;
ecc_point_t Xi;
bn_t ei, fi;
void convert(coinbase::converter_t& converter) { converter.convert(pid, Xi, ei, fi); }
/**
* @specs:
* - tdh2-spec | tdh2-combine-1P
* @notes:
* - This is a helper function used in tdh2-combine-1P
*/
error_t check_partial_decryption_helper(const ecc_point_t& Qi, const ciphertext_t& ciphertext, ecurve_t curve) const;
};
struct private_share_t {
public_key_t pub_key;
bn_t x;
int pid = 0;
/**
* @specs:
* - tdh2-spec | tdh2-local-decrypt-1P
*/
error_t decrypt(const ciphertext_t& ciphertext, mem_t label, partial_decryption_t& partial_decryption) const;
};
typedef std::vector<partial_decryption_t> partial_decryptions_t;
/**
* @specs:
* - tdh2-spec | tdh2-combine-1P
* @notes:
* - This is the special case where the shares are additive shares
*/
error_t combine_additive(const public_key_t& pub, const pub_shares_t& Qi, mem_t label,
const partial_decryptions_t& partial_decryptions, const ciphertext_t& ciphertext,
buf_t& plain);
/**
* @specs:
* - tdh2-spec | tdh2-combine-1P
* @notes:
* - This is the general case where the shares are general access structure
*/
error_t combine(const ss::ac_t& ac, const public_key_t& pub, ss::ac_pub_shares_t& pub_shares, mem_t label,
const ss::party_map_t<partial_decryption_t> partial_decryptions, const ciphertext_t& ciphertext,
buf_t& plain);
} // namespace coinbase::crypto::tdh2