|
| 1 | +#pragma once |
| 2 | +#include "base_bn.h" |
| 3 | + |
| 4 | +using uint128_t = unsigned __int128; |
| 5 | + |
| 6 | +namespace coinbase::crypto { |
| 7 | + |
| 8 | +#ifdef __x86_64__ |
| 9 | +bool support_x64_mulx(); |
| 10 | +#endif |
| 11 | + |
| 12 | +inline uint64_t addx(uint64_t x, uint64_t y, uint64_t& carry) { |
| 13 | +#ifdef __x86_64__ |
| 14 | + unsigned long long z; |
| 15 | + carry = _addcarry_u64(uint8_t(carry), x, y, &z); |
| 16 | + return z; |
| 17 | +#else |
| 18 | +#if __has_builtin(__builtin_addcl) |
| 19 | + return __builtin_addcl(x, y, carry, (unsigned long int*)&carry); |
| 20 | +#else |
| 21 | + auto r = uint128_t(x) + y + carry; |
| 22 | + carry = uint64_t(r >> 64); |
| 23 | + return uint64_t(r); |
| 24 | +#endif |
| 25 | +#endif |
| 26 | +} |
| 27 | + |
| 28 | +inline uint64_t subx(uint64_t x, uint64_t y, uint64_t& borrow) { |
| 29 | +#ifdef __x86_64__ |
| 30 | + unsigned long long z; |
| 31 | + borrow = _subborrow_u64(uint8_t(borrow), x, y, &z); |
| 32 | + return z; |
| 33 | +#else |
| 34 | +#if __has_builtin(__builtin_subcl) |
| 35 | + return __builtin_subcl(x, y, borrow, (unsigned long int*)&borrow); |
| 36 | +#else |
| 37 | + auto r = uint128_t(x) - y - borrow; |
| 38 | + borrow = uint64_t(r >> 64) & 1; |
| 39 | + return uint64_t(r); |
| 40 | +#endif |
| 41 | +#endif |
| 42 | +} |
| 43 | + |
| 44 | +struct alignas(32) uint256_t { |
| 45 | + uint64_t w0, w1, w2, w3; |
| 46 | + static uint256_t from_hex(const std::string& hex) { return from_bn(bn_t::from_hex(hex.c_str())); } |
| 47 | + static uint256_t from_str(const std::string& str) { return from_bn(bn_t::from_string(str.c_str())); } |
| 48 | + |
| 49 | + void to_bin(byte_ptr bin) const; |
| 50 | + buf_t to_bin() const; |
| 51 | + static uint256_t from_bin(mem_t bin); |
| 52 | + void to_bin_le(byte_ptr bin) const; |
| 53 | + buf_t to_bin_le() const; |
| 54 | + static uint256_t from_bin_le(mem_t bin); |
| 55 | + bn_t to_bn() const; |
| 56 | + static uint256_t from_bn(const bn_t& bn); |
| 57 | + |
| 58 | + bool is_zero() const; |
| 59 | + bool is_odd() const; |
| 60 | + bool operator==(const uint256_t& b) const; |
| 61 | + bool operator!=(const uint256_t& b) const; |
| 62 | + void cnd_assign(bool flag, const uint256_t& b); |
| 63 | + uint64_t mul_add_regular(const uint256_t& a, uint64_t b); |
| 64 | + uint64_t div_by_two(); |
| 65 | + void inv_mod(const uint256_t& x, const uint256_t& m); |
| 66 | + |
| 67 | + static void mul_noasm(uint64_t r[8], const uint256_t& a, const uint256_t& b); |
| 68 | + static void mul(uint64_t r[8], const uint256_t& a, const uint256_t& b); |
| 69 | + static void sqr_noasm(uint64_t r[8], const uint256_t& a); |
| 70 | + static void sqr(uint64_t r[8], const uint256_t& a); |
| 71 | + |
| 72 | + static uint256_t get_mont_rr(const uint256_t& mod); |
| 73 | + static uint256_t make(uint64_t w0, uint64_t w1 = 0, uint64_t w2 = 0, uint64_t w3 = 0); |
| 74 | + |
| 75 | + private: |
| 76 | + uint64_t add_raw(const uint256_t& a, const uint256_t& b); |
| 77 | + uint64_t sub_raw(const uint256_t& a, const uint256_t& b); |
| 78 | + uint64_t cnd_add_raw(bool flag, const uint256_t& a); |
| 79 | + uint64_t cnd_sub_raw(bool flag, const uint256_t& a); |
| 80 | + uint64_t cnd_neg_raw(bool flag); |
| 81 | + static void cnd_swap(bool flag, uint256_t& a, uint256_t& b); |
| 82 | +}; |
| 83 | + |
| 84 | +struct uint320_t { |
| 85 | + uint64_t w0, w1, w2, w3, w4; |
| 86 | + void from_bn(const bn_t& bn); |
| 87 | + static uint320_t get_barrett_mu(const uint256_t& mod); |
| 88 | +}; |
| 89 | + |
| 90 | +class alignas(32) bn256_t { |
| 91 | + public: |
| 92 | + bn256_t(); |
| 93 | + ~bn256_t(); |
| 94 | + bn256_t(const bn256_t& x); |
| 95 | + bn256_t(const bn_t& x); |
| 96 | + bn256_t(uint64_t x); |
| 97 | + |
| 98 | + bn256_t& operator=(const bn256_t& x); |
| 99 | + bn256_t& operator=(const bn_t& x); |
| 100 | + bn256_t& operator=(uint64_t x); |
| 101 | + operator bn_t() const; |
| 102 | + |
| 103 | + bool operator==(uint64_t x) const; |
| 104 | + bool operator!=(uint64_t x) const; |
| 105 | + |
| 106 | + bool operator==(const bn256_t& b) const; |
| 107 | + bool operator!=(const bn256_t& b) const; |
| 108 | + |
| 109 | + bn256_t operator+(const bn256_t& b) const; |
| 110 | + bn256_t operator-(const bn256_t& b) const; |
| 111 | + bn256_t operator*(const bn256_t& b) const; |
| 112 | + bn256_t operator/(const bn256_t& b) const; |
| 113 | + |
| 114 | + bn256_t& operator+=(const bn256_t& b); |
| 115 | + bn256_t& operator-=(const bn256_t& b); |
| 116 | + bn256_t& operator*=(const bn256_t& b); |
| 117 | + bn256_t& operator/=(const bn256_t& b); |
| 118 | + |
| 119 | + bn256_t operator-() const; |
| 120 | + bn256_t inv_mod(const mod_t& q) const; |
| 121 | + |
| 122 | + void to_bin(byte_ptr m) const; |
| 123 | + buf_t to_bin() const; |
| 124 | + static bn256_t from_bin(mem_t m); |
| 125 | + void convert(coinbase::converter_t& c); |
| 126 | + static bn256_t rand(const mod_t& q); |
| 127 | + int get_bit(int b) const; |
| 128 | + |
| 129 | + bn256_t to_mont() const; |
| 130 | + bn256_t from_mont() const; |
| 131 | + static bn256_t mont_mul(const bn256_t& a, const bn256_t& b); |
| 132 | + |
| 133 | + static void mul_add_no_reduce(uint64_t r[8], const bn256_t& a, const bn256_t& b); |
| 134 | + static void add_no_reduce(uint64_t r[8], const bn256_t& a); |
| 135 | + static bn256_t reduce(uint64_t r[8]); |
| 136 | + static bn256_t two_to_pow(int n); |
| 137 | + |
| 138 | + private: |
| 139 | + uint64_t w0, w1, w2, w3; |
| 140 | + void barrett_reduce(uint64_t x[8], const uint64_t* m, const uint64_t* mu); |
| 141 | + void mont_mul(const bn256_t& a, const bn256_t& b, const uint64_t* m, uint64_t mont_prime); |
| 142 | + void mont_reduce(uint64_t u[8], const uint64_t* m, uint64_t mont_prime); |
| 143 | +}; |
| 144 | + |
| 145 | +} // namespace coinbase::crypto |
| 146 | + |
| 147 | +using coinbase::crypto::bn256_t; |
0 commit comments