mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
updated block header
1. Updated block header, proofs now contain more data Notice, that old proofs may become invalid in the future 2. Fixed message routing 3. Fixed block creator id in block header 4. Support for full proofs in tonlib 5. Support for partial state download 6. Some other bugfixes
This commit is contained in:
parent
bce33f588a
commit
13140ddf29
73 changed files with 2084 additions and 304 deletions
261
crypto/common/bigexp.cpp
Normal file
261
crypto/common/bigexp.cpp
Normal file
|
@ -0,0 +1,261 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
*/
|
||||
#include "bigexp.h"
|
||||
#include "td/utils/bits.h"
|
||||
#include "td/utils/as.h"
|
||||
#include "td/utils/misc.h"
|
||||
|
||||
namespace td {
|
||||
|
||||
bool NegExpBinTable::init() {
|
||||
init_one();
|
||||
int k;
|
||||
for (k = minpw2; k <= 0; k++) {
|
||||
exp_pw2_table.emplace_back(series_exp(-k));
|
||||
exp_pw2_ref_table.emplace_back(true, exp_pw2_table.back());
|
||||
}
|
||||
for (; k < maxpw2; k++) {
|
||||
td::BigIntG<257 * 2> tmp{0};
|
||||
auto& x = exp_pw2_table.back();
|
||||
tmp.add_mul(x, x).rshift(precision, 0).normalize();
|
||||
exp_pw2_table.emplace_back(tmp);
|
||||
exp_pw2_ref_table.emplace_back(true, exp_pw2_table.back());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NegExpBinTable::adjust_precision(int new_precision, int rmode) {
|
||||
if (new_precision <= 0 || new_precision > precision) {
|
||||
return false;
|
||||
}
|
||||
if (new_precision == precision) {
|
||||
return true;
|
||||
}
|
||||
int s = precision - new_precision;
|
||||
for (auto& x : exp_pw2_table) {
|
||||
x.rshift(s, rmode).normalize();
|
||||
}
|
||||
for (auto& x : exp_pw2_ref_table) {
|
||||
x.write().rshift(s, rmode).normalize();
|
||||
}
|
||||
precision = new_precision;
|
||||
return init_one();
|
||||
}
|
||||
|
||||
bool NegExpBinTable::init_one() {
|
||||
One.set_pow2(precision);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NegExpBinTable::nexpf(td::BigInt256& res, long long x, int k) const { // res := 2^precision * exp(-x * 2^k)
|
||||
if (!x) {
|
||||
res.set_pow2(precision);
|
||||
return true;
|
||||
}
|
||||
if (x < 0) {
|
||||
return false;
|
||||
}
|
||||
int s = td::count_trailing_zeroes64(x);
|
||||
x >>= s;
|
||||
k -= s;
|
||||
if (k + minpw2 > 0) {
|
||||
return false;
|
||||
}
|
||||
int t = 63 - td::count_leading_zeroes64(x);
|
||||
if (t - k >= maxpw2) {
|
||||
return false;
|
||||
}
|
||||
res.set_pow2(precision);
|
||||
while (true) {
|
||||
td::BigIntG<257 * 2> tmp{0};
|
||||
tmp.add_mul(res, exp_pw2_table.at(t - k - minpw2)).rshift(precision, 0).normalize();
|
||||
res = tmp;
|
||||
x -= (1LL << t);
|
||||
if (!x) {
|
||||
return true;
|
||||
}
|
||||
t = 63 - td::count_leading_zeroes64(x);
|
||||
}
|
||||
}
|
||||
|
||||
td::RefInt256 NegExpBinTable::nexpf(long long x, int k) const {
|
||||
td::RefInt256 res{true};
|
||||
if (nexpf(res.unique_write(), x, k)) {
|
||||
return res;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
td::BigInt256 NegExpBinTable::series_exp(int k) const { // returns 2^precision * exp(-2^(-k)), k >= 0
|
||||
td::BigIntG<257 * 2> s{0}, q;
|
||||
const int prec = 52 * 6;
|
||||
q.set_pow2(prec);
|
||||
int i = 0;
|
||||
do {
|
||||
s += q;
|
||||
--i;
|
||||
q.rshift(k).add_tiny(i / 2).divmod_short(i);
|
||||
q.normalize();
|
||||
} while (q.sgn());
|
||||
s.rshift(prec - precision).normalize();
|
||||
return s;
|
||||
}
|
||||
|
||||
NegExpInt64Table::NegExpInt64Table() {
|
||||
NegExpBinTable t{252, 8, -32};
|
||||
CHECK(t.is_valid());
|
||||
table0[0] = 0;
|
||||
table0_shift[0] = 0;
|
||||
for (int i = 1; i <= max_exp; i++) {
|
||||
SuperFloat v(*t.nexpf(i, 0)); // compute exp(-i)
|
||||
CHECK(!v.is_nan());
|
||||
if (v.is_zero()) {
|
||||
table0[i] = 0;
|
||||
table0_shift[i] = 0;
|
||||
} else {
|
||||
CHECK(v.normalize());
|
||||
int k = v.s + 64 - 252;
|
||||
CHECK(k <= -64);
|
||||
if (k > -128) {
|
||||
table0[i] = v.top();
|
||||
table0_shift[i] = td::narrow_cast<unsigned char>(-k - 1);
|
||||
} else {
|
||||
table0[i] = 0;
|
||||
table0_shift[i] = 0;
|
||||
}
|
||||
}
|
||||
// std::cerr << "table0[" << i << "] = exp(-" << i << ") : " << table0[i] << " / 2^" << table0_shift[i] + 1 << std::endl;
|
||||
}
|
||||
td::BigInt256 One;
|
||||
One.set_pow2(252);
|
||||
for (int i = 0; i < 256; i++) {
|
||||
td::BigInt256 x;
|
||||
CHECK(t.nexpf(x, i, 8));
|
||||
(x.negate() += One).rshift(252 - 64, 0).normalize();
|
||||
table1[i] = SuperFloat::as_uint64(x);
|
||||
// std::cerr << "table1[" << i << "] = 1 - exp(-" << i << "/256) : " << table1[i] << " / 2^64" << std::endl;
|
||||
}
|
||||
for (int i = 0; i < 256; i++) {
|
||||
td::BigInt256 x;
|
||||
CHECK(t.nexpf(x, i, 16));
|
||||
(x.negate() += One).rshift(252 - 64 - 8, 0).normalize();
|
||||
table2[i] = SuperFloat::as_uint64(x);
|
||||
// std::cerr << "table2[" << i << "] = 1 - exp(-" << i << "/2^16) : " << table2[i] << " / 2^72" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
td::uint64 NegExpInt64Table::umulnexps32(td::uint64 x, unsigned k, bool trunc) const { // compute x * exp(-k / 2^16)
|
||||
if (!k || !x) {
|
||||
return x;
|
||||
}
|
||||
unsigned k0 = (k >> 16);
|
||||
if (k0 > max_exp) {
|
||||
return 0;
|
||||
}
|
||||
unsigned s = td::count_leading_zeroes_non_zero64(x);
|
||||
x <<= s;
|
||||
unsigned k1 = (k >> 8) & 0xff;
|
||||
unsigned k2 = k & 0xff;
|
||||
if (k2) {
|
||||
x -= ((td::uint128::from_unsigned(x).mult(table2[k2]).rounded_hi() + 0x80) >> 8);
|
||||
}
|
||||
if (k1) {
|
||||
x -= td::uint128::from_unsigned(x).mult(table1[k1]).rounded_hi();
|
||||
}
|
||||
if (k0) {
|
||||
if (trunc) {
|
||||
return td::uint128::from_unsigned(x).mult(table0[k0]).shr(table0_shift[k0] + s + 1).lo();
|
||||
} else {
|
||||
return (td::uint128::from_unsigned(x).mult(table0[k0]).shr(table0_shift[k0] + s).lo() + 1) >> 1;
|
||||
}
|
||||
}
|
||||
if (!s) {
|
||||
return x;
|
||||
} else if (trunc) {
|
||||
return x >> s;
|
||||
} else {
|
||||
return ((x >> (s - 1)) + 1) >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
td::int64 NegExpInt64Table::mulnexps32(td::int64 x, unsigned k, bool trunc) const {
|
||||
return x >= 0 ? umulnexps32(x, k, trunc) : -umulnexps32(-x, k, trunc);
|
||||
}
|
||||
|
||||
const NegExpInt64Table& NegExpInt64Table::table() {
|
||||
static NegExpInt64Table tab;
|
||||
return tab;
|
||||
}
|
||||
|
||||
td::uint64 umulnexps32(td::uint64 x, unsigned k, bool trunc) { // compute x * exp(-k / 2^16)
|
||||
return NegExpInt64Table::table().umulnexps32(x, k, trunc);
|
||||
}
|
||||
|
||||
td::int64 mulnexps32(td::int64 x, unsigned k, bool trunc) {
|
||||
return NegExpInt64Table::table().mulnexps32(x, k, trunc);
|
||||
}
|
||||
|
||||
td::uint128 SuperFloat::as_uint128(const td::BigInt256& x) {
|
||||
td::uint64 t[2];
|
||||
if (!x.export_bytes_lsb((unsigned char*)(void*)t, sizeof(t), false)) {
|
||||
return {std::numeric_limits<uint64>::max(), 0};
|
||||
} else {
|
||||
return {t[1], t[0]};
|
||||
}
|
||||
}
|
||||
|
||||
td::uint64 SuperFloat::as_uint64(const td::BigInt256& x) {
|
||||
td::uint64 t;
|
||||
if (!x.export_bytes_lsb((unsigned char*)&t, sizeof(t), false)) {
|
||||
return std::numeric_limits<uint64>::max();
|
||||
} else {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
SuperFloat::SuperFloat(td::BigInt256 x) {
|
||||
if (x.unsigned_fits_bits(128)) {
|
||||
m = as_uint128(x);
|
||||
s = 0;
|
||||
} else if (x.sgn() == 1) {
|
||||
s = x.bit_size(false) - 128;
|
||||
x.rshift(s, 0).normalize();
|
||||
m = as_uint128(x);
|
||||
} else {
|
||||
set_nan();
|
||||
}
|
||||
}
|
||||
|
||||
bool SuperFloat::normalize() {
|
||||
if (is_nan()) {
|
||||
return false;
|
||||
}
|
||||
if (is_zero()) {
|
||||
s = 0;
|
||||
return true;
|
||||
}
|
||||
auto hi = m.hi();
|
||||
int t = (hi ? td::count_leading_zeroes_non_zero64(hi) : 64 + td::count_leading_zeroes_non_zero64(m.lo()));
|
||||
m.shl(t);
|
||||
s -= t;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace td
|
145
crypto/common/bigexp.h
Normal file
145
crypto/common/bigexp.h
Normal file
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "common/refint.h"
|
||||
|
||||
namespace td {
|
||||
|
||||
class NegExpBinTable {
|
||||
int precision, maxpw2, minpw2;
|
||||
std::vector<td::BigInt256> exp_pw2_table; // table of 2^precision * exp(- 2^k) for k = max_pw2-1 .. min_pw2
|
||||
std::vector<td::RefInt256> exp_pw2_ref_table; // same data
|
||||
td::BigInt256 One;
|
||||
|
||||
public:
|
||||
NegExpBinTable(int _precision, int _maxpw2, int _minpw2) : precision(255), maxpw2(_maxpw2), minpw2(_minpw2) {
|
||||
(_precision > 0 && _precision < 256 && _minpw2 <= 0 && _maxpw2 > 0 && _maxpw2 <= 256 && _minpw2 >= -256 && init() &&
|
||||
adjust_precision(_precision)) ||
|
||||
invalidate();
|
||||
}
|
||||
bool is_valid() const {
|
||||
return minpw2 < maxpw2;
|
||||
}
|
||||
int get_precision() const {
|
||||
return precision;
|
||||
}
|
||||
int get_exponent_precision() const {
|
||||
return -minpw2;
|
||||
}
|
||||
int get_exponent_max_log2() const {
|
||||
return maxpw2;
|
||||
}
|
||||
const td::BigInt256* exp_pw2(int k) const { // returns 2^precision * exp(-2^k) or null
|
||||
return (k >= minpw2 && k < maxpw2) ? &exp_pw2_table[k - minpw2] : nullptr;
|
||||
}
|
||||
td::RefInt256 exp_pw2_ref(int k) const {
|
||||
if (k >= minpw2 && k < maxpw2) {
|
||||
return exp_pw2_ref_table[k - minpw2];
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
bool nexpf(td::BigInt256& res, long long x, int k) const; // res := 2^precision * exp(-x * 2^k)
|
||||
td::RefInt256 nexpf(long long x, int k) const;
|
||||
|
||||
private:
|
||||
bool init();
|
||||
bool init_one();
|
||||
bool adjust_precision(int new_precision, int rmode = 0);
|
||||
bool invalidate() {
|
||||
minpw2 = maxpw2 = 0;
|
||||
return false;
|
||||
}
|
||||
td::BigInt256 series_exp(int k) const; // returns 2^precision * exp(-2^(-k)), k >= 0
|
||||
};
|
||||
|
||||
struct SuperFloat {
|
||||
struct SetZero {};
|
||||
struct SetOne {};
|
||||
struct SetNan {};
|
||||
td::uint128 m;
|
||||
int s;
|
||||
SuperFloat() = default;
|
||||
SuperFloat(SetZero) : m(0, 0), s(0) {
|
||||
}
|
||||
SuperFloat(SetOne) : m(0, 1), s(0) {
|
||||
}
|
||||
SuperFloat(SetNan) : m(0, 0), s(std::numeric_limits<int>::min()) {
|
||||
}
|
||||
SuperFloat(td::uint128 _m, int _s = 0) : m(_m), s(_s) {
|
||||
}
|
||||
SuperFloat(td::uint64 _m, int _s = 0) : m(0, _m), s(_s) {
|
||||
}
|
||||
explicit SuperFloat(BigInt256 x);
|
||||
static SuperFloat Zero() {
|
||||
return SetZero{};
|
||||
}
|
||||
static SuperFloat One() {
|
||||
return SetOne{};
|
||||
}
|
||||
static SuperFloat NaN() {
|
||||
return SetNan{};
|
||||
}
|
||||
void set_zero() {
|
||||
m = td::uint128(0, 0);
|
||||
s = 0;
|
||||
}
|
||||
void set_one() {
|
||||
m = td::uint128(0, 1);
|
||||
s = 0;
|
||||
}
|
||||
void set_nan() {
|
||||
s = std::numeric_limits<int>::min();
|
||||
}
|
||||
bool is_nan() const {
|
||||
return s == std::numeric_limits<int>::min();
|
||||
}
|
||||
bool is_zero() const {
|
||||
return m.is_zero();
|
||||
}
|
||||
bool normalize();
|
||||
td::uint64 top() const {
|
||||
return m.rounded_hi();
|
||||
}
|
||||
static td::uint128 as_uint128(const td::BigInt256& x);
|
||||
static td::uint64 as_uint64(const td::BigInt256& x);
|
||||
};
|
||||
|
||||
class NegExpInt64Table {
|
||||
enum { max_exp = 45 };
|
||||
unsigned char table0_shift[max_exp + 1];
|
||||
td::uint64 table0[max_exp + 1], table1[256], table2[256];
|
||||
|
||||
public:
|
||||
NegExpInt64Table();
|
||||
// compute x * exp(-k / 2^16);
|
||||
// more precisely: computes 0 <= y <= x for 0 <= x < 2^60, s.that |y - x * exp(-k / 2^16)| < 1
|
||||
// two different implementations of this functions would return values differing by at most one
|
||||
td::uint64 umulnexps32(td::uint64 x, unsigned k, bool trunc = false) const;
|
||||
td::int64 mulnexps32(td::int64 x, unsigned k, bool trunc = false) const;
|
||||
static const NegExpInt64Table& table();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
td::uint64 umulnexps32(td::uint64 x, unsigned k, bool trunc = false); // compute x * exp(-k / 2^16)
|
||||
td::int64 mulnexps32(td::int64 x, unsigned k, bool trunc = false);
|
||||
|
||||
} // namespace td
|
|
@ -128,6 +128,9 @@ struct BitPtrGen {
|
|||
std::size_t scan(bool value, std::size_t len) const {
|
||||
return bitstring::bits_memscan(*this, len, value);
|
||||
}
|
||||
bool is_zero(std::size_t len) const {
|
||||
return scan(false, len) == len;
|
||||
}
|
||||
long long get_int(unsigned bits) const {
|
||||
return bitstring::bits_load_long(*this, bits);
|
||||
}
|
||||
|
@ -279,7 +282,7 @@ class BitSliceGen {
|
|||
ensure_throw(set_size_bool(bits));
|
||||
return *this;
|
||||
}
|
||||
BitSliceGen subslice(unsigned from, unsigned bits) const & {
|
||||
BitSliceGen subslice(unsigned from, unsigned bits) const& {
|
||||
return BitSliceGen(*this, from, bits);
|
||||
}
|
||||
BitSliceGen subslice(unsigned from, unsigned bits) && {
|
||||
|
@ -467,7 +470,7 @@ class BitArray {
|
|||
unsigned char* data() {
|
||||
return bytes.data();
|
||||
}
|
||||
unsigned size() const {
|
||||
static unsigned size() {
|
||||
return n;
|
||||
}
|
||||
const byte_array_t& as_array() const {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue