-
Notifications
You must be signed in to change notification settings - Fork 31
/
crypto_ext.hpp
453 lines (405 loc) · 16 KB
/
crypto_ext.hpp
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* @file
* @copyright defined in cdt/LICENSE
*/
#pragma once
#include "check.hpp"
#include "fixed_bytes.hpp"
#include "varint.hpp"
#include "serialize.hpp"
#include <array>
namespace eosio {
namespace internal_use_do_not_use {
extern "C" {
struct __attribute__((aligned (16))) capi_checksum256_ext { uint8_t hash[32]; };
__attribute__((eosio_wasm_import))
int32_t alt_bn128_add( const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* result, uint32_t result_len);
__attribute__((eosio_wasm_import))
int32_t alt_bn128_mul( const char* g1, uint32_t g1_len, const char* scalar, uint32_t scalar_len, char* result, uint32_t result_len);
__attribute__((eosio_wasm_import))
int32_t alt_bn128_pair( const char* pairs, uint32_t pairs_len);
__attribute__((eosio_wasm_import))
int32_t mod_exp( const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, const char* mod, uint32_t mod_len, char* result, uint32_t result_len);
__attribute__((eosio_wasm_import))
int32_t blake2_f( uint32_t rounds, const char* state, uint32_t state_len, const char* msg, uint32_t msg_len, const char* t0_offset, uint32_t t0_len, const char* t1_offset, uint32_t t1_len, int32_t final, char* result, uint32_t result_len);
__attribute__((eosio_wasm_import))
int32_t k1_recover( const char* sig, uint32_t sig_len, const char* dig, uint32_t dig_len, char* pub, uint32_t pub_len);
__attribute__((eosio_wasm_import))
void sha3( const char* data, uint32_t data_len, char* hash, uint32_t hash_len, int32_t keccak );
}
static inline auto sha3_helper(const char* data, uint32_t length, bool keccak) {
internal_use_do_not_use::capi_checksum256_ext hash;
internal_use_do_not_use::sha3( data, length, (char*)&hash, sizeof(hash), keccak);
eosio::checksum256 dg;
eosio::datastream<uint8_t*> ds = {&hash.hash[0], sizeof(hash)};
ds >> dg;
return dg;
}
}
/**
* @defgroup crypto Crypto
* @ingroup core
* @brief Defines API for calculating and checking hashes which
* require activating crypto protocol feature
*/
/**
* Abstracts mutable G1 and G2 points
*
* @ingroup crypto
*/
template <std::size_t Size = 32>
struct ec_point {
/**
* Bytes of the x coordinate
*/
std::vector<char> x;
/**
* Bytes of the y coordinate
*/
std::vector<char> y;
/**
* Construct a point given x and y
*
* @param x_ - The x coordinate, a vector of chars
* @param y_ - The y coordinate, a vector of chars
*/
ec_point(std::vector<char>& x_, std::vector<char>& y_)
:x(x_), y(y_)
{
eosio::check( x_.size() == y_.size(), "x's size must be equal to y's" );
eosio::check ( x_.size() == Size, "point size must match");
};
/**
* Construct a point given a serialized point
*
* @param p - The serialized point
*/
ec_point(std::vector<char>& p)
:x(p.data(), p.data() + Size), y(p.data() + Size, p.data() + p.size())
{
eosio::check ( p.size() == Size * 2, "point size must match");
};
/**
* Return serialzed point containing only x and y
*/
std::vector<char> serialized() const {
std::vector<char> x_and_y( x );
x_and_y.insert( x_and_y.end(), y.begin(), y.end() );
return x_and_y;
}
};
/**
* Abstracts read-only G1 and G2 points
*
* @ingroup crypto
*/
template <std::size_t Size = 32>
struct ec_point_view {
/**
* Pointer to the x coordinate
*/
const char* x;
/**
* Pointer to the y coordinate
*/
const char* y;
/**
* Number of bytes in each of x and y
*/
uint32_t size;
/**
* Construct a point view from x and y
*
* @param x_ - The x coordinate, poiter to chars
* @param x_size - x's size
* @param y_ - The y coordinate, poiter to chars
* @param y_size - y's size
*/
ec_point_view(const char* x_, uint32_t x_size, const char* y_, uint32_t y_size)
:x(x_), y(y_), size(x_size)
{
eosio::check ( x_size == y_size, "x's size must be equal to y's");
eosio::check ( size == Size, "point size must match");
};
/**
* Construct a point view from a serialized point
*
* @param p - The serialized point
*/
ec_point_view(const std::vector<char>& p)
:x(p.data()), y(p.data() + Size), size(Size)
{
eosio::check ( p.size() == Size * 2, "point size must match");
};
/**
* Construct a point view from a point
*
* @param p - The point
*/
ec_point_view(const ec_point<Size>& p)
:x(p.x.data()), y(p.y.data()), size(Size)
{
};
/**
* Return serialzed point containing only x and y
*/
std::vector<char> serialized() const {
std::vector<char> x_and_y( x, x + size );
x_and_y.insert( x_and_y.end(), y, y + size );
return x_and_y;
}
};
static constexpr size_t g1_coordinate_size = 32;
static constexpr size_t g2_coordinate_size = 64;
using g1_point = ec_point<g1_coordinate_size>;
using g2_point = ec_point<g2_coordinate_size>;
using g1_point_view = ec_point_view<g1_coordinate_size>;
using g2_point_view = ec_point_view<g2_coordinate_size>;
/**
* Big integer.
*
* @ingroup crypto
*/
using bigint = std::vector<char>;
/**
* Addition operation on the elliptic curve `alt_bn128`
*
* @ingroup crypto
* @param op1 - operand 1
* @param op2 - operand 2
* @return result of the addition operation; throw if error
*/
template <typename T>
inline g1_point alt_bn128_add( const T& op1, const T& op2 ) {
auto op_1 = op1.serialized();
auto op_2 = op2.serialized();
std::vector<char> buf ( 2 * g1_coordinate_size ); // buffer storing x and y
auto ret = internal_use_do_not_use::alt_bn128_add( op_1.data(), op_1.size(), op_2.data(), op_2.size(), buf.data(), buf.size());
eosio::check ( ret == 0, "internal_use_do_not_use::alt_bn128_add failed" );
return g1_point { buf };
}
/**
* Addition operation on the elliptic curve `alt_bn128`
*
* @ingroup crypto
* @param op1 - operand 1
* @param op1_len - size of operand 1
* @param op2 - operand 2
* @param op2_len - size of operand 2
* @param result - result of the addition operation
* @param result_len - size of result
* @return -1 if there is an error otherwise 0
*/
inline int32_t alt_bn128_add( const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* result, uint32_t result_len ) {
return internal_use_do_not_use::alt_bn128_add( op1, op1_len, op2, op2_len, result, result_len);
}
/**
* Scalar multiplication operation on the elliptic curve `alt_bn128`
*
* @ingroup crypto
* @param g1 - G1 point
* @param scalar - scalar factor
* @return result of the scalar multiplication operation; throw if error
*/
template <typename T>
inline g1_point alt_bn128_mul( const T& g1, const bigint& scalar) {
auto g1_bin = g1.serialized();
std::vector<char> buf( 2 * g1_coordinate_size ); // buffer storing x and y
auto ret = internal_use_do_not_use::alt_bn128_mul( g1_bin.data(), g1_bin.size(), scalar.data(), scalar.size(), buf.data(), buf.size());
eosio::check ( ret == 0, "internal_use_do_not_use::alt_bn128_mul failed");
return g1_point { buf };
}
/**
* Scalar multiplication operation on the elliptic curve `alt_bn128`
*
* @ingroup crypto
* @param g1 - G1 point
* @param g1_len - size of G1 point
* @param scalar - scalar factor
* @param scalar_len - size of scalar
* @param result - result of the scalar multiplication operation
* @param result_len - size of result
* @return -1 if there is an error otherwise 0
*/
inline int32_t alt_bn128_mul( const char* g1, uint32_t g1_len, const char* scalar, uint32_t scalar_len, char* result, uint32_t result_len ) {
return internal_use_do_not_use::alt_bn128_mul( g1, g1_len, scalar, scalar_len, result, result_len );
}
/**
* Optimal-Ate pairing check elliptic curve `alt_bn128`
*
* @ingroup crypto
* @param pairs - g1 and g2 pairs
* @return -1 if there is an error, 1 if false and 0 if true and successful
*/
template <typename G1_T, typename G2_T>
inline int32_t alt_bn128_pair( const std::vector<std::pair<G1_T, G2_T>>& pairs ) {
std::vector<char> g1_g2_pairs;
for ( const auto& pair: pairs ) {
auto g1_bin = pair.first.serialized();
auto g2_bin = pair.second.serialized();
g1_g2_pairs.insert( g1_g2_pairs.end(), g1_bin.begin(), g1_bin.end() );
g1_g2_pairs.insert( g1_g2_pairs.end(), g2_bin.begin(), g2_bin.end() );
}
return internal_use_do_not_use::alt_bn128_pair( g1_g2_pairs.data(), g1_g2_pairs.size() );
}
/**
* Optimal-Ate pairing check elliptic curve `alt_bn128`
*
* @ingroup crypto
* @param pairs - g1 and g2 pairs
* @param pairs_len - size of pairs
* @return -1 if there is an error, 1 if false and 0 if true and successful
*/
inline int32_t alt_bn128_pair( const char* pairs, uint32_t pairs_len ) {
return internal_use_do_not_use::alt_bn128_pair( pairs, pairs_len );
}
/**
* Big integer modular exponentiation
* returns an output ( BASE^EXP ) % MOD
*
* @ingroup crypto
* @param base - base of the exponentiation (BASE)
* @param exp - exponent to raise to that power (EXP)
* @param mod - modulus (MOD)
* @param result - result of the modular exponentiation
* @return -1 if there is an error otherwise 0
*/
inline int32_t mod_exp( const bigint& base, const bigint& exp, const bigint& mod, bigint& result) {
eosio::check( result.size() >= mod.size(), "mod_exp result parameter's size must be >= mod's size" );
auto ret = internal_use_do_not_use::mod_exp( base.data(), base.size(), exp.data(), exp.size(), mod.data(), mod.size(), result.data(), result.size());
return ret;
}
/**
* Big integer modular exponentiation
* returns an output ( BASE^EXP ) % MOD
*
* @ingroup crypto
* @param base - base of the exponentiation (BASE)
* @param base_len - size of base
* @param exp - exponent to raise to that power (EXP)
* @param exp_len - size of exp
* @param mod - modulus (MOD)
* @param mod_len - size of mod
* @param result - result of the modular exponentiation
* @param result_len - size of result
* @return -1 if there is an error otherwise 0
*/
inline int32_t mod_exp( const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, const char* mod, uint32_t mod_len, char* result, uint32_t result_len ) {
return internal_use_do_not_use::mod_exp( base, base_len, exp, exp_len, mod, mod_len, result, result_len);
}
static constexpr size_t blake2f_result_size = 64;
/**
* BLAKE2 compression function "F"
* https://eips.ethereum.org/EIPS/eip-152
*
* @ingroup crypto
* @param rounds - the number of rounds
* @param state - state vector
* @param msg - message block vector
* @param t0_offset - offset counters
* @param t1_offset - offset counters
* @param final - final block flag
* @param result - the result of the compression
* @return -1 if there is an error otherwise 0
*/
inline int32_t blake2_f( uint32_t rounds, const std::vector<char>& state, const std::vector<char>& msg, const std::vector<char>& t0_offset, const std::vector<char>& t1_offset, bool final, std::vector<char>& result) {
eosio::check( result.size() >= blake2f_result_size, "blake2_f result parameter's size must be >= 64" );
return internal_use_do_not_use::blake2_f( rounds, state.data(), state.size(), msg.data(), msg.size(), t0_offset.data(), t0_offset.size(), t1_offset.data(), t1_offset.size(), final, result.data(), result.size());
}
/**
* BLAKE2 compression function "F"
* https://eips.ethereum.org/EIPS/eip-152
*
* @ingroup crypto
* @param rounds - the number of rounds
* @param state - state vector
* @param state_len - size of state vector
* @param msg - message block vector
* @param msg_len - size of message block vector
* @param t0_offset - offset counters
* @param t0_len - size of t0_offset
* @param t1_offset - offset counters
* @param t1_len - size of t1_offset
* @param final - final block flag
* @param result - the result of the compression
* @param result_len - size of result
* @return -1 if there is an error otherwise 0
*/
inline int32_t blake2_f( uint32_t rounds, const char* state, uint32_t state_len, const char* msg, uint32_t msg_len,
const char* t0_offset, uint32_t t0_len, const char* t1_offset, uint32_t t1_len, int32_t final, char* result, uint32_t result_len) {
return internal_use_do_not_use::blake2_f( rounds, state, state_len, msg, msg_len, t0_offset, t0_len, t1_offset, t1_len, final, result, result_len);
}
/**
* Hashes `data` using `sha3`
*
* @param data - data you want to hash
* @param length - size of data
* @param keccak - whether to use `keccak` or NIST variant; keccak = 1 and NIST == 0
* @return eosio::checksum256 - Computed digest
*
*/
/**
* Hashes `data` using SHA3 NIST.
*
* @ingroup crypto
* @param data - Data you want to hash
* @param length - Data length
* @return eosio::checksum256 - Computed digest
*/
inline eosio::checksum256 sha3(const char* data, uint32_t length) {
return internal_use_do_not_use::sha3_helper(data, length, false);
}
/**
* Tests if the SHA3 hash generated from data matches the provided digest.
*
* @ingroup crypto
* @param data - Data you want to hash
* @param length - Data length
* @param hash - digest to compare to
* @note !This method is not optimized away during replay
*/
inline void assert_sha3(const char* data, uint32_t length, const eosio::checksum256& hash) {
const auto& res = internal_use_do_not_use::sha3_helper(data, length, false);
check( hash == res, "SHA3 hash of `data` does not match given `hash`");
}
/**
* Hashes `data` using SHA3 Keccak.
*
* @ingroup crypto
* @param data - Data you want to hash
* @param length - Data length
* @return eosio::checksum256 - Computed digest
*/
inline eosio::checksum256 keccak(const char* data, uint32_t length) {
return internal_use_do_not_use::sha3_helper(data, length, true);
}
/**
* Tests if the SHA3 keccak hash generated from data matches the provided digest.
*
* @ingroup crypto
* @param data - Data you want to hash
* @param length - Data length
* @param hash - digest to compare to
* @note !This method is not optimized away during replay
*/
inline void assert_keccak(const char* data, uint32_t length, const eosio::checksum256& hash) {
const auto& res = internal_use_do_not_use::sha3_helper(data, length, true);
check( hash == res, "Keccak hash of `data` does not match given `hash`");
}
/**
* Calculates the uncompressed public key used for a given signature on a given digest.
*
* @ingroup crypto
* @param sig - signature.
* @param sig_len - size of signature
* @param dig - digest of the message that was signed.
* @param dig_len - size of digest
* @param pub - public key result
* @param pub_len - size of public key result
*
* @return -1 if there was an error 0 otherwise.
*/
inline int32_t k1_recover( const char* sig, uint32_t sig_len, const char* dig, uint32_t dig_len, char* pub, uint32_t pub_len ) {
return internal_use_do_not_use::k1_recover( sig, sig_len, dig, dig_len, pub, pub_len );
}
}