|
| 1 | +--- |
| 2 | +content_title: Crypto Extensions |
| 3 | +--- |
| 4 | + |
| 5 | +As of `v3.0` crypto host functions were extended to include |
| 6 | +- `mod_exp`: Big integer modular exponentiation |
| 7 | +- `alt_bn128_add`, `alt_bn128_mul`, `alt_bn128_pair`: Add, multiply, and pairing check functions for the `alt_bn128` elliptic curve |
| 8 | +- `blake2_f`: `BLAKE2b F` compression function |
| 9 | +- `sha3`: sha3` hash function using `SHA3 NIST` |
| 10 | +- `Keccak256`: `sha3` hash function using `SHA3 Keccak` |
| 11 | +- `k1_recover`: Safe `ECDSA` uncompressed pubkey recover for the `secp256k1` curve |
| 12 | + |
| 13 | +In `v3.0`, `C` format was supported; in `v3.1`, `C++` format was added for better data abstraction. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- In `nodeos`, activate protocol feature `CRYPTO_PRIMITIVES` (`68d6405cb8df3de95bd834ebb408196578500a9f818ff62ccc68f60b932f7d82`) |
| 18 | +- In smart contract code, include `crypto_ext.hpp` |
| 19 | + |
| 20 | +## C Format |
| 21 | + |
| 22 | +- `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 )` |
| 23 | +Perform addition operation on the elliptic curve `alt_bn128`, store the result in `result`, and return `0` if success otherwise `-1` |
| 24 | +- `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 )` |
| 25 | +Perform scalar multiplication operation on the elliptic curve `alt_bn128` |
| 26 | +- `int32_t alt_bn128_pair( const char* pairs, uint32_t pairs_len )` |
| 27 | +Perform Optimal-Ate pairing check elliptic curve `alt_bn128`, and return `0` if `true` and successful, `1` if `false`, otherwise `-1` |
| 28 | +- `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 )` |
| 29 | +Calculate `( BASE^EXP ) % MOD`, store in `result`, and return `0` if successful, otherwise `-1` |
| 30 | +- `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)` |
| 31 | +Implement BLAKE2 compression function `F`. Return `0` if success otherwise `-1` |
| 32 | +- `eosio::checksum256 sha3(const char* data, uint32_t length)` |
| 33 | +Return hash of `data` using `SHA3 NIST` |
| 34 | +- `void assert_sha3(const char* data, uint32_t length, const eosio::checksum256& hash)` |
| 35 | +Test if the SHA3 hash generated from data matches the provided digest |
| 36 | +- `eosio::checksum256 keccak(const char* data, uint32_t length)` |
| 37 | +Return hash of `data` using `SHA3 Keccak` |
| 38 | +- `void assert_keccak(const char* data, uint32_t length, const eosio::checksum256& hash) |
| 39 | +Test if the SHA3 keccak hash generated from data matches the provided digest |
| 40 | +- `int32_t k1_recover( const char* sig, uint32_t sig_len, const char* dig, uint32_t dig_len, char* pub, uint32_t pub_len )` |
| 41 | +Calculates the uncompressed public key used for a given signature on a given digest. Return `0` if success otherwise `-1` |
| 42 | + |
| 43 | +## C++ Format |
| 44 | + |
| 45 | +C++ types were added to represent `G1` and `G2` points (read and write) and views (read-only), and represent big integers. Their definitions are |
| 46 | + |
| 47 | +### Types |
| 48 | + |
| 49 | +```c++ |
| 50 | + /** |
| 51 | + * Abstracts mutable G1 and G2 points |
| 52 | + * |
| 53 | + */ |
| 54 | + template <std::size_t Size = 32> |
| 55 | + struct ec_point { |
| 56 | + /** |
| 57 | + * Bytes of the x coordinate |
| 58 | + */ |
| 59 | + std::vector<char> x; |
| 60 | + |
| 61 | + /** |
| 62 | + * Bytes of the y coordinate |
| 63 | + */ |
| 64 | + std::vector<char> y; |
| 65 | + |
| 66 | + /** |
| 67 | + * Construct a point given x and y |
| 68 | + * |
| 69 | + * @param x_ - The x coordinate, a vector of chars |
| 70 | + * @param y_ - The y coordinate, a vector of chars |
| 71 | + */ |
| 72 | + ec_point(std::vector<char>& x_, std::vector<char>& y_); |
| 73 | + |
| 74 | + /** |
| 75 | + * Construct a point given a serialized point |
| 76 | + * |
| 77 | + * @param p - The serialized point |
| 78 | + */ |
| 79 | + ec_point(std::vector<char>& p); |
| 80 | + |
| 81 | + /** |
| 82 | + * Return serialzed point containing only x and y |
| 83 | + */ |
| 84 | + std::vector<char> serialized() const; |
| 85 | + }; |
| 86 | + |
| 87 | + /** |
| 88 | + * Abstracts read-only G1 and G2 points |
| 89 | + */ |
| 90 | + template <std::size_t Size = 32> |
| 91 | + struct ec_point_view { |
| 92 | + /** |
| 93 | + * Pointer to the x coordinate |
| 94 | + */ |
| 95 | + const char* x; |
| 96 | + |
| 97 | + /** |
| 98 | + * Pointer to the y coordinate |
| 99 | + */ |
| 100 | + const char* y; |
| 101 | + |
| 102 | + /** |
| 103 | + * Number of bytes in each of x and y |
| 104 | + */ |
| 105 | + uint32_t size; |
| 106 | + |
| 107 | + /** |
| 108 | + * Construct a point view from x and y |
| 109 | + * |
| 110 | + * @param x_ - The x coordinate, poiter to chars |
| 111 | + * @param x_size - x's size |
| 112 | + * @param y_ - The y coordinate, poiter to chars |
| 113 | + * @param y_size - y's size |
| 114 | + */ |
| 115 | + ec_point_view(const char* x_, uint32_t x_size, const char* y_, uint32_t y_size); |
| 116 | + |
| 117 | + /** |
| 118 | + * Construct a point view from a serialized point |
| 119 | + * |
| 120 | + * @param p - The serialized point |
| 121 | + */ |
| 122 | + ec_point_view(const std::vector<char>& p); |
| 123 | + |
| 124 | + /** |
| 125 | + * Construct a point view from a point |
| 126 | + * |
| 127 | + * @param p - The point |
| 128 | + */ |
| 129 | + ec_point_view(const ec_point<Size>& p); |
| 130 | + |
| 131 | + /** |
| 132 | + * Return serialzed point containing only x and y |
| 133 | + */ |
| 134 | + std::vector<char> serialized() const; |
| 135 | + }; |
| 136 | + |
| 137 | + static constexpr size_t g1_coordinate_size = 32; |
| 138 | + static constexpr size_t g2_coordinate_size = 64; |
| 139 | + |
| 140 | + using g1_point = ec_point<g1_coordinate_size>; |
| 141 | + using g2_point = ec_point<g2_coordinate_size>; |
| 142 | + using g1_point_view = ec_point_view<g1_coordinate_size>; |
| 143 | + using g2_point_view = ec_point_view<g2_coordinate_size>; |
| 144 | + |
| 145 | + /** |
| 146 | + * Big integer. |
| 147 | + * |
| 148 | + * @ingroup crypto |
| 149 | + */ |
| 150 | + using bigint = std::vector<char>; |
| 151 | +``` |
| 152 | +
|
| 153 | +### Methods |
| 154 | +
|
| 155 | +- `alt_bn128_add` |
| 156 | +```c++ |
| 157 | + template <typename T> |
| 158 | + g1_point alt_bn128_add( const T& op1, const T& op2 ) |
| 159 | +``` |
| 160 | +Take two G1 points or G1 views as input and return a G1 point. |
| 161 | +- `alt_bn128_mul` |
| 162 | +```c++ |
| 163 | + template <typename T> |
| 164 | + g1_point alt_bn128_mul( const T& g1, const bigint& scalar) |
| 165 | +``` |
| 166 | +Take a G1 point or view and a bigint as input and return a G1 point |
| 167 | +- `alt_bn128_pair` |
| 168 | +```c++ |
| 169 | + template <typename G1_T, typename G2_T> |
| 170 | + int32_t alt_bn128_pair( const std::vector<std::pair<G1_T, G2_T>>& pairs ) |
| 171 | +``` |
| 172 | +Take a pair of G1 and G2 as input. |
| 173 | +- `mod_exp` |
| 174 | +```c++ |
| 175 | + int32_t mod_exp( const bigint& base, const bigint& exp, const bigint& mod, bigint& result ) |
| 176 | +``` |
| 177 | +Take bigints as input |
| 178 | +
|
| 179 | +### Examples |
| 180 | +
|
| 181 | +- `alt_bn128_add` |
| 182 | +```c++ |
| 183 | + std::vector<char> x1, y1, x2, y2; |
| 184 | +
|
| 185 | + // point |
| 186 | + eosio::g1_point point1 {x1, y1}; |
| 187 | + eosio::g1_point point2 {x2, y2}; |
| 188 | + auto result = eosio::alt_bn128_add(point1, point2); |
| 189 | +
|
| 190 | + // view |
| 191 | + eosio::g1_point_view point_view1 {x1.data(), x1.size(), y1.data(), y1.size()}; |
| 192 | + eosio::g1_point_view point_view2 {x2.data(), x2.size(), y2.data(), y2.size()}; |
| 193 | + result = eosio::alt_bn128_add(point_view1, point_view2); |
| 194 | +``` |
| 195 | + |
| 196 | +- `alt_bn128_mul` |
| 197 | +```c++ |
| 198 | + std::vector<char> x, y, scaler; |
| 199 | + eosio::bigint s {scalar}; |
| 200 | + |
| 201 | + // point |
| 202 | + eosio::g1_point g1_point {x, y}; |
| 203 | + auto result = eosio::alt_bn128_mul(g1_point, s); |
| 204 | + |
| 205 | + // view |
| 206 | + eosio::g1_point_view g1_view {x.data(), x.size(), y.data(), y.size()}; |
| 207 | + result = eosio::alt_bn128_mul(g1_view, s); |
| 208 | +``` |
| 209 | +
|
| 210 | +- `alt_bn128_pair` |
| 211 | +```c++ |
| 212 | + std::vector<char> g1_a_x, g1_a_y, g2_a_x, g2_a_y, g1_b_x, g1_b_y, g2_b_x, g2_b_y; |
| 213 | +
|
| 214 | +
|
| 215 | + // point |
| 216 | + eosio::g1_point g1_a {g1_a_x, g1_a_y}; |
| 217 | + eosio::g2_point g2_a {g2_a_x, g2_a_y}; |
| 218 | + eosio::g1_point g1_b {g1_b_x, g1_b_y}; |
| 219 | + eosio::g2_point g2_b {g2_b_x, g2_b_y}; |
| 220 | + std::vector<std::pair<eosio::g1_point, eosio::g2_point>> pairs { {g1_a, g2_a}, {g1_b, g2_b} }; |
| 221 | + auto result = eosio::alt_bn128_pair(pairs); |
| 222 | +
|
| 223 | + // view |
| 224 | + eosio::g1_point_view g1_view_a {g1_a_x.data(), g1_a_x.size(), g1_a_y.data(), g1_a_y.size()}; |
| 225 | + eosio::g2_point_view g2_view_a {g2_a_x.data(), g2_a_x.size(), g2_a_y.data(), g2_a_y.size()}; |
| 226 | + eosio::g1_point_view g1_view_b {g1_b_x.data(), g1_b_x.size(), g1_b_y.data(), g1_b_y.size()}; |
| 227 | + eosio::g2_point_view g2_view_b {g2_b_x.data(), g2_b_x.size(), g2_b_y.data(), g2_b_y.size()}; |
| 228 | + std::vector<std::pair<eosio::g1_point_view, eosio::g2_point_view>> view_pairs { {g1_a, g2_a}, {g1_b, g2_b} }; |
| 229 | + result = eosio::alt_bn128_pair(view_pairs); |
| 230 | +``` |
| 231 | + |
| 232 | +- `mod_exp` |
| 233 | +```c++ |
| 234 | + std::vector<char> base, exp, modulo; |
| 235 | + eosio::bigint base_val {base}; |
| 236 | + eosio::bigint exp_val {exp}; |
| 237 | + eosio::bigint modulo_val {modulo}; |
| 238 | + eosio::bigint result( modulo.size(), '\0' ); |
| 239 | + |
| 240 | + auto rc = eosio::mod_exp(base_val, exp_val, modulo_val, result); |
| 241 | +``` |
0 commit comments