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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
// Copyright (c) 2022, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Transcript class for assembling data that needs to be hashed.
#pragma once
//local headers
#include "crypto/crypto.h"
#include "cryptonote_config.h"
#include "ringct/rctTypes.h"
#include "wipeable_string.h"
//third party headers
#include <boost/utility/string_ref.hpp>
//standard headers
#include <list>
#include <string>
#include <type_traits>
#include <vector>
//forward declarations
namespace sp
{
////
// SpTranscriptBuilder
// - build a transcript
// - the user must provide a label when trying to append something to the transcript; labels are prepended to objects in
// the transcript
// - data types
// - unsigned int: uint_flag || varint(uint_variable)
// - signed int: int_flag || uchar{int_variable < 0 ? 1 : 0} || varint(abs(int_variable))
// - byte buffer (assumed little-endian): buffer_flag || buffer_length || buffer
// - all labels are treated as byte buffers
// - named container: container_flag || container_name || data_member1 || ... || container_terminator_flag
// - list-type container (same-type elements only): list_flag || list_length || element1 || element2 || ...
// - the transcript can be used like a string via the .data() and .size() member functions
// - simple mode: exclude all labels, flags, and lengths
///
class SpTranscriptBuilder final
{
public:
//public member types
/// transcript builder mode
enum class Mode
{
FULL,
SIMPLE
};
//constructors
/// normal constructor
SpTranscriptBuilder(const std::size_t estimated_data_size, const Mode mode) :
m_mode{mode}
{
m_transcript.reserve(2 * estimated_data_size + 20);
}
//overloaded operators
/// disable copy/move (this is a scoped manager [of the 'transcript' concept])
SpTranscriptBuilder& operator=(SpTranscriptBuilder&&) = delete;
//member functions
/// append a value to the transcript
template <typename T>
void append(const boost::string_ref label, const T &value)
{
this->append_impl(label, value);
}
/// access the transcript data
const void* data() const { return m_transcript.data(); }
std::size_t size() const { return m_transcript.size(); }
private:
//member variables
/// in simple mode, exclude labels, flags, and lengths
Mode m_mode;
/// the transcript buffer (wipeable in case it contains sensitive data)
epee::wipeable_string m_transcript;
//private member types
/// flags for separating items added to the transcript
enum SpTranscriptBuilderFlag : unsigned char
{
UNSIGNED_INTEGER = 0,
SIGNED_INTEGER = 1,
BYTE_BUFFER = 2,
NAMED_CONTAINER = 3,
NAMED_CONTAINER_TERMINATOR = 4,
LIST_TYPE_CONTAINER = 5
};
//transcript builders
void append_character(const unsigned char character)
{
m_transcript += static_cast<char>(character);
}
void append_uint(const std::uint64_t unsigned_integer)
{
unsigned char v_variable[(sizeof(std::uint64_t) * 8 + 6) / 7];
unsigned char *v_variable_end = v_variable;
// append uint to string as a varint
tools::write_varint(v_variable_end, unsigned_integer);
assert(v_variable_end <= v_variable + sizeof(v_variable));
m_transcript.append(reinterpret_cast<const char*>(v_variable), v_variable_end - v_variable);
}
void append_flag(const SpTranscriptBuilderFlag flag)
{
if (m_mode == Mode::SIMPLE)
return;
static_assert(sizeof(SpTranscriptBuilderFlag) == sizeof(unsigned char), "");
this->append_character(static_cast<unsigned char>(flag));
}
void append_length(const std::size_t length)
{
if (m_mode == Mode::SIMPLE)
return;
static_assert(sizeof(std::size_t) <= sizeof(std::uint64_t), "");
this->append_uint(static_cast<std::uint64_t>(length));
}
void append_buffer(const void *data, const std::size_t length)
{
this->append_flag(SpTranscriptBuilderFlag::BYTE_BUFFER);
this->append_length(length);
m_transcript.append(reinterpret_cast<const char*>(data), length);
}
void append_label(const boost::string_ref label)
{
if (m_mode == Mode::SIMPLE ||
label.size() == 0)
return;
this->append_buffer(label.data(), label.size());
}
void append_labeled_buffer(const boost::string_ref label, const void *data, const std::size_t length)
{
this->append_label(label);
this->append_buffer(data, length);
}
void begin_named_container(const boost::string_ref container_name)
{
this->append_flag(SpTranscriptBuilderFlag::NAMED_CONTAINER);
this->append_label(container_name);
}
void end_named_container()
{
this->append_flag(SpTranscriptBuilderFlag::NAMED_CONTAINER_TERMINATOR);
}
void begin_list_type_container(const std::size_t list_length)
{
this->append_flag(SpTranscriptBuilderFlag::LIST_TYPE_CONTAINER);
this->append_length(list_length);
}
//append overloads
void append_impl(const boost::string_ref label, const rct::key &key_buffer)
{
this->append_labeled_buffer(label, key_buffer.bytes, sizeof(key_buffer));
}
void append_impl(const boost::string_ref label, const rct::ctkey &ctkey)
{
this->append_label(label);
this->append_impl("ctmask", ctkey.mask);
this->append_impl("ctdest", ctkey.dest);
}
void append_impl(const boost::string_ref label, const crypto::secret_key &point_buffer)
{
this->append_labeled_buffer(label, point_buffer.data, sizeof(point_buffer));
}
void append_impl(const boost::string_ref label, const crypto::public_key &scalar_buffer)
{
this->append_labeled_buffer(label, scalar_buffer.data, sizeof(scalar_buffer));
}
void append_impl(const boost::string_ref label, const crypto::key_derivation &derivation_buffer)
{
this->append_labeled_buffer(label, derivation_buffer.data, sizeof(derivation_buffer));
}
void append_impl(const boost::string_ref label, const crypto::key_image &key_image_buffer)
{
this->append_labeled_buffer(label, key_image_buffer.data, sizeof(key_image_buffer));
}
void append_impl(const boost::string_ref label, const std::string &string_buffer)
{
this->append_labeled_buffer(label, string_buffer.data(), string_buffer.size());
}
void append_impl(const boost::string_ref label, const epee::wipeable_string &string_buffer)
{
this->append_labeled_buffer(label, string_buffer.data(), string_buffer.size());
}
void append_impl(const boost::string_ref label, const boost::string_ref string_buffer)
{
this->append_labeled_buffer(label, string_buffer.data(), string_buffer.size());
}
template<std::size_t Sz>
void append_impl(const boost::string_ref label, const unsigned char(&uchar_buffer)[Sz])
{
this->append_labeled_buffer(label, uchar_buffer, Sz);
}
template<std::size_t Sz>
void append_impl(const boost::string_ref label, const char(&char_buffer)[Sz])
{
this->append_labeled_buffer(label, char_buffer, Sz);
}
void append_impl(const boost::string_ref label, const std::vector<unsigned char> &vector_buffer)
{
this->append_labeled_buffer(label, vector_buffer.data(), vector_buffer.size());
}
void append_impl(const boost::string_ref label, const std::vector<char> &vector_buffer)
{
this->append_labeled_buffer(label, vector_buffer.data(), vector_buffer.size());
}
void append_impl(const boost::string_ref label, const char single_character)
{
this->append_label(label);
this->append_character(static_cast<unsigned char>(single_character));
}
void append_impl(const boost::string_ref label, const unsigned char single_character)
{
this->append_label(label);
this->append_character(single_character);
}
template<typename T,
std::enable_if_t<std::is_unsigned<T>::value, bool> = true>
void append_impl(const boost::string_ref label, const T unsigned_integer)
{
static_assert(sizeof(T) <= sizeof(std::uint64_t), "SpTranscriptBuilder: unsupported unsigned integer type.");
this->append_label(label);
this->append_flag(SpTranscriptBuilderFlag::UNSIGNED_INTEGER);
this->append_uint(unsigned_integer);
}
template<typename T,
std::enable_if_t<std::is_integral<T>::value, bool> = true,
std::enable_if_t<!std::is_unsigned<T>::value, bool> = true>
void append_impl(const boost::string_ref label, const T signed_integer)
{
using unsigned_type = std::make_unsigned<T>::type;
static_assert(sizeof(unsigned_type) <= sizeof(std::uint64_t),
"SpTranscriptBuilder: unsupported signed integer type.");
this->append_label(label);
this->append_flag(SpTranscriptBuilderFlag::SIGNED_INTEGER);
this->append_uint(static_cast<std::uint64_t>(static_cast<unsigned_type>(signed_integer)));
}
template<typename T,
std::enable_if_t<!std::is_integral<T>::value, bool> = true>
void append_impl(const boost::string_ref label, const T &named_container)
{
// named containers must satisfy two concepts:
// const boost::string_ref container_name(const T &container);
// void append_to_transcript(const T &container, SpTranscriptBuilder &transcript_inout);
//todo: container_name() and append_to_transcript() must be defined in the sp namespace, but that is not generic
this->append_label(label);
this->begin_named_container(container_name(named_container));
append_to_transcript(named_container, *this); //non-member function assumed to be implemented elsewhere
this->end_named_container();
}
template<typename T>
void append_impl(const boost::string_ref label, const std::vector<T> &list_container)
{
this->append_label(label);
this->begin_list_type_container(list_container.size());
for (const T &element : list_container)
this->append_impl("", element);
}
template<typename T>
void append_impl(const boost::string_ref label, const std::list<T> &list_container)
{
this->append_label(label);
this->begin_list_type_container(list_container.size());
for (const T &element : list_container)
this->append_impl("", element);
}
};
////
// SpFSTranscript
// - build a Fiat-Shamir transcript
// - main format: prefix || domain_separator || object1_label || object1 || object2_label || object2 || ...
// note: prefix defaults to "monero"
///
class SpFSTranscript final
{
public:
//constructors
/// normal constructor: start building a transcript with the domain separator
SpFSTranscript(const boost::string_ref domain_separator,
const std::size_t estimated_data_size,
const boost::string_ref prefix = config::TRANSCRIPT_PREFIX) :
m_transcript_builder{15 + domain_separator.size() + estimated_data_size + prefix.size(),
SpTranscriptBuilder::Mode::FULL}
{
// transcript = prefix || domain_separator
m_transcript_builder.append("FSp", prefix);
m_transcript_builder.append("ds", domain_separator);
}
//overloaded operators
/// disable copy/move (this is a scoped manager [of the 'transcript' concept])
SpFSTranscript& operator=(SpFSTranscript&&) = delete;
//member functions
/// build the transcript
template<typename T>
void append(const boost::string_ref label, const T &value)
{
m_transcript_builder.append(label, value);
}
/// access the transcript data
const void* data() const { return m_transcript_builder.data(); }
std::size_t size() const { return m_transcript_builder.size(); }
//member variables
private:
/// underlying transcript builder
SpTranscriptBuilder m_transcript_builder;
};
////
// SpKDFTranscript
// - build a data string for a key-derivation function
// - mainly intended for short transcripts (~128 bytes or less) with fixed-length and statically ordered components
// - main format: prefix || domain_separator || object1 || object2 || ...
// - simple transcript mode: no labels, flags, or lengths
// note: prefix defaults to "monero"
///
class SpKDFTranscript final
{
public:
//constructors
/// normal constructor: start building a transcript with the domain separator
SpKDFTranscript(const boost::string_ref domain_separator,
const std::size_t estimated_data_size,
const boost::string_ref prefix = config::TRANSCRIPT_PREFIX) :
m_transcript_builder{10 + domain_separator.size() + estimated_data_size + prefix.size(),
SpTranscriptBuilder::Mode::SIMPLE}
{
// transcript = prefix || domain_separator
m_transcript_builder.append("", prefix);
m_transcript_builder.append("", domain_separator);
}
//overloaded operators
/// disable copy/move (this is a scoped manager [of the 'transcript' concept])
SpKDFTranscript& operator=(SpKDFTranscript&&) = delete;
//member functions
/// build the transcript
template<typename T>
void append(const boost::string_ref, const T &value)
{
m_transcript_builder.append("", value);
}
/// access the transcript data
const void* data() const { return m_transcript_builder.data(); }
std::size_t size() const { return m_transcript_builder.size(); }
//member variables
private:
/// underlying transcript builder
SpTranscriptBuilder m_transcript_builder;
};
} //namespace sp
|