Debian Codegraph @

src/multisig/multisig_clsag_context.cpp

raw symbols SCIP
1 // Copyright (c) 2021, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "multisig_clsag_context.h"
30
31 #include "int-util.h"
32
33 #include "crypto/crypto.h"
34 #include "cryptonote_config.h"
35 #include "ringct/rctOps.h"
36 #include "ringct/rctTypes.h"
37
38 #include <cstring>
39 #include <string>
40 #include <vector>
41
42 #undef MONERO_DEFAULT_LOG_CATEGORY
43 #define MONERO_DEFAULT_LOG_CATEGORY "multisig"
44
45 namespace multisig {
46
47 namespace signing {
48 //----------------------------------------------------------------------------------------------------------------------
49 //----------------------------------------------------------------------------------------------------------------------
50 template<std::size_t N>
51 static rct::key string_to_key(const unsigned char (&str)[N]) {
52 rct::key tmp{};
53 static_assert(sizeof(tmp.bytes) >= N, "");
54 std::memcpy(tmp.bytes, str, N);
55 return tmp;
56 }
57 //----------------------------------------------------------------------------------------------------------------------
58 //----------------------------------------------------------------------------------------------------------------------
59 static void encode_int_to_key_le(const unsigned int i, rct::key &k_out)
60 {
61 static_assert(sizeof(unsigned int) <= sizeof(std::uint64_t), "unsigned int max too large");
62 static_assert(sizeof(std::uint64_t) <= sizeof(rct::key), "");
63 std::uint64_t temp_i{SWAP64LE(i)};
64 std::memcpy(k_out.bytes, &temp_i, sizeof(temp_i));
65 }
66 //----------------------------------------------------------------------------------------------------------------------
67 //----------------------------------------------------------------------------------------------------------------------
68 bool CLSAG_context_t::init(
69 const rct::keyV& P,
70 const rct::keyV& C_nonzero,
71 const rct::key& C_offset,
72 const rct::key& message,
73 const rct::key& I,
74 const rct::key& D,
75 const unsigned int l,
76 const rct::keyV& s,
77 const std::size_t num_alpha_components
78 )
79 {
80 initialized = false;
81
82 n = P.size();
83 if (n <= 0)
84 return false;
85 if (C_nonzero.size() != n)
86 return false;
87 if (s.size() != n)
88 return false;
89 if (l >= n)
90 return false;
91
92 c_params.clear();
93 c_params.reserve(n * 2 + 5);
94 b_params.clear();
95 b_params.reserve(n * 3 + 2 * num_alpha_components + 7);
96
97 c_params.push_back(string_to_key(config::HASH_KEY_CLSAG_ROUND));
98 b_params.push_back(string_to_key(config::HASH_KEY_CLSAG_ROUND_MULTISIG));
99 c_params.insert(c_params.end(), P.begin(), P.end());
100 b_params.insert(b_params.end(), P.begin(), P.end());
101 c_params.insert(c_params.end(), C_nonzero.begin(), C_nonzero.end());
102 b_params.insert(b_params.end(), C_nonzero.begin(), C_nonzero.end());
103 c_params.emplace_back(C_offset);
104 b_params.emplace_back(C_offset);
105 c_params.emplace_back(message);
106 b_params.emplace_back(message);
107 c_params_L_offset = c_params.size();
108 b_params_L_offset = b_params.size();
109 c_params.resize(c_params.size() + 1); //this is where L will be inserted later
110 b_params.resize(b_params.size() + num_alpha_components); //multisig aggregate public nonces for L will be inserted here later
111 c_params_R_offset = c_params.size();
112 b_params_R_offset = b_params.size();
113 c_params.resize(c_params.size() + 1); //this is where R will be inserted later
114 b_params.resize(b_params.size() + num_alpha_components); //multisig aggregate public nonces for R will be inserted here later
115 b_params.emplace_back(I);
116 b_params.emplace_back(D);
117 b_params.insert(b_params.end(), s.begin(), s.begin() + l); //fake responses before 'l'
118 b_params.insert(b_params.end(), s.begin() + l + 1, s.end()); //fake responses after 'l'
119 b_params.emplace_back();
120 encode_int_to_key_le(l, b_params.back()); //real signing index 'l'
121 b_params.emplace_back();
122 encode_int_to_key_le(num_alpha_components, b_params.back()); //number of parallel nonces
123 b_params.emplace_back();
124 encode_int_to_key_le(n, b_params.back()); //number of ring members
125
126 rct::keyV mu_P_params;
127 rct::keyV mu_C_params;
128 mu_P_params.reserve(n * 2 + 4);
129 mu_C_params.reserve(n * 2 + 4);
130
131 mu_P_params.push_back(string_to_key(config::HASH_KEY_CLSAG_AGG_0));
132 mu_C_params.push_back(string_to_key(config::HASH_KEY_CLSAG_AGG_1));
133 mu_P_params.insert(mu_P_params.end(), P.begin(), P.end());
134 mu_C_params.insert(mu_C_params.end(), P.begin(), P.end());
135 mu_P_params.insert(mu_P_params.end(), C_nonzero.begin(), C_nonzero.end());
136 mu_C_params.insert(mu_C_params.end(), C_nonzero.begin(), C_nonzero.end());
137 mu_P_params.emplace_back(I);
138 mu_C_params.emplace_back(I);
139 mu_P_params.emplace_back(scalarmultKey(D, rct::INV_EIGHT));
140 mu_C_params.emplace_back(mu_P_params.back());
141 mu_P_params.emplace_back(C_offset);
142 mu_C_params.emplace_back(C_offset);
143 mu_P = hash_to_scalar(mu_P_params);
144 mu_C = hash_to_scalar(mu_C_params);
145
146 rct::geDsmp I_precomp;
147 rct::geDsmp D_precomp;
148 rct::precomp(I_precomp.k, I);
149 rct::precomp(D_precomp.k, D);
150 rct::key wH_l;
151 rct::addKeys3(wH_l, mu_P, I_precomp.k, mu_C, D_precomp.k);
152 rct::precomp(wH_l_precomp.k, wH_l);
153 W_precomp.resize(n);
154 H_precomp.resize(n);
155 for (std::size_t i = 0; i < n; ++i) {
156 rct::geDsmp P_precomp;
157 rct::geDsmp C_precomp;
158 rct::key C;
159 rct::subKeys(C, C_nonzero[i], C_offset);
160 rct::precomp(P_precomp.k, P[i]);
161 rct::precomp(C_precomp.k, C);
162 rct::key W;
163 rct::addKeys3(W, mu_P, P_precomp.k, mu_C, C_precomp.k);
164 rct::precomp(W_precomp[i].k, W);
165 ge_p3 Hi_p3;
166 rct::hash_to_p3(Hi_p3, P[i]);
167 ge_dsm_precomp(H_precomp[i].k, &Hi_p3);
168 }
169 rct::precomp(G_precomp.k, rct::G);
170 this->l = l;
171 this->s = s;
172 this->num_alpha_components = num_alpha_components;
173
174 initialized = true;
175 return true;
176 }
177 //----------------------------------------------------------------------------------------------------------------------
178 bool CLSAG_context_t::combine_alpha_and_compute_challenge(
179 const rct::keyV& total_alpha_G,
180 const rct::keyV& total_alpha_H,
181 const rct::keyV& alpha,
182 rct::key& alpha_combined,
183 rct::key& c_0,
184 rct::key& c
185 )
186 {
187 if (not initialized)
188 return false;
189
190 if (num_alpha_components != total_alpha_G.size())
191 return false;
192 if (num_alpha_components != total_alpha_H.size())
193 return false;
194 if (num_alpha_components != alpha.size())
195 return false;
196
197 // insert aggregate public nonces for L and R components
198 for (std::size_t i = 0; i < num_alpha_components; ++i) {
199 b_params[b_params_L_offset + i] = total_alpha_G[i];
200 b_params[b_params_R_offset + i] = total_alpha_H[i];
201 }
202
203 // musig2-style combination factor 'b'
204 const rct::key b = rct::hash_to_scalar(b_params);
205
206 // 1) store combined public nonces in the 'L' and 'R' slots for computing the initial challenge
207 // - L = sum_i(b^i total_alpha_G[i])
208 // - R = sum_i(b^i total_alpha_H[i])
209 // 2) compute the local signer's combined private nonce
210 // - alpha_combined = sum_i(b^i * alpha[i])
211 rct::key& L_l = c_params[c_params_L_offset];
212 rct::key& R_l = c_params[c_params_R_offset];
213 rct::key b_i = rct::identity();
214 L_l = rct::identity();
215 R_l = rct::identity();
216 alpha_combined = rct::zero();
217 for (std::size_t i = 0; i < num_alpha_components; ++i) {
218 rct::addKeys(L_l, L_l, rct::scalarmultKey(total_alpha_G[i], b_i));
219 rct::addKeys(R_l, R_l, rct::scalarmultKey(total_alpha_H[i], b_i));
220 sc_muladd(alpha_combined.bytes, alpha[i].bytes, b_i.bytes, alpha_combined.bytes);
221 sc_mul(b_i.bytes, b_i.bytes, b.bytes);
222 }
223
224 // compute initial challenge from real spend components
225 c = rct::hash_to_scalar(c_params);
226
227 // 1) c_0: find the CLSAG's challenge for index '0', which will be stored in the proof
228 // note: in the CLSAG implementation in ringct/rctSigs, c_0 is denoted 'c1' (a notation error)
229 // 2) c: find the final challenge for the multisig signers to respond to
230 for (std::size_t i = (l + 1) % n; i != l; i = (i + 1) % n) {
231 if (i == 0)
232 c_0 = c;
233 rct::addKeys3(c_params[c_params_L_offset], s[i], G_precomp.k, c, W_precomp[i].k);
234 rct::addKeys3(c_params[c_params_R_offset], s[i], H_precomp[i].k, c, wH_l_precomp.k);
235 c = rct::hash_to_scalar(c_params);
236 }
237 if (l == 0)
238 c_0 = c;
239
240 return true;
241 }
242 //----------------------------------------------------------------------------------------------------------------------
243 bool CLSAG_context_t::get_mu(
244 rct::key& mu_P,
245 rct::key& mu_C
246 ) const
247 {
248 if (not initialized)
249 return false;
250 mu_P = this->mu_P;
251 mu_C = this->mu_C;
252 return true;
253 }
254 //----------------------------------------------------------------------------------------------------------------------
255 } //namespace signing
256
257 } //namespace multisig