From c82a076e38e41a6b973386a8f512e9d8b61b379f Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Sat, 22 Apr 2023 08:39:49 -0500 Subject: [PATCH 001/158] added bls12-381 crypto primitives --- .../eosiolib/capi/eosio/crypto_bls_ext.h | 40 +++++++ libraries/eosiolib/capi/eosio/types.h | 42 +++++++ .../eosiolib/core/eosio/crypto_bls_ext.hpp | 106 ++++++++++++++++++ libraries/native/intrinsics.cpp | 52 ++++++++- .../native/native/eosio/intrinsics_def.hpp | 13 ++- tools/include/eosio/gen.hpp | 17 ++- 6 files changed, 266 insertions(+), 4 deletions(-) create mode 100644 libraries/eosiolib/capi/eosio/crypto_bls_ext.h create mode 100644 libraries/eosiolib/core/eosio/crypto_bls_ext.hpp diff --git a/libraries/eosiolib/capi/eosio/crypto_bls_ext.h b/libraries/eosiolib/capi/eosio/crypto_bls_ext.h new file mode 100644 index 0000000000..a19984dbf3 --- /dev/null +++ b/libraries/eosiolib/capi/eosio/crypto_bls_ext.h @@ -0,0 +1,40 @@ +#pragma once +#include "types.h" +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((eosio_wasm_import)) +void bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +void bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); + +#ifdef __cplusplus +} +#endif + diff --git a/libraries/eosiolib/capi/eosio/types.h b/libraries/eosiolib/capi/eosio/types.h index b3f54915b9..198ed5b08d 100644 --- a/libraries/eosiolib/capi/eosio/types.h +++ b/libraries/eosiolib/capi/eosio/types.h @@ -76,4 +76,46 @@ struct ALIGNED(capi_checksum512) { uint8_t hash[64]; }; +/** + * BLS12-381 scalar type (LE) + */ +struct ALIGNED(capi_bls_scalar) { + uint8_t data[32]; +}; + +/** + * BLS12-381 fp type (LE) + */ +struct ALIGNED(capi_bls_fp) { + uint8_t data[48]; +}; + +/** + * BLS12-381 fp2 type (LE) + */ +struct ALIGNED(capi_bls_fp2) { + uint8_t data[96]; +}; + +/** + * BLS12-381 G1 type (Jacobian, LE) + */ +struct ALIGNED(capi_bls_g1) { + uint8_t data[144]; +}; + +/** + * BLS12-381 G2 type (Jacobian, LE) + */ +struct ALIGNED(capi_bls_g2) { + uint8_t data[288]; +}; + +/** + * BLS12-381 GT type (LE) + */ +struct ALIGNED(capi_bls_gt) { + uint8_t data[576]; +}; + /// @} diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp new file mode 100644 index 0000000000..a1c4f3502c --- /dev/null +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -0,0 +1,106 @@ +#pragma once + +#include "fixed_bytes.hpp" +#include "varint.hpp" +#include "serialize.hpp" + +#include +#include + +namespace eosio { + + using bls_scalar = std::array; + using bls_fp = std::array; + using bls_fp2 = std::array; + using bls_g1 = std::array; + using bls_g2 = std::array; + using bls_gt = std::array; + + namespace internal_use_do_not_use { + extern "C" { + __attribute__((eosio_wasm_import)) + void bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + void bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); + } + } + + void bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) + { + return internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1.data()), op1.size(), reinterpret_cast(op2.data()), op2.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) + { + return internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1.data()), op1.size(), reinterpret_cast(op2.data()), op2.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) + { + return internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point.data()), point.size(), reinterpret_cast(scalar.data()), scalar.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) + { + return internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point.data()), point.size(), reinterpret_cast(scalar.data()), scalar.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_g1_exp(const std::vector& points, const std::vector& scalars, bls_g1& res) + { + if(points.size() != scalars.size()) return; + return internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points.data()), points.size() * sizeof(bls_g1), reinterpret_cast(scalars.data()), scalars.size() * sizeof(bls_scalar), points.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_g2_exp(const std::vector& points, const std::vector& scalars, bls_g2& res) + { + if(points.size() != scalars.size()) return; + return internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points.data()), points.size() * sizeof(bls_g2), reinterpret_cast(scalars.data()), scalars.size() * sizeof(bls_scalar), points.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_pairing(const std::vector& g1_points, const std::vector& g2_points, bls_gt& res) + { + if(g1_points.size() != g2_points.size()) return; + return internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points.data()), g1_points.size() * sizeof(bls_g1), reinterpret_cast(g2_points.data()), g2_points.size() * sizeof(bls_g2), g1_points.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_g1_map(const bls_fp& e, bls_g1& res) + { + return internal_use_do_not_use::bls_g1_map(reinterpret_cast(e.data()), e.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_g2_map(const bls_fp2& e, bls_g2& res) + { + return internal_use_do_not_use::bls_g2_map(reinterpret_cast(e.data()), e.size(), reinterpret_cast(res.data()), res.size()); + } + + void bls_fp_mod(const std::array& s, bls_fp& res) + { + return internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s.data()), s.size(), reinterpret_cast(res.data()), res.size()); + } +} + diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index 0d5a80786a..0658c45b37 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -935,4 +935,54 @@ int32_t mod_exp( const char* base, uint32_t base_len, const char* exp, uint32_t void sha3( const char* data, uint32_t data_len, char* hash, uint32_t hash_len, int32_t keccak ) { intrinsics::get().call(data, data_len, hash, hash_len, keccak); -} \ No newline at end of file +} + +void bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) +{ + intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); +} + +void bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) +{ + intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); +} + +void bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) +{ + intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); +} + +void bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) +{ + intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); +} + +void bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) +{ + intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); +} + +void bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) +{ + intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); +} + +void bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len) +{ + intrinsics::get().call(g1_points, g1_points_len, g1_points, g1_points_len, n, res, res_len); +} + +void bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) +{ + intrinsics::get().call(e, e_len, res, res_len); +} + +void bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) +{ + intrinsics::get().call(e, e_len, res, res_len); +} + +void bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len) +{ + intrinsics::get().call(s, s_len, res, res_len); +} diff --git a/libraries/native/native/eosio/intrinsics_def.hpp b/libraries/native/native/eosio/intrinsics_def.hpp index 4dd728117e..7affd102e0 100644 --- a/libraries/native/native/eosio/intrinsics_def.hpp +++ b/libraries/native/native/eosio/intrinsics_def.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -172,7 +173,17 @@ intrinsic_macro(k1_recover) \ intrinsic_macro(alt_bn128_add) \ intrinsic_macro(alt_bn128_mul) \ intrinsic_macro(alt_bn128_pair) \ -intrinsic_macro(mod_exp) +intrinsic_macro(mod_exp) \ +intrinsic_macro(bls_g1_add) \ +intrinsic_macro(bls_g2_add) \ +intrinsic_macro(bls_g1_mul) \ +intrinsic_macro(bls_g2_mul) \ +intrinsic_macro(bls_g1_exp) \ +intrinsic_macro(bls_g2_exp) \ +intrinsic_macro(bls_pairing) \ +intrinsic_macro(bls_g1_map) \ +intrinsic_macro(bls_g2_map) \ +intrinsic_macro(bls_fp_mod) diff --git a/tools/include/eosio/gen.hpp b/tools/include/eosio/gen.hpp index 8e2494d428..7bb214a7be 100644 --- a/tools/include/eosio/gen.hpp +++ b/tools/include/eosio/gen.hpp @@ -469,7 +469,14 @@ struct generation_utils { {"capi_checksum512", "checksum512"}, {"fixed_bytes_20", "checksum160"}, {"fixed_bytes_32", "checksum256"}, - {"fixed_bytes_64", "checksum512"} + {"fixed_bytes_64", "checksum512"}, + + {"capi_bls_scalar", "bls_scalar"}, + {"capi_bls_g1", "bls_fp"}, + {"capi_bls_g2", "bls_fp2"}, + {"capi_bls_g1", "bls_g1"}, + {"capi_bls_g2", "bls_g2"}, + {"capi_bls_gt", "bls_gt"} }; auto ret = translation_table[t]; @@ -764,7 +771,13 @@ struct generation_utils { "symbol", "symbol_code", "asset", - "extended_asset" + "extended_asset", + "bls_scalar", + "bls_fp", + "bls_fp2", + "bls_g1", + "bls_g2", + "bls_gt" }; return builtins.count(_translate_type(t)) >= 1; } From be7b70ccae3f737df92ae868f2b9cac0fd8dc000 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Mon, 1 May 2023 07:08:35 -0700 Subject: [PATCH 002/158] note to install dep clang-tidy --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7c2cd2cf0b..9760e1ad05 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ The instructions below assume that you are building on Ubuntu 20.04. apt-get update && apt-get install \ build-essential \ clang \ + clang-tidy \ cmake \ git \ libxml2-dev \ From 19d33cca18ecef683003bf40682f0b2707e4e38d Mon Sep 17 00:00:00 2001 From: Alexander Molchevsky Date: Wed, 3 May 2023 13:47:40 +0300 Subject: [PATCH 003/158] The crypto extensions documentation was reviewed and updated --- docs/05_features/50_crypto-extensions.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/05_features/50_crypto-extensions.md b/docs/05_features/50_crypto-extensions.md index 414517be80..bbb16fe35d 100644 --- a/docs/05_features/50_crypto-extensions.md +++ b/docs/05_features/50_crypto-extensions.md @@ -7,10 +7,10 @@ As of `v3.0` crypto host functions were extended to include - `alt_bn128_add`, `alt_bn128_mul`, `alt_bn128_pair`: Add, multiply, and pairing check functions for the `alt_bn128` elliptic curve - `blake2_f`: `BLAKE2b F` compression function - `sha3`: sha3` hash function using `SHA3 NIST` -- `Keccak256`: `sha3` hash function using `SHA3 Keccak` +- `keccak`: `Keccak256` hash function using `SHA3 Keccak` - `k1_recover`: Safe `ECDSA` uncompressed pubkey recover for the `secp256k1` curve -In `v3.0`, `C` format was supported; in `v3.1`, `C++` format was added for better data abstraction. +In `v3.0`, `C` format was supported; in `v3.1` and newer versions, `C++` format was added for better data abstraction. ## Prerequisites @@ -79,7 +79,7 @@ C++ types were added to represent `G1` and `G2` points (read and write) and view ec_point(std::vector& p); /** - * Return serialzed point containing only x and y + * Return serialized point containing only x and y */ std::vector serialized() const; }; @@ -129,7 +129,7 @@ C++ types were added to represent `G1` and `G2` points (read and write) and view ec_point_view(const ec_point& p); /** - * Return serialzed point containing only x and y + * Return serialized point containing only x and y */ std::vector serialized() const; }; From 9a7e23f8f4e622b75dfc4fbbb9056ecb32d27b63 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 10 May 2023 19:55:37 -0400 Subject: [PATCH 004/158] fix antler-proj ubuntu 18 build --- tools/external/antler-proj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/external/antler-proj b/tools/external/antler-proj index 8fb86d24da..2034b9a38f 160000 --- a/tools/external/antler-proj +++ b/tools/external/antler-proj @@ -1 +1 @@ -Subproject commit 8fb86d24dac9c8f83dde59c3a15c441ab380c6db +Subproject commit 2034b9a38f61aa1a55f17eed657d466bbcfcc6e6 From 23510b2d3c11e0651b40fb075da082ea5eadc383 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 15 May 2023 21:57:27 -0400 Subject: [PATCH 005/158] fixing abi for singleton with non-table type --- .../abigen-pass/singleton_contract_simple.abi | 31 +++++++++++++++++++ .../abigen-pass/singleton_contract_simple.cpp | 16 ++++++++++ .../singleton_contract_simple.json | 10 ++++++ tools/include/eosio/abigen.hpp | 17 +++++----- 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 tests/toolchain/abigen-pass/singleton_contract_simple.abi create mode 100644 tests/toolchain/abigen-pass/singleton_contract_simple.cpp create mode 100644 tests/toolchain/abigen-pass/singleton_contract_simple.json diff --git a/tests/toolchain/abigen-pass/singleton_contract_simple.abi b/tests/toolchain/abigen-pass/singleton_contract_simple.abi new file mode 100644 index 0000000000..8600eb2f76 --- /dev/null +++ b/tests/toolchain/abigen-pass/singleton_contract_simple.abi @@ -0,0 +1,31 @@ +{ + "____comment": "This file was generated with eosio-abigen. DO NOT EDIT ", + "version": "eosio::abi/1.2", + "types": [], + "structs": [ + { + "name": "whatever", + "base": "", + "fields": [] + } + ], + "actions": [ + { + "name": "whatever", + "type": "whatever", + "ricardian_contract": "" + } + ], + "tables": [ + { + "name": "smpl.config", + "type": "name", + "index_type": "i64", + "key_names": [], + "key_types": [] + } + ], + "ricardian_clauses": [], + "variants": [], + "action_results": [] +} diff --git a/tests/toolchain/abigen-pass/singleton_contract_simple.cpp b/tests/toolchain/abigen-pass/singleton_contract_simple.cpp new file mode 100644 index 0000000000..2afdc7c332 --- /dev/null +++ b/tests/toolchain/abigen-pass/singleton_contract_simple.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +using namespace eosio; + +class [[eosio::contract("singleton_contract_simple")]] singleton_contract_simple : public contract { + public: + using contract::contract; + + [[eosio::action]] + void whatever() {}; + + typedef eosio::singleton<"smpl.config"_n, name> config; +}; diff --git a/tests/toolchain/abigen-pass/singleton_contract_simple.json b/tests/toolchain/abigen-pass/singleton_contract_simple.json new file mode 100644 index 0000000000..0bf81acc1e --- /dev/null +++ b/tests/toolchain/abigen-pass/singleton_contract_simple.json @@ -0,0 +1,10 @@ +{ + "tests" : [ + { + "expected" : { + "abi-file" : "singleton_contract_simple.abi" + } + } + ] + } + \ No newline at end of file diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 728c8f955f..9becbda752 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -243,14 +243,13 @@ namespace eosio { namespace cdt { ctables.insert(t); } - void add_table( uint64_t name, const clang::CXXRecordDecl* decl ) { - if (!(decl->isEosioTable() && abigen::is_eosio_contract(decl, get_contract_name()))) - return; - - abi_table t; - t.type = decl->getNameAsString(); - t.name = name_to_string(name); - _abi.tables.insert(t); + void add_table( uint64_t name, const clang::CXXRecordDecl* decl, bool force=false ) { + if (force || decl->isEosioTable() && abigen::is_eosio_contract(decl, get_contract_name())) { + abi_table t; + t.type = decl->getNameAsString(); + t.name = name_to_string(name); + _abi.tables.insert(t); + } } void add_clauses( const std::vector>& clauses ) { @@ -804,7 +803,7 @@ namespace eosio { namespace cdt { if (const auto* d = dyn_cast(decl)) { if (d->getName() == "multi_index" || d->getName() == "singleton") { ag.add_table(d->getTemplateArgs()[0].getAsIntegral().getExtValue(), - (clang::CXXRecordDecl*)((clang::RecordType*)d->getTemplateArgs()[1].getAsType().getTypePtr())->getDecl()); + d->getTemplateArgs()[1].getAsType().getTypePtr()->getAsCXXRecordDecl(), true); } } return true; From 5931b591c5d483392ed8158a784245f03a8a1da8 Mon Sep 17 00:00:00 2001 From: Alexander Molchevsky Date: Wed, 17 May 2023 15:08:49 +0300 Subject: [PATCH 006/158] Revert "Added an implementation of a new cdt-abidiff" This reverts commit 20890cd510018556d1302e746c53310ae2306c5b. --- tools/abidiff/cdt-abidiff.py | 46 ------------------------------------ 1 file changed, 46 deletions(-) delete mode 100644 tools/abidiff/cdt-abidiff.py diff --git a/tools/abidiff/cdt-abidiff.py b/tools/abidiff/cdt-abidiff.py deleted file mode 100644 index 4ef2514708..0000000000 --- a/tools/abidiff/cdt-abidiff.py +++ /dev/null @@ -1,46 +0,0 @@ -import json -import sys - -def compare_abi_files(file1, file2): - - # check if the files are exist and try to read data from them - for file in [file1, file2]: - if not os.path.isfile(file): - print(f"Error: File '{file}' does not exist.") - return - try: - with open(file1, 'r') as f1, open(file2, 'r') as f2: - data1, data2 = json.load(f1), json.load(f2) - except json.JSONDecodeError as e: - print(f"Error: Invalid JSON in file '{e.filename}' at position {e.pos}.") - return - for data, file in [(data1, file1), (data2, file2)]: - if not isinstance(data, dict): - print(f"Error: File '{file}' does not contain a JSON object.") - return - - diff = {} - - # add to the diff dictionary the different pairs of values for a key - # if a key is exists in the only file add None instead of the paired data for the second file. - for key in data1: - if key not in data2: - diff[key] = {file1: data1[key], file2: None} - elif data1[key] != data2[key]: - diff[key] = {file1: data1[key], file2: data2[key]} - for key in data2: - if key not in data1: - diff[key] = {file1: None, file2: data2[key]} - - if not diff: - print("Files are identical.") - else: - print("Differences found:") - print(json.dumps(diff, indent=4)) - - -if len(sys.argv) != 3: - print("Usage: python3" + sys.argv[0] + " ") -else: - file1, file2 = sys.argv[1], sys.argv[2] - compare_abi_files(file1, file2) From 46c13e9e9dc13ecef455fbad0c261d7f363a4a76 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 17 May 2023 17:34:17 -0400 Subject: [PATCH 007/158] fix integration tests --- tools/include/eosio/abigen.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 9becbda752..479c9fec73 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -801,9 +801,10 @@ namespace eosio { namespace cdt { } virtual bool VisitDecl(clang::Decl* decl) { if (const auto* d = dyn_cast(decl)) { - if (d->getName() == "multi_index" || d->getName() == "singleton") { + bool is_singleton = d->getName() == "singleton"; + if (d->getName() == "multi_index" || is_singleton) { ag.add_table(d->getTemplateArgs()[0].getAsIntegral().getExtValue(), - d->getTemplateArgs()[1].getAsType().getTypePtr()->getAsCXXRecordDecl(), true); + d->getTemplateArgs()[1].getAsType().getTypePtr()->getAsCXXRecordDecl(), is_singleton); } } return true; From 94ca0508dddd1d1d0d93847088e3b49ca0ce00f2 Mon Sep 17 00:00:00 2001 From: Michal Lesiak Date: Tue, 16 May 2023 12:57:13 +0200 Subject: [PATCH 008/158] Add insert, remove and update methods to multi_index (and index) --- .../eosiolib/contracts/eosio/multi_index.hpp | 224 ++++++++++++++++++ .../unit/test_contracts/multi_index_tests.cpp | 35 +++ 2 files changed, 259 insertions(+) diff --git a/libraries/eosiolib/contracts/eosio/multi_index.hpp b/libraries/eosiolib/contracts/eosio/multi_index.hpp index 2b06384aac..2ae6ae0e0d 100644 --- a/libraries/eosiolib/contracts/eosio/multi_index.hpp +++ b/libraries/eosiolib/contracts/eosio/multi_index.hpp @@ -732,11 +732,21 @@ class multi_index _multidx->modify( *itr, payer, std::forward(updater) ); } + template + void update( const_iterator itr, eosio::name payer, Lambda&& updater ) { + modify( itr, payer, std::forward(updater) ); + } + template void modify( const T& obj, eosio::name payer, Lambda&& updater ) { _multidx->modify( obj, payer, std::forward(updater) ); } + template + void update( const T& obj, eosio::name payer, Lambda&& updater ) { + modify( obj, payer, std::forward(updater) ); + } + const_iterator erase( const_iterator itr ) { eosio::check( itr != cend(), "cannot pass end iterator to erase" ); @@ -748,6 +758,10 @@ class multi_index return itr; } + const_iterator remove( const_iterator itr ) { + return erase(itr); + } + eosio::name get_code()const { return _multidx->get_code(); } uint64_t get_scope()const { return _multidx->get_scope(); } @@ -1531,6 +1545,7 @@ class multi_index eosio::check( objitem.__idx == this, "object passed to iterator_to is not in multi_index" ); return {this, &objitem}; } + /** * Adds a new object (i.e., row) to the table. * @ingroup multiindex @@ -1613,6 +1628,48 @@ class multi_index return {this, ptr}; } + /** + * Adds a new object (i.e., row) to the table, alias for emplace() + * @ingroup multiindex + * + * @param payer - Account name of the payer for the Storage usage of the new object + * @param constructor - Lambda function that does an in-place initialization of the object to be created in the table + * + * @pre A multi index table has been instantiated + * @post A new object is created in the Multi-Index table, with a unique primary key (as specified in the object). The object is serialized and written to the table. If the table does not exist, it is created. + * @post Secondary indices are updated to refer to the newly added object. If the secondary index tables do not exist, they are created. + * @post The payer is charged for the storage usage of the new object and, if the table (and secondary index tables) must be created, for the overhead of the table creation. + * + * @return A primary key iterator to the newly created object + * + * Exception - The account is not authorized to write to the table. + * + * Example: + * + * @code + * // This assumes the code from the constructor example. Replace myaction() {...} + * + * void myaction() { + * address_index addresses(_self, _self.value); // code, scope + * // add to table, first argument is account to bill for storage + * addresses.insert(_self, [&](auto& address) { + * address.account_name = "dan"_n; + * address.first_name = "Daniel"; + * address.last_name = "Larimer"; + * address.street = "1 EOS Way"; + * address.city = "Blacksburg"; + * address.state = "VA"; + * }); + * } + * } + * EOSIO_DISPATCH( addressbook, (myaction) ) + * @endcode + */ + template + const_iterator insert( name payer, Lambda&& constructor ) { + return emplace(payer, std::forward(constructor)); + } + /** * Modifies an existing object in a table. * @ingroup multiindex @@ -1660,6 +1717,51 @@ class multi_index modify( *itr, payer, std::forward(updater) ); } + /** + * Updates an existing object in a table, alias for modify() + * @ingroup multiindex + * + * @param itr - an iterator pointing to the object to be updated + * @param payer - account name of the payer for the storage usage of the updated row + * @param updater - lambda function that updates the target object + * + * @pre itr points to an existing element + * @pre payer is a valid account that is authorized to execute the action and be billed for storage usage. + * + * @post The updated object is serialized, then replaces the existing object in the table. + * @post Secondary indices are updated; the primary key of the updated object is not changed. + * @post The payer is charged for the storage usage of the updated object. + * @post If payer is the same as the existing payer, payer only pays for the usage difference between existing and updated object (and is refunded if this difference is negative). + * @post If payer is different from the existing payer, the existing payer is refunded for the storage usage of the existing object. + * + * Exceptions: + * If called with an invalid precondition, execution is aborted. + * + * Example: + * + * @code + * // This assumes the code from the constructor example. Replace myaction() {...} + * + * void myaction() { + * // create reference to address_index - see emplace example + * // add dan account to table - see emplace example + * + * auto itr = addresses.find("dan"_n); + * eosio::check(itr != addresses.end(), "Address for account not found"); + * addresses.update( itr, account payer, [&]( auto& address ) { + * address.city = "San Luis Obispo"; + * address.state = "CA"; + * }); + * } + * } + * EOSIO_DISPATCH( addressbook, (myaction) ) + * @endcode + */ + template + void update( const_iterator itr, name payer, Lambda&& updater ) { + modify( itr, payer, std::forward(updater) ); + } + /** * Modifies an existing object in a table. * @ingroup multiindex @@ -1752,6 +1854,52 @@ class multi_index } ); } + /** + * Updates an existing object in a table, alias for modify() + * @ingroup multiindex + * + * @param obj - a reference to the object to be updated + * @param payer - account name of the payer for the storage usage of the updated row + * @param updater - lambda function that updates the target object + * + * @pre obj is an existing object in the table + * @pre payer is a valid account that is authorized to execute the action and be billed for storage usage. + * + * @post The modified object is serialized, then replaces the existing object in the table. + * @post Secondary indices are updated; the primary key of the updated object is not changed. + * @post The payer is charged for the storage usage of the updated object. + * @post If payer is the same as the existing payer, payer only pays for the usage difference between existing and updated object (and is refunded if this difference is negative). + * @post If payer is different from the existing payer, the existing payer is refunded for the storage usage of the existing object. + * + * Exceptions: + * If called with an invalid precondition, execution is aborted. + * + * Example: + * + * @code + * // This assumes the code from the constructor example. Replace myaction() {...} + * + * void myaction() { + * // create reference to address_index - see emplace example + * // add dan account to table - see emplace example + * + * auto itr = addresses.find("dan"_n); + * eosio::check(itr != addresses.end(), "Address for account not found"); + * addresses.update( *itr, payer, [&]( auto& address ) { + * address.city = "San Luis Obispo"; + * address.state = "CA"; + * }); + * eosio::check(itr->city == "San Luis Obispo", "Lock arf, Address not modified"); + * } + * } + * EOSIO_DISPATCH( addressbook, (myaction) ) + * @endcode + */ + template + void update( const T& obj, name payer, Lambda&& updater ) { + modify(obj, payer, std::forward(updater)); + } + /** * Retrieves an existing object from a table using its primary key. * @ingroup multiindex @@ -1902,6 +2050,46 @@ class multi_index return itr; } + /** + * Deletes an existing object from a table using its primary key, alias for erase() + * @ingroup multiindex + * + * @param itr - An iterator pointing to the object to be removed + * + * @pre itr points to an existing element + * @post The object is removed from the table and all associated storage is reclaimed. + * @post Secondary indices associated with the table are updated. + * @post The existing payer for storage usage of the object is refunded for the table and secondary indices usage of the removed object, and if the table and indices are removed, for the associated overhead. + * + * @return For the signature with `const_iterator`, returns a pointer to the object following the removed object. + * + * Exceptions: + * The object to be removed is not in the table. + * The action is not authorized to modify the table. + * The given iterator is invalid. + * + * Example: + * + * @code + * // This assumes the code from the constructor example. Replace myaction() {...} + * + * void myaction() { + * // create reference to address_index - see emplace example + * // add dan account to table - see emplace example + * + * auto itr = addresses.find("dan"_n); + * eosio::check(itr != addresses.end(), "Address for account not found"); + * addresses.delete( itr ); + * eosio::check(itr != addresses.end(), "Everting lock arf, Address not erased properly"); + * } + * } + * EOSIO_ABI( addressbook, (myaction) ) + * @endcode + */ + const_iterator remove( const_iterator itr ) { + return erase(itr); + } + /** * Remove an existing object from a table using its primary key. * @ingroup multiindex @@ -1965,5 +2153,41 @@ class multi_index _items_vector.erase(--(itr2.base())); } +/** + * Remove an existing object from a table using its primary key, alias for erase() + * @ingroup multiindex + * + * @param obj - Object to be removed + * + * @pre obj is an existing object in the table + * @post The object is removed from the table and all associated storage is reclaimed. + * @post Secondary indices associated with the table are updated. + * @post The existing payer for storage usage of the object is refunded for the table and secondary indices usage of the removed object, and if the table and indices are removed, for the associated overhead. + * + * Exceptions: + * The object to be removed is not in the table. + * The action is not authorized to modify the table. + * The given iterator is invalid. + * + * Example: + * + * @code + * // This assumes the code from the constructor example. Replace myaction() {...} + * + * void myaction() { + * auto itr = addresses.find("dan"_n); + * eosio::check(itr != addresses.end(), "Record is not found"); + * addresses.remove(*itr); + * itr = addresses.find("dan"_n); + * eosio::check(itr == addresses.end(), "Record is not deleted"); + * } + * } + * EOSIO_DISPATCH( addressbook, (myaction) ) + * @endcode + */ + void remove( const T& obj ) { + erase(obj); + } + }; } /// eosio diff --git a/tests/unit/test_contracts/multi_index_tests.cpp b/tests/unit/test_contracts/multi_index_tests.cpp index 8c62358081..8fa99e7f69 100644 --- a/tests/unit/test_contracts/multi_index_tests.cpp +++ b/tests/unit/test_contracts/multi_index_tests.cpp @@ -204,6 +204,41 @@ namespace _test_multi_index auto itr2 = table.find(ssn); eosio::check(itr2 == table.end(), "idx64_general - table.erase()"); } + + // insert, update and delete by iterator + { + const uint64_t ssn = 421; + auto new_person = table.insert(payer, [&](auto &r) + { + r.id = ssn; + r.sec = "bob"_n.value; }); + + table.update(new_person, payer, [&](auto &r) + { r.sec = "billy"_n.value; }); + + auto itr1 = table.find(ssn); + eosio::check(itr1 != table.end() && itr1->sec == "billy"_n.value, "idx64_general - table.update()"); + + table.remove(itr1); + auto itr2 = table.find(ssn); + eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); + } + + // insert, update and delete by object + { + const uint64_t ssn = 421; + auto new_person = table.insert(payer, [&](auto &r) + { + r.id = ssn; + r.sec = "bob"_n.value; }); + + table.update(new_person, payer, [&](auto &r) + { r.sec = "billy"_n.value; }); + + table.remove(new_person); + auto itr2 = table.find(ssn); + eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); + } } template From 2b0fd5c55bd8ff32400ff0d65a8d461e226b8db0 Mon Sep 17 00:00:00 2001 From: Michal Lesiak Date: Wed, 17 May 2023 12:05:17 +0200 Subject: [PATCH 009/158] Get rid of const obj for new modify() method. --- .../eosiolib/contracts/eosio/multi_index.hpp | 106 +++++++++--------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/multi_index.hpp b/libraries/eosiolib/contracts/eosio/multi_index.hpp index 2ae6ae0e0d..5d2eb457d3 100644 --- a/libraries/eosiolib/contracts/eosio/multi_index.hpp +++ b/libraries/eosiolib/contracts/eosio/multi_index.hpp @@ -743,8 +743,8 @@ class multi_index } template - void update( const T& obj, eosio::name payer, Lambda&& updater ) { - modify( obj, payer, std::forward(updater) ); + void update( T& obj, eosio::name payer, Lambda&& updater ) { + _multidx->update( obj, payer, std::forward(updater) ); } const_iterator erase( const_iterator itr ) { @@ -855,6 +855,56 @@ class multi_index return *ptr; } /// load_object_by_primary_iterator + template + void modify_object(T& obj, name payer, Lambda&& updater) { + using namespace _multi_index_detail; + + auto& objitem = static_cast(obj); + eosio::check( objitem.__idx == this, "object passed to modify is not in multi_index" ); + + eosio::check( _code == current_receiver(), "cannot modify objects in table of another contract" ); // Quick fix for mutating db using multi_index that shouldn't allow mutation. Real fix can come in RC2. + + auto secondary_keys = make_extractor_tuple::get_extractor_tuple(indices_type{}, obj); + + uint64_t pk = _multi_index_detail::to_raw_key(obj.primary_key()); + + updater( obj ); + + eosio::check( pk == _multi_index_detail::to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); + + size_t size = pack_size( obj ); + //using malloc/free here potentially is not exception-safe, although WASM doesn't support exceptions + void* buffer = max_stack_buffer_size < size ? malloc(size) : alloca(size); + + datastream ds( (char*)buffer, size ); + ds << obj; + + internal_use_do_not_use::db_update_i64( objitem.__primary_itr, payer.value, buffer, size ); + + if ( max_stack_buffer_size < size ) { + free( buffer ); + } + + if( pk >= _next_primary_key ) + _next_primary_key = (pk >= no_available_primary_key) ? no_available_primary_key : (pk + 1); + + bluegrass::meta::for_each(indices_type{}, [&](auto idx){ + typedef std::tuple_element_t index_type; + auto secondary = index_type::extract_secondary_key( obj ); + if( memcmp( &std::get(secondary_keys), &secondary, sizeof(secondary) ) != 0 ) { + auto indexitr = objitem.__iters[index_type::number()]; + + if( indexitr < 0 ) { + typename index_type::secondary_key_type temp_secondary_key; + indexitr = objitem.__iters[index_type::number()] + = secondary_index_db_functions::db_idx_find_primary( _code.value, _scope, index_type::name(), pk, temp_secondary_key ); + } + + secondary_index_db_functions::db_idx_update( indexitr, payer.value, secondary ); + } + } ); + } + public: /** * Constructs an instance of a Multi-Index table. @@ -1805,53 +1855,9 @@ class multi_index */ template void modify( const T& obj, name payer, Lambda&& updater ) { - using namespace _multi_index_detail; - - const auto& objitem = static_cast(obj); - eosio::check( objitem.__idx == this, "object passed to modify is not in multi_index" ); - auto& mutableitem = const_cast(objitem); - eosio::check( _code == current_receiver(), "cannot modify objects in table of another contract" ); // Quick fix for mutating db using multi_index that shouldn't allow mutation. Real fix can come in RC2. - - auto secondary_keys = make_extractor_tuple::get_extractor_tuple(indices_type{}, obj); - - uint64_t pk = _multi_index_detail::to_raw_key(obj.primary_key()); - - auto& mutableobj = const_cast(obj); // Do not forget the auto& otherwise it would make a copy and thus not update at all. - updater( mutableobj ); - - eosio::check( pk == _multi_index_detail::to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); - - size_t size = pack_size( obj ); - //using malloc/free here potentially is not exception-safe, although WASM doesn't support exceptions - void* buffer = max_stack_buffer_size < size ? malloc(size) : alloca(size); - - datastream ds( (char*)buffer, size ); - ds << obj; - - internal_use_do_not_use::db_update_i64( objitem.__primary_itr, payer.value, buffer, size ); - - if ( max_stack_buffer_size < size ) { - free( buffer ); - } + T& mutable_obj = const_cast(obj); - if( pk >= _next_primary_key ) - _next_primary_key = (pk >= no_available_primary_key) ? no_available_primary_key : (pk + 1); - - bluegrass::meta::for_each(indices_type{}, [&](auto idx){ - typedef std::tuple_element_t index_type; - auto secondary = index_type::extract_secondary_key( obj ); - if( memcmp( &std::get(secondary_keys), &secondary, sizeof(secondary) ) != 0 ) { - auto indexitr = mutableitem.__iters[index_type::number()]; - - if( indexitr < 0 ) { - typename index_type::secondary_key_type temp_secondary_key; - indexitr = mutableitem.__iters[index_type::number()] - = secondary_index_db_functions::db_idx_find_primary( _code.value, _scope, index_type::name(), pk, temp_secondary_key ); - } - - secondary_index_db_functions::db_idx_update( indexitr, payer.value, secondary ); - } - } ); + modify_object(mutable_obj, payer, std::forward(updater)); } /** @@ -1896,8 +1902,8 @@ class multi_index * @endcode */ template - void update( const T& obj, name payer, Lambda&& updater ) { - modify(obj, payer, std::forward(updater)); + void update( T& obj, name payer, Lambda&& updater ) { + modify_object(obj, payer, std::forward(updater)); } /** From a1aae5f8d2d2c4e9de6f8344e22e46067cd03c52 Mon Sep 17 00:00:00 2001 From: Michal Lesiak Date: Wed, 17 May 2023 12:20:09 +0200 Subject: [PATCH 010/158] Update tests to cover both const and non-const version of remove() --- .../unit/test_contracts/multi_index_tests.cpp | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_contracts/multi_index_tests.cpp b/tests/unit/test_contracts/multi_index_tests.cpp index 8fa99e7f69..155d753a1b 100644 --- a/tests/unit/test_contracts/multi_index_tests.cpp +++ b/tests/unit/test_contracts/multi_index_tests.cpp @@ -205,7 +205,7 @@ namespace _test_multi_index eosio::check(itr2 == table.end(), "idx64_general - table.erase()"); } - // insert, update and delete by iterator + // insert, update and remove by iterator { const uint64_t ssn = 421; auto new_person = table.insert(payer, [&](auto &r) @@ -224,7 +224,7 @@ namespace _test_multi_index eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); } - // insert, update and delete by object + // insert, update and remove by object { const uint64_t ssn = 421; auto new_person = table.insert(payer, [&](auto &r) @@ -235,7 +235,26 @@ namespace _test_multi_index table.update(new_person, payer, [&](auto &r) { r.sec = "billy"_n.value; }); - table.remove(new_person); + const auto& person_object = table.get(ssn); + auto& mutable_person = const_cast(person_object); + table.remove(mutable_person); + auto itr2 = table.find(ssn); + eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); + } + + // insert, update and remove by const object + { + const uint64_t ssn = 421; + auto new_person = table.insert(payer, [&](auto &r) + { + r.id = ssn; + r.sec = "bob"_n.value; }); + + table.update(new_person, payer, [&](auto &r) + { r.sec = "billy"_n.value; }); + + const auto& person_object = table.get(ssn); + table.remove(person_object); auto itr2 = table.find(ssn); eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); } From 65f928fcde87eeaddb20ae8f684bceac74a3e902 Mon Sep 17 00:00:00 2001 From: Michal Lesiak Date: Thu, 18 May 2023 15:03:23 +0200 Subject: [PATCH 011/158] Code review improvements --- libraries/eosiolib/contracts/eosio/multi_index.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/multi_index.hpp b/libraries/eosiolib/contracts/eosio/multi_index.hpp index 5d2eb457d3..64dd85e227 100644 --- a/libraries/eosiolib/contracts/eosio/multi_index.hpp +++ b/libraries/eosiolib/contracts/eosio/multi_index.hpp @@ -866,11 +866,11 @@ class multi_index auto secondary_keys = make_extractor_tuple::get_extractor_tuple(indices_type{}, obj); - uint64_t pk = _multi_index_detail::to_raw_key(obj.primary_key()); + uint64_t pk = to_raw_key(obj.primary_key()); updater( obj ); - eosio::check( pk == _multi_index_detail::to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); + eosio::check( pk == to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); size_t size = pack_size( obj ); //using malloc/free here potentially is not exception-safe, although WASM doesn't support exceptions From 4bae47bf94d65edf5d8e25cc162837af4fffd56d Mon Sep 17 00:00:00 2001 From: Alexander Molchevsky Date: Fri, 19 May 2023 08:06:44 +0300 Subject: [PATCH 012/158] Description of the Crypto API was updated --- docs/05_features/50_crypto-extensions.md | 189 +++++++++-------------- 1 file changed, 71 insertions(+), 118 deletions(-) diff --git a/docs/05_features/50_crypto-extensions.md b/docs/05_features/50_crypto-extensions.md index bbb16fe35d..cc8eb1ad93 100644 --- a/docs/05_features/50_crypto-extensions.md +++ b/docs/05_features/50_crypto-extensions.md @@ -1,23 +1,22 @@ --- -content_title: Crypto Extensions +content_title: Crypto Extensions API --- -As of `v3.0` crypto host functions were extended to include -- `mod_exp`: Big integer modular exponentiation -- `alt_bn128_add`, `alt_bn128_mul`, `alt_bn128_pair`: Add, multiply, and pairing check functions for the `alt_bn128` elliptic curve -- `blake2_f`: `BLAKE2b F` compression function -- `sha3`: sha3` hash function using `SHA3 NIST` -- `keccak`: `Keccak256` hash function using `SHA3 Keccak` -- `k1_recover`: Safe `ECDSA` uncompressed pubkey recover for the `secp256k1` curve -In `v3.0`, `C` format was supported; in `v3.1` and newer versions, `C++` format was added for better data abstraction. +Antelope blockchain implements cryptographic functions for operations on points on elliptic curves, computing hashes +of big integers, Big integer modular exponentiation, and other operations useful for implementing +cryptographic algorithms in your contracts. -## Prerequisites +In order to use the Crypto Extensions API you need to activate a protocol feature `CRYPTO_PRIMITIVES` +in `nodeos`. To do this you should call the following command in your command line: -- In `nodeos`, activate protocol feature `CRYPTO_PRIMITIVES` (`68d6405cb8df3de95bd834ebb408196578500a9f818ff62ccc68f60b932f7d82`) -- In smart contract code, include `crypto_ext.hpp` +`"cleos push action eosio activate ["6bcb40a24e49c26d0a60513b6aeb8551d264e4717f306b81a37a5afb3b47cedc"] -p eosio@active"` -## C Format +And you need to include to the source code of your contract the following header: [crypto_ext.hpp](https://github.com/AntelopeIO/cdt/blob/main/libraries/eosiolib/core/eosio/crypto_ext.hpp) + +The header declares following basic plain C functions which implement the core functionality: + +## Plain C API - `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 )` Perform addition operation on the elliptic curve `alt_bn128`, store the result in `result`, and return `0` if success otherwise `-1` @@ -40,154 +39,92 @@ Test if the SHA3 keccak hash generated from data matches the provided digest - `int32_t k1_recover( const char* sig, uint32_t sig_len, const char* dig, uint32_t dig_len, char* pub, uint32_t pub_len )` Calculates the uncompressed public key used for a given signature on a given digest. Return `0` if success otherwise `-1` -## C++ Format -C++ types were added to represent `G1` and `G2` points (read and write) and views (read-only), and represent big integers. Their definitions are +Also, the header contains a set of handy C++ wrappers to simplify the contracts code. + +## C++ API + +`ec_point` type was added to represent `G1` and `G2` points on the elliptic curve. +`ec_point_view` type was added to provide a read-only interface for access to `G1` and `G2` points data. -### Types +### Data Types ```c++ - /** - * Abstracts mutable G1 and G2 points - * - */ + // Abstracts mutable G1 and G2 points template struct ec_point { - /** - * Bytes of the x coordinate - */ - std::vector x; - - /** - * Bytes of the y coordinate - */ - std::vector 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 - */ + std::vector x; // x coordinate + std::vector y; // y coordinate + // Construct from two coordinates represented by big integers ec_point(std::vector& x_, std::vector& y_); - - /** - * Construct a point given a serialized point - * - * @param p - The serialized point - */ + // Construct from a serialized point ec_point(std::vector& p); - - /** - * Return serialized point containing only x and y - */ + // Return a serialized point containing only x and y std::vector serialized() const; }; - - /** - * Abstracts read-only G1 and G2 points - */ + + // Abstracts read-only G1 and G2 points template 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 - */ + // 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 - */ + // Construct from two coordinates. Their sizes must be equal ec_point_view(const char* x_, uint32_t x_size, const char* y_, uint32_t y_size); - - /** - * Construct a point view from a serialized point - * - * @param p - The serialized point - */ + // Construct from a serialized point ec_point_view(const std::vector& p); - - /** - * Construct a point view from a point - * - * @param p - The point - */ + // Construct a point view from a point ec_point_view(const ec_point& p); - - /** - * Return serialized point containing only x and y - */ + // Return serialized point containing only x and y std::vector serialized() const; }; - - static constexpr size_t g1_coordinate_size = 32; - static constexpr size_t g2_coordinate_size = 64; - - using g1_point = ec_point; - using g2_point = ec_point; - using g1_point_view = ec_point_view; - using g2_point_view = ec_point_view; - - /** - * Big integer. - * - * @ingroup crypto - */ - using bigint = std::vector; + + // Big integer. + using bigint = std::vector; ``` ### Methods -- `alt_bn128_add` +- Addition operation on the elliptic curve `alt_bn128` ```c++ template g1_point alt_bn128_add( const T& op1, const T& op2 ) ``` -Take two G1 points or G1 views as input and return a G1 point. -- `alt_bn128_mul` + +- Scalar multiplication operation on the elliptic curve `alt_bn128` ```c++ template g1_point alt_bn128_mul( const T& g1, const bigint& scalar) ``` -Take a G1 point or view and a bigint as input and return a G1 point -- `alt_bn128_pair` + +- Optimal-Ate pairing check elliptic curve `alt_bn128` ```c++ template int32_t alt_bn128_pair( const std::vector>& pairs ) ``` -Take a pair of G1 and G2 as input. -- `mod_exp` + +- Big integer modular exponentiation returns `( BASE^EXP ) % MOD` ```c++ int32_t mod_exp( const bigint& base, const bigint& exp, const bigint& mod, bigint& result ) ``` -Take bigints as input + +Please take a look into a [crypto_ext.hpp](https://github.com/AntelopeIO/cdt/blob/main/libraries/eosiolib/core/eosio/crypto_ext.hpp) +header file, it contains more wrappers and helper functions which may be useful in your code. ### Examples - `alt_bn128_add` ```c++ - std::vector x1, y1, x2, y2; + std::vector x1, y1, x2, y2; // Declare coordinates for points on an elliptic curve - // point + // Create the points on the curve eosio::g1_point point1 {x1, y1}; eosio::g1_point point2 {x2, y2}; + // Add two points and get a third point on the curve as result auto result = eosio::alt_bn128_add(point1, point2); - // view + // Do the same addition but with g1_point_view data types eosio::g1_point_view point_view1 {x1.data(), x1.size(), y1.data(), y1.size()}; eosio::g1_point_view point_view2 {x2.data(), x2.size(), y2.data(), y2.size()}; result = eosio::alt_bn128_add(point_view1, point_view2); @@ -195,32 +132,45 @@ Take bigints as input - `alt_bn128_mul` ```c++ - std::vector x, y, scaler; + // Declare coordinates for a point on an elliptic curve and a big integer + std::vector x, y, scalar; eosio::bigint s {scalar}; - // point + // Create the point object by given coordinates eosio::g1_point g1_point {x, y}; + // Multiply the point to a big integer and get a point on the curve as result auto result = eosio::alt_bn128_mul(g1_point, s); - // view + // Do the same multiplication but with g1_point_view data type eosio::g1_point_view g1_view {x.data(), x.size(), y.data(), y.size()}; result = eosio::alt_bn128_mul(g1_view, s); ``` - `alt_bn128_pair` ```c++ + // Declare coordinates for the pairs of points on an elliptic curve std::vector g1_a_x, g1_a_y, g2_a_x, g2_a_y, g1_b_x, g1_b_y, g2_b_x, g2_b_y; - // point + // Create the point object by given coordinates + // First pair eosio::g1_point g1_a {g1_a_x, g1_a_y}; eosio::g2_point g2_a {g2_a_x, g2_a_y}; + // Second pair eosio::g1_point g1_b {g1_b_x, g1_b_y}; eosio::g2_point g2_b {g2_b_x, g2_b_y}; + + // Create the pairs object std::vector> pairs { {g1_a, g2_a}, {g1_b, g2_b} }; + + // Perform the pairing check + // Return: + // -1 if there is an error + // 1 if the points not in a target group + // 0 if the points in a target group auto result = eosio::alt_bn128_pair(pairs); - // view + // Do the same pairing check but with g1_point_view and g2_point_view data types eosio::g1_point_view g1_view_a {g1_a_x.data(), g1_a_x.size(), g1_a_y.data(), g1_a_y.size()}; eosio::g2_point_view g2_view_a {g2_a_x.data(), g2_a_x.size(), g2_a_y.data(), g2_a_y.size()}; eosio::g1_point_view g1_view_b {g1_b_x.data(), g1_b_x.size(), g1_b_y.data(), g1_b_y.size()}; @@ -231,11 +181,14 @@ Take bigints as input - `mod_exp` ```c++ + // Declare big integer variables for the base, the exponent and the modulo std::vector base, exp, modulo; eosio::bigint base_val {base}; eosio::bigint exp_val {exp}; eosio::bigint modulo_val {modulo}; + // Declare and init a big integer variable for the result of exponentiation eosio::bigint result( modulo.size(), '\0' ); - + // Perform the exponentiation + // return -1 if there is an error otherwise 0 auto rc = eosio::mod_exp(base_val, exp_val, modulo_val, result); ``` From a7fa7519b67e60e3bbaa028de2155b5eef4b0840 Mon Sep 17 00:00:00 2001 From: Alexander Molchevsky Date: Fri, 19 May 2023 21:48:41 +0300 Subject: [PATCH 013/158] Updated after review --- docs/05_features/50_crypto-extensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/05_features/50_crypto-extensions.md b/docs/05_features/50_crypto-extensions.md index cc8eb1ad93..0cbabca2b2 100644 --- a/docs/05_features/50_crypto-extensions.md +++ b/docs/05_features/50_crypto-extensions.md @@ -4,7 +4,7 @@ content_title: Crypto Extensions API Antelope blockchain implements cryptographic functions for operations on points on elliptic curves, computing hashes -of big integers, Big integer modular exponentiation, and other operations useful for implementing +of big integers, big integer modular exponentiation, and other operations useful for implementing cryptographic algorithms in your contracts. In order to use the Crypto Extensions API you need to activate a protocol feature `CRYPTO_PRIMITIVES` From 5b06bd3451d6b26f104d79e74e2f9c6de1a7a827 Mon Sep 17 00:00:00 2001 From: Alexander Molchevsky Date: Tue, 23 May 2023 00:06:53 +0300 Subject: [PATCH 014/158] Apply suggestions from code review a few grammatical changes were made Co-authored-by: Scott B --- docs/05_features/50_crypto-extensions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/05_features/50_crypto-extensions.md b/docs/05_features/50_crypto-extensions.md index 0cbabca2b2..bb7100e3f5 100644 --- a/docs/05_features/50_crypto-extensions.md +++ b/docs/05_features/50_crypto-extensions.md @@ -12,7 +12,7 @@ in `nodeos`. To do this you should call the following command in your command li `"cleos push action eosio activate ["6bcb40a24e49c26d0a60513b6aeb8551d264e4717f306b81a37a5afb3b47cedc"] -p eosio@active"` -And you need to include to the source code of your contract the following header: [crypto_ext.hpp](https://github.com/AntelopeIO/cdt/blob/main/libraries/eosiolib/core/eosio/crypto_ext.hpp) +And you need to include in the source code of your contract the following header: [crypto_ext.hpp](https://github.com/AntelopeIO/cdt/blob/main/libraries/eosiolib/core/eosio/crypto_ext.hpp) The header declares following basic plain C functions which implement the core functionality: @@ -136,9 +136,9 @@ header file, it contains more wrappers and helper functions which may be useful std::vector x, y, scalar; eosio::bigint s {scalar}; - // Create the point object by given coordinates + // Create the point object with given coordinates eosio::g1_point g1_point {x, y}; - // Multiply the point to a big integer and get a point on the curve as result + // Multiply the point with a big integer and get a point on the curve as result auto result = eosio::alt_bn128_mul(g1_point, s); // Do the same multiplication but with g1_point_view data type From 861a86e50062aa5bd3feff73ed88ee1a33d33690 Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Wed, 24 May 2023 06:01:23 -0500 Subject: [PATCH 015/158] removed bls types --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index a1c4f3502c..39cd74a888 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -9,13 +9,6 @@ namespace eosio { - using bls_scalar = std::array; - using bls_fp = std::array; - using bls_fp2 = std::array; - using bls_g1 = std::array; - using bls_g2 = std::array; - using bls_gt = std::array; - namespace internal_use_do_not_use { extern "C" { __attribute__((eosio_wasm_import)) @@ -50,57 +43,54 @@ namespace eosio { } } - void bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) + void bls_g1_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) { - return internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1.data()), op1.size(), reinterpret_cast(op2.data()), op2.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1), 144, reinterpret_cast(op2), 144, reinterpret_cast(res), 144); } - void bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) + void bls_g2_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) { - return internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1.data()), op1.size(), reinterpret_cast(op2.data()), op2.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1), 288, reinterpret_cast(op2), 288, reinterpret_cast(res), 288); } - void bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) + void bls_g1_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) { - return internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point.data()), point.size(), reinterpret_cast(scalar.data()), scalar.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point), 144, reinterpret_cast(scalar), 32, reinterpret_cast(res), 144); } - void bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) + void bls_g2_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) { - return internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point.data()), point.size(), reinterpret_cast(scalar.data()), scalar.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point), 288, reinterpret_cast(scalar), 32, reinterpret_cast(res), 288); } - void bls_g1_exp(const std::vector& points, const std::vector& scalars, bls_g1& res) + void bls_g1_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) { - if(points.size() != scalars.size()) return; - return internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points.data()), points.size() * sizeof(bls_g1), reinterpret_cast(scalars.data()), scalars.size() * sizeof(bls_scalar), points.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points), num * 144, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 144); } - void bls_g2_exp(const std::vector& points, const std::vector& scalars, bls_g2& res) + void bls_g2_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) { - if(points.size() != scalars.size()) return; - return internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points.data()), points.size() * sizeof(bls_g2), reinterpret_cast(scalars.data()), scalars.size() * sizeof(bls_scalar), points.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points), num * 288, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 288); } - void bls_pairing(const std::vector& g1_points, const std::vector& g2_points, bls_gt& res) + void bls_pairing(const uint8_t* g1_points, const uint8_t* g2_points, const uint32_t num, uint8_t* res) { - if(g1_points.size() != g2_points.size()) return; - return internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points.data()), g1_points.size() * sizeof(bls_g1), reinterpret_cast(g2_points.data()), g2_points.size() * sizeof(bls_g2), g1_points.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points), num * 144, reinterpret_cast(g2_points), num * 288, num, reinterpret_cast(res), 576); } - void bls_g1_map(const bls_fp& e, bls_g1& res) + void bls_g1_map(const uint8_t* e, uint8_t* res) { - return internal_use_do_not_use::bls_g1_map(reinterpret_cast(e.data()), e.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g1_map(reinterpret_cast(e), 48, reinterpret_cast(res), 144); } - void bls_g2_map(const bls_fp2& e, bls_g2& res) + void bls_g2_map(const uint8_t* e, uint8_t* res) { - return internal_use_do_not_use::bls_g2_map(reinterpret_cast(e.data()), e.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_g2_map(reinterpret_cast(e), 96, reinterpret_cast(res), 288); } - void bls_fp_mod(const std::array& s, bls_fp& res) + void bls_fp_mod(const uint8_t* s, uint8_t* res) { - return internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s.data()), s.size(), reinterpret_cast(res.data()), res.size()); + internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s), 64, reinterpret_cast(res), 48); } } From e2bec049867e25fb7f3c06a9fae41f5694ed415e Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Wed, 24 May 2023 06:19:51 -0500 Subject: [PATCH 016/158] synced with Antelope/main --- tools/external/antler-proj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/external/antler-proj b/tools/external/antler-proj index 2034b9a38f..ef4edc2e15 160000 --- a/tools/external/antler-proj +++ b/tools/external/antler-proj @@ -1 +1 @@ -Subproject commit 2034b9a38f61aa1a55f17eed657d466bbcfcc6e6 +Subproject commit ef4edc2e153262b4c12399ed142fb34e3c25ed09 From 80ef3056dc265f8054d90ac7317fd5dc78c0ff43 Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Wed, 24 May 2023 06:28:13 -0500 Subject: [PATCH 017/158] fixed submodule --- tools/external/antler-proj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/external/antler-proj b/tools/external/antler-proj index ef4edc2e15..2034b9a38f 160000 --- a/tools/external/antler-proj +++ b/tools/external/antler-proj @@ -1 +1 @@ -Subproject commit ef4edc2e153262b4c12399ed142fb34e3c25ed09 +Subproject commit 2034b9a38f61aa1a55f17eed657d466bbcfcc6e6 From 5a97108c6e1ea0d29ad19aedfb66d3d344432fe2 Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Tue, 6 Jun 2023 12:53:35 -0500 Subject: [PATCH 018/158] removed bls types leftovers --- libraries/eosiolib/capi/eosio/types.h | 42 --------------------------- tools/include/eosio/gen.hpp | 17 ++--------- 2 files changed, 2 insertions(+), 57 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/types.h b/libraries/eosiolib/capi/eosio/types.h index 198ed5b08d..b3f54915b9 100644 --- a/libraries/eosiolib/capi/eosio/types.h +++ b/libraries/eosiolib/capi/eosio/types.h @@ -76,46 +76,4 @@ struct ALIGNED(capi_checksum512) { uint8_t hash[64]; }; -/** - * BLS12-381 scalar type (LE) - */ -struct ALIGNED(capi_bls_scalar) { - uint8_t data[32]; -}; - -/** - * BLS12-381 fp type (LE) - */ -struct ALIGNED(capi_bls_fp) { - uint8_t data[48]; -}; - -/** - * BLS12-381 fp2 type (LE) - */ -struct ALIGNED(capi_bls_fp2) { - uint8_t data[96]; -}; - -/** - * BLS12-381 G1 type (Jacobian, LE) - */ -struct ALIGNED(capi_bls_g1) { - uint8_t data[144]; -}; - -/** - * BLS12-381 G2 type (Jacobian, LE) - */ -struct ALIGNED(capi_bls_g2) { - uint8_t data[288]; -}; - -/** - * BLS12-381 GT type (LE) - */ -struct ALIGNED(capi_bls_gt) { - uint8_t data[576]; -}; - /// @} diff --git a/tools/include/eosio/gen.hpp b/tools/include/eosio/gen.hpp index 7bb214a7be..8e2494d428 100644 --- a/tools/include/eosio/gen.hpp +++ b/tools/include/eosio/gen.hpp @@ -469,14 +469,7 @@ struct generation_utils { {"capi_checksum512", "checksum512"}, {"fixed_bytes_20", "checksum160"}, {"fixed_bytes_32", "checksum256"}, - {"fixed_bytes_64", "checksum512"}, - - {"capi_bls_scalar", "bls_scalar"}, - {"capi_bls_g1", "bls_fp"}, - {"capi_bls_g2", "bls_fp2"}, - {"capi_bls_g1", "bls_g1"}, - {"capi_bls_g2", "bls_g2"}, - {"capi_bls_gt", "bls_gt"} + {"fixed_bytes_64", "checksum512"} }; auto ret = translation_table[t]; @@ -771,13 +764,7 @@ struct generation_utils { "symbol", "symbol_code", "asset", - "extended_asset", - "bls_scalar", - "bls_fp", - "bls_fp2", - "bls_g1", - "bls_g2", - "bls_gt" + "extended_asset" }; return builtins.count(_translate_type(t)) >= 1; } From 2975faaca22f7a18b82449de0c16ce35159b37ee Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Wed, 7 Jun 2023 18:15:35 -0500 Subject: [PATCH 019/158] update according to leap update --- .../eosiolib/capi/eosio/crypto_bls_ext.h | 20 ++++---- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 50 +++++++++++-------- libraries/native/intrinsics.cpp | 40 +++++++-------- 3 files changed, 60 insertions(+), 50 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/crypto_bls_ext.h b/libraries/eosiolib/capi/eosio/crypto_bls_ext.h index a19984dbf3..8071a16f91 100644 --- a/libraries/eosiolib/capi/eosio/crypto_bls_ext.h +++ b/libraries/eosiolib/capi/eosio/crypto_bls_ext.h @@ -5,34 +5,34 @@ extern "C" { #endif __attribute__((eosio_wasm_import)) -void bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); +int32_t bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); +int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); +int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); +int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); +int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); +int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); +int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); +int32_t bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); +int32_t bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -void bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); +int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); #ifdef __cplusplus } diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 39cd74a888..bd56c42665 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -12,85 +12,95 @@ namespace eosio { namespace internal_use_do_not_use { extern "C" { __attribute__((eosio_wasm_import)) - void bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + int32_t bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); + int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); + int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); + int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); + int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); + int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); + int32_t bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); + int32_t bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - void bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); + int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); } } void bls_g1_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) { - internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1), 144, reinterpret_cast(op2), 144, reinterpret_cast(res), 144); + int32_t ret = internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1), 144, reinterpret_cast(op2), 144, reinterpret_cast(res), 144); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_add failed"); } void bls_g2_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) { - internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1), 288, reinterpret_cast(op2), 288, reinterpret_cast(res), 288); + int32_t ret = internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1), 288, reinterpret_cast(op2), 288, reinterpret_cast(res), 288); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_add failed"); } void bls_g1_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) { - internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point), 144, reinterpret_cast(scalar), 32, reinterpret_cast(res), 144); + int32_t ret = internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point), 144, reinterpret_cast(scalar), 32, reinterpret_cast(res), 144); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_mul failed"); } void bls_g2_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) { - internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point), 288, reinterpret_cast(scalar), 32, reinterpret_cast(res), 288); + int32_t ret = internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point), 288, reinterpret_cast(scalar), 32, reinterpret_cast(res), 288); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_mul failed"); } void bls_g1_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) { - internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points), num * 144, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 144); + int32_t ret = internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points), num * 144, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 144); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_exp failed"); } void bls_g2_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) { - internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points), num * 288, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 288); + int32_t ret = internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points), num * 288, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 288); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_exp failed"); } void bls_pairing(const uint8_t* g1_points, const uint8_t* g2_points, const uint32_t num, uint8_t* res) { - internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points), num * 144, reinterpret_cast(g2_points), num * 288, num, reinterpret_cast(res), 576); + int32_t ret = internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points), num * 144, reinterpret_cast(g2_points), num * 288, num, reinterpret_cast(res), 576); + eosio::check(ret == 0, "internal_use_do_not_use::bls_pairing failed"); } void bls_g1_map(const uint8_t* e, uint8_t* res) { - internal_use_do_not_use::bls_g1_map(reinterpret_cast(e), 48, reinterpret_cast(res), 144); + int32_t ret = internal_use_do_not_use::bls_g1_map(reinterpret_cast(e), 48, reinterpret_cast(res), 144); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_map failed"); } void bls_g2_map(const uint8_t* e, uint8_t* res) { - internal_use_do_not_use::bls_g2_map(reinterpret_cast(e), 96, reinterpret_cast(res), 288); + int32_t ret = internal_use_do_not_use::bls_g2_map(reinterpret_cast(e), 96, reinterpret_cast(res), 288); + eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_map failed"); } void bls_fp_mod(const uint8_t* s, uint8_t* res) { - internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s), 64, reinterpret_cast(res), 48); + int32_t ret = internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s), 64, reinterpret_cast(res), 48); + eosio::check(ret == 0, "internal_use_do_not_use::bls_fp_mod failed"); } } diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index 0658c45b37..a1db008f82 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -937,52 +937,52 @@ void sha3( const char* data, uint32_t data_len, char* hash, uint32_t hash_len, i intrinsics::get().call(data, data_len, hash, hash_len, keccak); } -void bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) +int32_t bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) { - intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); + return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); } -void bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) +int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) { - intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); + return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); } -void bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) +int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) { - intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); + return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); } -void bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) +int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) { - intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); + return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); } -void bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) +int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) { - intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); + return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); } -void bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) +int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) { - intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); + return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); } -void bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len) +int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len) { - intrinsics::get().call(g1_points, g1_points_len, g1_points, g1_points_len, n, res, res_len); + return intrinsics::get().call(g1_points, g1_points_len, g1_points, g1_points_len, n, res, res_len); } -void bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) +int32_t bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) { - intrinsics::get().call(e, e_len, res, res_len); + return intrinsics::get().call(e, e_len, res, res_len); } -void bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) +int32_t bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) { - intrinsics::get().call(e, e_len, res, res_len); + return intrinsics::get().call(e, e_len, res, res_len); } -void bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len) +int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len) { - intrinsics::get().call(s, s_len, res, res_len); + return intrinsics::get().call(s, s_len, res, res_len); } From 6ed17a2b1009fc771ba8a289fbdf235cbc75cc71 Mon Sep 17 00:00:00 2001 From: Michal Lesiak Date: Fri, 16 Jun 2023 13:44:53 +0200 Subject: [PATCH 020/158] Replace is_callable_v with is_invocable --- .../include/bluegrass/meta/function_traits.hpp | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp index 3940c2d8f3..5f2ba12d24 100644 --- a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp +++ b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp @@ -65,11 +65,6 @@ namespace bluegrass { namespace meta { template constexpr bool pass_type() { return true; } - template - constexpr bool is_callable_impl(...) { - return false; - } - template struct wrapper_t { using type = T; @@ -81,12 +76,6 @@ namespace bluegrass { namespace meta { inline constexpr U&& make_dependent(U&& u) { return static_cast(u); } } - template - inline constexpr static bool is_callable_v = BLUEGRASS_HAS_MEMBER(AUTO_PARAM_WORKAROUND(FN), operator()); - - template - constexpr bool is_callable(F&& fn) { return BLUEGRASS_HAS_MEMBER(fn, operator()); } - namespace detail { template constexpr auto get_types(R(Args...)) -> std::tuple, Args>...>>; template constexpr auto get_types(F&& fn) { - if constexpr (is_callable_v) + if constexpr (std::is_invocable::value) return get_types(&F::operator()); else return get_types(fn); @@ -153,7 +142,7 @@ namespace bluegrass { namespace meta { constexpr auto parameters_from_impl(R(Cls::*)(Args...)const &&) -> pack_from_t; template constexpr auto parameters_from_impl(F&& fn) { - if constexpr (is_callable_v) + if constexpr (std::is_invocable::value) return parameters_from_impl(&F::operator()); else return parameters_from_impl(fn); From 7b058b424afe1d4e8ea698e5595d5b84508082bf Mon Sep 17 00:00:00 2001 From: Michal Lesiak Date: Fri, 16 Jun 2023 14:41:11 +0200 Subject: [PATCH 021/158] Add llvm on ubuntu22 --- .cicd/platforms.json | 3 +++ .cicd/platforms/ubuntu22-llvm.Dockerfile | 23 +++++++++++++++++++++++ .cicd/platforms/ubuntu22.Dockerfile | 2 +- .github/workflows/build.yaml | 2 +- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 .cicd/platforms/ubuntu22-llvm.Dockerfile diff --git a/.cicd/platforms.json b/.cicd/platforms.json index 08910862ec..69f7a69afb 100644 --- a/.cicd/platforms.json +++ b/.cicd/platforms.json @@ -7,5 +7,8 @@ }, "ubuntu22": { "dockerfile": ".cicd/platforms/ubuntu22.Dockerfile" + }, + "ubuntu22-llvm": { + "dockerfile": ".cicd/platforms/ubuntu22-llvm.Dockerfile" } } diff --git a/.cicd/platforms/ubuntu22-llvm.Dockerfile b/.cicd/platforms/ubuntu22-llvm.Dockerfile new file mode 100644 index 0000000000..ff54bc3c98 --- /dev/null +++ b/.cicd/platforms/ubuntu22-llvm.Dockerfile @@ -0,0 +1,23 @@ +FROM ubuntu:jammy + +RUN apt-get update && apt-get upgrade -y && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential \ + cmake \ + wget \ + git \ + ninja-build \ + python3 \ + pkg-config \ + libboost-all-dev \ + libcurl4-gnutls-dev \ + lsb-release \ + software-properties-common \ + gnupg \ + clang-tidy + +RUN wget https://apt.llvm.org/llvm.sh +RUN chmod +x llvm.sh +RUN ./llvm.sh 16 + +ENV CC=clang-16 +ENV CXX=clang++-16 \ No newline at end of file diff --git a/.cicd/platforms/ubuntu22.Dockerfile b/.cicd/platforms/ubuntu22.Dockerfile index 67b04586a1..ca38aad5cd 100644 --- a/.cicd/platforms/ubuntu22.Dockerfile +++ b/.cicd/platforms/ubuntu22.Dockerfile @@ -9,4 +9,4 @@ RUN apt-get update && apt-get upgrade -y && \ pkg-config \ libboost-all-dev \ libcurl4-gnutls-dev \ - clang-tidy + clang-tidy \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5345c2bfe1..fd75357002 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -60,7 +60,7 @@ jobs: strategy: fail-fast: false matrix: - platform: [ubuntu18, ubuntu20, ubuntu22] + platform: [ubuntu18, ubuntu20, ubuntu22, ubuntu22-llvm] runs-on: ["self-hosted", "enf-x86-beefy"] container: ${{fromJSON(needs.d.outputs.p)[matrix.platform].image}} steps: From baa6147ee3f2263e5a3ca9ffc8ae62461b150d16 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 17 Jun 2023 13:21:36 -0500 Subject: [PATCH 022/158] [Meta-Refl] Update catch2 to v2.13.10 The version of catch2 in use is old enough to fail compiles on updated Linux operating systems. Update to latest version in v2.0 series to fix builds. [Ref: https://github.com/catchorg/Catch2/issues/2421] --- libraries/meta_refl/modules/catch2_install.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/meta_refl/modules/catch2_install.cmake.in b/libraries/meta_refl/modules/catch2_install.cmake.in index eeca0099e3..cf9b641422 100644 --- a/libraries/meta_refl/modules/catch2_install.cmake.in +++ b/libraries/meta_refl/modules/catch2_install.cmake.in @@ -16,7 +16,7 @@ if (NOT ${found_project}) ExternalProject_Add( catch2_external_proj GIT_REPOSITORY "https://github.com/catchorg/Catch2" - GIT_TAG "v2.12.2" + GIT_TAG "v2.13.10" SOURCE_DIR @CATCH2_DIRECTORY@/catch2 BINARY_DIR @CATCH2_DIRECTORY@/catch2 BUILD_ALWAYS 0 From 21a82212b337a55e707aa62f001dcd19053f398e Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Tue, 27 Jun 2023 19:32:22 -0500 Subject: [PATCH 023/158] don't throw but propagate error back to smart contract to be handled there --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index bd56c42665..c4cdf42bd4 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -43,64 +43,54 @@ namespace eosio { } } - void bls_g1_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) + int32_t bls_g1_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1), 144, reinterpret_cast(op2), 144, reinterpret_cast(res), 144); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_add failed"); + return internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1), 144, reinterpret_cast(op2), 144, reinterpret_cast(res), 144); } - void bls_g2_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) + int32_t bls_g2_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1), 288, reinterpret_cast(op2), 288, reinterpret_cast(res), 288); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_add failed"); + return internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1), 288, reinterpret_cast(op2), 288, reinterpret_cast(res), 288); } - void bls_g1_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) + int32_t bls_g1_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point), 144, reinterpret_cast(scalar), 32, reinterpret_cast(res), 144); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_mul failed"); + return internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point), 144, reinterpret_cast(scalar), 32, reinterpret_cast(res), 144); } - void bls_g2_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) + int32_t bls_g2_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point), 288, reinterpret_cast(scalar), 32, reinterpret_cast(res), 288); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_mul failed"); + return internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point), 288, reinterpret_cast(scalar), 32, reinterpret_cast(res), 288); } - void bls_g1_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) + int32_t bls_g1_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points), num * 144, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 144); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_exp failed"); + return internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points), num * 144, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 144); } - void bls_g2_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) + int32_t bls_g2_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points), num * 288, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 288); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_exp failed"); + return internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points), num * 288, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 288); } - void bls_pairing(const uint8_t* g1_points, const uint8_t* g2_points, const uint32_t num, uint8_t* res) + int32_t bls_pairing(const uint8_t* g1_points, const uint8_t* g2_points, const uint32_t num, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points), num * 144, reinterpret_cast(g2_points), num * 288, num, reinterpret_cast(res), 576); - eosio::check(ret == 0, "internal_use_do_not_use::bls_pairing failed"); + return internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points), num * 144, reinterpret_cast(g2_points), num * 288, num, reinterpret_cast(res), 576); } - void bls_g1_map(const uint8_t* e, uint8_t* res) + int32_t bls_g1_map(const uint8_t* e, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g1_map(reinterpret_cast(e), 48, reinterpret_cast(res), 144); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g1_map failed"); + return internal_use_do_not_use::bls_g1_map(reinterpret_cast(e), 48, reinterpret_cast(res), 144); } - void bls_g2_map(const uint8_t* e, uint8_t* res) + int32_t bls_g2_map(const uint8_t* e, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_g2_map(reinterpret_cast(e), 96, reinterpret_cast(res), 288); - eosio::check(ret == 0, "internal_use_do_not_use::bls_g2_map failed"); + return internal_use_do_not_use::bls_g2_map(reinterpret_cast(e), 96, reinterpret_cast(res), 288); } - void bls_fp_mod(const uint8_t* s, uint8_t* res) + int32_t bls_fp_mod(const uint8_t* s, uint8_t* res) { - int32_t ret = internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s), 64, reinterpret_cast(res), 48); - eosio::check(ret == 0, "internal_use_do_not_use::bls_fp_mod failed"); + return internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s), 64, reinterpret_cast(res), 48); } } From 7a6dfcb4a5165d31514c5e20648a01f3c700fea0 Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Fri, 30 Jun 2023 08:42:30 -0500 Subject: [PATCH 024/158] added bls integration tests --- tests/integration/bls_tests.cpp | 108 +++++ tests/integration/contracts.hpp.in | 3 + tests/unit/test_contracts/CMakeLists.txt | 1 + .../test_contracts/bls_primitives_tests.cpp | 410 ++++++++++++++++++ 4 files changed, 522 insertions(+) create mode 100644 tests/integration/bls_tests.cpp create mode 100644 tests/unit/test_contracts/bls_primitives_tests.cpp diff --git a/tests/integration/bls_tests.cpp b/tests/integration/bls_tests.cpp new file mode 100644 index 0000000000..91bc18ca34 --- /dev/null +++ b/tests/integration/bls_tests.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + +#include + +#include + +using namespace eosio; +using namespace eosio::testing; +using namespace eosio::chain; +using namespace eosio::testing; +using namespace fc; + +using mvo = fc::mutable_variant_object; + +struct bls_primitives_tester : tester { + bls_primitives_tester() { + create_accounts( { "test"_n } ); + produce_block(); + + set_code( "eosio"_n, contracts::bls_primitives_test_wasm() ); + set_abi( "eosio"_n, contracts::bls_primitives_test_abi().data() ); + + produce_blocks(); + } +}; + +BOOST_AUTO_TEST_SUITE(bls_primitives_tests) + +BOOST_FIXTURE_TEST_CASE( g1_add_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg1add"_n, "test"_n, mvo() + ("op1", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") + ("op2", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") + ("res", "2b90dabdf4613e10d269d70050e61bdc53c6d01ac517ad33cd8e82799d5515dfba05bc172fd280e2a73ff01aca77e30cbf82182b9005141106ef83a6d33dcda8bece738c9f9d6313f7e05945fd92c23a208efbe7a2048c6250f7f14df9f5c215e244ce19aa2759751dfb31f234c644189d4b0eae26beb2ba29a380a052b058a380b3005a7f18391cd44411a0f87d7817")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( g2_add_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg2add"_n, "test"_n, mvo() + ("op1", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") + ("op2", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") + ("res", "1987cff592d8ae5c83ba9e9a1a016afd3a4e80d646e10a1274ba259b60a405c189945d07b3608d4d339a96429d60f80d8290d66f4e963c0e8c00e35db541ab46e93148fd7e41449f0be0a1883e36e4b56cf87121991d6d4778d499fdf1501c09a9220f11cdfe60560b15d7e6ec33825a8d9ce209fe8e20391d32210ba83dbd77ce4cd6019ca50465f8f5fed4a8a631048739c8d9b8fdc26a962b24f0306f8293a00d72d37fb2fb1b0643d9a8453cbf6a520463a54e25e8c42134d6798d7cce00949892c0f015e698b4386dbc3ef4f9b2b4c61454d90acdcfbf921adcd26bdf77454bdee1eb52a70fcab501fd1cfb0701ba60c9be25f9815bb9c32856144e543140d7c977d4585b0d75467b929db892f2da957948a1b02ecee537bcc742855716")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( g1_mul_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg1mul"_n, "test"_n, mvo() + ("point", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") + ("scalar", "2a000000000000002a000000000000002a000000000000002a00000000000000") + ("res", "de3e7eeee055abe12a480e58411508d51a356ff6692b14b43426d22cc354cd5d7469c41e0f1f5e40503c91e11419a30285cb057a62c93e2caaaff6c9c1dbc8f88c0a122157f51a617ce0e2890442cd9ce004a8ba972442e61bce9dabf1c6780c191984ae3c11ef21884a536f0d3450974df37295e9579d16cdb8dfdf9252091ca3cd9d05f4c6e645535add05ac197b08")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( g2_mul_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg2mul"_n, "test"_n, mvo() + ("point", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") + ("scalar", "2a000000000000002a000000000000002a000000000000002a00000000000000") + ("res", "aa25f3f539b6f3215318d81b22d14bf9108294790e8a50545a404ae2057278304d8c3b5844271202b757767d1555a106ede90a9967cdc27b527d4a5720efda79a68b17072ad9402ed373ce9a2d28f5496106fc4cd23234b083181e8325734417f8330d2ced14040b815a006f7f905361f654d483c45abb90b4a958b4ca20ee2bb97cc1c9b6ff45644539abb32149610f33858c88450dad3d2adb82df72f9ed9c42dc2ef78e17f5a2a1abd0468853d55f05f6458179fdf5671db784795a686c0ffae139afaed0212d18d615b0ff90a9deba090f723190521dc8c822621b0a7e70a03b9f3faaeb862846dccd418855d70406fc57e73783c2da92433c1a3873640217539ec7c01f3d354506d86db49fddad225e82421506d99c19b749170aa4f805")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( g1_exp_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg1exp"_n, "test"_n, mvo() + ("points", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") + ("scalars", "2a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a00000000000000") + ("res", "9e80ed609e62978a3a7f0d6bf57e1df4070725c1bf4dab43a6b710adef7450fb41f0cf14a7895ec26fd8c830c327cc0e2c8fe7687d23ff84647b47bbad04cf77625dee1049e53ad5162fe772278e5fe3ceb0bdc472f31952343da7b75532f7016613af892092131ad77d13afc8c9192487ac19f8454ad932653145f06e25790d26b23ac6b83025326f3397efbd845511")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( g2_exp_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg2exp"_n, "test"_n, mvo() + ("points", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") + ("scalars", "2a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a00000000000000") + ("res", "595a2cbaa4315ecc0dd9838a81712e0cfb88cb2c4468640ff382abb321b3a9bedebb0aad985a9057f664fc52eb52cd194b1c9611b25ef4281e439a52d1832813decd66c22440821f0288dcb7a82151386aa0240e943b6905e61619be2e11c208fa03df16e11a1b1368abac90598bb237f785701d5d1d5cb0af6934ed633d366de28703431b8d70899d92797689207c0cd35c345460f971dff5d648d9ddec8f5fabef99b15ea7c4440ac1564d6e0326076f32a4ec9cf0d10059593d64afd35c03d10f16794821628b565c9319f4af3c96b98c4fb11bc0c04172bb57372531f6e76798142d4b00488adbea850a2649a305b71fa389c3c226f2df4a58c8bce5d90698452f4126046be0c82d8817b64162a53787f1cb73af27d969adc626c1392716")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( pairing_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testpairing"_n, "test"_n, mvo() + ("g1_points", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") + ("g2_points", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") + ("res", "af587ca8f5f0760b888e4ec66748623d1b13986547b5de2042ce89233c2c837bbb080329a505285c948d8bb4c88a5914ae216a64db582ba084178db4364a7b3843ef7fb9056a90526d27dc9e252b38600fc54ce7a5ec15771c555816edc26d0166e75d3315789c846387c8c234b1c98b50baf233b7312aa317ae56a2bfa170b1bc43f0e046b4a1f45cb7aacea7d0ae0320496e3960d4dbc2039027ba8cfe1ca120ea98fa94cf25034ccb5e74033f447b837a78b03affd44b87f865f3d713000de78ab5629dbde8d0c8b7a28941717be3ebd23924cf16897144bb69730f68bff5a109fc2e6d563b15e2eb883cf4becd11edcbc2ec4402c93249389ed612fa0396ed6eadcbe4d703667d46b0150ee3a5158caef956791e4f527e1312c8402f8509acf7001dc0dc311549d76398c247e79b737b614d0a6663f7dbdb314e5b51eace14457ee7b1f3f54d5987c16c8f89d1022d91d4649f5f6a204047077e5791a654cd2277506cd0af77f9ea789278b364c115ec07ba14390a9c22d59aa9c97a9c09ce025b5e14443f3c4e4cd602ef34105fadd82837fc5ce60a461e6ea11b13ae67b82e3366a2b2d1bbe78b2579173b3c0c5aed88b2949403060c3e065782bcb742c55c559e75e373293d80dec54120773d80144b21b353ead58dc8427e5b9cbd0c1431f1a74caf5f57c4b55b89810029cdd24ef4a029797ced68ff882492a8f55f31fe52217356366983e35d2a5640e818cc8bddf9274de0100e624a6bf03e2b9ff22f8dda09a46b50b1b305cb6adfa2ae775febbce4a2b507cd2390db968ceb13")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( g1_map_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg1map"_n, "test"_n, mvo() + ("e", "c93f817b159bdf8404dc378514f845192bbae4faac7f4a568924f2d9725125000489408fd796461c288900add00d4618") + ("res", "d8e09005badb694b1e5dba8476ba3012cd2bf0c472ee1811ac9b34c869638b47e669bce16933e98ce9b129f83d93d71890d6fd13b77d4ad8df1a5f1c6b81df2b3f305721b7b7874cd81eba23ca05e8bc974b14f7e4c2c77cbdf321c340ba4903a2a46af290abe078e426f913e492b8d9b997232dbe6198aea719a31e73b40e110c4efa9e42d737a42883e0ca5f344d02")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( g2_map_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg2map"_n, "test"_n, mvo() + ("e", "d4f2cfec99387809574fcc2dba105603d950d490e2bebe0c212c05e16b784745ef4fe8e70b554d0a52fe0bed5ea6690ade2348eb8972a96740a430df162d920e175f5923a76d18650ea24a8ec06d414c6d1d218d673dac3619a1a5c142785708") + ("res", "9968ae9e9bf3d22ec6c9670efa64ea23d4966b41bb25f76ea2ef71b96fa35e031adb866b3df234065b9aa72d0b12ab14978678279eb944f05b5af53d91e700b57aa87076727def2e9f2fba3cf6784a25591ae1669c2cf15cdcf038b826d1e81178bd7b59b7e911e0c2d11d6805756222201e3364f8010fb651939eca63f7e77709042ee1030cd938f53905a714e815112a7dfeed207757d30382f69617014bc683a175d0dfbd74f2684de26c771f3f55a538e6d2f969eb282bddfec4fc08dd18f37df0889292f288dff631b02f07b88134410fd86526d234d75b9a329bc9a8b6e71c7ad516b87af9717d962cba5d2b19fd1b77746f1e484a54e6aec81ede148f01e2c8283c598a4976182d2ce287fe6888e23996ce03b03ce6709e4aa8e66416")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { + push_action("eosio"_n, "verify"_n, "test"_n, mvo() + ("pk", "90ace8f520a5e056eab099d4e6a6f761a8f51301e1e15910908453007d94e33f8e86b834808dcb5323313153be066d01d49660f594a805ad9a4249d0af4190407e3e221e1d6fdad866e022308357776ed5e396777c7f7dfd020e7d736511ac06fdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") + ("sig", "3c38a59a6c016fb598d8da531e205e4d8e0953868675760f8333c7fee725d81ddf246db75792eb7cf0ab8aeba051800babf407ddd8ec721db83fe1b1098b9334bf45ad63f10cf5a6b56b60f74b391615d7373088d182711358a6032f8c31fe1694ff3f3254a7e02e3e2811ce0becdec6031b193b5541d577b49cdb6cf09f919917b09776b1f05c977ffa17c2de876205f402f7e4fb00896cb9b3df1a25f761239e5625ddaab4bfacce8e6c505c13eb73632b12cedd30b0804fe29f317015c307ec2dfe48cb5060d84fd2c2756f41ae79f4c0a3a37ba62b22877b23042bb30a063d4ad3255de844545e89f0886041b513410df52d380fac23c31277997381115651c02014a9aeeaaec85c34b81a93ee73cd712ce241f394dc860ea5e792b0f50e")); + +} FC_LOG_AND_RETHROW() + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/integration/contracts.hpp.in b/tests/integration/contracts.hpp.in index 6fea3a3d08..982b8b72f8 100644 --- a/tests/integration/contracts.hpp.in +++ b/tests/integration/contracts.hpp.in @@ -23,6 +23,9 @@ namespace eosio::testing { static std::vector crypto_primitives_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.wasm"); } static std::vector crypto_primitives_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.abi"); } + static std::vector bls_primitives_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/bls_primitives_tests.wasm"); } + static std::vector bls_primitives_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/bls_primitives_tests.abi"); } + static std::vector get_code_hash_write_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.wasm"); } static std::vector get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); } static std::vector get_code_hash_read_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.wasm"); } diff --git a/tests/unit/test_contracts/CMakeLists.txt b/tests/unit/test_contracts/CMakeLists.txt index 48dc55e7e7..8fc0e6e8a8 100644 --- a/tests/unit/test_contracts/CMakeLists.txt +++ b/tests/unit/test_contracts/CMakeLists.txt @@ -7,6 +7,7 @@ add_contract(explicit_nested_tests explicit_nested_tests explicit_nested_tests.c add_contract(transfer_contract transfer_contract transfer.cpp) add_contract(minimal_tests minimal_tests minimal_tests.cpp) add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp) +add_contract(bls_primitives_tests bls_primitives_tests bls_primitives_tests.cpp) add_contract(get_code_hash_tests get_code_hash_write get_code_hash_write.cpp) add_contract(get_code_hash_tests get_code_hash_read get_code_hash_read.cpp) add_contract(name_pk_tests name_pk_tests name_pk_tests.cpp) diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp new file mode 100644 index 0000000000..e6f507b294 --- /dev/null +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -0,0 +1,410 @@ +#include +#include +#include +#include + +using namespace eosio; + +namespace bls12_381 +{ +class sha256 +{ +public: + sha256(): m_blocklen(0), m_bitlen(0) + { + m_state[0] = 0x6a09e667; + m_state[1] = 0xbb67ae85; + m_state[2] = 0x3c6ef372; + m_state[3] = 0xa54ff53a; + m_state[4] = 0x510e527f; + m_state[5] = 0x9b05688c; + m_state[6] = 0x1f83d9ab; + m_state[7] = 0x5be0cd19; + } + void update(const uint8_t * data, size_t length) + { + for(size_t i = 0 ; i < length ; i++) + { + m_data[m_blocklen++] = data[i]; + if (m_blocklen == 64) + { + transform(); + + // End of the block + m_bitlen += 512; + m_blocklen = 0; + } + } + } + void update(const std::string &data) + { + update(reinterpret_cast (data.c_str()), data.size()); + } + std::array digest() + { + std::array hash; + + pad(); + revert(hash); + + return hash; + } + void digest(uint8_t* dst) + { + std::array* phash = reinterpret_cast*>(dst); + + pad(); + revert(*phash); + } + + //static string toString(const array& digest); + +private: + uint8_t m_data[64]; + uint32_t m_blocklen; + uint64_t m_bitlen; + uint32_t m_state[8]; //A, B, C, D, E, F, G, H + + static constexpr std::array K = { + 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5, + 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3, + 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc, + 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7, + 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13, + 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3, + 0xd192e819,0xd6990624,0xf40e3585,0x106aa070, + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5, + 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208, + 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 + }; + + static uint32_t rotr(uint32_t x, uint32_t n) + { + return (x >> n) | (x << (32 - n)); + } + static uint32_t choose(uint32_t e, uint32_t f, uint32_t g) + { + return (e & f) ^ (~e & g); + } + static uint32_t majority(uint32_t a, uint32_t b, uint32_t c) + { + return (a & (b | c)) | (b & c); + } + static uint32_t sig0(uint32_t x) + { + return sha256::rotr(x, 7) ^ sha256::rotr(x, 18) ^ (x >> 3); + } + static uint32_t sig1(uint32_t x) + { + return sha256::rotr(x, 17) ^ sha256::rotr(x, 19) ^ (x >> 10); + } + void transform() + { + uint32_t maj, xorA, ch, xorE, sum, newA, newE, m[64]; + uint32_t state[8]; + + for(uint8_t i = 0, j = 0; i < 16; i++, j += 4) + { + // Split data in 32 bit blocks for the 16 first words + m[i] = (m_data[j] << 24) | (m_data[j + 1] << 16) | (m_data[j + 2] << 8) | (m_data[j + 3]); + } + + for(uint8_t k = 16 ; k < 64; k++) + { + // Remaining 48 blocks + m[k] = sha256::sig1(m[k - 2]) + m[k - 7] + sha256::sig0(m[k - 15]) + m[k - 16]; + } + + for(uint8_t i = 0 ; i < 8 ; i++) + { + state[i] = m_state[i]; + } + + for(uint8_t i = 0; i < 64; i++) + { + maj = sha256::majority(state[0], state[1], state[2]); + xorA = sha256::rotr(state[0], 2) ^ sha256::rotr(state[0], 13) ^ sha256::rotr(state[0], 22); + + ch = choose(state[4], state[5], state[6]); + + xorE = sha256::rotr(state[4], 6) ^ sha256::rotr(state[4], 11) ^ sha256::rotr(state[4], 25); + + sum = m[i] + K[i] + state[7] + ch + xorE; + newA = xorA + maj + sum; + newE = state[3] + sum; + + state[7] = state[6]; + state[6] = state[5]; + state[5] = state[4]; + state[4] = newE; + state[3] = state[2]; + state[2] = state[1]; + state[1] = state[0]; + state[0] = newA; + } + + for(uint8_t i = 0 ; i < 8 ; i++) + { + m_state[i] += state[i]; + } + } + void pad() + { + uint64_t i = m_blocklen; + uint8_t end = m_blocklen < 56 ? 56 : 64; + + m_data[i++] = 0x80; // Append a bit 1 + while(i < end) + { + m_data[i++] = 0x00; // Pad with zeros + } + + if(m_blocklen >= 56) + { + transform(); + memset(m_data, 0, 56); + } + + // Append to the padding the total message's length in bits and transform. + m_bitlen += m_blocklen * 8; + m_data[63] = m_bitlen; + m_data[62] = m_bitlen >> 8; + m_data[61] = m_bitlen >> 16; + m_data[60] = m_bitlen >> 24; + m_data[59] = m_bitlen >> 32; + m_data[58] = m_bitlen >> 40; + m_data[57] = m_bitlen >> 48; + m_data[56] = m_bitlen >> 56; + transform(); + } + void revert(std::array& hash) + { + // SHA uses big endian byte ordering + // Revert all bytes + for(uint8_t i = 0 ; i < 4 ; i++) + { + for(uint8_t j = 0 ; j < 8 ; j++) + { + hash[i + (j * 4)] = (m_state[j] >> (24 - i * 8)) & 0x000000ff; + } + } + } +}; +} // namespace bls12_381 + +class [[eosio::contract]] bls_primitives_tests : public contract{ + public: + using contract::contract; + + [[eosio::action]] + void testg1add(const std::vector& op1, const std::vector& op2, const std::vector& res) + { + std::vector r; + r.resize(144); + bls_g1_add(op1.data(), op2.data(), r.data()); + check(r == res, "bls_g1_add test failed"); + } + + [[eosio::action]] + void testg2add(const std::vector& op1, const std::vector& op2, const std::vector& res) + { + std::vector r; + r.resize(288); + bls_g2_add(op1.data(), op2.data(), r.data()); + check(r == res, "bls_g2_add test failed"); + } + + [[eosio::action]] + void testg1mul(const std::vector& point, const std::vector& scalar, const std::vector& res) + { + std::vector r; + r.resize(144); + bls_g1_mul(point.data(), scalar.data(), r.data()); + check(r == res, "bls_g1_mul test failed"); + } + + [[eosio::action]] + void testg2mul(const std::vector& point, const std::vector& scalar, const std::vector& res) + { + std::vector r; + r.resize(288); + bls_g2_mul(point.data(), scalar.data(), r.data()); + check(r == res, "bls_g2_mul test failed"); + } + + [[eosio::action]] + void testg1exp(const std::vector& points, const std::vector& scalars, const std::vector& res) + { + check(points.size()/144 == scalars.size()/32, "number of elements in points and scalars must be equal"); + std::vector r; + r.resize(144); + bls_g1_exp(points.data(), scalars.data(), points.size()/144, r.data()); + check(r == res, "bls_g1_exp test failed"); + } + + [[eosio::action]] + void testg2exp(const std::vector& points, const std::vector& scalars, const std::vector& res) + { + check(points.size()/288 == scalars.size()/32, "number of elements in points and scalars must be equal"); + std::vector r; + r.resize(288); + bls_g2_exp(points.data(), scalars.data(), points.size()/288, r.data()); + check(r == res, "bls_g2_exp test failed"); + } + + [[eosio::action]] + void testpairing(const std::vector& g1_points, const std::vector& g2_points, const std::vector& res) + { + check(g1_points.size()/144 == g2_points.size()/288, "number of elements in g1_points and g2_points must be equal"); + std::vector r; + r.resize(576); + bls_pairing(g1_points.data(), g2_points.data(), g1_points.size()/144, r.data()); + check(r == res, "bls_pairing test failed"); + } + + [[eosio::action]] + void testg1map(const std::vector& e, const std::vector& res) + { + std::vector r; + r.resize(144); + bls_g1_map(e.data(), r.data()); + check(r == res, "bls_g1_map test failed"); + } + + [[eosio::action]] + void testg2map(const std::vector& e, const std::vector& res) + { + std::vector r; + r.resize(288); + bls_g2_map(e.data(), r.data()); + check(r == res, "bls_g2_map test failed"); + } + + // Construct an extensible-output function based on SHA256 + void xmd_sh256( + uint8_t *buf, + int buf_len, + const uint8_t *in, + int in_len, + const uint8_t *dst, + int dst_len + ) + { + const unsigned int SHA256HashSize = 32; + const unsigned int SHA256_Message_Block_Size = 64; + const unsigned ell = (buf_len + SHA256HashSize - 1) / SHA256HashSize; + if (buf_len < 0 || ell > 255 || dst_len > 255) + { + return; + } + const uint8_t Z_pad[SHA256_Message_Block_Size] = { 0, }; + const uint8_t l_i_b_0_str[] = { + static_cast(buf_len >> 8), + static_cast(buf_len & 0xff), + 0, + static_cast(dst_len) + }; + const uint8_t *dstlen_str = l_i_b_0_str + 3; + uint8_t b_0[SHA256HashSize]; + bls12_381::sha256 sha; + sha.update(Z_pad, SHA256_Message_Block_Size); + sha.update(in, in_len); + sha.update(l_i_b_0_str, 3); + sha.update(dst, dst_len); + sha.update(dstlen_str, 1); + sha.digest(b_0); + uint8_t b_i[SHA256HashSize + 1] = { 0, }; + for (unsigned i = 1; i <= ell; ++i) + { + for (unsigned j = 0; j < SHA256HashSize; ++j) + { + b_i[j] = b_0[j] ^ b_i[j]; + } + b_i[SHA256HashSize] = i; + bls12_381::sha256 s; + s.update(b_i, SHA256HashSize + 1); + s.update(dst, dst_len); + s.update(dstlen_str, 1); + s.digest(b_i); + const int rem_after = buf_len - i * SHA256HashSize; + const int copy_len = SHA256HashSize + (rem_after < 0 ? rem_after : 0); + memcpy(buf + (i - 1) * SHA256HashSize, b_i, copy_len); + } + } + + std::array scalar_fromBytesBE(const std::array& in) + { + std::array out; + for(uint64_t i = 0; i < 8; i++) + { + int64_t a = 8*8 - i*8; + out[i] = + static_cast(in[a-1]) | static_cast(in[a-2]) << 8 | + static_cast(in[a-3]) << 16 | static_cast(in[a-4]) << 24 | + static_cast(in[a-5]) << 32 | static_cast(in[a-6]) << 40 | + static_cast(in[a-7]) << 48 | static_cast(in[a-8]) << 56; + } + return out; + } + + std::vector g2_fromMessage(const std::vector& msg, const std::string& dst) + { + uint8_t buf[4 * 64]; + xmd_sh256(buf, 4 * 64, msg.data(), msg.size(), reinterpret_cast(dst.c_str()), dst.length()); + + std::array k; + std::vector t; + t.resize(96); + std::vector p, q, res; + p.resize(288); + q.resize(288); + res.resize(288); + + k = scalar_fromBytesBE(*reinterpret_cast*>(buf)); + bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[0])); + k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 64)); + bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[48])); + + bls_g2_map(t.data(), p.data()); + + k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 2*64)); + bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[0])); + k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 3*64)); + bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[48])); + + bls_g2_map(t.data(), q.data()); + bls_g2_add(p.data(), q.data(), res.data()); + + return res; + } + + const std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; + const std::vector G1_ONE_NEG = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; + const std::vector GT_ONE = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; + + [[eosio::action]] + void verify(const std::vector& pk, const std::vector& sig) + { + uint8_t g1_points[2 * 144]; + uint8_t g2_points[2 * 288]; + + memcpy(g1_points, G1_ONE_NEG.data(), 144); + memcpy(g2_points, sig.data(), 288); + + memcpy(&g1_points[144], pk.data(), 144); + memcpy(&g2_points[288], g2_fromMessage(msg, CIPHERSUITE_ID).data(), 288); + + std::vector r; + r.resize(576); + bls_pairing(g1_points, g2_points, 2, r.data()); + check(r == GT_ONE, "bls signature verify failed"); + } +}; From 9d1424dfcf038cbaa096dda1976fcc44c0443adb Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Fri, 30 Jun 2023 10:18:46 -0500 Subject: [PATCH 025/158] added bls types to enforce correct operand sizes --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 114 +++++++++++--- .../test_contracts/bls_primitives_tests.cpp | 139 ++++++++++-------- 2 files changed, 168 insertions(+), 85 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index c4cdf42bd4..72e5997cce 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -43,54 +43,128 @@ namespace eosio { } } - int32_t bls_g1_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) + using bls_scalar = uint8_t[32]; + using bls_fp = uint8_t[48]; + using bls_fp2 = uint8_t[96]; + using bls_g1 = uint8_t[144]; + using bls_g2 = uint8_t[288]; + using bls_gt = uint8_t[576]; + + int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { - return internal_use_do_not_use::bls_g1_add(reinterpret_cast(op1), 144, reinterpret_cast(op2), 144, reinterpret_cast(res), 144); + return internal_use_do_not_use::bls_g1_add( + reinterpret_cast(op1), + sizeof(bls_g1), + reinterpret_cast(op2), + sizeof(bls_g1), + reinterpret_cast(res), + sizeof(bls_g1) + ); } - int32_t bls_g2_add(const uint8_t* op1, const uint8_t* op2, uint8_t* res) + int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) { - return internal_use_do_not_use::bls_g2_add(reinterpret_cast(op1), 288, reinterpret_cast(op2), 288, reinterpret_cast(res), 288); + return internal_use_do_not_use::bls_g2_add( + reinterpret_cast(op1), + sizeof(bls_g2), + reinterpret_cast(op2), + sizeof(bls_g2), + reinterpret_cast(res), + sizeof(bls_g2) + ); } - int32_t bls_g1_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) + int32_t bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) { - return internal_use_do_not_use::bls_g1_mul(reinterpret_cast(point), 144, reinterpret_cast(scalar), 32, reinterpret_cast(res), 144); + return internal_use_do_not_use::bls_g1_mul( + reinterpret_cast(point), + sizeof(bls_g1), + reinterpret_cast(scalar), + sizeof(bls_scalar), + reinterpret_cast(res), + sizeof(bls_g1) + ); } - int32_t bls_g2_mul(const uint8_t* point, const uint8_t* scalar, uint8_t* res) + int32_t bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) { - return internal_use_do_not_use::bls_g2_mul(reinterpret_cast(point), 288, reinterpret_cast(scalar), 32, reinterpret_cast(res), 288); + return internal_use_do_not_use::bls_g2_mul( + reinterpret_cast(point), + sizeof(bls_g2), + reinterpret_cast(scalar), + sizeof(bls_scalar), + reinterpret_cast(res), + sizeof(bls_g2) + ); } - int32_t bls_g1_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) + int32_t bls_g1_exp(const bls_g1* points, const bls_scalar* scalars, const uint32_t num, bls_g1& res) { - return internal_use_do_not_use::bls_g1_exp(reinterpret_cast(points), num * 144, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 144); + return internal_use_do_not_use::bls_g1_exp( + reinterpret_cast(points), + num * sizeof(bls_g1), + reinterpret_cast(scalars), + num * sizeof(bls_scalar), + num, + reinterpret_cast(res), + sizeof(bls_g1) + ); } - int32_t bls_g2_exp(const uint8_t* points, const uint8_t* scalars, const uint32_t num, uint8_t* res) + int32_t bls_g2_exp(const bls_g2* points, const bls_scalar* scalars, const uint32_t num, bls_g2& res) { - return internal_use_do_not_use::bls_g2_exp(reinterpret_cast(points), num * 288, reinterpret_cast(scalars), num * 32, num, reinterpret_cast(res), 288); + return internal_use_do_not_use::bls_g2_exp( + reinterpret_cast(points), + num * sizeof(bls_g2), + reinterpret_cast(scalars), + num * sizeof(bls_scalar), + num, + reinterpret_cast(res), + sizeof(bls_g2) + ); } - int32_t bls_pairing(const uint8_t* g1_points, const uint8_t* g2_points, const uint32_t num, uint8_t* res) + int32_t bls_pairing(const bls_g1* g1_points, const bls_g2* g2_points, const uint32_t num, bls_gt& res) { - return internal_use_do_not_use::bls_pairing(reinterpret_cast(g1_points), num * 144, reinterpret_cast(g2_points), num * 288, num, reinterpret_cast(res), 576); + return internal_use_do_not_use::bls_pairing( + reinterpret_cast(g1_points), + num * sizeof(bls_g1), + reinterpret_cast(g2_points), + num * sizeof(bls_g2), + num, + reinterpret_cast(res), + sizeof(bls_gt) + ); } - int32_t bls_g1_map(const uint8_t* e, uint8_t* res) + int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { - return internal_use_do_not_use::bls_g1_map(reinterpret_cast(e), 48, reinterpret_cast(res), 144); + return internal_use_do_not_use::bls_g1_map( + reinterpret_cast(e), + sizeof(bls_fp), + reinterpret_cast(res), + sizeof(bls_g1) + ); } - int32_t bls_g2_map(const uint8_t* e, uint8_t* res) + int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) { - return internal_use_do_not_use::bls_g2_map(reinterpret_cast(e), 96, reinterpret_cast(res), 288); + return internal_use_do_not_use::bls_g2_map( + reinterpret_cast(e), + sizeof(bls_fp2), + reinterpret_cast(res), + sizeof(bls_g2) + ); } - int32_t bls_fp_mod(const uint8_t* s, uint8_t* res) + int32_t bls_fp_mod(const uint8_t* s, bls_fp& res) { - return internal_use_do_not_use::bls_fp_mod(reinterpret_cast(s), 64, reinterpret_cast(res), 48); + return internal_use_do_not_use::bls_fp_mod( + reinterpret_cast(s), + 64, + reinterpret_cast(res), + sizeof(bls_fp) + ); } } diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index e6f507b294..a950725d33 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -205,85 +205,99 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ [[eosio::action]] void testg1add(const std::vector& op1, const std::vector& op2, const std::vector& res) { - std::vector r; - r.resize(144); - bls_g1_add(op1.data(), op2.data(), r.data()); - check(r == res, "bls_g1_add test failed"); + bls_g1 lhs, rhs, r; + memcpy(lhs, op1.data(), sizeof(bls_g1)); + memcpy(rhs, op1.data(), sizeof(bls_g1)); + bls_g1_add(lhs, rhs, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_add test failed"); } [[eosio::action]] void testg2add(const std::vector& op1, const std::vector& op2, const std::vector& res) { - std::vector r; - r.resize(288); - bls_g2_add(op1.data(), op2.data(), r.data()); - check(r == res, "bls_g2_add test failed"); + bls_g2 lhs, rhs, r; + memcpy(lhs, op1.data(), sizeof(bls_g2)); + memcpy(rhs, op1.data(), sizeof(bls_g2)); + bls_g2_add(lhs, rhs, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_add test failed"); } [[eosio::action]] void testg1mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { - std::vector r; - r.resize(144); - bls_g1_mul(point.data(), scalar.data(), r.data()); - check(r == res, "bls_g1_mul test failed"); + bls_g1 p, r; + bls_scalar s; + memcpy(p, point.data(), sizeof(bls_g1)); + memcpy(s, scalar.data(), sizeof(bls_scalar)); + bls_g1_mul(p, s, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_mul test failed"); } [[eosio::action]] void testg2mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { - std::vector r; - r.resize(288); - bls_g2_mul(point.data(), scalar.data(), r.data()); - check(r == res, "bls_g2_mul test failed"); + bls_g2 p, r; + bls_scalar s; + memcpy(p, point.data(), sizeof(bls_g2)); + memcpy(s, scalar.data(), sizeof(bls_scalar)); + bls_g2_mul(p, s, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_mul test failed"); } [[eosio::action]] void testg1exp(const std::vector& points, const std::vector& scalars, const std::vector& res) { - check(points.size()/144 == scalars.size()/32, "number of elements in points and scalars must be equal"); - std::vector r; - r.resize(144); - bls_g1_exp(points.data(), scalars.data(), points.size()/144, r.data()); - check(r == res, "bls_g1_exp test failed"); + check(points.size()/sizeof(bls_g1) == scalars.size()/sizeof(bls_scalar), "number of elements in points and scalars must be equal"); + uint32_t num = scalars.size()/sizeof(bls_scalar); + const bls_g1* pp = reinterpret_cast(points.data()); + const bls_scalar* ps = reinterpret_cast(scalars.data()); + bls_g1 r; + bls_g1_exp(pp, ps, num, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_exp test failed"); } [[eosio::action]] void testg2exp(const std::vector& points, const std::vector& scalars, const std::vector& res) { - check(points.size()/288 == scalars.size()/32, "number of elements in points and scalars must be equal"); - std::vector r; - r.resize(288); - bls_g2_exp(points.data(), scalars.data(), points.size()/288, r.data()); - check(r == res, "bls_g2_exp test failed"); + check(points.size()/sizeof(bls_g2) == scalars.size()/sizeof(bls_scalar), "number of elements in points and scalars must be equal"); + uint32_t num = scalars.size()/sizeof(bls_scalar); + const bls_g2* pp = reinterpret_cast(points.data()); + const bls_scalar* ps = reinterpret_cast(scalars.data()); + bls_g2 r; + bls_g2_exp(pp, ps, num, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_exp test failed"); } [[eosio::action]] void testpairing(const std::vector& g1_points, const std::vector& g2_points, const std::vector& res) { - check(g1_points.size()/144 == g2_points.size()/288, "number of elements in g1_points and g2_points must be equal"); - std::vector r; - r.resize(576); - bls_pairing(g1_points.data(), g2_points.data(), g1_points.size()/144, r.data()); - check(r == res, "bls_pairing test failed"); + check(g1_points.size()/sizeof(bls_g1) == g2_points.size()/sizeof(bls_g2), "number of elements in g1_points and g2_points must be equal"); + uint32_t num = g1_points.size()/sizeof(bls_g1); + const bls_g1* pp1 = reinterpret_cast(g1_points.data()); + const bls_g2* pp2 = reinterpret_cast(g2_points.data()); + bls_gt r; + bls_pairing(pp1, pp2, num, r); + check(0 == memcmp(r, res.data(), sizeof(bls_gt)), "bls_pairing test failed"); } [[eosio::action]] void testg1map(const std::vector& e, const std::vector& res) { - std::vector r; - r.resize(144); - bls_g1_map(e.data(), r.data()); - check(r == res, "bls_g1_map test failed"); + bls_fp element; + memcpy(element, e.data(), sizeof(bls_fp)); + bls_g1 r; + bls_g1_map(element, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_map test failed"); } [[eosio::action]] void testg2map(const std::vector& e, const std::vector& res) { - std::vector r; - r.resize(288); - bls_g2_map(e.data(), r.data()); - check(r == res, "bls_g2_map test failed"); + bls_fp2 element; + memcpy(element, e.data(), sizeof(bls_fp2)); + bls_g2 r; + bls_g2_map(element, r); + check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_map test failed"); } // Construct an extensible-output function based on SHA256 @@ -353,35 +367,29 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ return out; } - std::vector g2_fromMessage(const std::vector& msg, const std::string& dst) + void g2_fromMessage(const std::vector& msg, const std::string& dst, bls_g2& res) { uint8_t buf[4 * 64]; xmd_sh256(buf, 4 * 64, msg.data(), msg.size(), reinterpret_cast(dst.c_str()), dst.length()); std::array k; - std::vector t; - t.resize(96); - std::vector p, q, res; - p.resize(288); - q.resize(288); - res.resize(288); + bls_fp t[2]; + bls_g2 p, q; k = scalar_fromBytesBE(*reinterpret_cast*>(buf)); - bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[0])); + bls_fp_mod(reinterpret_cast(&k[0]), t[0]); k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 64)); - bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[48])); + bls_fp_mod(reinterpret_cast(&k[0]), t[1]); - bls_g2_map(t.data(), p.data()); + bls_g2_map(*reinterpret_cast(t), p); k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 2*64)); - bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[0])); + bls_fp_mod(reinterpret_cast(&k[0]), t[0]); k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 3*64)); - bls_fp_mod(reinterpret_cast(&k[0]), reinterpret_cast(&t[48])); + bls_fp_mod(reinterpret_cast(&k[0]), t[1]); - bls_g2_map(t.data(), q.data()); - bls_g2_add(p.data(), q.data(), res.data()); - - return res; + bls_g2_map(*reinterpret_cast(t), q); + bls_g2_add(p, q, res); } const std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; @@ -393,18 +401,19 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ [[eosio::action]] void verify(const std::vector& pk, const std::vector& sig) { - uint8_t g1_points[2 * 144]; - uint8_t g2_points[2 * 288]; + bls_g1 g1_points[2]; + bls_g2 g2_points[2]; - memcpy(g1_points, G1_ONE_NEG.data(), 144); - memcpy(g2_points, sig.data(), 288); + memcpy(&g1_points[0], G1_ONE_NEG.data(), sizeof(bls_g1)); + memcpy(&g2_points[0], sig.data(), sizeof(bls_g2)); - memcpy(&g1_points[144], pk.data(), 144); - memcpy(&g2_points[288], g2_fromMessage(msg, CIPHERSUITE_ID).data(), 288); + bls_g2 p_msg; + g2_fromMessage(msg, CIPHERSUITE_ID, p_msg); + memcpy(&g1_points[1], pk.data(), sizeof(bls_g1)); + memcpy(&g2_points[1], p_msg, sizeof(bls_g2)); - std::vector r; - r.resize(576); - bls_pairing(g1_points, g2_points, 2, r.data()); - check(r == GT_ONE, "bls signature verify failed"); + bls_gt r; + bls_pairing(g1_points, g2_points, 2, r); + check(0 == memcmp(r, GT_ONE.data(), sizeof(bls_gt)), "bls signature verify failed"); } }; From 3f701e2ea9c3a81d5030bbe0d08caf86ec0c969c Mon Sep 17 00:00:00 2001 From: mschoenebeck Date: Fri, 30 Jun 2023 14:29:38 -0500 Subject: [PATCH 026/158] better fp2 type --- libraries/eosiolib/core/eosio/crypto_bls_ext.hpp | 2 +- tests/unit/test_contracts/bls_primitives_tests.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 72e5997cce..1708f292ed 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -45,7 +45,7 @@ namespace eosio { using bls_scalar = uint8_t[32]; using bls_fp = uint8_t[48]; - using bls_fp2 = uint8_t[96]; + using bls_fp2 = bls_fp[2]; using bls_g1 = uint8_t[144]; using bls_g2 = uint8_t[288]; using bls_gt = uint8_t[576]; diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index a950725d33..35f244f509 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -373,7 +373,7 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ xmd_sh256(buf, 4 * 64, msg.data(), msg.size(), reinterpret_cast(dst.c_str()), dst.length()); std::array k; - bls_fp t[2]; + bls_fp2 t; bls_g2 p, q; k = scalar_fromBytesBE(*reinterpret_cast*>(buf)); @@ -381,14 +381,14 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 64)); bls_fp_mod(reinterpret_cast(&k[0]), t[1]); - bls_g2_map(*reinterpret_cast(t), p); + bls_g2_map(t, p); k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 2*64)); bls_fp_mod(reinterpret_cast(&k[0]), t[0]); k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 3*64)); bls_fp_mod(reinterpret_cast(&k[0]), t[1]); - bls_g2_map(*reinterpret_cast(t), q); + bls_g2_map(t, q); bls_g2_add(p, q, res); } From 459fa3226d29775507e306fcf74dae84650ff586 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 10 Jul 2023 16:20:10 -0400 Subject: [PATCH 027/158] handle correctly included contracts types --- tools/include/eosio/abigen.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 479c9fec73..0ac4bc655e 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -787,7 +787,7 @@ namespace eosio { namespace cdt { ag.add_contracts(ag.parse_contracts()); has_added_clauses = true; } - if ((decl->isEosioAction() || decl->isEosioTable()) && ag.is_eosio_contract(decl, ag.get_contract_name())) { + if (decl->isEosioAction() || decl->isEosioTable()) { ag.add_struct(decl); if (decl->isEosioAction()) ag.add_action(decl); From 19b3b249aaca43a8ed06cb06d3d9aa420c438cc7 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 17 Jul 2023 23:27:35 -0400 Subject: [PATCH 028/158] redesign of abigen for multi_index and singleton --- tests/CMakeLists.txt | 2 +- .../abigen-pass/exclude_from_abi.hpp | 50 +++++++++++ .../abigen-pass/singleton_contract.abi | 7 ++ .../abigen-pass/singleton_contract.cpp | 23 ++++- .../abigen-pass/singleton_contract_simple.abi | 31 ------- .../abigen-pass/singleton_contract_simple.cpp | 16 ---- .../singleton_contract_simple.json | 10 --- tools/include/eosio/abigen.hpp | 90 ++++++++++++++++++- 8 files changed, 167 insertions(+), 62 deletions(-) create mode 100644 tests/toolchain/abigen-pass/exclude_from_abi.hpp delete mode 100644 tests/toolchain/abigen-pass/singleton_contract_simple.abi delete mode 100644 tests/toolchain/abigen-pass/singleton_contract_simple.cpp delete mode 100644 tests/toolchain/abigen-pass/singleton_contract_simple.json diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d4bd2cf6e4..2590f7345b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,7 +21,7 @@ add_unit_test( system_tests ) add_unit_test( time_tests ) add_unit_test( varint_tests ) -add_test( NAME toolchain_tests COMMAND ${CMAKE_BINARY_DIR}/tools/toolchain-tester/toolchain-tester ${CMAKE_SOURCE_DIR}/tests/toolchain --cdt ${CMAKE_BINARY_DIR}/bin ) +add_test( NAME toolchain_tests COMMAND ${CMAKE_BINARY_DIR}/tools/toolchain-tester/toolchain-tester ${CMAKE_SOURCE_DIR}/tests/toolchain --cdt ${CMAKE_BINARY_DIR}/bin --verbose ) set_property(TEST toolchain_tests PROPERTY LABELS toolchain_tests) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/unit/version_tests.sh ${CMAKE_BINARY_DIR}/tests/unit/version_tests.sh COPYONLY) diff --git a/tests/toolchain/abigen-pass/exclude_from_abi.hpp b/tests/toolchain/abigen-pass/exclude_from_abi.hpp new file mode 100644 index 0000000000..3b90f866ac --- /dev/null +++ b/tests/toolchain/abigen-pass/exclude_from_abi.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include +#include + + + +struct [[eosio::table]] out_of_class { + uint64_t id; + uint64_t primary_key() const { return id; } +}; +typedef eosio::multi_index<"mi.config55"_n, out_of_class> out_of_class_index; +using uout_of_class_index = eosio::multi_index<"mi.config551"_n, out_of_class>; + +typedef eosio::singleton<"smpl.conf55"_n, eosio::name> smpl_config55; +typedef eosio::singleton<"config55"_n, out_of_class> config55; +typedef smpl_config55 smpl_config551; +typedef config55 config551; +using smpl_conf551 = eosio::singleton<"smpl.conf551"_n, eosio::name>; +using config552 = eosio::singleton<"config552"_n, out_of_class>; +using smpl_conf552 = smpl_conf551; +using config553 = config551; + +class [[eosio::contract("singleton_contract_simple2")]] singleton_contract_simple2 : public eosio::contract { + public: + using eosio::contract::contract; + + [[eosio::action]] + void whatever() {}; + + struct [[eosio::table]] inside_class { + uint64_t id; + uint64_t primary_key() const { return id; } + }; + typedef eosio::singleton<"smpl.conf552"_n, eosio::name> smpl_conf552; + typedef eosio::singleton<"config552"_n, inside_class> config552; + typedef smpl_conf552 smpl_conf553; + typedef config552 config553; + using smpl_conf554 = eosio::singleton<"smpl.conf554"_n, eosio::name>; + using config554 = eosio::singleton<"config554"_n, inside_class>; + using smpl_conf555 = smpl_conf554; + using config555 = config554; + + + + typedef eosio::multi_index<"mi.config553"_n, inside_class> inside_class_index; + using uinside_class_index = eosio::multi_index<"mi.config554"_n, inside_class>; +}; diff --git a/tests/toolchain/abigen-pass/singleton_contract.abi b/tests/toolchain/abigen-pass/singleton_contract.abi index 4c6e9853a5..ade07a444d 100644 --- a/tests/toolchain/abigen-pass/singleton_contract.abi +++ b/tests/toolchain/abigen-pass/singleton_contract.abi @@ -37,6 +37,13 @@ "index_type": "i64", "key_names": [], "key_types": [] + }, + { + "name": "smpl.config", + "type": "name", + "index_type": "i64", + "key_names": [], + "key_types": [] } ], "ricardian_clauses": [], diff --git a/tests/toolchain/abigen-pass/singleton_contract.cpp b/tests/toolchain/abigen-pass/singleton_contract.cpp index 89831854c9..0a27c52aa4 100644 --- a/tests/toolchain/abigen-pass/singleton_contract.cpp +++ b/tests/toolchain/abigen-pass/singleton_contract.cpp @@ -2,9 +2,27 @@ #include #include #include + +#include "exclude_from_abi.hpp" using namespace eosio; - + +struct [[eosio::table]] out_of_class2 { + uint64_t id; + uint64_t primary_key() const { return id; } +}; +typedef eosio::multi_index<"mi.config5"_n, out_of_class2> out_of_class_index51; +using uout_of_class_index51 = eosio::multi_index<"mi.config51"_n, out_of_class2>; + +typedef eosio::singleton<"smpl.conf5"_n, eosio::name> smpl_config5; +typedef eosio::singleton<"config5"_n, out_of_class2> config5; +typedef smpl_config5 smpl_config51; +typedef config5 config51; +using smpl_conf51 = eosio::singleton<"smpl.conf51"_n, eosio::name>; +using config52 = eosio::singleton<"config52"_n, out_of_class2>; +using smpl_conf52 = smpl_conf51; +using config53 = config51; + class [[eosio::contract("singleton_contract")]] singleton_contract : public contract { public: using contract::contract; @@ -17,5 +35,6 @@ class [[eosio::contract("singleton_contract")]] singleton_contract : public cont uint64_t x; }; - typedef eosio::singleton<"config"_n, tbl_config> config; + typedef eosio::singleton<"config"_n, tbl_config> config; + typedef eosio::singleton<"smpl.config"_n, name> smpl_config; }; diff --git a/tests/toolchain/abigen-pass/singleton_contract_simple.abi b/tests/toolchain/abigen-pass/singleton_contract_simple.abi deleted file mode 100644 index 8600eb2f76..0000000000 --- a/tests/toolchain/abigen-pass/singleton_contract_simple.abi +++ /dev/null @@ -1,31 +0,0 @@ -{ - "____comment": "This file was generated with eosio-abigen. DO NOT EDIT ", - "version": "eosio::abi/1.2", - "types": [], - "structs": [ - { - "name": "whatever", - "base": "", - "fields": [] - } - ], - "actions": [ - { - "name": "whatever", - "type": "whatever", - "ricardian_contract": "" - } - ], - "tables": [ - { - "name": "smpl.config", - "type": "name", - "index_type": "i64", - "key_names": [], - "key_types": [] - } - ], - "ricardian_clauses": [], - "variants": [], - "action_results": [] -} diff --git a/tests/toolchain/abigen-pass/singleton_contract_simple.cpp b/tests/toolchain/abigen-pass/singleton_contract_simple.cpp deleted file mode 100644 index 2afdc7c332..0000000000 --- a/tests/toolchain/abigen-pass/singleton_contract_simple.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include -#include - -using namespace eosio; - -class [[eosio::contract("singleton_contract_simple")]] singleton_contract_simple : public contract { - public: - using contract::contract; - - [[eosio::action]] - void whatever() {}; - - typedef eosio::singleton<"smpl.config"_n, name> config; -}; diff --git a/tests/toolchain/abigen-pass/singleton_contract_simple.json b/tests/toolchain/abigen-pass/singleton_contract_simple.json deleted file mode 100644 index 0bf81acc1e..0000000000 --- a/tests/toolchain/abigen-pass/singleton_contract_simple.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "tests" : [ - { - "expected" : { - "abi-file" : "singleton_contract_simple.abi" - } - } - ] - } - \ No newline at end of file diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 0ac4bc655e..77c301ae09 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -755,6 +755,7 @@ namespace eosio { namespace cdt { private: bool has_added_clauses = false; abigen& ag = abigen::get(); + const clang::CXXRecordDecl* contract_class = NULL; public: explicit eosio_abigen_visitor(CompilerInstance *CI) { @@ -787,7 +788,7 @@ namespace eosio { namespace cdt { ag.add_contracts(ag.parse_contracts()); has_added_clauses = true; } - if (decl->isEosioAction() || decl->isEosioTable()) { + if ((decl->isEosioAction() || decl->isEosioTable()) && ag.is_eosio_contract(decl, ag.get_contract_name())) { ag.add_struct(decl); if (decl->isEosioAction()) ag.add_action(decl); @@ -799,10 +800,95 @@ namespace eosio { namespace cdt { } return true; } + + const clang::CXXRecordDecl* find_contract_class(const clang::Decl* decl) const { + const auto* translation_unit = decl->getASTContext().getTranslationUnitDecl(); + + for (clang::Decl* cur_decl : translation_unit->decls()) { + if (const auto* cxx_decl = llvm::dyn_cast(cur_decl)) { + + if (cxx_decl->isEosioContract()) { + auto attr_name = cxx_decl->getEosioContractAttr()->getName().str(); + auto name = attr_name.empty() ? cxx_decl->getName().str() : attr_name; + if (name == ag.get_contract_name()) + return cxx_decl; + } + else { + auto pd = llvm::dyn_cast(cxx_decl->getParent()); + if (pd && pd->isEosioContract()) { + auto attr_name = pd->getEosioContractAttr()->getName().str(); + auto name = attr_name.empty() ? pd->getName().str() : attr_name; + if (name == ag.get_contract_name()) + return pd; + } + } + } + } + + return NULL; + } + + bool is_same_type(const clang::Decl* decl1, const clang::CXXRecordDecl* decl2) const { + if (decl1 == decl2) + return true; + + if (const clang::TypedefNameDecl* typedef_decl = llvm::dyn_cast(decl1)) { + if (const auto* cxx_rec1 = typedef_decl->getUnderlyingType()->getAsCXXRecordDecl()) { + if (cxx_rec1 == decl2) + return true; + } + } + + return false; + } + + bool aliased_in_contract(const clang::Decl* decl) const { + for (const auto* contract_decl : contract_class->decls()) { + if (const auto* cur_contract_decl = llvm::dyn_cast(contract_decl)) { + if (const auto* cur_contract_rec = cur_contract_decl->getUnderlyingType()->getAsCXXRecordDecl()) { + if (cur_contract_rec == decl) + return true; + } + } + } + } + + // recursively check all declarations from declaration context if any of them is equal to decl + bool is_decl_in_contract(const clang::Decl* cur_decl, const clang::CXXRecordDecl* decl) { + if (is_same_type(cur_decl, decl) && aliased_in_contract(decl)) + return true; + + if (const auto* decl_ctx = llvm::dyn_cast(cur_decl)) { + // The Decl is also a DeclContext, so it might contain other Decls. + for (const auto* child_decl : decl_ctx->decls()) { + if(is_decl_in_contract(child_decl, decl)) + return true; + } + } + + return false; + } + + bool defined_in_contract(const clang::ClassTemplateSpecializationDecl* decl) { + + if (!contract_class) + contract_class = find_contract_class(decl); + if (!contract_class) CDT_ERROR("abigen_error", decl->getLocation(), "contract class not found"); + + const auto* translation_unit = decl->getASTContext().getTranslationUnitDecl(); + + for (const auto* cur_decl : translation_unit->decls()) { + // Recursively traverse the AST. + if (is_decl_in_contract(cur_decl, decl)) + return true; + } + return false; + } + virtual bool VisitDecl(clang::Decl* decl) { if (const auto* d = dyn_cast(decl)) { bool is_singleton = d->getName() == "singleton"; - if (d->getName() == "multi_index" || is_singleton) { + if ((d->getName() == "multi_index" || is_singleton) && defined_in_contract(d)) { ag.add_table(d->getTemplateArgs()[0].getAsIntegral().getExtValue(), d->getTemplateArgs()[1].getAsType().getTypePtr()->getAsCXXRecordDecl(), is_singleton); } From 482de2c29ed9977ce92d66dcfef5853f441f1ba1 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 17 Jul 2023 23:41:06 -0400 Subject: [PATCH 029/158] singleton tests extension --- tests/toolchain/abigen-pass/singleton_contract.abi | 14 ++++++++++++++ tests/toolchain/abigen-pass/singleton_contract.cpp | 2 ++ 2 files changed, 16 insertions(+) diff --git a/tests/toolchain/abigen-pass/singleton_contract.abi b/tests/toolchain/abigen-pass/singleton_contract.abi index ade07a444d..6cdc4a8c72 100644 --- a/tests/toolchain/abigen-pass/singleton_contract.abi +++ b/tests/toolchain/abigen-pass/singleton_contract.abi @@ -38,6 +38,20 @@ "key_names": [], "key_types": [] }, + { + "name": "config55", + "type": "out_of_class", + "index_type": "i64", + "key_names": [], + "key_types": [] + }, + { + "name": "smpl.conf5", + "type": "name", + "index_type": "i64", + "key_names": [], + "key_types": [] + }, { "name": "smpl.config", "type": "name", diff --git a/tests/toolchain/abigen-pass/singleton_contract.cpp b/tests/toolchain/abigen-pass/singleton_contract.cpp index 0a27c52aa4..f1999c27bc 100644 --- a/tests/toolchain/abigen-pass/singleton_contract.cpp +++ b/tests/toolchain/abigen-pass/singleton_contract.cpp @@ -37,4 +37,6 @@ class [[eosio::contract("singleton_contract")]] singleton_contract : public cont typedef eosio::singleton<"config"_n, tbl_config> config; typedef eosio::singleton<"smpl.config"_n, name> smpl_config; + using smpl_config2 = smpl_config5; + typedef config551 config2; //from exclude_from_abi.hpp }; From 134a7cd14194449957941dad2d63ad57ccbf3d43 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Tue, 18 Jul 2023 00:37:50 -0400 Subject: [PATCH 030/158] update cicd ubuntu 20.04->20.10 --- .cicd/platforms/ubuntu22.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cicd/platforms/ubuntu22.Dockerfile b/.cicd/platforms/ubuntu22.Dockerfile index ca38aad5cd..9b510d36c8 100644 --- a/.cicd/platforms/ubuntu22.Dockerfile +++ b/.cicd/platforms/ubuntu22.Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:jammy +FROM ubuntu:kinetic RUN apt-get update && apt-get upgrade -y && \ DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential \ From 0a747a936d262573dc1ca2fef750cda62a51ff22 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Tue, 18 Jul 2023 22:22:42 -0400 Subject: [PATCH 031/158] try to fix crash in tests --- tools/include/eosio/abigen.hpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 77c301ae09..ba0062f5f8 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -861,7 +861,7 @@ namespace eosio { namespace cdt { if (const auto* decl_ctx = llvm::dyn_cast(cur_decl)) { // The Decl is also a DeclContext, so it might contain other Decls. for (const auto* child_decl : decl_ctx->decls()) { - if(is_decl_in_contract(child_decl, decl)) + if(child_decl && is_decl_in_contract(child_decl, decl)) return true; } } @@ -875,14 +875,15 @@ namespace eosio { namespace cdt { contract_class = find_contract_class(decl); if (!contract_class) CDT_ERROR("abigen_error", decl->getLocation(), "contract class not found"); - const auto* translation_unit = decl->getASTContext().getTranslationUnitDecl(); - - for (const auto* cur_decl : translation_unit->decls()) { - // Recursively traverse the AST. - if (is_decl_in_contract(cur_decl, decl)) - return true; - } - return false; + return aliased_in_contract(decl); + // const auto* translation_unit = decl->getASTContext().getTranslationUnitDecl(); + + // for (const auto* cur_decl : translation_unit->decls()) { + // // Recursively traverse the AST. + // if (is_decl_in_contract(cur_decl, decl)) + // return true; + // } + // return false; } virtual bool VisitDecl(clang::Decl* decl) { From e644e75d3ec58cb1a93558ea8352d22edc2a9bf6 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 19 Jul 2023 13:17:59 -0400 Subject: [PATCH 032/158] trying to fix build seg fault --- tools/include/eosio/abigen.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index ba0062f5f8..904836b2fc 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -845,9 +845,11 @@ namespace eosio { namespace cdt { bool aliased_in_contract(const clang::Decl* decl) const { for (const auto* contract_decl : contract_class->decls()) { if (const auto* cur_contract_decl = llvm::dyn_cast(contract_decl)) { - if (const auto* cur_contract_rec = cur_contract_decl->getUnderlyingType()->getAsCXXRecordDecl()) { - if (cur_contract_rec == decl) - return true; + if (const auto* cur_type = cur_contract_decl->getUnderlyingType().getTypePtrOrNull()) { + if (const auto* cur_contract_rec = cur_type->getAsCXXRecordDecl()) { + if (cur_contract_rec == decl) + return true; + } } } } From 9e3d4a0a72a3a9d7eced5ea1f6e06ebf65d4dd2d Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 19 Jul 2023 13:38:52 -0400 Subject: [PATCH 033/158] more checks to deal with seg fault --- tools/include/eosio/abigen.hpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 904836b2fc..79d54fba5a 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -829,13 +829,17 @@ namespace eosio { namespace cdt { } bool is_same_type(const clang::Decl* decl1, const clang::CXXRecordDecl* decl2) const { + if (!decl1 || !decl2) + return false; if (decl1 == decl2) return true; - if (const clang::TypedefNameDecl* typedef_decl = llvm::dyn_cast(decl1)) { - if (const auto* cxx_rec1 = typedef_decl->getUnderlyingType()->getAsCXXRecordDecl()) { - if (cxx_rec1 == decl2) - return true; + if (const clang::TypedefNameDecl* typedef_decl = llvm::dyn_cast_or_null(decl1)) { + if (const auto* cur_type = typedef_decl->getUnderlyingType().getTypePtrOrNull()) { + if (const auto* cxx_rec1 = cur_type->getAsCXXRecordDecl()) { + if (cxx_rec1 == decl2) + return true; + } } } @@ -844,7 +848,7 @@ namespace eosio { namespace cdt { bool aliased_in_contract(const clang::Decl* decl) const { for (const auto* contract_decl : contract_class->decls()) { - if (const auto* cur_contract_decl = llvm::dyn_cast(contract_decl)) { + if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(contract_decl)) { if (const auto* cur_type = cur_contract_decl->getUnderlyingType().getTypePtrOrNull()) { if (const auto* cur_contract_rec = cur_type->getAsCXXRecordDecl()) { if (cur_contract_rec == decl) From c1eca818a9cd9e0ef96cd1600e014ea354ce9f04 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 19 Jul 2023 17:10:22 -0400 Subject: [PATCH 034/158] fix build seg fault --- tools/include/eosio/abigen.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 79d54fba5a..976b19d6ac 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -847,8 +847,8 @@ namespace eosio { namespace cdt { } bool aliased_in_contract(const clang::Decl* decl) const { - for (const auto* contract_decl : contract_class->decls()) { - if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(contract_decl)) { + for (const auto decl_iter : contract_class->decls()) { + if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(&*decl_iter)) { if (const auto* cur_type = cur_contract_decl->getUnderlyingType().getTypePtrOrNull()) { if (const auto* cur_contract_rec = cur_type->getAsCXXRecordDecl()) { if (cur_contract_rec == decl) From b2d6f0b18d7648e19b1ed26697a0c55f8f7bac58 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 19 Jul 2023 21:22:18 -0400 Subject: [PATCH 035/158] trying to fix seg fault --- tools/include/eosio/abigen.hpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 976b19d6ac..5667f524a1 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -848,11 +848,13 @@ namespace eosio { namespace cdt { bool aliased_in_contract(const clang::Decl* decl) const { for (const auto decl_iter : contract_class->decls()) { - if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(&*decl_iter)) { - if (const auto* cur_type = cur_contract_decl->getUnderlyingType().getTypePtrOrNull()) { - if (const auto* cur_contract_rec = cur_type->getAsCXXRecordDecl()) { - if (cur_contract_rec == decl) - return true; + if (llvm::isa(&*decl_iter)) { + if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(&*decl_iter)) { + if (const auto* cur_type = cur_contract_decl->getUnderlyingType().getTypePtrOrNull()) { + if (const auto* cur_contract_rec = cur_type->getAsCXXRecordDecl()) { + if (cur_contract_rec == decl) + return true; + } } } } From dc8d62c5c74c01da6d83f75ebaa7c7b9c2b6cafc Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 19 Jul 2023 22:29:40 -0400 Subject: [PATCH 036/158] trying to fix seg fault + logs --- tools/include/eosio/abigen.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 5667f524a1..b54198e51b 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -848,8 +848,11 @@ namespace eosio { namespace cdt { bool aliased_in_contract(const clang::Decl* decl) const { for (const auto decl_iter : contract_class->decls()) { - if (llvm::isa(&*decl_iter)) { - if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(&*decl_iter)) { + const clang::Decl* decl_ptr = &*decl_iter; + llvm::errs() << "before llvm::isa(decl_ptr)\n"; + if (decl_ptr && llvm::isa(decl_ptr)) { + llvm::errs() << "after llvm::isa(decl_ptr)\n"; + if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(decl_ptr)) { if (const auto* cur_type = cur_contract_decl->getUnderlyingType().getTypePtrOrNull()) { if (const auto* cur_contract_rec = cur_type->getAsCXXRecordDecl()) { if (cur_contract_rec == decl) From 208274a8f6b0cc3d002970a475f3bc31b4a635f9 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Fri, 21 Jul 2023 11:13:26 -0400 Subject: [PATCH 037/158] fixing seg fault in release build --- tools/include/eosio/abigen.hpp | 79 +++++++++------------------------- 1 file changed, 21 insertions(+), 58 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index b54198e51b..2db8a4abf2 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -801,10 +801,10 @@ namespace eosio { namespace cdt { return true; } - const clang::CXXRecordDecl* find_contract_class(const clang::Decl* decl) const { - const auto* translation_unit = decl->getASTContext().getTranslationUnitDecl(); - - for (clang::Decl* cur_decl : translation_unit->decls()) { + const clang::CXXRecordDecl* find_contract_class(const clang::ASTContext &ctx) const { + const auto* translation_unit = ctx.getTranslationUnitDecl(); + // scanning entire translation unit to find contract class + for (const clang::Decl* cur_decl : translation_unit->decls()) { if (const auto* cxx_decl = llvm::dyn_cast(cur_decl)) { if (cxx_decl->isEosioContract()) { @@ -814,12 +814,12 @@ namespace eosio { namespace cdt { return cxx_decl; } else { - auto pd = llvm::dyn_cast(cxx_decl->getParent()); - if (pd && pd->isEosioContract()) { - auto attr_name = pd->getEosioContractAttr()->getName().str(); - auto name = attr_name.empty() ? pd->getName().str() : attr_name; + const auto* parent_decl = llvm::dyn_cast(cxx_decl->getParent()); + if (parent_decl && parent_decl->isEosioContract()) { + auto attr_name = parent_decl->getEosioContractAttr()->getName().str(); + auto name = attr_name.empty() ? parent_decl->getName().str() : attr_name; if (name == ag.get_contract_name()) - return pd; + return parent_decl; } } } @@ -834,10 +834,10 @@ namespace eosio { namespace cdt { if (decl1 == decl2) return true; - if (const clang::TypedefNameDecl* typedef_decl = llvm::dyn_cast_or_null(decl1)) { + // checking if declaration is a typedef or using + if (const clang::TypedefNameDecl* typedef_decl = llvm::dyn_cast(decl1)) { if (const auto* cur_type = typedef_decl->getUnderlyingType().getTypePtrOrNull()) { - if (const auto* cxx_rec1 = cur_type->getAsCXXRecordDecl()) { - if (cxx_rec1 == decl2) + if (decl2 == cur_type->getAsCXXRecordDecl()) { return true; } } @@ -845,56 +845,19 @@ namespace eosio { namespace cdt { return false; } + + bool defined_in_contract(const clang::ClassTemplateSpecializationDecl* decl) { - bool aliased_in_contract(const clang::Decl* decl) const { - for (const auto decl_iter : contract_class->decls()) { - const clang::Decl* decl_ptr = &*decl_iter; - llvm::errs() << "before llvm::isa(decl_ptr)\n"; - if (decl_ptr && llvm::isa(decl_ptr)) { - llvm::errs() << "after llvm::isa(decl_ptr)\n"; - if (const auto* cur_contract_decl = llvm::dyn_cast_or_null(decl_ptr)) { - if (const auto* cur_type = cur_contract_decl->getUnderlyingType().getTypePtrOrNull()) { - if (const auto* cur_contract_rec = cur_type->getAsCXXRecordDecl()) { - if (cur_contract_rec == decl) - return true; - } - } - } - } + if (!contract_class) { + contract_class = find_contract_class(decl->getASTContext()); + if (!contract_class) + CDT_ERROR("abigen_error", decl->getLocation(), "contract class not found"); } - } - - // recursively check all declarations from declaration context if any of them is equal to decl - bool is_decl_in_contract(const clang::Decl* cur_decl, const clang::CXXRecordDecl* decl) { - if (is_same_type(cur_decl, decl) && aliased_in_contract(decl)) - return true; - if (const auto* decl_ctx = llvm::dyn_cast(cur_decl)) { - // The Decl is also a DeclContext, so it might contain other Decls. - for (const auto* child_decl : decl_ctx->decls()) { - if(child_decl && is_decl_in_contract(child_decl, decl)) - return true; - } + for (const clang::Decl* cur_decl : contract_class->decls()) { + if (is_same_type(cur_decl, decl)) + return true; } - - return false; - } - - bool defined_in_contract(const clang::ClassTemplateSpecializationDecl* decl) { - - if (!contract_class) - contract_class = find_contract_class(decl); - if (!contract_class) CDT_ERROR("abigen_error", decl->getLocation(), "contract class not found"); - - return aliased_in_contract(decl); - // const auto* translation_unit = decl->getASTContext().getTranslationUnitDecl(); - - // for (const auto* cur_decl : translation_unit->decls()) { - // // Recursively traverse the AST. - // if (is_decl_in_contract(cur_decl, decl)) - // return true; - // } - // return false; } virtual bool VisitDecl(clang::Decl* decl) { From 19749dd297ad8ef6b3e363a54737ed57d37f404f Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Fri, 21 Jul 2023 12:07:32 -0400 Subject: [PATCH 038/158] seg fault fix --- tools/include/eosio/abigen.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 2db8a4abf2..228441625d 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -825,7 +825,7 @@ namespace eosio { namespace cdt { } } - return NULL; + return nullptr; } bool is_same_type(const clang::Decl* decl1, const clang::CXXRecordDecl* decl2) const { @@ -858,6 +858,8 @@ namespace eosio { namespace cdt { if (is_same_type(cur_decl, decl)) return true; } + + return false; } virtual bool VisitDecl(clang::Decl* decl) { From 73662c240a1f16478ff66077de8841ed828f9383 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 24 Jul 2023 14:52:59 -0400 Subject: [PATCH 039/158] cicd revert ubuntu 22 to LTS --- .cicd/platforms/ubuntu22.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cicd/platforms/ubuntu22.Dockerfile b/.cicd/platforms/ubuntu22.Dockerfile index 9b510d36c8..ca38aad5cd 100644 --- a/.cicd/platforms/ubuntu22.Dockerfile +++ b/.cicd/platforms/ubuntu22.Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:kinetic +FROM ubuntu:jammy RUN apt-get update && apt-get upgrade -y && \ DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential \ From 6c78f0ae61c29c311440415583c994ae440e0900 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 26 Jul 2023 17:26:59 -0400 Subject: [PATCH 040/158] change error macro to throw error --- tools/include/eosio/abigen.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 228441625d..b7710107ad 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -851,7 +851,7 @@ namespace eosio { namespace cdt { if (!contract_class) { contract_class = find_contract_class(decl->getASTContext()); if (!contract_class) - CDT_ERROR("abigen_error", decl->getLocation(), "contract class not found"); + CDT_INTERNAL_ERROR("contract class not found"); } for (const clang::Decl* cur_decl : contract_class->decls()) { From 34ad214294173d9806a60a9b0b5bc72d3dd28c08 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 27 Jul 2023 13:17:17 -0400 Subject: [PATCH 041/158] fix CDT_INTERNAL_ERROR macro --- tools/include/eosio/error_emitter.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/include/eosio/error_emitter.hpp b/tools/include/eosio/error_emitter.hpp index 70e4157b3e..0b2de429dd 100644 --- a/tools/include/eosio/error_emitter.hpp +++ b/tools/include/eosio/error_emitter.hpp @@ -61,6 +61,8 @@ namespace eosio { namespace cdt { #define CDT_ERROR(e, l, s) \ get_error_emitter().emit_error(l, get_error_emitter().diags.get(e), s); -#define CDT_INTERNAL_ERROR(s) \ - std::cerr << s << "\n"; \ - throw internal_error_ex; +#define CDT_INTERNAL_ERROR(s) \ + do { \ + std::cerr << s << "\n"; \ + throw internal_error_ex; \ + } while (false) From 8f467812bc413bc89fc6d5e5ccee4c1a5abd24d7 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 27 Jul 2023 15:21:45 -0400 Subject: [PATCH 042/158] #201 reivew concerns addressed --- tools/include/eosio/abigen.hpp | 15 +++------------ tools/include/eosio/gen.hpp | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index b7710107ad..e05ff9faff 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -808,20 +808,11 @@ namespace eosio { namespace cdt { if (const auto* cxx_decl = llvm::dyn_cast(cur_decl)) { if (cxx_decl->isEosioContract()) { - auto attr_name = cxx_decl->getEosioContractAttr()->getName().str(); - auto name = attr_name.empty() ? cxx_decl->getName().str() : attr_name; - if (name == ag.get_contract_name()) + auto attr_name = cxx_decl->getEosioContractAttr()->getName(); + auto name = attr_name.empty() ? cxx_decl->getName() : attr_name; + if (name == llvm::StringRef(ag.get_contract_name())) return cxx_decl; } - else { - const auto* parent_decl = llvm::dyn_cast(cxx_decl->getParent()); - if (parent_decl && parent_decl->isEosioContract()) { - auto attr_name = parent_decl->getEosioContractAttr()->getName().str(); - auto name = attr_name.empty() ? parent_decl->getName().str() : attr_name; - if (name == ag.get_contract_name()) - return parent_decl; - } - } } } diff --git a/tools/include/eosio/gen.hpp b/tools/include/eosio/gen.hpp index 8e2494d428..3637e74fea 100644 --- a/tools/include/eosio/gen.hpp +++ b/tools/include/eosio/gen.hpp @@ -155,7 +155,7 @@ struct generation_utils { inline void set_contract_name( const std::string& cn ) { contract_name = cn; } - inline std::string get_contract_name()const { return contract_name; } + inline const std::string& get_contract_name()const { return contract_name; } static inline std::string get_parsed_contract_name() { return parsed_contract_name; } inline void set_resource_dirs( const std::vector& rd ) { llvm::SmallString<128> cwd; @@ -274,30 +274,30 @@ struct generation_utils { } static inline bool is_eosio_contract( const clang::CXXMethodDecl* decl, const std::string& cn ) { - std::string name = ""; + llvm::StringRef name; if (decl->isEosioContract()) name = decl->getEosioContractAttr()->getName(); else if (decl->getParent()->isEosioContract()) name = decl->getParent()->getEosioContractAttr()->getName(); if (name.empty()) { - name = decl->getParent()->getName().str(); + name = decl->getParent()->getName(); } - parsed_contract_name = name; + parsed_contract_name = name.str(); return cn == parsed_contract_name; } static inline bool is_eosio_contract( const clang::CXXRecordDecl* decl, const std::string& cn ) { - std::string name = ""; + llvm::StringRef name; auto pd = llvm::dyn_cast(decl->getParent()); if (decl->isEosioContract()) { - auto nm = decl->getEosioContractAttr()->getName().str(); - name = nm.empty() ? decl->getName().str() : nm; + auto nm = decl->getEosioContractAttr()->getName(); + name = nm.empty() ? decl->getName() : nm; } else if (pd && pd->isEosioContract()) { - auto nm = pd->getEosioContractAttr()->getName().str(); - name = nm.empty() ? pd->getName().str() : nm; + auto nm = pd->getEosioContractAttr()->getName(); + name = nm.empty() ? pd->getName() : nm; } - parsed_contract_name = name; + parsed_contract_name = name.str(); return cn == parsed_contract_name; } From 6e98fda59f02d209d9a99bd07f96e2413b6d2039 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 27 Jul 2023 16:07:02 -0400 Subject: [PATCH 043/158] error->warning if contract class is not found + refactor --- tools/include/eosio/abigen.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index e05ff9faff..9d83babcd2 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -841,8 +841,11 @@ namespace eosio { namespace cdt { if (!contract_class) { contract_class = find_contract_class(decl->getASTContext()); - if (!contract_class) - CDT_INTERNAL_ERROR("contract class not found"); + if (!contract_class) { + // currently this is unreachable as we do not traverse non-main file translation units + CDT_WARN("codegen_warning", decl->getLocation(), "contract class not found"); + return false; + } } for (const clang::Decl* cur_decl : contract_class->decls()) { @@ -880,7 +883,6 @@ namespace eosio { namespace cdt { auto& f_mgr = src_mgr.getFileManager(); auto main_fe = f_mgr.getFile(main_file); if (main_fe) { - auto fid = src_mgr.getOrCreateFileID(f_mgr.getFile(main_file), SrcMgr::CharacteristicKind::C_User); visitor->TraverseDecl(Context.getTranslationUnitDecl()); } } From b2386d9ff36468fc46ead51ebb4e17d3f9335b5c Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 27 Jul 2023 17:23:28 -0400 Subject: [PATCH 044/158] empty commit to address gh issue From 3777db32129ab2a855eb9b5e124e68d156d7cf86 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Thu, 10 Aug 2023 17:32:23 -0700 Subject: [PATCH 045/158] init roadmap --- docs/roadmap.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 docs/roadmap.md diff --git a/docs/roadmap.md b/docs/roadmap.md new file mode 100644 index 0000000000..f1f9a4ec38 --- /dev/null +++ b/docs/roadmap.md @@ -0,0 +1,17 @@ +# Roadmap for CDT + +## Summary Milestone 5 +Top priority is clean-up of the code and optimizing for support. +- Remove AntlerProj repos from build and archive AntlerProj +- Vanilla Clang/LLVM +- Upgrade to llvm 16 + + +## Antler +We lack the reasources to complete the Antler project and support it going forward. Removing dependancies will simplify the build process and simplify things. + +## Vanilla Clang/LLVM +Remove the custom extentions to Clang/LLVM. This will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. + +## Upgrade to LLVM 16 +Upgrade to the latest From 9a1164b48795487715bdb37116623a5df2755a78 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 21 Aug 2023 17:48:15 -0400 Subject: [PATCH 046/158] leap: 4.0 -> main --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index fd75357002..c21e18c3da 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -74,7 +74,7 @@ jobs: owner: AntelopeIO repo: leap file: 'leap-dev.*(x86_64|amd64).deb' - target: 4.0 + target: main artifact-name: leap-dev-ubuntu20-amd64 container-package: experimental-binaries token: ${{github.token}} From 8160de1e9aa298b24aff59d4c22cf90271da6bac Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Tue, 22 Aug 2023 15:53:43 -0700 Subject: [PATCH 047/158] remove ubuntu18 from builds --- .github/workflows/build.yaml | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index fd75357002..daf1174ebc 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -60,7 +60,7 @@ jobs: strategy: fail-fast: false matrix: - platform: [ubuntu18, ubuntu20, ubuntu22, ubuntu22-llvm] + platform: [ubuntu20, ubuntu22, ubuntu22-llvm] runs-on: ["self-hosted", "enf-x86-beefy"] container: ${{fromJSON(needs.d.outputs.p)[matrix.platform].image}} steps: @@ -92,14 +92,3 @@ jobs: make -j $(nproc) cd tests ctest -j $(nproc) --output-on-failure - - name: Package (Ubuntu 18 only) - if: matrix.platform == 'ubuntu18' - run: | - cd build/packages - bash generate_package.sh deb ubuntu amd64 - - name: Upload (Ubuntu 18 only) - if: matrix.platform == 'ubuntu18' - uses: actions/upload-artifact@v3 - with: - name: cdt_ubuntu_package_amd64 - path: build/packages/cdt*amd64.deb From c27e69e49ba5b24204a224b96419d116bdf7e58d Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Wed, 23 Aug 2023 14:22:08 -0700 Subject: [PATCH 048/158] added ubuntu20 artifact upload --- .cicd/platforms.json | 3 --- .github/workflows/build.yaml | 11 +++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.cicd/platforms.json b/.cicd/platforms.json index 69f7a69afb..c1528f30f0 100644 --- a/.cicd/platforms.json +++ b/.cicd/platforms.json @@ -1,7 +1,4 @@ { - "ubuntu18": { - "dockerfile": ".cicd/platforms/ubuntu18.Dockerfile" - }, "ubuntu20": { "dockerfile": ".cicd/platforms/ubuntu20.Dockerfile" }, diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index daf1174ebc..ca219f647f 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -92,3 +92,14 @@ jobs: make -j $(nproc) cd tests ctest -j $(nproc) --output-on-failure + - name: Package (Ubuntu 20 only) + if: matrix.platform == 'ubuntu20' + run: | + cd build/packages + bash generate_package.sh deb ubuntu amd64 + - name: Upload (Ubuntu 20 only) + if: matrix.platform == 'ubuntu20' + uses: actions/upload-artifact@v3 + with: + name: cdt_ubuntu_package_amd64 + path: build/packages/cdt*amd64.deb From 1219fe772eeb07687ff8c1c4451ad7e9e1c96c3e Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Wed, 23 Aug 2023 15:25:50 -0700 Subject: [PATCH 049/158] removed ubuntu18 dockerfile --- .cicd/platforms/ubuntu18.Dockerfile | 31 ----------------------------- 1 file changed, 31 deletions(-) delete mode 100644 .cicd/platforms/ubuntu18.Dockerfile diff --git a/.cicd/platforms/ubuntu18.Dockerfile b/.cicd/platforms/ubuntu18.Dockerfile deleted file mode 100644 index 8f9c618be1..0000000000 --- a/.cicd/platforms/ubuntu18.Dockerfile +++ /dev/null @@ -1,31 +0,0 @@ -FROM ubuntu:bionic - -RUN apt-get update && apt-get upgrade -y && \ - DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential \ - g++-8 \ - curl \ - ninja-build \ - software-properties-common \ - zlib1g-dev \ - pkg-config \ - libboost-all-dev \ - libcurl4-gnutls-dev - -RUN curl -L https://cmake.org/files/v3.13/cmake-3.13.5.tar.gz | tar zx && \ - cd cmake-3.13.5 && \ - ./configure && \ - make -j$(nproc) install && \ - cd .. && \ - rm -rf cmake-3.13.5 - -RUN add-apt-repository ppa:git-core/ppa && apt update && apt install -y git - -RUN curl -L https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tgz | tar zx && \ - cd Python* && \ - ./configure --enable-optimizations --prefix=/usr && \ - make -j$(nproc) install && \ - cd .. && \ - rm -rf Python* - -ENV CC=gcc-8 -ENV CXX=g++-8 From 7b55d921777dd12b901728af18ab20d5058c17b5 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 24 Aug 2023 22:16:57 -0400 Subject: [PATCH 050/158] refactoring --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 101 ++++--- .../test_contracts/bls_primitives_tests.cpp | 253 +++++++----------- 2 files changed, 145 insertions(+), 209 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 1708f292ed..7a62b57b1c 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -43,126 +43,117 @@ namespace eosio { } } - using bls_scalar = uint8_t[32]; - using bls_fp = uint8_t[48]; - using bls_fp2 = bls_fp[2]; - using bls_g1 = uint8_t[144]; - using bls_g2 = uint8_t[288]; - using bls_gt = uint8_t[576]; - - int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) - { + using bls_scalar = std::array; + using bls_fp = std::array; + using bls_s = std::array; + using bls_fp2 = std::array; + using bls_g1 = std::array; + using bls_g2 = std::array; + using bls_gt = std::array; + + int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { return internal_use_do_not_use::bls_g1_add( - reinterpret_cast(op1), + op1.data(), sizeof(bls_g1), - reinterpret_cast(op2), + op2.data(), sizeof(bls_g1), - reinterpret_cast(res), + res.data(), sizeof(bls_g1) ); } - int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) - { + int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) { return internal_use_do_not_use::bls_g2_add( - reinterpret_cast(op1), + op1.data(), sizeof(bls_g2), - reinterpret_cast(op2), + op2.data(), sizeof(bls_g2), - reinterpret_cast(res), + res.data(), sizeof(bls_g2) ); } - int32_t bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) - { + int32_t bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) { return internal_use_do_not_use::bls_g1_mul( - reinterpret_cast(point), + point.data(), sizeof(bls_g1), - reinterpret_cast(scalar), + scalar.data(), sizeof(bls_scalar), - reinterpret_cast(res), + res.data(), sizeof(bls_g1) ); } - int32_t bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) - { + int32_t bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) { return internal_use_do_not_use::bls_g2_mul( - reinterpret_cast(point), + point.data(), sizeof(bls_g2), - reinterpret_cast(scalar), + scalar.data(), sizeof(bls_scalar), - reinterpret_cast(res), + res.data(), sizeof(bls_g2) ); } - int32_t bls_g1_exp(const bls_g1* points, const bls_scalar* scalars, const uint32_t num, bls_g1& res) - { + int32_t bls_g1_exp(const bls_g1 points[], const bls_scalar scalars[], const uint32_t num, bls_g1& res) { return internal_use_do_not_use::bls_g1_exp( - reinterpret_cast(points), + num ? points[0].data() : nullptr, num * sizeof(bls_g1), - reinterpret_cast(scalars), + num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar), num, - reinterpret_cast(res), + res.data(), sizeof(bls_g1) ); } - int32_t bls_g2_exp(const bls_g2* points, const bls_scalar* scalars, const uint32_t num, bls_g2& res) - { + int32_t bls_g2_exp(const bls_g2 points[], const bls_scalar scalars[], const uint32_t num, bls_g2& res) { return internal_use_do_not_use::bls_g2_exp( - reinterpret_cast(points), + num ? points[0].data() : nullptr, num * sizeof(bls_g2), - reinterpret_cast(scalars), + num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar), num, - reinterpret_cast(res), + res.data(), sizeof(bls_g2) ); } - int32_t bls_pairing(const bls_g1* g1_points, const bls_g2* g2_points, const uint32_t num, bls_gt& res) - { + int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { return internal_use_do_not_use::bls_pairing( - reinterpret_cast(g1_points), + num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1), - reinterpret_cast(g2_points), + num ? g2_points[0].data() : nullptr, num * sizeof(bls_g2), num, - reinterpret_cast(res), + res.data(), sizeof(bls_gt) ); } - int32_t bls_g1_map(const bls_fp& e, bls_g1& res) - { + int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { return internal_use_do_not_use::bls_g1_map( - reinterpret_cast(e), + e.data(), sizeof(bls_fp), - reinterpret_cast(res), + res.data(), sizeof(bls_g1) ); } - int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) - { + int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) { return internal_use_do_not_use::bls_g2_map( - reinterpret_cast(e), + e[0].data(), sizeof(bls_fp2), - reinterpret_cast(res), + res.data(), sizeof(bls_g2) ); } - int32_t bls_fp_mod(const uint8_t* s, bls_fp& res) - { + int32_t bls_fp_mod(const bls_s& s, bls_fp& res) { return internal_use_do_not_use::bls_fp_mod( - reinterpret_cast(s), - 64, - reinterpret_cast(res), + s.data(), + sizeof(bls_s), + res.data(), sizeof(bls_fp) ); } diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 35f244f509..0c2e51d29a 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -3,15 +3,14 @@ #include #include +#include + using namespace eosio; -namespace bls12_381 -{ -class sha256 -{ +namespace bls12_381 { +class sha256 { public: - sha256(): m_blocklen(0), m_bitlen(0) - { + sha256(): m_blocklen(0), m_bitlen(0) { m_state[0] = 0x6a09e667; m_state[1] = 0xbb67ae85; m_state[2] = 0x3c6ef372; @@ -21,13 +20,10 @@ class sha256 m_state[6] = 0x1f83d9ab; m_state[7] = 0x5be0cd19; } - void update(const uint8_t * data, size_t length) - { - for(size_t i = 0 ; i < length ; i++) - { + void update(const uint8_t * data, size_t length) { + for(size_t i = 0 ; i < length ; i++) { m_data[m_blocklen++] = data[i]; - if (m_blocklen == 64) - { + if (m_blocklen == 64) { transform(); // End of the block @@ -36,12 +32,13 @@ class sha256 } } } - void update(const std::string &data) - { - update(reinterpret_cast (data.c_str()), data.size()); + inline void update(const char* data, size_t length) { + update(reinterpret_cast(data), length); + } + inline void update(const std::string &data) { + update(reinterpret_cast(data.data()), data.size()); } - std::array digest() - { + std::array digest() { std::array hash; pad(); @@ -49,8 +46,7 @@ class sha256 return hash; } - void digest(uint8_t* dst) - { + void digest(uint8_t* dst) { std::array* phash = reinterpret_cast*>(dst); pad(); @@ -84,50 +80,40 @@ class sha256 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 }; - static uint32_t rotr(uint32_t x, uint32_t n) - { + static uint32_t rotr(uint32_t x, uint32_t n) { return (x >> n) | (x << (32 - n)); } - static uint32_t choose(uint32_t e, uint32_t f, uint32_t g) - { + static uint32_t choose(uint32_t e, uint32_t f, uint32_t g) { return (e & f) ^ (~e & g); } - static uint32_t majority(uint32_t a, uint32_t b, uint32_t c) - { + static uint32_t majority(uint32_t a, uint32_t b, uint32_t c) { return (a & (b | c)) | (b & c); } - static uint32_t sig0(uint32_t x) - { + static uint32_t sig0(uint32_t x) { return sha256::rotr(x, 7) ^ sha256::rotr(x, 18) ^ (x >> 3); } - static uint32_t sig1(uint32_t x) - { + static uint32_t sig1(uint32_t x) { return sha256::rotr(x, 17) ^ sha256::rotr(x, 19) ^ (x >> 10); } - void transform() - { + void transform() { uint32_t maj, xorA, ch, xorE, sum, newA, newE, m[64]; uint32_t state[8]; - for(uint8_t i = 0, j = 0; i < 16; i++, j += 4) - { + for(uint8_t i = 0, j = 0; i < 16; i++, j += 4) { // Split data in 32 bit blocks for the 16 first words m[i] = (m_data[j] << 24) | (m_data[j + 1] << 16) | (m_data[j + 2] << 8) | (m_data[j + 3]); } - for(uint8_t k = 16 ; k < 64; k++) - { + for(uint8_t k = 16 ; k < 64; k++) { // Remaining 48 blocks m[k] = sha256::sig1(m[k - 2]) + m[k - 7] + sha256::sig0(m[k - 15]) + m[k - 16]; } - for(uint8_t i = 0 ; i < 8 ; i++) - { + for(uint8_t i = 0 ; i < 8 ; i++) { state[i] = m_state[i]; } - for(uint8_t i = 0; i < 64; i++) - { + for(uint8_t i = 0; i < 64; i++) { maj = sha256::majority(state[0], state[1], state[2]); xorA = sha256::rotr(state[0], 2) ^ sha256::rotr(state[0], 13) ^ sha256::rotr(state[0], 22); @@ -149,24 +135,20 @@ class sha256 state[0] = newA; } - for(uint8_t i = 0 ; i < 8 ; i++) - { + for(uint8_t i = 0 ; i < 8 ; i++) { m_state[i] += state[i]; } } - void pad() - { + void pad() { uint64_t i = m_blocklen; uint8_t end = m_blocklen < 56 ? 56 : 64; m_data[i++] = 0x80; // Append a bit 1 - while(i < end) - { + while(i < end) { m_data[i++] = 0x00; // Pad with zeros } - if(m_blocklen >= 56) - { + if(m_blocklen >= 56) { transform(); memset(m_data, 0, 56); } @@ -183,14 +165,11 @@ class sha256 m_data[56] = m_bitlen >> 56; transform(); } - void revert(std::array& hash) - { + void revert(std::array& hash) { // SHA uses big endian byte ordering // Revert all bytes - for(uint8_t i = 0 ; i < 4 ; i++) - { - for(uint8_t j = 0 ; j < 8 ; j++) - { + for(uint8_t i = 0 ; i < 4 ; i++) { + for(uint8_t j = 0 ; j < 8 ; j++) { hash[i + (j * 4)] = (m_state[j] >> (24 - i * 8)) & 0x000000ff; } } @@ -203,118 +182,89 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ using contract::contract; [[eosio::action]] - void testg1add(const std::vector& op1, const std::vector& op2, const std::vector& res) - { - bls_g1 lhs, rhs, r; - memcpy(lhs, op1.data(), sizeof(bls_g1)); - memcpy(rhs, op1.data(), sizeof(bls_g1)); - bls_g1_add(lhs, rhs, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_add test failed"); + void testg1add(const std::vector& op1, const std::vector& op2, const std::vector& res) { + check(op1.size() == std::tuple_size::value, "wrong op1 size passed"); + check(op2.size() == std::tuple_size::value, "wrong op2 size passed"); + bls_g1 r; + bls_g1_add(reinterpret_cast(op1[0]), reinterpret_cast(op2[0]), r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_add test failed"); } [[eosio::action]] - void testg2add(const std::vector& op1, const std::vector& op2, const std::vector& res) - { - bls_g2 lhs, rhs, r; - memcpy(lhs, op1.data(), sizeof(bls_g2)); - memcpy(rhs, op1.data(), sizeof(bls_g2)); - bls_g2_add(lhs, rhs, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_add test failed"); + void testg2add(const std::vector& op1, const std::vector& op2, const std::vector& res) { + bls_g2 r; + bls_g2_add(reinterpret_cast(op1[0]), reinterpret_cast(op2[0]), r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_add test failed"); } [[eosio::action]] - void testg1mul(const std::vector& point, const std::vector& scalar, const std::vector& res) - { - bls_g1 p, r; - bls_scalar s; - memcpy(p, point.data(), sizeof(bls_g1)); - memcpy(s, scalar.data(), sizeof(bls_scalar)); - bls_g1_mul(p, s, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_mul test failed"); + void testg1mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { + bls_g1 r; + bls_g1_mul(reinterpret_cast(point[0]), reinterpret_cast(scalar[0]), r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_mul test failed"); } [[eosio::action]] - void testg2mul(const std::vector& point, const std::vector& scalar, const std::vector& res) - { - bls_g2 p, r; - bls_scalar s; - memcpy(p, point.data(), sizeof(bls_g2)); - memcpy(s, scalar.data(), sizeof(bls_scalar)); - bls_g2_mul(p, s, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_mul test failed"); + void testg2mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { + bls_g2 r; + bls_g2_mul(reinterpret_cast(point[0]), reinterpret_cast(scalar[0]), r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_mul test failed"); } [[eosio::action]] - void testg1exp(const std::vector& points, const std::vector& scalars, const std::vector& res) - { - check(points.size()/sizeof(bls_g1) == scalars.size()/sizeof(bls_scalar), "number of elements in points and scalars must be equal"); - uint32_t num = scalars.size()/sizeof(bls_scalar); - const bls_g1* pp = reinterpret_cast(points.data()); - const bls_scalar* ps = reinterpret_cast(scalars.data()); + void testg1exp(const std::vector& points, const std::vector& scalars, const std::vector& res) { + auto num = scalars.size()/sizeof(bls_scalar); + check(points.size()/sizeof(bls_g1) == num, "number of elements in points and scalars must be equal"); bls_g1 r; - bls_g1_exp(pp, ps, num, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_exp test failed"); + bls_g1_exp(reinterpret_cast(&points[0]), reinterpret_cast(&scalars[0]), num, r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_exp test failed"); } [[eosio::action]] - void testg2exp(const std::vector& points, const std::vector& scalars, const std::vector& res) - { - check(points.size()/sizeof(bls_g2) == scalars.size()/sizeof(bls_scalar), "number of elements in points and scalars must be equal"); - uint32_t num = scalars.size()/sizeof(bls_scalar); - const bls_g2* pp = reinterpret_cast(points.data()); - const bls_scalar* ps = reinterpret_cast(scalars.data()); + void testg2exp(const std::vector& points, const std::vector& scalars, const std::vector& res) { + auto num = scalars.size()/sizeof(bls_scalar); + check(points.size()/sizeof(bls_g2) == num, "number of elements in points and scalars must be equal"); bls_g2 r; - bls_g2_exp(pp, ps, num, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_exp test failed"); + bls_g2_exp(reinterpret_cast(&points[0]), reinterpret_cast(&scalars[0]), num, r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_exp test failed"); } [[eosio::action]] - void testpairing(const std::vector& g1_points, const std::vector& g2_points, const std::vector& res) - { - check(g1_points.size()/sizeof(bls_g1) == g2_points.size()/sizeof(bls_g2), "number of elements in g1_points and g2_points must be equal"); - uint32_t num = g1_points.size()/sizeof(bls_g1); - const bls_g1* pp1 = reinterpret_cast(g1_points.data()); - const bls_g2* pp2 = reinterpret_cast(g2_points.data()); + void testpairing(const std::vector& g1_points, const std::vector& g2_points, const std::vector& res) { + auto num = g2_points.size()/sizeof(bls_g2); + check(g1_points.size()/sizeof(bls_g1) == num, "number of elements in g1_points and g2_points must be equal"); bls_gt r; - bls_pairing(pp1, pp2, num, r); - check(0 == memcmp(r, res.data(), sizeof(bls_gt)), "bls_pairing test failed"); + bls_pairing(reinterpret_cast(&g1_points[0]), reinterpret_cast(&g2_points[0]), num, r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_pairing test failed"); } [[eosio::action]] - void testg1map(const std::vector& e, const std::vector& res) - { - bls_fp element; - memcpy(element, e.data(), sizeof(bls_fp)); + void testg1map(const std::vector& e, const std::vector& res) { bls_g1 r; - bls_g1_map(element, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g1)), "bls_g1_map test failed"); + bls_g1_map(reinterpret_cast(e[0]), r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_map test failed"); } [[eosio::action]] - void testg2map(const std::vector& e, const std::vector& res) - { - bls_fp2 element; - memcpy(element, e.data(), sizeof(bls_fp2)); + void testg2map(const std::vector& e, const std::vector& res) { bls_g2 r; - bls_g2_map(element, r); - check(0 == memcmp(r, res.data(), sizeof(bls_g2)), "bls_g2_map test failed"); + bls_g2_map(reinterpret_cast(e[0]), r); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_map test failed"); } // Construct an extensible-output function based on SHA256 void xmd_sh256( - uint8_t *buf, + char *buf, int buf_len, const uint8_t *in, int in_len, - const uint8_t *dst, + const char *dst, int dst_len - ) - { + ) { const unsigned int SHA256HashSize = 32; const unsigned int SHA256_Message_Block_Size = 64; const unsigned ell = (buf_len + SHA256HashSize - 1) / SHA256HashSize; - if (buf_len < 0 || ell > 255 || dst_len > 255) - { + if (buf_len < 0 || ell > 255 || dst_len > 255) { return; } const uint8_t Z_pad[SHA256_Message_Block_Size] = { 0, }; @@ -334,10 +284,8 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ sha.update(dstlen_str, 1); sha.digest(b_0); uint8_t b_i[SHA256HashSize + 1] = { 0, }; - for (unsigned i = 1; i <= ell; ++i) - { - for (unsigned j = 0; j < SHA256HashSize; ++j) - { + for (unsigned i = 1; i <= ell; ++i) { + for (unsigned j = 0; j < SHA256HashSize; ++j) { b_i[j] = b_0[j] ^ b_i[j]; } b_i[SHA256HashSize] = i; @@ -352,11 +300,9 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ } } - std::array scalar_fromBytesBE(const std::array& in) - { + bls_s scalar_fromBE(const bls_s& in) { std::array out; - for(uint64_t i = 0; i < 8; i++) - { + for(uint64_t i = 0; i < 8; i++) { int64_t a = 8*8 - i*8; out[i] = static_cast(in[a-1]) | static_cast(in[a-2]) << 8 | @@ -364,29 +310,29 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ static_cast(in[a-5]) << 32 | static_cast(in[a-6]) << 40 | static_cast(in[a-7]) << 48 | static_cast(in[a-8]) << 56; } - return out; + return reinterpret_cast(std::move(out)); } - void g2_fromMessage(const std::vector& msg, const std::string& dst, bls_g2& res) - { - uint8_t buf[4 * 64]; - xmd_sh256(buf, 4 * 64, msg.data(), msg.size(), reinterpret_cast(dst.c_str()), dst.length()); + void g2_fromMessage(const std::vector& msg, const std::string& dst, bls_g2& res) { + + std::array buf; + xmd_sh256(buf.data()->data(), sizeof(buf), msg.data(), msg.size(), dst.data(), dst.length()); - std::array k; + bls_s k; bls_fp2 t; bls_g2 p, q; - k = scalar_fromBytesBE(*reinterpret_cast*>(buf)); - bls_fp_mod(reinterpret_cast(&k[0]), t[0]); - k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 64)); - bls_fp_mod(reinterpret_cast(&k[0]), t[1]); + k = scalar_fromBE(buf[0]); + bls_fp_mod(k, t[0]); + k = scalar_fromBE(buf[1]); + bls_fp_mod(k, t[1]); bls_g2_map(t, p); - k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 2*64)); - bls_fp_mod(reinterpret_cast(&k[0]), t[0]); - k = scalar_fromBytesBE(*reinterpret_cast*>(buf + 3*64)); - bls_fp_mod(reinterpret_cast(&k[0]), t[1]); + k = scalar_fromBE(buf[2]); + bls_fp_mod(k, t[0]); + k = scalar_fromBE(buf[3]); + bls_fp_mod(k, t[1]); bls_g2_map(t, q); bls_g2_add(p, q, res); @@ -399,21 +345,20 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; [[eosio::action]] - void verify(const std::vector& pk, const std::vector& sig) - { + void verify(const std::vector& pk, const std::vector& sig) { bls_g1 g1_points[2]; bls_g2 g2_points[2]; - memcpy(&g1_points[0], G1_ONE_NEG.data(), sizeof(bls_g1)); - memcpy(&g2_points[0], sig.data(), sizeof(bls_g2)); + memcpy(g1_points[0].data(), G1_ONE_NEG.data(), sizeof(bls_g1)); + memcpy(g2_points[0].data(), sig.data(), sizeof(bls_g2)); bls_g2 p_msg; g2_fromMessage(msg, CIPHERSUITE_ID, p_msg); - memcpy(&g1_points[1], pk.data(), sizeof(bls_g1)); - memcpy(&g2_points[1], p_msg, sizeof(bls_g2)); + memcpy(g1_points[1].data(), pk.data(), sizeof(bls_g1)); + memcpy(g2_points[1].data(), p_msg.data(), sizeof(bls_g2)); bls_gt r; bls_pairing(g1_points, g2_points, 2, r); - check(0 == memcmp(r, GT_ONE.data(), sizeof(bls_gt)), "bls signature verify failed"); + check(0 == memcmp(r.data(), GT_ONE.data(), sizeof(bls_gt)), "bls signature verify failed"); } }; From 0f0a02c314d15a5bced2a88c61194ab5ffae119e Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 24 Aug 2023 23:13:34 -0400 Subject: [PATCH 051/158] unit tests fix --- tests/unit/test_contracts/bls_primitives_tests.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 0c2e51d29a..70af198837 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -303,12 +303,9 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ bls_s scalar_fromBE(const bls_s& in) { std::array out; for(uint64_t i = 0; i < 8; i++) { - int64_t a = 8*8 - i*8; - out[i] = - static_cast(in[a-1]) | static_cast(in[a-2]) << 8 | - static_cast(in[a-3]) << 16 | static_cast(in[a-4]) << 24 | - static_cast(in[a-5]) << 32 | static_cast(in[a-6]) << 40 | - static_cast(in[a-7]) << 48 | static_cast(in[a-8]) << 56; + uint64_t temp; + memcpy(&temp, &in[in.size() - i*8 - 8], sizeof(uint64_t)); + out[i] = htobe64(temp); } return reinterpret_cast(std::move(out)); } From 14351e0d84607c248dc2c88b25c0db02ce771b7a Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Fri, 25 Aug 2023 15:58:48 -0400 Subject: [PATCH 052/158] #211 concerns addressed --- .../test_contracts/bls_primitives_tests.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 70af198837..1b97e8a338 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -186,28 +186,28 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ check(op1.size() == std::tuple_size::value, "wrong op1 size passed"); check(op2.size() == std::tuple_size::value, "wrong op2 size passed"); bls_g1 r; - bls_g1_add(reinterpret_cast(op1[0]), reinterpret_cast(op2[0]), r); + bls_g1_add(*reinterpret_cast(op1.data()), *reinterpret_cast(op2.data()), r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_add test failed"); } [[eosio::action]] void testg2add(const std::vector& op1, const std::vector& op2, const std::vector& res) { bls_g2 r; - bls_g2_add(reinterpret_cast(op1[0]), reinterpret_cast(op2[0]), r); + bls_g2_add(*reinterpret_cast(op1.data()), *reinterpret_cast(op2.data()), r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_add test failed"); } [[eosio::action]] void testg1mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { bls_g1 r; - bls_g1_mul(reinterpret_cast(point[0]), reinterpret_cast(scalar[0]), r); + bls_g1_mul(*reinterpret_cast(point.data()), *reinterpret_cast(scalar.data()), r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_mul test failed"); } [[eosio::action]] void testg2mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { bls_g2 r; - bls_g2_mul(reinterpret_cast(point[0]), reinterpret_cast(scalar[0]), r); + bls_g2_mul(*reinterpret_cast(point.data()), *reinterpret_cast(scalar.data()), r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_mul test failed"); } @@ -216,7 +216,7 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ auto num = scalars.size()/sizeof(bls_scalar); check(points.size()/sizeof(bls_g1) == num, "number of elements in points and scalars must be equal"); bls_g1 r; - bls_g1_exp(reinterpret_cast(&points[0]), reinterpret_cast(&scalars[0]), num, r); + bls_g1_exp(reinterpret_cast(points.data()), reinterpret_cast(scalars.data()), num, r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_exp test failed"); } @@ -225,7 +225,7 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ auto num = scalars.size()/sizeof(bls_scalar); check(points.size()/sizeof(bls_g2) == num, "number of elements in points and scalars must be equal"); bls_g2 r; - bls_g2_exp(reinterpret_cast(&points[0]), reinterpret_cast(&scalars[0]), num, r); + bls_g2_exp(reinterpret_cast(points.data()), reinterpret_cast(scalars.data()), num, r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_exp test failed"); } @@ -234,21 +234,21 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ auto num = g2_points.size()/sizeof(bls_g2); check(g1_points.size()/sizeof(bls_g1) == num, "number of elements in g1_points and g2_points must be equal"); bls_gt r; - bls_pairing(reinterpret_cast(&g1_points[0]), reinterpret_cast(&g2_points[0]), num, r); + bls_pairing(reinterpret_cast(g1_points.data()), reinterpret_cast(g2_points.data()), num, r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_pairing test failed"); } [[eosio::action]] void testg1map(const std::vector& e, const std::vector& res) { bls_g1 r; - bls_g1_map(reinterpret_cast(e[0]), r); + bls_g1_map(*reinterpret_cast(e.data()), r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_map test failed"); } [[eosio::action]] void testg2map(const std::vector& e, const std::vector& res) { bls_g2 r; - bls_g2_map(reinterpret_cast(e[0]), r); + bls_g2_map(*reinterpret_cast(e.data()), r); check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_map test failed"); } From 2601d3e2a51c4a02cc94a5472182616accf11cbb Mon Sep 17 00:00:00 2001 From: Peter Oschwald Date: Tue, 5 Sep 2023 17:17:13 -0500 Subject: [PATCH 053/158] Make leap-dev download target configurable. Use defaults.json to specify default location/target to attempt to downlaod the leap-dev package from. Allow dynamic configuration through workflow_dispatch input in github. --- .cicd/defaults.json | 6 ++++++ .github/workflows/build.yaml | 42 +++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 .cicd/defaults.json diff --git a/.cicd/defaults.json b/.cicd/defaults.json new file mode 100644 index 0000000000..5aadcc5e12 --- /dev/null +++ b/.cicd/defaults.json @@ -0,0 +1,6 @@ +{ + "leap-dev":{ + "target":"main", + "prerelease":false + } + } \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 1ab9eedf7c..64a7e87710 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -7,6 +7,17 @@ on: - "release/*" pull_request: workflow_dispatch: + inputs: + override-leap-dev: + description: Override leap-dev target + type: string + override-leap-dev-prerelease: + type: choice + description: Override leap-dev prelease + options: + - default + - true + - false permissions: packages: write @@ -53,9 +64,33 @@ jobs: push: true tags: ${{fromJSON(needs.d.outputs.p)[matrix.platform].image}} file: ${{fromJSON(needs.d.outputs.p)[matrix.platform].dockerfile}} + + versions: + name: Determine Versions + runs-on: ubuntu-latest + outputs: + leap-dev-target: ${{steps.versions.outputs.leap-dev-target}} + leap-dev-prerelease: ${{steps.versions.outputs.leap-dev-prerelease}} + steps: + - name: Setup versions from input or defaults + id: versions + env: + GH_TOKEN: ${{github.token}} + run: | + DEFAULTS_JSON=$(curl -sSfL $(gh api https://api.github.com/repos/${{github.repository}}/contents/.cicd/defaults.json?ref=${{github.sha}} --jq .download_url)) + echo leap-dev-target=$(echo "$DEFAULTS_JSON" | jq -r '."leap-dev".target') >> $GITHUB_OUTPUT + echo leap-dev-prerelease=$(echo "$DEFAULTS_JSON" | jq -r '."leap-dev".prerelease') >> $GITHUB_OUTPUT + + if [[ "${{inputs.override-leap-dev}}" != "" ]]; then + echo leap-dev-target=${{inputs.override-leap-dev}} >> $GITHUB_OUTPUT + fi + if [[ "${{inputs.override-leap-dev-prerelease}}" == +(true|false) ]]; then + echo leap-dev-prerelease=${{inputs.override-leap-dev-prerelease}} >> $GITHUB_OUTPUT + fi + Build: name: Build & Test - needs: [d, build-platforms] + needs: [d, build-platforms, versions] if: always() && needs.d.result == 'success' && (needs.build-platforms.result == 'success' || needs.build-platforms.result == 'skipped') strategy: fail-fast: false @@ -69,12 +104,13 @@ jobs: submodules: recursive - name: Download leap-dev.deb (Ubuntu 20 only) if: matrix.platform == 'ubuntu20' - uses: AntelopeIO/asset-artifact-download-action@v2 + uses: AntelopeIO/asset-artifact-download-action@v3 with: owner: AntelopeIO repo: leap file: 'leap-dev.*(x86_64|amd64).deb' - target: main + target: '${{needs.versions.outputs.leap-dev-target}}' + prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} artifact-name: leap-dev-ubuntu20-amd64 container-package: experimental-binaries token: ${{github.token}} From 116c6ba28d57c5b5eb9705ebc1049e43ef525ac1 Mon Sep 17 00:00:00 2001 From: Peter Oschwald Date: Tue, 5 Sep 2023 18:49:39 -0500 Subject: [PATCH 054/158] Remove unneeded token with v3. --- .github/workflows/build.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 64a7e87710..7f70ff46a8 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -113,7 +113,6 @@ jobs: prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} artifact-name: leap-dev-ubuntu20-amd64 container-package: experimental-binaries - token: ${{github.token}} - name: Install leap-dev.deb (Ubuntu 20 only) if: matrix.platform == 'ubuntu20' run: | From 01d06ab6509056e0f1447af1f7a573fc6431d29f Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 6 Sep 2023 16:47:45 -0400 Subject: [PATCH 055/158] added set_finalizers host function --- libraries/eosiolib/capi/eosio/privileged.h | 10 ++ .../contracts/eosio/finalizer_set.hpp | 39 +++++ libraries/native/intrinsics.cpp | 154 +++++++++--------- .../native/native/eosio/intrinsics_def.hpp | 3 +- tests/integration/contracts.hpp.in | 3 + tests/integration/set_finalizers_tests.cpp | 79 +++++++++ .../compile-fail/host_functions_tests.cpp | 1 + tests/unit/test_contracts/CMakeLists.txt | 1 + tests/unit/test_contracts/capi/privileged.c | 1 + .../test_contracts/set_finalizers_tests.cpp | 12 ++ tools/include/eosio/gen.hpp | 3 +- 11 files changed, 224 insertions(+), 82 deletions(-) create mode 100644 libraries/eosiolib/contracts/eosio/finalizer_set.hpp create mode 100644 tests/integration/set_finalizers_tests.cpp create mode 100644 tests/unit/test_contracts/set_finalizers_tests.cpp diff --git a/libraries/eosiolib/capi/eosio/privileged.h b/libraries/eosiolib/capi/eosio/privileged.h index 8628c00769..7c909c6a12 100644 --- a/libraries/eosiolib/capi/eosio/privileged.h +++ b/libraries/eosiolib/capi/eosio/privileged.h @@ -123,6 +123,16 @@ void set_kv_parameters_packed( const char* data, uint32_t datalen ); __attribute__((eosio_wasm_import)) void preactivate_feature( const struct capi_checksum256* feature_digest ); +/** + * Submits a finalizer set change to Hotstuff + * + * @param data - pointer finalizer_set object packed as bytes + * @param len - size of packed finalazer_set object + * @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_set data + */ +__attribute__((eosio_wasm_import)) +void set_finalizers( const char* data, uint32_t len ); + #ifdef __cplusplus } #endif diff --git a/libraries/eosiolib/contracts/eosio/finalizer_set.hpp b/libraries/eosiolib/contracts/eosio/finalizer_set.hpp new file mode 100644 index 0000000000..a81b6c4aaf --- /dev/null +++ b/libraries/eosiolib/contracts/eosio/finalizer_set.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "../../capi/eosio/types.h" +#include "../../core/eosio/crypto_bls_ext.hpp" + +#include +#include +#include + +namespace eosio { + namespace internal_use_do_not_use { + extern "C" { + __attribute__((eosio_wasm_import)) + void set_finalizers( const char* data, uint32_t len ); + } // extern "C" + } //internal_use_do_not_use + + struct abi_finalizer_authority { + std::string description; + uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold + std::vector public_key_g1_jacobian; // Jacobian little endian + + EOSLIB_SERIALIZE(abi_finalizer_authority, (description)(fweight)(public_key_g1_jacobian)); + }; + struct abi_finalizer_set { + uint64_t fthreshold = 0; + std::vector finalizers; + + EOSLIB_SERIALIZE(abi_finalizer_set, (fthreshold)(finalizers)); + }; + + void set_finalizers( const abi_finalizer_set& fin_set ) { + for (const auto& finalizer : fin_set.finalizers) + eosio::check(finalizer.public_key_g1_jacobian.size() == sizeof(bls_g1), "public key has a wrong size" ); + auto packed = eosio::pack(fin_set); + internal_use_do_not_use::set_finalizers(packed.data(), packed.size()); + } + +} //eosio \ No newline at end of file diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index a1db008f82..9b758123de 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -906,83 +906,77 @@ extern "C" { return intrinsics::get().call(data, datalen); } -} - -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 intrinsics::get().call(rounds, state, state_len, msg, msg_len, t0_offset, t0_len, t1_offset, t1_len, final, result, result_len); -} - -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 intrinsics::get().call(sig, sig_len, dig, dig_len, pub, pub_len); -} - -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 intrinsics::get().call(op1, op1_len, op2, op2_len, result, result_len); -} - -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 intrinsics::get().call(g1, g1_len, scalar, scalar_len, result, result_len); -} - -int32_t alt_bn128_pair( const char* pairs, uint32_t pairs_len) { - return intrinsics::get().call(pairs, pairs_len); -} - -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 intrinsics::get().call(base, base_len, exp, exp_len, mod, mod_len, result, result_len); -} - -void sha3( const char* data, uint32_t data_len, char* hash, uint32_t hash_len, int32_t keccak ) { - intrinsics::get().call(data, data_len, hash, hash_len, keccak); -} - -int32_t bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) -{ - return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); -} - -int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) -{ - return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); -} - -int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) -{ - return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); -} - -int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) -{ - return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); -} - -int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) -{ - return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); -} - -int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) -{ - return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); -} - -int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len) -{ - return intrinsics::get().call(g1_points, g1_points_len, g1_points, g1_points_len, n, res, res_len); -} - -int32_t bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) -{ - return intrinsics::get().call(e, e_len, res, res_len); -} - -int32_t bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) -{ - return intrinsics::get().call(e, e_len, res, res_len); -} - -int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len) -{ - return intrinsics::get().call(s, s_len, res, res_len); -} + 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 intrinsics::get().call(rounds, state, state_len, msg, msg_len, t0_offset, t0_len, t1_offset, t1_len, final, result, result_len); + } + + 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 intrinsics::get().call(sig, sig_len, dig, dig_len, pub, pub_len); + } + + 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 intrinsics::get().call(op1, op1_len, op2, op2_len, result, result_len); + } + + 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 intrinsics::get().call(g1, g1_len, scalar, scalar_len, result, result_len); + } + + int32_t alt_bn128_pair( const char* pairs, uint32_t pairs_len) { + return intrinsics::get().call(pairs, pairs_len); + } + + 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 intrinsics::get().call(base, base_len, exp, exp_len, mod, mod_len, result, result_len); + } + + void sha3( const char* data, uint32_t data_len, char* hash, uint32_t hash_len, int32_t keccak ) { + intrinsics::get().call(data, data_len, hash, hash_len, keccak); + } + + int32_t bls_g1_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) { + return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); + } + + int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) { + return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); + } + + int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) { + return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); + } + + int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) { + return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); + } + + int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) { + return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); + } + + int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) { + return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); + } + + int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len) { + return intrinsics::get().call(g1_points, g1_points_len, g1_points, g1_points_len, n, res, res_len); + } + + int32_t bls_g1_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) { + return intrinsics::get().call(e, e_len, res, res_len); + } + + int32_t bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len) { + return intrinsics::get().call(e, e_len, res, res_len); + } + + int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len) { + return intrinsics::get().call(s, s_len, res, res_len); + } + + void set_finalizers(const char* data, uint32_t len) { + intrinsics::get().call(data, len); + } + +} // extern "C" diff --git a/libraries/native/native/eosio/intrinsics_def.hpp b/libraries/native/native/eosio/intrinsics_def.hpp index 7affd102e0..ca4d602c6a 100644 --- a/libraries/native/native/eosio/intrinsics_def.hpp +++ b/libraries/native/native/eosio/intrinsics_def.hpp @@ -183,7 +183,8 @@ intrinsic_macro(bls_g2_exp) \ intrinsic_macro(bls_pairing) \ intrinsic_macro(bls_g1_map) \ intrinsic_macro(bls_g2_map) \ -intrinsic_macro(bls_fp_mod) +intrinsic_macro(bls_fp_mod) \ +intrinsic_macro(set_finalizers) diff --git a/tests/integration/contracts.hpp.in b/tests/integration/contracts.hpp.in index 982b8b72f8..d8dc4cf017 100644 --- a/tests/integration/contracts.hpp.in +++ b/tests/integration/contracts.hpp.in @@ -25,6 +25,9 @@ namespace eosio::testing { static std::vector bls_primitives_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/bls_primitives_tests.wasm"); } static std::vector bls_primitives_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/bls_primitives_tests.abi"); } + + static std::vector set_finalizers_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/set_finalizers_tests.wasm"); } + static std::vector set_finalizers_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/set_finalizers_tests.abi"); } static std::vector get_code_hash_write_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.wasm"); } static std::vector get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); } diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp new file mode 100644 index 0000000000..48ca8a97e7 --- /dev/null +++ b/tests/integration/set_finalizers_tests.cpp @@ -0,0 +1,79 @@ +#include + +#include +#include +#include + +#include + +namespace eosio::chain { +struct finalizer_authority { + + std::string description; + uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold + std::array public_key; + +}; + +struct finalizer_set { + + uint32_t generation = 0; ///< sequentially incrementing version number + uint64_t fthreshold = 0; ///< vote fweight threshold to finalize blocks + std::vector finalizers; ///< Instant Finality voter set +}; + +} // eosio::chain + +using namespace eosio; +using namespace eosio::testing; +using namespace fc; + +using mvo = fc::mutable_variant_object; + +BOOST_AUTO_TEST_SUITE(set_finalizers_tests) + +BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { + create_accounts( { "test"_n } ); + produce_block(); + + set_code( config::system_account_name, contracts::set_finalizers_test_wasm() ); + set_abi( config::system_account_name, contracts::set_finalizers_test_abi().data() ); + + produce_blocks(); + + + BOOST_ASSERT( control->get_finalizers().finalizers.empty() ); + + const std::vector G1 = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; + // fin_set should be the following struct: + // struct abi_finalizer_authority { + // std::string description; + // uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold + // std::vector public_key_g1_jacobian; // Jacobian little endian + // }; + // struct abi_finalizer_set { + // uint64_t fthreshold = 0; + // std::vector finalizers; + // }; + push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() + ("fin_set", mvo()("fthreshold", 1) + ("finalizers", std::vector{mvo() + ("description", "test_desc") + ("fweight", 1) + ("public_key_g1_jacobian", *reinterpret_cast*>(&G1))}))); + produce_blocks(); + + BOOST_ASSERT( control->get_finalizers().finalizers.size() == 1 ); + + // testing wrong public key size + BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() + ("fin_set", mvo()("fthreshold", 1) + ("finalizers", std::vector{mvo() + ("description", "test_desc") + ("fweight", 1) + ("public_key_g1_jacobian", std::vector{'a', 'b', 'c'})}))), fc::exception); + BOOST_ASSERT( control->get_finalizers().finalizers.size() == 1 ); + +} FC_LOG_AND_RETHROW() + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/tests/toolchain/compile-fail/host_functions_tests.cpp b/tests/toolchain/compile-fail/host_functions_tests.cpp index 27068436e7..06fdcc98f7 100644 --- a/tests/toolchain/compile-fail/host_functions_tests.cpp +++ b/tests/toolchain/compile-fail/host_functions_tests.cpp @@ -8,6 +8,7 @@ set_blockchain_parameters_packed : yes set_parameters_packed : yes set_privileged : yes send_deferred : yes +set_finalizers : yes */ #include diff --git a/tests/unit/test_contracts/CMakeLists.txt b/tests/unit/test_contracts/CMakeLists.txt index 8fc0e6e8a8..3728039199 100644 --- a/tests/unit/test_contracts/CMakeLists.txt +++ b/tests/unit/test_contracts/CMakeLists.txt @@ -8,6 +8,7 @@ add_contract(transfer_contract transfer_contract transfer.cpp) add_contract(minimal_tests minimal_tests minimal_tests.cpp) add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp) add_contract(bls_primitives_tests bls_primitives_tests bls_primitives_tests.cpp) +add_contract(set_finalizers_tests set_finalizers_tests set_finalizers_tests.cpp) add_contract(get_code_hash_tests get_code_hash_write get_code_hash_write.cpp) add_contract(get_code_hash_tests get_code_hash_read get_code_hash_read.cpp) add_contract(name_pk_tests name_pk_tests name_pk_tests.cpp) diff --git a/tests/unit/test_contracts/capi/privileged.c b/tests/unit/test_contracts/capi/privileged.c index e4ea864c52..3bc040cf50 100644 --- a/tests/unit/test_contracts/capi/privileged.c +++ b/tests/unit/test_contracts/capi/privileged.c @@ -11,4 +11,5 @@ void test_privileged( void ) { set_blockchain_parameters_packed(NULL, 0); get_blockchain_parameters_packed(NULL, 0); preactivate_feature(NULL); + set_finalizers(NULL, 0); } diff --git a/tests/unit/test_contracts/set_finalizers_tests.cpp b/tests/unit/test_contracts/set_finalizers_tests.cpp new file mode 100644 index 0000000000..32c4982884 --- /dev/null +++ b/tests/unit/test_contracts/set_finalizers_tests.cpp @@ -0,0 +1,12 @@ +#include +#include + +class [[eosio::contract]] set_finalizers_tests : public eosio::contract{ +public: + using contract::contract; + + [[eosio::action]] + void setfinal(const eosio::abi_finalizer_set& fin_set) { + eosio::set_finalizers(fin_set); + } +}; \ No newline at end of file diff --git a/tools/include/eosio/gen.hpp b/tools/include/eosio/gen.hpp index 3637e74fea..6a6a7bd8c5 100644 --- a/tools/include/eosio/gen.hpp +++ b/tools/include/eosio/gen.hpp @@ -895,7 +895,8 @@ struct generation_utils { "db_idx_long_double_remove", "send_deferred", "send_inline", - "send_context_free_inline" + "send_context_free_inline", + "set_finalizers" }; return write_host_funcs.count(func_decl->getNameInfo().getAsString()) >= 1; From 6f61e695547e4646ef36b072339b973c62133578 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 6 Sep 2023 16:53:26 -0400 Subject: [PATCH 056/158] leap main -> hotstuff_integration --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 1ab9eedf7c..d22916152a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -74,7 +74,7 @@ jobs: owner: AntelopeIO repo: leap file: 'leap-dev.*(x86_64|amd64).deb' - target: main + target: hotstuff_integration artifact-name: leap-dev-ubuntu20-amd64 container-package: experimental-binaries token: ${{github.token}} From 5a0664705c47420b1ba1131bd62970295625aed3 Mon Sep 17 00:00:00 2001 From: Peter Oschwald Date: Tue, 5 Sep 2023 17:17:13 -0500 Subject: [PATCH 057/158] Make leap-dev download target configurable. Use defaults.json to specify default location/target to attempt to downlaod the leap-dev package from. Allow dynamic configuration through workflow_dispatch input in github. --- .cicd/defaults.json | 6 ++++++ .github/workflows/build.yaml | 40 ++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 .cicd/defaults.json diff --git a/.cicd/defaults.json b/.cicd/defaults.json new file mode 100644 index 0000000000..5aadcc5e12 --- /dev/null +++ b/.cicd/defaults.json @@ -0,0 +1,6 @@ +{ + "leap-dev":{ + "target":"main", + "prerelease":false + } + } \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d22916152a..9df3a3ff40 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -7,6 +7,17 @@ on: - "release/*" pull_request: workflow_dispatch: + inputs: + override-leap-dev: + description: Override leap-dev target + type: string + override-leap-dev-prerelease: + type: choice + description: Override leap-dev prelease + options: + - default + - true + - false permissions: packages: write @@ -53,9 +64,33 @@ jobs: push: true tags: ${{fromJSON(needs.d.outputs.p)[matrix.platform].image}} file: ${{fromJSON(needs.d.outputs.p)[matrix.platform].dockerfile}} + + versions: + name: Determine Versions + runs-on: ubuntu-latest + outputs: + leap-dev-target: ${{steps.versions.outputs.leap-dev-target}} + leap-dev-prerelease: ${{steps.versions.outputs.leap-dev-prerelease}} + steps: + - name: Setup versions from input or defaults + id: versions + env: + GH_TOKEN: ${{github.token}} + run: | + DEFAULTS_JSON=$(curl -sSfL $(gh api https://api.github.com/repos/${{github.repository}}/contents/.cicd/defaults.json?ref=${{github.sha}} --jq .download_url)) + echo leap-dev-target=$(echo "$DEFAULTS_JSON" | jq -r '."leap-dev".target') >> $GITHUB_OUTPUT + echo leap-dev-prerelease=$(echo "$DEFAULTS_JSON" | jq -r '."leap-dev".prerelease') >> $GITHUB_OUTPUT + + if [[ "${{inputs.override-leap-dev}}" != "" ]]; then + echo leap-dev-target=${{inputs.override-leap-dev}} >> $GITHUB_OUTPUT + fi + if [[ "${{inputs.override-leap-dev-prerelease}}" == +(true|false) ]]; then + echo leap-dev-prerelease=${{inputs.override-leap-dev-prerelease}} >> $GITHUB_OUTPUT + fi + Build: name: Build & Test - needs: [d, build-platforms] + needs: [d, build-platforms, versions] if: always() && needs.d.result == 'success' && (needs.build-platforms.result == 'success' || needs.build-platforms.result == 'skipped') strategy: fail-fast: false @@ -69,12 +104,13 @@ jobs: submodules: recursive - name: Download leap-dev.deb (Ubuntu 20 only) if: matrix.platform == 'ubuntu20' - uses: AntelopeIO/asset-artifact-download-action@v2 + uses: AntelopeIO/asset-artifact-download-action@v3 with: owner: AntelopeIO repo: leap file: 'leap-dev.*(x86_64|amd64).deb' target: hotstuff_integration + prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} artifact-name: leap-dev-ubuntu20-amd64 container-package: experimental-binaries token: ${{github.token}} From 492d92373ec2c81a9e92058b337531afce217761 Mon Sep 17 00:00:00 2001 From: Peter Oschwald Date: Tue, 5 Sep 2023 18:49:39 -0500 Subject: [PATCH 058/158] Remove unneeded token with v3. --- .github/workflows/build.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 9df3a3ff40..59e18c4090 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -113,7 +113,6 @@ jobs: prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} artifact-name: leap-dev-ubuntu20-amd64 container-package: experimental-binaries - token: ${{github.token}} - name: Install leap-dev.deb (Ubuntu 20 only) if: matrix.platform == 'ubuntu20' run: | From 409e5b2f31276b29f8c111fb218bd8a09bb07359 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 7 Sep 2023 14:01:17 -0400 Subject: [PATCH 059/158] correct leap-dev branch --- .cicd/defaults.json | 2 +- .github/workflows/build.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.cicd/defaults.json b/.cicd/defaults.json index 5aadcc5e12..49400aa35c 100644 --- a/.cicd/defaults.json +++ b/.cicd/defaults.json @@ -1,6 +1,6 @@ { "leap-dev":{ - "target":"main", + "target":"hotstuff_integration", "prerelease":false } } \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 59e18c4090..7f70ff46a8 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -109,7 +109,7 @@ jobs: owner: AntelopeIO repo: leap file: 'leap-dev.*(x86_64|amd64).deb' - target: hotstuff_integration + target: '${{needs.versions.outputs.leap-dev-target}}' prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} artifact-name: leap-dev-ubuntu20-amd64 container-package: experimental-binaries From 17b6b4dcc9786db43f0cf8b03a302766b4c08344 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 7 Sep 2023 16:13:18 -0400 Subject: [PATCH 060/158] fix after leap branch update --- tests/integration/set_finalizers_tests.cpp | 44 +++++++--------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp index 48ca8a97e7..ce30b48dcf 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/set_finalizers_tests.cpp @@ -6,6 +6,12 @@ #include +using namespace eosio; +using namespace eosio::testing; +using namespace fc; + +using mvo = fc::mutable_variant_object; + namespace eosio::chain { struct finalizer_authority { @@ -15,21 +21,8 @@ struct finalizer_authority { }; -struct finalizer_set { - - uint32_t generation = 0; ///< sequentially incrementing version number - uint64_t fthreshold = 0; ///< vote fweight threshold to finalize blocks - std::vector finalizers; ///< Instant Finality voter set -}; - } // eosio::chain -using namespace eosio; -using namespace eosio::testing; -using namespace fc; - -using mvo = fc::mutable_variant_object; - BOOST_AUTO_TEST_SUITE(set_finalizers_tests) BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { @@ -39,31 +32,21 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { set_code( config::system_account_name, contracts::set_finalizers_test_wasm() ); set_abi( config::system_account_name, contracts::set_finalizers_test_abi().data() ); - produce_blocks(); - - - BOOST_ASSERT( control->get_finalizers().finalizers.empty() ); + signed_block_ptr cur_block = produce_block(); + std::optional ext = cur_block->extract_header_extension(hs_finalizer_set_extension::extension_id()); + BOOST_ASSERT(!ext); const std::vector G1 = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; - // fin_set should be the following struct: - // struct abi_finalizer_authority { - // std::string description; - // uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold - // std::vector public_key_g1_jacobian; // Jacobian little endian - // }; - // struct abi_finalizer_set { - // uint64_t fthreshold = 0; - // std::vector finalizers; - // }; + push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() ("fin_set", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() ("description", "test_desc") ("fweight", 1) ("public_key_g1_jacobian", *reinterpret_cast*>(&G1))}))); - produce_blocks(); - - BOOST_ASSERT( control->get_finalizers().finalizers.size() == 1 ); + cur_block = produce_block(); + ext = cur_block->extract_header_extension(hs_finalizer_set_extension::extension_id()); + BOOST_ASSERT(ext && std::get(*ext).finalizers.size() == 1 ); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() @@ -72,7 +55,6 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { ("description", "test_desc") ("fweight", 1) ("public_key_g1_jacobian", std::vector{'a', 'b', 'c'})}))), fc::exception); - BOOST_ASSERT( control->get_finalizers().finalizers.size() == 1 ); } FC_LOG_AND_RETHROW() From 824cf1f41041417e39b66e214a903d45c53ff5c5 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Fri, 8 Sep 2023 17:43:39 -0700 Subject: [PATCH 061/158] updated with c++ 20 --- docs/roadmap.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/roadmap.md b/docs/roadmap.md index f1f9a4ec38..67b547aa46 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,17 +1,20 @@ # Roadmap for CDT ## Summary Milestone 5 -Top priority is clean-up of the code and optimizing for support. +Top priority is clean-up of the code and optimizing for support. Target Date for Milestone 5 is May/June 2024. - Remove AntlerProj repos from build and archive AntlerProj +- Move to C++20 - Vanilla Clang/LLVM -- Upgrade to llvm 16 +- Upgrade to LLVM +## Move to C++20 +In addition to the benefits from the latest language features. Currently, ENF maintains an additional fork of one of our upstream dependencies for EOS EVM simply to get around the fact that the upstream assumes C++20 but we cannot build C++20 code in our contracts. ## Antler -We lack the reasources to complete the Antler project and support it going forward. Removing dependancies will simplify the build process and simplify things. +We lack the resources to complete the Antler project and support it going forward. Removing dependencies will simplify the build process and simplify things. ## Vanilla Clang/LLVM -Remove the custom extentions to Clang/LLVM. This will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. +Remove the custom extensions to Clang/LLVM. This will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. ## Upgrade to LLVM 16 Upgrade to the latest From 938204c5c79326f2ff406991c49b33a3701b65fd Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Fri, 8 Sep 2023 17:47:27 -0700 Subject: [PATCH 062/158] clarified C++ 20 support for contract language --- docs/roadmap.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/roadmap.md b/docs/roadmap.md index 67b547aa46..cbf3a0bc48 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -3,16 +3,17 @@ ## Summary Milestone 5 Top priority is clean-up of the code and optimizing for support. Target Date for Milestone 5 is May/June 2024. - Remove AntlerProj repos from build and archive AntlerProj -- Move to C++20 +- Move to C++20 - allow contracts to compile C++20 - Vanilla Clang/LLVM - Upgrade to LLVM -## Move to C++20 -In addition to the benefits from the latest language features. Currently, ENF maintains an additional fork of one of our upstream dependencies for EOS EVM simply to get around the fact that the upstream assumes C++20 but we cannot build C++20 code in our contracts. ## Antler We lack the resources to complete the Antler project and support it going forward. Removing dependencies will simplify the build process and simplify things. +## Move to C++20 +Allow contracts to compile C++20. In addition to the benefits from the latest language features. Currently, ENF maintains an additional fork of one of our upstream dependencies for EOS EVM simply to get around the fact that the upstream assumes C++20 but we cannot build C++20 code in our contracts. + ## Vanilla Clang/LLVM Remove the custom extensions to Clang/LLVM. This will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. From 3e233826c037f841e5276285ed096dbec295fa0e Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 11 Sep 2023 17:30:39 -0400 Subject: [PATCH 063/158] partialy #218 review concerns addressed --- libraries/eosiolib/capi/eosio/privileged.h | 1 + .../contracts/eosio/finalizer_set.hpp | 6 ++-- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 15 +++++---- tests/integration/set_finalizers_tests.cpp | 32 +++++++++++++++---- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/privileged.h b/libraries/eosiolib/capi/eosio/privileged.h index 7c909c6a12..edd3dd9047 100644 --- a/libraries/eosiolib/capi/eosio/privileged.h +++ b/libraries/eosiolib/capi/eosio/privileged.h @@ -129,6 +129,7 @@ void preactivate_feature( const struct capi_checksum256* feature_digest ); * @param data - pointer finalizer_set object packed as bytes * @param len - size of packed finalazer_set object * @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_set data + * abi_finalizer_set structure is defined in finalizer_set.hpp */ __attribute__((eosio_wasm_import)) void set_finalizers( const char* data, uint32_t len ); diff --git a/libraries/eosiolib/contracts/eosio/finalizer_set.hpp b/libraries/eosiolib/contracts/eosio/finalizer_set.hpp index a81b6c4aaf..450d85e3cc 100644 --- a/libraries/eosiolib/contracts/eosio/finalizer_set.hpp +++ b/libraries/eosiolib/contracts/eosio/finalizer_set.hpp @@ -18,9 +18,9 @@ namespace eosio { struct abi_finalizer_authority { std::string description; uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold - std::vector public_key_g1_jacobian; // Jacobian little endian + std::vector public_key_g1_affine_le; // Affine little endian - EOSLIB_SERIALIZE(abi_finalizer_authority, (description)(fweight)(public_key_g1_jacobian)); + EOSLIB_SERIALIZE(abi_finalizer_authority, (description)(fweight)(public_key_g1_affine_le)); }; struct abi_finalizer_set { uint64_t fthreshold = 0; @@ -31,7 +31,7 @@ namespace eosio { void set_finalizers( const abi_finalizer_set& fin_set ) { for (const auto& finalizer : fin_set.finalizers) - eosio::check(finalizer.public_key_g1_jacobian.size() == sizeof(bls_g1), "public key has a wrong size" ); + eosio::check(finalizer.public_key_g1_affine_le.size() == sizeof(bls_g1_affine), "public key has a wrong size" ); auto packed = eosio::pack(fin_set); internal_use_do_not_use::set_finalizers(packed.data(), packed.size()); } diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 7a62b57b1c..613858c397 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -43,13 +43,14 @@ namespace eosio { } } - using bls_scalar = std::array; - using bls_fp = std::array; - using bls_s = std::array; - using bls_fp2 = std::array; - using bls_g1 = std::array; - using bls_g2 = std::array; - using bls_gt = std::array; + using bls_scalar = std::array; + using bls_fp = std::array; + using bls_s = std::array; + using bls_fp2 = std::array; + using bls_g1 = std::array; + using bls_g1_affine = std::array; + using bls_g2 = std::array; + using bls_gt = std::array; int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { return internal_use_do_not_use::bls_g1_add( diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp index ce30b48dcf..69c8a659fa 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/set_finalizers_tests.cpp @@ -34,19 +34,39 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { signed_block_ptr cur_block = produce_block(); std::optional ext = cur_block->extract_header_extension(hs_finalizer_set_extension::extension_id()); - BOOST_ASSERT(!ext); - - const std::vector G1 = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; + BOOST_CHECK(!ext); + + const std::vector G1 = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, + 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, + 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, + 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, + 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, + 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, + 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, + 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, + 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, + 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, + 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, + 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, + 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, + 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, + 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, + 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, + 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, + 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; // 18x8 = 144 bytes push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() ("fin_set", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() ("description", "test_desc") ("fweight", 1) - ("public_key_g1_jacobian", *reinterpret_cast*>(&G1))}))); + ("public_key_g1_affine_le", *reinterpret_cast*>(&G1))}))); cur_block = produce_block(); ext = cur_block->extract_header_extension(hs_finalizer_set_extension::extension_id()); - BOOST_ASSERT(ext && std::get(*ext).finalizers.size() == 1 ); + BOOST_CHECK(ext); + auto finalizers = std::get(*ext).finalizers; + BOOST_CHECK(finalizers.size() == 1 ); + BOOST_CHECK(finalizers[0].public_key == *reinterpret_cast*>(G1.data()) ); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() @@ -54,7 +74,7 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { ("finalizers", std::vector{mvo() ("description", "test_desc") ("fweight", 1) - ("public_key_g1_jacobian", std::vector{'a', 'b', 'c'})}))), fc::exception); + ("public_key_g1_affine_le", std::vector{'a', 'b', 'c'})}))), fc::exception); } FC_LOG_AND_RETHROW() From 2a2db21d0a3d010a57466d0e258abb8f28c91b4d Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Tue, 12 Sep 2023 20:10:22 -0400 Subject: [PATCH 064/158] base64, bls pop verify --- libraries/eosiolib/core/eosio/base64.hpp | 303 ++++++++++++ .../eosiolib/core/eosio/crypto_bls_ext.hpp | 445 +++++++++++++++++- tests/CMakeLists.txt | 1 + tests/integration/bls_tests.cpp | 20 +- tests/unit/CMakeLists.txt | 1 + tests/unit/base64_tests.cpp | 193 ++++++++ .../test_contracts/bls_primitives_tests.cpp | 285 +---------- 7 files changed, 969 insertions(+), 279 deletions(-) create mode 100644 libraries/eosiolib/core/eosio/base64.hpp create mode 100644 tests/unit/base64_tests.cpp diff --git a/libraries/eosiolib/core/eosio/base64.hpp b/libraries/eosiolib/core/eosio/base64.hpp new file mode 100644 index 0000000000..4bd754c2e2 --- /dev/null +++ b/libraries/eosiolib/core/eosio/base64.hpp @@ -0,0 +1,303 @@ +/* + base64 encoding and decoding with C++. + More information at + https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp + https://github.com/ReneNyffenegger/cpp-base64 + + Version: 2.rc.09 (release candidate) + + Copyright (C) 2004-2017, 2020-2022 René Nyffenegger + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + + 3. This notice may not be removed or altered from any source distribution. + + René Nyffenegger rene.nyffenegger@adp-gmbh.ch + +*/ + +#include "check.hpp" + +#include +#include +#if __cplusplus >= 201703L +#include +#endif // __cplusplus >= 201703L + +namespace eosio { + +std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len); +inline std::string base64_encode(char const* bytes_to_encode, unsigned int in_len) { return base64_encode( (unsigned char const*)bytes_to_encode, in_len); } +std::string base64_encode( std::string_view enc ); +std::string base64_decode( std::string_view encoded_string); + +namespace detail { +std::string base64_encode (std::string const& s, bool url = false); + +std::string base64_decode(std::string const& s, bool remove_linebreaks = false); +std::string base64_encode(unsigned char const*, size_t len, bool url = false); + +std::string base64_encode (std::string_view s, bool url = false); + +std::string base64_decode(std::string_view s, bool remove_linebreaks = false); + +unsigned int pos_of_char(const unsigned char chr) { + // Return the position of chr within base64_encode() + const unsigned char from_base64_chars[256] = { + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 62, 64, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, + 64, 0, 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, 64, 64, 64, 64, 63, + 64, 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, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 + }; + auto c = from_base64_chars[chr]; + eosio::check(c != 64, "encountered non-base64 character"); + + return c; +} + +std::string insert_linebreaks(std::string str, size_t distance) { + // Provided by https://github.com/JomaCorpFX + if (!str.length()) { + return std::string{}; + } + + size_t pos = distance; + + while (pos < str.size()) { + str.insert(pos, "\n"); + pos += distance + 1; + } + + return str; +} + +template +std::string encode_with_line_breaks(String s) { + return insert_linebreaks(base64_encode(s, false), line_length); +} + +template +std::string encode_pem(String s) { + return encode_with_line_breaks(s); +} + +template +std::string encode_mime(String s) { + return encode_with_line_breaks(s); +} + +template +std::string encode(String s, bool url) { + return base64_encode(reinterpret_cast(s.data()), s.length(), url); +} + +std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) { + + const size_t len_encoded = (in_len +2) / 3 * 4; + const unsigned char trailing_char = url ? '.' : '='; + + // Includes performance improvement from unmerged PR: https://github.com/ReneNyffenegger/cpp-base64/pull/27 + + // Depending on the url parameter in base64_chars, one of + // two sets of base64 characters needs to be chosen. + // They differ in their last two characters. + const char* base64_chars[2] = { + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "-_"}; + + // Choose set of base64 characters. They differ + // for the last two positions, depending on the url + // parameter. + // A bool (as is the parameter url) is guaranteed + // to evaluate to either 0 or 1 in C++ therefore, + // the correct character set is chosen by subscripting + // base64_chars with url. + const char* base64_chars_ = base64_chars[url]; + + std::string ret; + ret.reserve(len_encoded); + + unsigned int pos = 0; + + while (pos < in_len) { + ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0xfc) >> 2]); + + if (pos+1 < in_len) { + ret.push_back(base64_chars_[((bytes_to_encode[pos + 0] & 0x03) << 4) + ((bytes_to_encode[pos + 1] & 0xf0) >> 4)]); + + if (pos+2 < in_len) { + ret.push_back(base64_chars_[((bytes_to_encode[pos + 1] & 0x0f) << 2) + ((bytes_to_encode[pos + 2] & 0xc0) >> 6)]); + ret.push_back(base64_chars_[ bytes_to_encode[pos + 2] & 0x3f]); + } + else { + ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]); + ret.push_back(trailing_char); + } + } + else { + + ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]); + ret.push_back(trailing_char); + ret.push_back(trailing_char); + } + + pos += 3; + } + + + return ret; +} + +template +static std::string decode(const String& encoded_string, bool remove_linebreaks) { + // + // decode(…) is templated so that it can be used with String = const std::string& + // or std::string_view (requires at least C++17) + // + + if (encoded_string.empty()) return std::string{}; + + if (remove_linebreaks) { + + std::string copy(encoded_string); + + copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end()); + + return base64_decode(copy, false); + } + + size_t length_of_string = encoded_string.length(); + size_t pos = 0; + + // + // The approximate length (bytes) of the decoded string might be one or + // two bytes smaller, depending on the amount of trailing equal signs + // in the encoded string. This approximation is needed to reserve + // enough space in the string to be returned. + // + size_t approx_length_of_decoded_string = length_of_string / 4 * 3; + std::string ret; + ret.reserve(approx_length_of_decoded_string); + + while (pos < length_of_string) { + // + // Iterate over encoded input string in chunks. The size of all + // chunks except the last one is 4 bytes. + // + // The last chunk might be padded with equal signs or dots + // in order to make it 4 bytes in size as well, but this + // is not required as per RFC 2045. + // + // All chunks except the last one produce three output bytes. + // + // The last chunk produces at least one and up to three bytes. + // + eosio::check(pos+1 < length_of_string, "wrong encoded string size"); + size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1) ); + + // + // Emit the first output byte that is produced in each chunk: + // + ret.push_back(static_cast( ( (pos_of_char(encoded_string.at(pos+0)) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4))); + + if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045) + encoded_string.at(pos+2) != '=' && + encoded_string.at(pos+2) != '.' // accept URL-safe base 64 strings, too, so check for '.' also. + ) + { + // + // Emit a chunk's second byte (which might not be produced in the last chunk). + // + unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2) ); + ret.push_back(static_cast( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2))); + + if ( ( pos + 3 < length_of_string ) && + encoded_string.at(pos+3) != '=' && + encoded_string.at(pos+3) != '.' + ) + { + // + // Emit a chunk's third byte (which might not be produced in the last chunk). + // + ret.push_back(static_cast( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3)) )); + } + } + + pos += 4; + } + + return ret; +} + +std::string base64_decode(std::string const& s, bool remove_linebreaks) { + return decode(s, remove_linebreaks); +} + +std::string base64_encode(std::string const& s, bool url) { + return encode(s, url); +} + +std::string base64_encode(std::string_view s, bool url) { + return encode(s, url); +} + +std::string base64_decode(std::string_view s, bool remove_linebreaks) { + return decode(s, remove_linebreaks); +} + +} // anonymous namespace + + +std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { + return detail::base64_encode(bytes_to_encode, in_len, false); +} +std::string base64_encode(std::string_view enc) { + return detail::base64_encode(enc, false); +} +std::string base64_decode(std::string_view encoded_string) { + return detail::base64_decode(encoded_string, false); +} + +std::string base64url_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { + return detail::base64_encode(bytes_to_encode, in_len, true); +} +std::string base64url_encode(std::string_view enc) { + return detail::base64_encode(enc, true); +} +std::string base64url_decode(std::string_view encoded_string) { + return detail::base64_decode(encoded_string, true); +} + +} // namespace eosio diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 7a62b57b1c..1d140414ee 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -1,12 +1,187 @@ #pragma once +#include "crypto.hpp" + #include "fixed_bytes.hpp" #include "varint.hpp" #include "serialize.hpp" +#include "base64.hpp" #include #include +#include + +namespace bls12_381 { +class sha256 { +public: + sha256(): m_blocklen(0), m_bitlen(0) { + m_state[0] = 0x6a09e667; + m_state[1] = 0xbb67ae85; + m_state[2] = 0x3c6ef372; + m_state[3] = 0xa54ff53a; + m_state[4] = 0x510e527f; + m_state[5] = 0x9b05688c; + m_state[6] = 0x1f83d9ab; + m_state[7] = 0x5be0cd19; + } + void update(const uint8_t * data, size_t length) { + for(size_t i = 0 ; i < length ; i++) { + m_data[m_blocklen++] = data[i]; + if (m_blocklen == 64) { + transform(); + + // End of the block + m_bitlen += 512; + m_blocklen = 0; + } + } + } + inline void update(const char* data, size_t length) { + update(reinterpret_cast(data), length); + } + inline void update(const std::string &data) { + update(reinterpret_cast(data.data()), data.size()); + } + std::array digest() { + std::array hash; + + pad(); + revert(hash); + + return hash; + } + void digest(uint8_t* dst) { + std::array* phash = reinterpret_cast*>(dst); + + pad(); + revert(*phash); + } + + //static string toString(const array& digest); + +private: + uint8_t m_data[64]; + uint32_t m_blocklen; + uint64_t m_bitlen; + uint32_t m_state[8]; //A, B, C, D, E, F, G, H + + static constexpr std::array K = { + 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5, + 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3, + 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc, + 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7, + 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13, + 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3, + 0xd192e819,0xd6990624,0xf40e3585,0x106aa070, + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5, + 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208, + 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 + }; + + static uint32_t rotr(uint32_t x, uint32_t n) { + return (x >> n) | (x << (32 - n)); + } + static uint32_t choose(uint32_t e, uint32_t f, uint32_t g) { + return (e & f) ^ (~e & g); + } + static uint32_t majority(uint32_t a, uint32_t b, uint32_t c) { + return (a & (b | c)) | (b & c); + } + static uint32_t sig0(uint32_t x) { + return sha256::rotr(x, 7) ^ sha256::rotr(x, 18) ^ (x >> 3); + } + static uint32_t sig1(uint32_t x) { + return sha256::rotr(x, 17) ^ sha256::rotr(x, 19) ^ (x >> 10); + } + void transform() { + uint32_t maj, xorA, ch, xorE, sum, newA, newE, m[64]; + uint32_t state[8]; + + for(uint8_t i = 0, j = 0; i < 16; i++, j += 4) { + // Split data in 32 bit blocks for the 16 first words + m[i] = (m_data[j] << 24) | (m_data[j + 1] << 16) | (m_data[j + 2] << 8) | (m_data[j + 3]); + } + + for(uint8_t k = 16 ; k < 64; k++) { + // Remaining 48 blocks + m[k] = sha256::sig1(m[k - 2]) + m[k - 7] + sha256::sig0(m[k - 15]) + m[k - 16]; + } + + for(uint8_t i = 0 ; i < 8 ; i++) { + state[i] = m_state[i]; + } + + for(uint8_t i = 0; i < 64; i++) { + maj = sha256::majority(state[0], state[1], state[2]); + xorA = sha256::rotr(state[0], 2) ^ sha256::rotr(state[0], 13) ^ sha256::rotr(state[0], 22); + + ch = choose(state[4], state[5], state[6]); + + xorE = sha256::rotr(state[4], 6) ^ sha256::rotr(state[4], 11) ^ sha256::rotr(state[4], 25); + + sum = m[i] + K[i] + state[7] + ch + xorE; + newA = xorA + maj + sum; + newE = state[3] + sum; + + state[7] = state[6]; + state[6] = state[5]; + state[5] = state[4]; + state[4] = newE; + state[3] = state[2]; + state[2] = state[1]; + state[1] = state[0]; + state[0] = newA; + } + + for(uint8_t i = 0 ; i < 8 ; i++) { + m_state[i] += state[i]; + } + } + void pad() { + uint64_t i = m_blocklen; + uint8_t end = m_blocklen < 56 ? 56 : 64; + + m_data[i++] = 0x80; // Append a bit 1 + while(i < end) { + m_data[i++] = 0x00; // Pad with zeros + } + + if(m_blocklen >= 56) { + transform(); + memset(m_data, 0, 56); + } + + // Append to the padding the total message's length in bits and transform. + m_bitlen += m_blocklen * 8; + m_data[63] = m_bitlen; + m_data[62] = m_bitlen >> 8; + m_data[61] = m_bitlen >> 16; + m_data[60] = m_bitlen >> 24; + m_data[59] = m_bitlen >> 32; + m_data[58] = m_bitlen >> 40; + m_data[57] = m_bitlen >> 48; + m_data[56] = m_bitlen >> 56; + transform(); + } + void revert(std::array& hash) { + // SHA uses big endian byte ordering + // Revert all bytes + for(uint8_t i = 0 ; i < 4 ; i++) { + for(uint8_t j = 0 ; j < 8 ; j++) { + hash[i + (j * 4)] = (m_state[j] >> (24 - i * 8)) & 0x000000ff; + } + } + } +}; +} // namespace bls12_381 + namespace eosio { namespace internal_use_do_not_use { @@ -43,13 +218,15 @@ namespace eosio { } } - using bls_scalar = std::array; - using bls_fp = std::array; - using bls_s = std::array; - using bls_fp2 = std::array; - using bls_g1 = std::array; - using bls_g2 = std::array; - using bls_gt = std::array; + using bls_scalar = std::array; + using bls_fp = std::array; + using bls_s = std::array; + using bls_fp2 = std::array; + using bls_g1 = std::array; + using bls_g1_affine = std::array; + using bls_g2 = std::array; + using bls_g2_affine = std::array; + using bls_gt = std::array; int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { return internal_use_do_not_use::bls_g1_add( @@ -157,5 +334,259 @@ namespace eosio { sizeof(bls_fp) ); } + + const inline std::string bls_public_key_prefix = "PUB_BLS_"; + const inline uint32_t bls_publick_key_checksum_size = sizeof(uint32_t); + const inline std::string bls_signature_prefix = "SIG_BLS_"; + + template + std::string bls_type_to_base64(const T& g1) { + std::array g1_with_checksum; + auto it = std::copy(g1.begin(), g1.end(), g1_with_checksum.begin()); + + auto csum = ripemd160(g1.data(), g1.size()).extract_as_byte_array(); + std::copy(reinterpret_cast(csum.data()), + reinterpret_cast(csum.data())+bls_publick_key_checksum_size, + it); + + return Prefix + base64_encode(g1_with_checksum.data(), g1_with_checksum.size()); + } + inline std::string bls_g1_affine_to_base64(const bls_g1_affine& g1) { + return bls_type_to_base64(g1); + } + template + T bls_base64_to_type(const char* data, size_t size) { + eosio::check(size > Prefix.size(), "encoded base64 key is too short"); + eosio::check(0 == memcmp(data, Prefix.data(), Prefix.size()), "base64 encoded type must begin from corresponding prefix"); + + std::string decoded = base64_decode({data+Prefix.size(), size - Prefix.size()}); + T ret; + eosio::check(decoded.size() == ret.size() + bls_publick_key_checksum_size, "decoded size " + std::to_string(decoded.size()) + + " doesn't match structure size " + std::to_string(ret.size()) + + " + checksum " + std::to_string(bls_publick_key_checksum_size)); + + auto it = decoded.end(); + std::advance(it, -bls_publick_key_checksum_size); + std::copy(decoded.begin(), it, ret.begin()); + + auto csum = ripemd160(ret.data(), ret.size()).extract_as_byte_array(); + eosio::check(0 == memcmp(&*it, csum.data(), bls_publick_key_checksum_size), "checksum of structure doesn't match"); + + return ret; + } + inline bls_g1_affine bls_base64_to_g1_affine(const char* data, size_t size) { + return bls_base64_to_type(data, size); + } + inline std::string bls_sig_to_base64_affine(const bls_g2_affine& g2) { + return bls_type_to_base64(g2); + } + inline bls_g2_affine bls_base64_to_sig_affine(const char* data, size_t size) { + return bls_base64_to_type(data, size); + } + + const inline std::string POP_CIPHERSUITE_ID = "BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; + const inline std::vector G1_ONE_NEG = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, + 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, + 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, + 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, + 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, + 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, + 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, + 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, + 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, + 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, + 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, + 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, + 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, + 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, + 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, + 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, + 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, + 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; //18x8 = 144 bytes + const inline std::vector GT_ONE = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, + 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, + 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, + 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, + 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, + 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 73x8 = 584 bytes + const inline std::vector R1 = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, + 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, + 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, + 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, + 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, + 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; // 6x8 = 48 bytes + + // Construct an extensible-output function based on SHA256 + void xmd_sh256( + char *buf, + int buf_len, + const char *in, + int in_len, + const char *dst, + int dst_len + ) { + const unsigned int SHA256HashSize = 32; + const unsigned int SHA256_Message_Block_Size = 64; + const unsigned ell = (buf_len + SHA256HashSize - 1) / SHA256HashSize; + if (buf_len < 0 || ell > 255 || dst_len > 255) { + return; + } + const uint8_t Z_pad[SHA256_Message_Block_Size] = { 0, }; + const uint8_t l_i_b_0_str[] = { + static_cast(buf_len >> 8), + static_cast(buf_len & 0xff), + 0, + static_cast(dst_len) + }; + const uint8_t *dstlen_str = l_i_b_0_str + 3; + uint8_t b_0[SHA256HashSize]; + bls12_381::sha256 sha; + sha.update(Z_pad, SHA256_Message_Block_Size); + sha.update(in, in_len); + sha.update(l_i_b_0_str, 3); + sha.update(dst, dst_len); + sha.update(dstlen_str, 1); + sha.digest(b_0); + uint8_t b_i[SHA256HashSize + 1] = { 0, }; + for (unsigned i = 1; i <= ell; ++i) { + for (unsigned j = 0; j < SHA256HashSize; ++j) { + b_i[j] = b_0[j] ^ b_i[j]; + } + b_i[SHA256HashSize] = i; + bls12_381::sha256 s; + s.update(b_i, SHA256HashSize + 1); + s.update(dst, dst_len); + s.update(dstlen_str, 1); + s.digest(b_i); + const int rem_after = buf_len - i * SHA256HashSize; + const int copy_len = SHA256HashSize + (rem_after < 0 ? rem_after : 0); + memcpy(buf + (i - 1) * SHA256HashSize, b_i, copy_len); + } + } + bls_s scalar_fromBE(const bls_s& in) { + std::array out; + for(uint64_t i = 0; i < 8; i++) { + uint64_t temp; + memcpy(&temp, &in[in.size() - i*8 - 8], sizeof(uint64_t)); + out[i] = htobe64(temp); + } + return reinterpret_cast(std::move(out)); + } + void g2_fromMessage(const bls_g1_affine& msg, const std::string& dst, bls_g2& res) { + + std::array buf; + xmd_sh256(buf.data()->data(), sizeof(buf), msg.data(), msg.size(), dst.data(), dst.length()); + + bls_s k; + bls_fp2 t; + bls_g2 p, q; + + k = scalar_fromBE(buf[0]); + bls_fp_mod(k, t[0]); + k = scalar_fromBE(buf[1]); + bls_fp_mod(k, t[1]); + + bls_g2_map(t, p); + + k = scalar_fromBE(buf[2]); + bls_fp_mod(k, t[0]); + k = scalar_fromBE(buf[3]); + bls_fp_mod(k, t[1]); + + bls_g2_map(t, q); + bls_g2_add(p, q, res); + } + + // pubkey and signature are assumed to be in RAW affine little-endian bytes + bool bls_pop_verify(const bls_g1_affine& pubkey, const bls_g2_affine& signature_proof) { + bls_g1 g1_points[2] = {0}; + bls_g2 g2_points[2] = {0}; + + // add z coordinate (R1) to pubkey and signature_proof + std::vector pubkey_ex(sizeof(bls_g1_affine) + R1.size(), 0); + auto insert_it = std::copy(pubkey.begin(), pubkey.end(), pubkey_ex.begin()); + std::copy(R1.begin(), R1.end(), insert_it); + + std::vector sig_ex(sizeof(bls_g2), 0); + insert_it = std::copy(signature_proof.begin(), signature_proof.end(), sig_ex.begin()); + std::copy(R1.begin(), R1.end(), insert_it); + + memcpy(&g1_points[0], G1_ONE_NEG.data(), G1_ONE_NEG.size()); + memcpy(&g2_points[0], sig_ex.data(), sig_ex.size()); + + memcpy(&g1_points[1], pubkey_ex.data(), pubkey_ex.size()); + g2_fromMessage(pubkey, POP_CIPHERSUITE_ID, g2_points[1]); + + bls_gt r; + bls_pairing(g1_points, g2_points, 2, r); + return 0 == std::memcmp(r.data(), GT_ONE.data(), sizeof(bls_gt)); + } + } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2590f7345b..bcb4e052b0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,6 +4,7 @@ macro(add_unit_test TEST_NAME) endmacro() add_unit_test( asset_tests ) +add_unit_test( base64_tests ) add_unit_test( binary_extension_tests ) add_unit_test( crt_tests ) add_unit_test( crypto_tests ) diff --git a/tests/integration/bls_tests.cpp b/tests/integration/bls_tests.cpp index 91bc18ca34..5d7e93a942 100644 --- a/tests/integration/bls_tests.cpp +++ b/tests/integration/bls_tests.cpp @@ -98,11 +98,23 @@ BOOST_FIXTURE_TEST_CASE( g2_map_test, bls_primitives_tester ) try { } FC_LOG_AND_RETHROW() -BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { - push_action("eosio"_n, "verify"_n, "test"_n, mvo() - ("pk", "90ace8f520a5e056eab099d4e6a6f761a8f51301e1e15910908453007d94e33f8e86b834808dcb5323313153be066d01d49660f594a805ad9a4249d0af4190407e3e221e1d6fdad866e022308357776ed5e396777c7f7dfd020e7d736511ac06fdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") - ("sig", "3c38a59a6c016fb598d8da531e205e4d8e0953868675760f8333c7fee725d81ddf246db75792eb7cf0ab8aeba051800babf407ddd8ec721db83fe1b1098b9334bf45ad63f10cf5a6b56b60f74b391615d7373088d182711358a6032f8c31fe1694ff3f3254a7e02e3e2811ce0becdec6031b193b5541d577b49cdb6cf09f919917b09776b1f05c977ffa17c2de876205f402f7e4fb00896cb9b3df1a25f761239e5625ddaab4bfacce8e6c505c13eb73632b12cedd30b0804fe29f317015c307ec2dfe48cb5060d84fd2c2756f41ae79f4c0a3a37ba62b22877b23042bb30a063d4ad3255de844545e89f0886041b513410df52d380fac23c31277997381115651c02014a9aeeaaec85c34b81a93ee73cd712ce241f394dc860ea5e792b0f50e")); +BOOST_FIXTURE_TEST_CASE( base64_test, bls_primitives_tester ) try { + push_action("eosio"_n, "g1baseb4enc"_n, "test"_n, mvo() + ("g1", "dbf44b63155bdd1b124f9155ee40b00ac626d5c8aa3f88e9da5dd890df006da142833846ff233e13a40c90b596aa07190bb4de63a265b9848078cae7a3d7fca7786c160d12d705323a758849455eaf88a5a2646fafbb811da450dd07695f1b00") + ("base64", "PUB_BLS_2/RLYxVb3RsST5FV7kCwCsYm1ciqP4jp2l3YkN8AbaFCgzhG/yM+E6QMkLWWqgcZC7TeY6JluYSAeMrno9f8p3hsFg0S1wUyOnWISUVer4ilomRvr7uBHaRQ3QdpXxsAwOc0YQ==")); + push_action("eosio"_n, "sigbaseb4enc"_n, "test"_n, mvo() + ("g2", "6bda63c93a6f638fda29b393cec8bcea5136dd2e27cec50425463f21eb1f2f09a5347024a427e302d48646565109f4181c861299edc24015b62cc146ed7b17d44510fcbf933ef0eae764dc793f68f73fe203a4c18ece7cd9d3103a6299f21519514660ccf94f0ea221d0335b4d3daf557ddeb3a8819c7136f75625e29e1bac230b88a290ff7900fb1be3147ca4a2e40fe68b2dc8c938f23e054f17603953659e2cd46265a6b2df86eb99e012ab534d4219a9cdc8d0e5038b11888533503c2f05") + ("base64", "SIG_BLS_a9pjyTpvY4/aKbOTzsi86lE23S4nzsUEJUY/IesfLwmlNHAkpCfjAtSGRlZRCfQYHIYSme3CQBW2LMFG7XsX1EUQ/L+TPvDq52TceT9o9z/iA6TBjs582dMQOmKZ8hUZUUZgzPlPDqIh0DNbTT2vVX3es6iBnHE291Yl4p4brCMLiKKQ/3kA+xvjFHykouQP5ostyMk48j4FTxdgOVNlnizUYmWmst+G65ngEqtTTUIZqc3I0OUDixGIhTNQPC8FRe+RNQ==")); } FC_LOG_AND_RETHROW() +BOOST_FIXTURE_TEST_CASE( sig_pop_verify_test, bls_primitives_tester ) try { + push_action("eosio"_n, "popverifyraw"_n, "test"_n, mvo() + ("pk", "220ef5c49c1868e85b82658df9766fc2cee4bd0b5d9880d4cdee9139edd3c4ebdae4074f1d3db3f3c9213962942eef091b00d9f2a9b837015c8dbe507f242aee459272589b6b2973bcf33eb4608722c70c1ac3d1ade40c845b9e15ea6380080a") + ("sig", "2f6bc758f0eec23b9218b665e72b522804c2c941dd3a18c4a6686f1f329bb4f6a0cc75ce532f366fc50962d0f58ce40f4ce009e581fe0dde6a13eff94eb041f266e8e386b1036dc866e941345b96dad688039d3859dcb767e261462c9e6d0300598567f1d3e47e3d01a81588cff6a0a8fb24aeb3d725cdb83effd1483c12780e75702508692ae64083358f42fab19d10410ccd774c25d38b4fc9066f7fc2ba00c312ef35b9ddbe4aa9b932d4a516291e2da04da856c3216d852336a672bf2a06")); + push_action("eosio"_n, "popverify"_n, "test"_n, mvo() + ("pk", "PUB_BLS_2/RLYxVb3RsST5FV7kCwCsYm1ciqP4jp2l3YkN8AbaFCgzhG/yM+E6QMkLWWqgcZC7TeY6JluYSAeMrno9f8p3hsFg0S1wUyOnWISUVer4ilomRvr7uBHaRQ3QdpXxsAwOc0YQ==") + ("sig", "SIG_BLS_a9pjyTpvY4/aKbOTzsi86lE23S4nzsUEJUY/IesfLwmlNHAkpCfjAtSGRlZRCfQYHIYSme3CQBW2LMFG7XsX1EUQ/L+TPvDq52TceT9o9z/iA6TBjs582dMQOmKZ8hUZUUZgzPlPDqIh0DNbTT2vVX3es6iBnHE291Yl4p4brCMLiKKQ/3kA+xvjFHykouQP5ostyMk48j4FTxdgOVNlnizUYmWmst+G65ngEqtTTUIZqc3I0OUDixGIhTNQPC8FRe+RNQ==")); +} FC_LOG_AND_RETHROW() + BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index d5a6eeab10..4407c51898 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -14,6 +14,7 @@ macro(add_cdt_unit_test TEST_NAME) endmacro() add_cdt_unit_test(asset_tests) +add_cdt_unit_test(base64_tests) add_cdt_unit_test(binary_extension_tests) add_cdt_unit_test(crt_tests) add_cdt_unit_test(crypto_tests) diff --git a/tests/unit/base64_tests.cpp b/tests/unit/base64_tests.cpp new file mode 100644 index 0000000000..ac8cd8a92a --- /dev/null +++ b/tests/unit/base64_tests.cpp @@ -0,0 +1,193 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + +#include +#include + +using namespace eosio; +using namespace std::literals; + +EOSIO_TEST_BEGIN(base64enc) + auto input = "abc123$&()'?\xb4\xf5\x01\xfa~a"s; + auto expected_output = "YWJjMTIzJCYoKSc/tPUB+n5h"s; + + CHECK_EQUAL(expected_output, base64_encode(input)); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(base64urlenc) + auto input = "abc123$&()'?\xb4\xf5\x01\xfa~a"s; + auto expected_output = "YWJjMTIzJCYoKSc_tPUB-n5h"s; + + CHECK_EQUAL(expected_output, base64url_encode(input)); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(base64dec) + auto input = "YWJjMTIzJCYoKSc/tPUB+n5h"s; + auto expected_output = "abc123$&()'?\xb4\xf5\x01\xfa~a"s; + + CHECK_EQUAL(expected_output, base64_decode(input)); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(base64urldec) + auto input = "YWJjMTIzJCYoKSc_tPUB-n5h"s; + auto expected_output = "abc123$&()'?\xb4\xf5\x01\xfa~a"s; + + CHECK_EQUAL(expected_output, base64url_decode(input)); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(base64dec_extraequals) + CHECK_ASSERT( "encountered non-base64 character", + ([](){ + base64_decode("YWJjMTIzJCYoKSc/tPUB+n5h========="s); + })); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(base64dec_bad_stuff) + CHECK_ASSERT( "encountered non-base64 character", + ([](){ + base64_decode("YWJjMTIzJCYoKSc/tPU$B+n5h="s); + })); +EOSIO_TEST_END + +// tests from https://github.com/ReneNyffenegger/cpp-base64/blob/master/test.cpp +EOSIO_TEST_BEGIN(base64_cpp_base64_tests) + // + // Note: this file must be encoded in UTF-8 + // for the following test, otherwise, the test item + // fails. + // + const std::string orig = + "René Nyffenegger\n" + "http://www.renenyffenegger.ch\n" + "passion for data\n"; + + std::string encoded = base64_encode(reinterpret_cast(orig.c_str()), orig.length()); + std::string decoded = base64_decode(encoded); + + CHECK_EQUAL(encoded, "UmVuw6kgTnlmZmVuZWdnZXIKaHR0cDovL3d3dy5yZW5lbnlmZmVuZWdnZXIuY2gKcGFzc2lvbiBmb3IgZGF0YQo="); + CHECK_EQUAL(decoded, orig); + + // Test all possibilites of fill bytes (none, one =, two ==) + // References calculated with: https://www.base64encode.org/ + + std::string rest0_original = "abc"; + std::string rest0_reference = "YWJj"; + + std::string rest0_encoded = base64_encode(reinterpret_cast(rest0_original.c_str()), + rest0_original.length()); + std::string rest0_decoded = base64_decode(rest0_encoded); + + CHECK_EQUAL(rest0_decoded, rest0_original); + CHECK_EQUAL(rest0_reference, rest0_encoded); + + std::string rest1_original = "abcd"; + std::string rest1_reference = "YWJjZA=="; + + std::string rest1_encoded = base64_encode(reinterpret_cast(rest1_original.c_str()), + rest1_original.length()); + std::string rest1_decoded = base64_decode(rest1_encoded); + + CHECK_EQUAL(rest1_decoded, rest1_original); + CHECK_EQUAL(rest1_reference, rest1_encoded); + + std::string rest2_original = "abcde"; + std::string rest2_reference = "YWJjZGU="; + + std::string rest2_encoded = base64_encode(reinterpret_cast(rest2_original.c_str()), + rest2_original.length()); + std::string rest2_decoded = base64_decode(rest2_encoded); + + CHECK_EQUAL(rest2_decoded, rest2_original); + CHECK_EQUAL(rest2_reference, rest2_encoded); + + // -------------------------------------------------------------- + // + // Data that is 17 bytes long requires one padding byte when + // base-64 encoded. Such an encoded string could not correctly + // be decoded when encoded with «url semantics». This bug + // was discovered by https://github.com/kosniaz. The following + // test checks if this bug was fixed: + // + std::string a17_orig = "aaaaaaaaaaaaaaaaa"; + std::string a17_encoded = base64_encode(a17_orig); + std::string a17_encoded_url = base64url_encode(a17_orig); + + CHECK_EQUAL(a17_encoded, "YWFhYWFhYWFhYWFhYWFhYWE="); + CHECK_EQUAL(a17_encoded_url, "YWFhYWFhYWFhYWFhYWFhYWE."); + CHECK_EQUAL(base64_decode(a17_encoded_url), a17_orig); + CHECK_EQUAL(base64_decode(a17_encoded), a17_orig); + + // -------------------------------------------------------------- + + // characters 63 and 64 / URL encoding + + std::string s_6364 = "\x03" "\xef" "\xff" "\xf9"; + + std::string s_6364_encoded = base64_encode(s_6364); + std::string s_6364_encoded_url = base64url_encode(s_6364); + + CHECK_EQUAL(s_6364_encoded, "A+//+Q=="); + CHECK_EQUAL(s_6364_encoded_url, "A-__-Q.."); + CHECK_EQUAL(base64_decode(s_6364_encoded), s_6364); + CHECK_EQUAL(base64_decode(s_6364_encoded_url), s_6364); + + // ---------------------------------------------- + + std::string unpadded_input = "YWJjZGVmZw"; // Note the 'missing' "==" + std::string unpadded_decoded = base64_decode(unpadded_input); + CHECK_EQUAL(unpadded_decoded, "abcdefg"); + + unpadded_input = "YWJjZGU"; // Note the 'missing' "=" + unpadded_decoded = base64_decode(unpadded_input); + CHECK_EQUAL(unpadded_decoded, "abcde"); + + unpadded_input = ""; + unpadded_decoded = base64_decode(unpadded_input); + CHECK_EQUAL(unpadded_decoded, ""); + + unpadded_input = "YQ"; + unpadded_decoded = base64_decode(unpadded_input); + CHECK_EQUAL(unpadded_decoded, "a"); + + unpadded_input = "YWI"; + unpadded_decoded = base64_decode(unpadded_input); + CHECK_EQUAL(unpadded_decoded, "ab"); + + CHECK_ASSERT( "wrong encoded string size", + ([](){ + std::string not_null_terminated = std::string(1, 'a'); + base64_decode(not_null_terminated); + })); + // -------------------------------------------------------------- + // + // Test the string_view interface (which required C++17) + // + std::string_view sv_orig = "foobarbaz"; + std::string sv_encoded = base64_encode(sv_orig); + + CHECK_EQUAL(sv_encoded, "Zm9vYmFyYmF6"); + + std::string sv_decoded = base64_decode(sv_encoded); + + CHECK_EQUAL(sv_decoded, sv_orig); + +EOSIO_TEST_END + +int main(int argc, char* argv[]) { + bool verbose = false; + if( argc >= 2 && std::strcmp( argv[1], "-v" ) == 0 ) { + verbose = true; + } + silence_output(!verbose); + + EOSIO_TEST(base64enc); + EOSIO_TEST(base64urlenc); + EOSIO_TEST(base64dec); + EOSIO_TEST(base64urldec); + EOSIO_TEST(base64dec_extraequals); + EOSIO_TEST(base64dec_bad_stuff); + EOSIO_TEST(base64_cpp_base64_tests); + return has_failed(); +} diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 1b97e8a338..03d17f4abd 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -3,180 +3,8 @@ #include #include -#include - using namespace eosio; -namespace bls12_381 { -class sha256 { -public: - sha256(): m_blocklen(0), m_bitlen(0) { - m_state[0] = 0x6a09e667; - m_state[1] = 0xbb67ae85; - m_state[2] = 0x3c6ef372; - m_state[3] = 0xa54ff53a; - m_state[4] = 0x510e527f; - m_state[5] = 0x9b05688c; - m_state[6] = 0x1f83d9ab; - m_state[7] = 0x5be0cd19; - } - void update(const uint8_t * data, size_t length) { - for(size_t i = 0 ; i < length ; i++) { - m_data[m_blocklen++] = data[i]; - if (m_blocklen == 64) { - transform(); - - // End of the block - m_bitlen += 512; - m_blocklen = 0; - } - } - } - inline void update(const char* data, size_t length) { - update(reinterpret_cast(data), length); - } - inline void update(const std::string &data) { - update(reinterpret_cast(data.data()), data.size()); - } - std::array digest() { - std::array hash; - - pad(); - revert(hash); - - return hash; - } - void digest(uint8_t* dst) { - std::array* phash = reinterpret_cast*>(dst); - - pad(); - revert(*phash); - } - - //static string toString(const array& digest); - -private: - uint8_t m_data[64]; - uint32_t m_blocklen; - uint64_t m_bitlen; - uint32_t m_state[8]; //A, B, C, D, E, F, G, H - - static constexpr std::array K = { - 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5, - 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, - 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3, - 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, - 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc, - 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, - 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7, - 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, - 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13, - 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, - 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3, - 0xd192e819,0xd6990624,0xf40e3585,0x106aa070, - 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5, - 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, - 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208, - 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 - }; - - static uint32_t rotr(uint32_t x, uint32_t n) { - return (x >> n) | (x << (32 - n)); - } - static uint32_t choose(uint32_t e, uint32_t f, uint32_t g) { - return (e & f) ^ (~e & g); - } - static uint32_t majority(uint32_t a, uint32_t b, uint32_t c) { - return (a & (b | c)) | (b & c); - } - static uint32_t sig0(uint32_t x) { - return sha256::rotr(x, 7) ^ sha256::rotr(x, 18) ^ (x >> 3); - } - static uint32_t sig1(uint32_t x) { - return sha256::rotr(x, 17) ^ sha256::rotr(x, 19) ^ (x >> 10); - } - void transform() { - uint32_t maj, xorA, ch, xorE, sum, newA, newE, m[64]; - uint32_t state[8]; - - for(uint8_t i = 0, j = 0; i < 16; i++, j += 4) { - // Split data in 32 bit blocks for the 16 first words - m[i] = (m_data[j] << 24) | (m_data[j + 1] << 16) | (m_data[j + 2] << 8) | (m_data[j + 3]); - } - - for(uint8_t k = 16 ; k < 64; k++) { - // Remaining 48 blocks - m[k] = sha256::sig1(m[k - 2]) + m[k - 7] + sha256::sig0(m[k - 15]) + m[k - 16]; - } - - for(uint8_t i = 0 ; i < 8 ; i++) { - state[i] = m_state[i]; - } - - for(uint8_t i = 0; i < 64; i++) { - maj = sha256::majority(state[0], state[1], state[2]); - xorA = sha256::rotr(state[0], 2) ^ sha256::rotr(state[0], 13) ^ sha256::rotr(state[0], 22); - - ch = choose(state[4], state[5], state[6]); - - xorE = sha256::rotr(state[4], 6) ^ sha256::rotr(state[4], 11) ^ sha256::rotr(state[4], 25); - - sum = m[i] + K[i] + state[7] + ch + xorE; - newA = xorA + maj + sum; - newE = state[3] + sum; - - state[7] = state[6]; - state[6] = state[5]; - state[5] = state[4]; - state[4] = newE; - state[3] = state[2]; - state[2] = state[1]; - state[1] = state[0]; - state[0] = newA; - } - - for(uint8_t i = 0 ; i < 8 ; i++) { - m_state[i] += state[i]; - } - } - void pad() { - uint64_t i = m_blocklen; - uint8_t end = m_blocklen < 56 ? 56 : 64; - - m_data[i++] = 0x80; // Append a bit 1 - while(i < end) { - m_data[i++] = 0x00; // Pad with zeros - } - - if(m_blocklen >= 56) { - transform(); - memset(m_data, 0, 56); - } - - // Append to the padding the total message's length in bits and transform. - m_bitlen += m_blocklen * 8; - m_data[63] = m_bitlen; - m_data[62] = m_bitlen >> 8; - m_data[61] = m_bitlen >> 16; - m_data[60] = m_bitlen >> 24; - m_data[59] = m_bitlen >> 32; - m_data[58] = m_bitlen >> 40; - m_data[57] = m_bitlen >> 48; - m_data[56] = m_bitlen >> 56; - transform(); - } - void revert(std::array& hash) { - // SHA uses big endian byte ordering - // Revert all bytes - for(uint8_t i = 0 ; i < 4 ; i++) { - for(uint8_t j = 0 ; j < 8 ; j++) { - hash[i + (j * 4)] = (m_state[j] >> (24 - i * 8)) & 0x000000ff; - } - } - } -}; -} // namespace bls12_381 - class [[eosio::contract]] bls_primitives_tests : public contract{ public: using contract::contract; @@ -252,110 +80,31 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_map test failed"); } - // Construct an extensible-output function based on SHA256 - void xmd_sh256( - char *buf, - int buf_len, - const uint8_t *in, - int in_len, - const char *dst, - int dst_len - ) { - const unsigned int SHA256HashSize = 32; - const unsigned int SHA256_Message_Block_Size = 64; - const unsigned ell = (buf_len + SHA256HashSize - 1) / SHA256HashSize; - if (buf_len < 0 || ell > 255 || dst_len > 255) { - return; - } - const uint8_t Z_pad[SHA256_Message_Block_Size] = { 0, }; - const uint8_t l_i_b_0_str[] = { - static_cast(buf_len >> 8), - static_cast(buf_len & 0xff), - 0, - static_cast(dst_len) - }; - const uint8_t *dstlen_str = l_i_b_0_str + 3; - uint8_t b_0[SHA256HashSize]; - bls12_381::sha256 sha; - sha.update(Z_pad, SHA256_Message_Block_Size); - sha.update(in, in_len); - sha.update(l_i_b_0_str, 3); - sha.update(dst, dst_len); - sha.update(dstlen_str, 1); - sha.digest(b_0); - uint8_t b_i[SHA256HashSize + 1] = { 0, }; - for (unsigned i = 1; i <= ell; ++i) { - for (unsigned j = 0; j < SHA256HashSize; ++j) { - b_i[j] = b_0[j] ^ b_i[j]; - } - b_i[SHA256HashSize] = i; - bls12_381::sha256 s; - s.update(b_i, SHA256HashSize + 1); - s.update(dst, dst_len); - s.update(dstlen_str, 1); - s.digest(b_i); - const int rem_after = buf_len - i * SHA256HashSize; - const int copy_len = SHA256HashSize + (rem_after < 0 ? rem_after : 0); - memcpy(buf + (i - 1) * SHA256HashSize, b_i, copy_len); - } + [[eosio::action]] + void popverifyraw(const std::vector& pk, const std::vector& sig) { + check(pk.size() == std::tuple_size::value, "wrong public key size passed"); + check(sig.size() == std::tuple_size::value, "wrong signature size passed"); + check(bls_pop_verify(*reinterpret_cast(pk.data()), *reinterpret_cast(sig.data())), "raw pop verify failed"); } - bls_s scalar_fromBE(const bls_s& in) { - std::array out; - for(uint64_t i = 0; i < 8; i++) { - uint64_t temp; - memcpy(&temp, &in[in.size() - i*8 - 8], sizeof(uint64_t)); - out[i] = htobe64(temp); - } - return reinterpret_cast(std::move(out)); + [[eosio::action]] + void popverify(const std::string& pk, const std::string& sig) { + check(bls_pop_verify(bls_base64_to_g1_affine(pk.data(), pk.size()), bls_base64_to_sig_affine(sig.data(), sig.size())), "pop verify failed"); } - void g2_fromMessage(const std::vector& msg, const std::string& dst, bls_g2& res) { - - std::array buf; - xmd_sh256(buf.data()->data(), sizeof(buf), msg.data(), msg.size(), dst.data(), dst.length()); - - bls_s k; - bls_fp2 t; - bls_g2 p, q; - - k = scalar_fromBE(buf[0]); - bls_fp_mod(k, t[0]); - k = scalar_fromBE(buf[1]); - bls_fp_mod(k, t[1]); - - bls_g2_map(t, p); - - k = scalar_fromBE(buf[2]); - bls_fp_mod(k, t[0]); - k = scalar_fromBE(buf[3]); - bls_fp_mod(k, t[1]); + [[eosio::action]] + void g1baseb4enc(const std::vector& g1, const std::string& base64 ) { + check(g1.size() == std::tuple_size::value, "wrong g1 size passed"); - bls_g2_map(t, q); - bls_g2_add(p, q, res); + check(bls_g1_affine_to_base64(*reinterpret_cast(g1.data())) == base64, "g1 to base64 encoding doesn't match" ); + check(bls_base64_to_g1_affine(base64.data(), base64.size()) == *reinterpret_cast(g1.data()), "base64 to g1 decoding doesn't match" ); } - const std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; - const std::vector G1_ONE_NEG = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; - const std::vector GT_ONE = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - - std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; - [[eosio::action]] - void verify(const std::vector& pk, const std::vector& sig) { - bls_g1 g1_points[2]; - bls_g2 g2_points[2]; - - memcpy(g1_points[0].data(), G1_ONE_NEG.data(), sizeof(bls_g1)); - memcpy(g2_points[0].data(), sig.data(), sizeof(bls_g2)); + void sigbaseb4enc(const std::vector& g2, const std::string& base64) { + check(g2.size() == std::tuple_size::value, "wrong g2 size passed"); - bls_g2 p_msg; - g2_fromMessage(msg, CIPHERSUITE_ID, p_msg); - memcpy(g1_points[1].data(), pk.data(), sizeof(bls_g1)); - memcpy(g2_points[1].data(), p_msg.data(), sizeof(bls_g2)); - - bls_gt r; - bls_pairing(g1_points, g2_points, 2, r); - check(0 == memcmp(r.data(), GT_ONE.data(), sizeof(bls_gt)), "bls signature verify failed"); + check(bls_sig_to_base64_affine(*reinterpret_cast(g2.data())) == base64, "g2 to base64 encoding doesn't match" ); + check(bls_base64_to_sig_affine(base64.data(), base64.size()) == *reinterpret_cast(g2.data()), "base64 to g2 decoding doesn't match" ); } }; From 1dc5b7d8d6aa510a0e2849c4e72e3ba88b704914 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 14 Sep 2023 16:43:51 -0400 Subject: [PATCH 065/158] key type switched to affine --- tests/integration/set_finalizers_tests.cpp | 36 +++++++++------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp index 69c8a659fa..6d42e5118e 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/set_finalizers_tests.cpp @@ -17,7 +17,7 @@ struct finalizer_authority { std::string description; uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold - std::array public_key; + std::array public_key; }; @@ -36,25 +36,19 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { std::optional ext = cur_block->extract_header_extension(hs_finalizer_set_extension::extension_id()); BOOST_CHECK(!ext); - const std::vector G1 = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, - 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, - 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, - 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, - 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, - 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, - 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, - 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, - 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, - 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, - 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, - 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, - 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, - 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, - 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, - 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, - 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, - 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; // 18x8 = 144 bytes - + const std::vector G1 = {0x22, 0x0e, 0xf5, 0xc4, 0x9c, 0x18, 0x68, 0xe8, + 0x5b, 0x82, 0x65, 0x8d, 0xf9, 0x76, 0x6f, 0xc2, + 0xce, 0xe4, 0xbd, 0x0b, 0x5d, 0x98, 0x80, 0xd4, + 0xcd, 0xee, 0x91, 0x39, 0xed, 0xd3, 0xc4, 0xeb, + 0xda, 0xe4, 0x07, 0x4f, 0x1d, 0x3d, 0xb3, 0xf3, + 0xc9, 0x21, 0x39, 0x62, 0x94, 0x2e, 0xef, 0x09, + 0x1b, 0x00, 0xd9, 0xf2, 0xa9, 0xb8, 0x37, 0x01, + 0x5c, 0x8d, 0xbe, 0x50, 0x7f, 0x24, 0x2a, 0xee, + 0x45, 0x92, 0x72, 0x58, 0x9b, 0x6b, 0x29, 0x73, + 0xbc, 0xf3, 0x3e, 0xb4, 0x60, 0x87, 0x22, 0xc7, + 0x0c, 0x1a, 0xc3, 0xd1, 0xad, 0xe4, 0x0c, 0x84, + 0x5b, 0x9e, 0x15, 0xea, 0x63, 0x80, 0x08, 0x0a}; // 12x8 = 96 bytes + push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() ("fin_set", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() @@ -66,7 +60,7 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { BOOST_CHECK(ext); auto finalizers = std::get(*ext).finalizers; BOOST_CHECK(finalizers.size() == 1 ); - BOOST_CHECK(finalizers[0].public_key == *reinterpret_cast*>(G1.data()) ); + BOOST_CHECK(finalizers[0].description == "test_desc" ); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() From 8d6265dd983b7303fc7e0136a501ffc69769d3b5 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Fri, 15 Sep 2023 19:44:46 -0400 Subject: [PATCH 066/158] use abi_serializer instead of structure definition --- tests/integration/set_finalizers_tests.cpp | 31 ++++++++-------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp index 6d42e5118e..81fdebf421 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/set_finalizers_tests.cpp @@ -12,17 +12,6 @@ using namespace fc; using mvo = fc::mutable_variant_object; -namespace eosio::chain { -struct finalizer_authority { - - std::string description; - uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold - std::array public_key; - -}; - -} // eosio::chain - BOOST_AUTO_TEST_SUITE(set_finalizers_tests) BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { @@ -32,9 +21,7 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { set_code( config::system_account_name, contracts::set_finalizers_test_wasm() ); set_abi( config::system_account_name, contracts::set_finalizers_test_abi().data() ); - signed_block_ptr cur_block = produce_block(); - std::optional ext = cur_block->extract_header_extension(hs_finalizer_set_extension::extension_id()); - BOOST_CHECK(!ext); + produce_block(); const std::vector G1 = {0x22, 0x0e, 0xf5, 0xc4, 0x9c, 0x18, 0x68, 0xe8, 0x5b, 0x82, 0x65, 0x8d, 0xf9, 0x76, 0x6f, 0xc2, @@ -55,12 +42,16 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { ("description", "test_desc") ("fweight", 1) ("public_key_g1_affine_le", *reinterpret_cast*>(&G1))}))); - cur_block = produce_block(); - ext = cur_block->extract_header_extension(hs_finalizer_set_extension::extension_id()); - BOOST_CHECK(ext); - auto finalizers = std::get(*ext).finalizers; - BOOST_CHECK(finalizers.size() == 1 ); - BOOST_CHECK(finalizers[0].description == "test_desc" ); + signed_block_ptr cur_block = produce_block(); + fc::variant pretty_output; + abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); + BOOST_ASSERT(pretty_output.get_object().contains("proposed_finalizer_set")); + BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["generation"] == 1); + BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["fthreshold"] == 1); + BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["finalizers"].size() == 1); + BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["finalizers"][size_t(0)]["description"] == "test_desc"); + BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["finalizers"][size_t(0)]["fweight"] == 1); + //TODO: add key check here after base64 support will be added // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() From 6f637b05cb100221c5da3382b4d41054951a893a Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Fri, 15 Sep 2023 20:40:09 -0400 Subject: [PATCH 067/158] #221 concerns addressed --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 1d140414ee..1bce39544c 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -228,7 +228,7 @@ namespace eosio { using bls_g2_affine = std::array; using bls_gt = std::array; - int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { + inline int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { return internal_use_do_not_use::bls_g1_add( op1.data(), sizeof(bls_g1), @@ -239,7 +239,7 @@ namespace eosio { ); } - int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) { + inline int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) { return internal_use_do_not_use::bls_g2_add( op1.data(), sizeof(bls_g2), @@ -250,7 +250,7 @@ namespace eosio { ); } - int32_t bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) { + inline int32_t bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) { return internal_use_do_not_use::bls_g1_mul( point.data(), sizeof(bls_g1), @@ -261,7 +261,7 @@ namespace eosio { ); } - int32_t bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) { + inline int32_t bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) { return internal_use_do_not_use::bls_g2_mul( point.data(), sizeof(bls_g2), @@ -272,7 +272,7 @@ namespace eosio { ); } - int32_t bls_g1_exp(const bls_g1 points[], const bls_scalar scalars[], const uint32_t num, bls_g1& res) { + inline int32_t bls_g1_exp(const bls_g1 points[], const bls_scalar scalars[], const uint32_t num, bls_g1& res) { return internal_use_do_not_use::bls_g1_exp( num ? points[0].data() : nullptr, num * sizeof(bls_g1), @@ -284,7 +284,7 @@ namespace eosio { ); } - int32_t bls_g2_exp(const bls_g2 points[], const bls_scalar scalars[], const uint32_t num, bls_g2& res) { + inline int32_t bls_g2_exp(const bls_g2 points[], const bls_scalar scalars[], const uint32_t num, bls_g2& res) { return internal_use_do_not_use::bls_g2_exp( num ? points[0].data() : nullptr, num * sizeof(bls_g2), @@ -296,7 +296,7 @@ namespace eosio { ); } - int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { + inline int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { return internal_use_do_not_use::bls_pairing( num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1), @@ -308,7 +308,7 @@ namespace eosio { ); } - int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { + inline int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { return internal_use_do_not_use::bls_g1_map( e.data(), sizeof(bls_fp), @@ -317,7 +317,7 @@ namespace eosio { ); } - int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) { + inline int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) { return internal_use_do_not_use::bls_g2_map( e[0].data(), sizeof(bls_fp2), @@ -326,7 +326,7 @@ namespace eosio { ); } - int32_t bls_fp_mod(const bls_s& s, bls_fp& res) { + inline int32_t bls_fp_mod(const bls_s& s, bls_fp& res) { return internal_use_do_not_use::bls_fp_mod( s.data(), sizeof(bls_s), @@ -335,6 +335,7 @@ namespace eosio { ); } +namespace detail { const inline std::string bls_public_key_prefix = "PUB_BLS_"; const inline uint32_t bls_publick_key_checksum_size = sizeof(uint32_t); const inline std::string bls_signature_prefix = "SIG_BLS_"; @@ -349,17 +350,14 @@ namespace eosio { reinterpret_cast(csum.data())+bls_publick_key_checksum_size, it); - return Prefix + base64_encode(g1_with_checksum.data(), g1_with_checksum.size()); - } - inline std::string bls_g1_affine_to_base64(const bls_g1_affine& g1) { - return bls_type_to_base64(g1); + return Prefix + eosio::base64_encode(g1_with_checksum.data(), g1_with_checksum.size()); } template T bls_base64_to_type(const char* data, size_t size) { eosio::check(size > Prefix.size(), "encoded base64 key is too short"); eosio::check(0 == memcmp(data, Prefix.data(), Prefix.size()), "base64 encoded type must begin from corresponding prefix"); - std::string decoded = base64_decode({data+Prefix.size(), size - Prefix.size()}); + std::string decoded = eosio::base64_decode({data+Prefix.size(), size - Prefix.size()}); T ret; eosio::check(decoded.size() == ret.size() + bls_publick_key_checksum_size, "decoded size " + std::to_string(decoded.size()) + " doesn't match structure size " + std::to_string(ret.size()) + @@ -374,15 +372,6 @@ namespace eosio { return ret; } - inline bls_g1_affine bls_base64_to_g1_affine(const char* data, size_t size) { - return bls_base64_to_type(data, size); - } - inline std::string bls_sig_to_base64_affine(const bls_g2_affine& g2) { - return bls_type_to_base64(g2); - } - inline bls_g2_affine bls_base64_to_sig_affine(const char* data, size_t size) { - return bls_base64_to_type(data, size); - } const inline std::string POP_CIPHERSUITE_ID = "BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; const inline std::vector G1_ONE_NEG = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, @@ -562,9 +551,25 @@ namespace eosio { bls_g2_map(t, q); bls_g2_add(p, q, res); } +} + + inline std::string bls_g1_affine_to_base64(const bls_g1_affine& g1) { + return detail::bls_type_to_base64(g1); + } + inline bls_g1_affine bls_base64_to_g1_affine(const char* data, size_t size) { + return detail::bls_base64_to_type(data, size); + } + inline std::string bls_sig_to_base64_affine(const bls_g2_affine& g2) { + return detail::bls_type_to_base64(g2); + } + inline bls_g2_affine bls_base64_to_sig_affine(const char* data, size_t size) { + return detail::bls_base64_to_type(data, size); + } // pubkey and signature are assumed to be in RAW affine little-endian bytes bool bls_pop_verify(const bls_g1_affine& pubkey, const bls_g2_affine& signature_proof) { + using namespace detail; + bls_g1 g1_points[2] = {0}; bls_g2 g2_points[2] = {0}; @@ -587,6 +592,5 @@ namespace eosio { bls_pairing(g1_points, g2_points, 2, r); return 0 == std::memcmp(r.data(), GT_ONE.data(), sizeof(bls_gt)); } - } From 93f6a186aa64edd45e71d1a45464e3b9a496bb7d Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Mon, 18 Sep 2023 22:43:57 -0400 Subject: [PATCH 068/158] improve abi generation --- .../abigen-pass/singleton_contract.abi | 17 ++++ .../abigen-pass/singleton_contract.cpp | 6 ++ tools/include/eosio/abigen.hpp | 91 ++++++++++++------- 3 files changed, 81 insertions(+), 33 deletions(-) diff --git a/tests/toolchain/abigen-pass/singleton_contract.abi b/tests/toolchain/abigen-pass/singleton_contract.abi index 6cdc4a8c72..86c6fe4927 100644 --- a/tests/toolchain/abigen-pass/singleton_contract.abi +++ b/tests/toolchain/abigen-pass/singleton_contract.abi @@ -3,6 +3,16 @@ "version": "eosio::abi/1.2", "types": [], "structs": [ + { + "name": "out_of_class3", + "base": "", + "fields": [ + { + "name": "id", + "type": "uint64" + } + ] + }, { "name": "tbl_config", "base": "", @@ -45,6 +55,13 @@ "key_names": [], "key_types": [] }, + { + "name": "mi.config52", + "type": "out_of_class3", + "index_type": "i64", + "key_names": [], + "key_types": [] + }, { "name": "smpl.conf5", "type": "name", diff --git a/tests/toolchain/abigen-pass/singleton_contract.cpp b/tests/toolchain/abigen-pass/singleton_contract.cpp index f1999c27bc..0a0c96c9bc 100644 --- a/tests/toolchain/abigen-pass/singleton_contract.cpp +++ b/tests/toolchain/abigen-pass/singleton_contract.cpp @@ -14,6 +14,12 @@ struct [[eosio::table]] out_of_class2 { typedef eosio::multi_index<"mi.config5"_n, out_of_class2> out_of_class_index51; using uout_of_class_index51 = eosio::multi_index<"mi.config51"_n, out_of_class2>; +struct [[eosio::table, eosio::contract("singleton_contract")]] out_of_class3 { + uint64_t id; + uint64_t primary_key() const { return id; } +}; +typedef eosio::multi_index<"mi.config52"_n, out_of_class3> out_of_class_index52; + typedef eosio::singleton<"smpl.conf5"_n, eosio::name> smpl_config5; typedef eosio::singleton<"config5"_n, out_of_class2> config5; typedef smpl_config5 smpl_config51; diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 9d83babcd2..2dc4317e37 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -243,13 +243,11 @@ namespace eosio { namespace cdt { ctables.insert(t); } - void add_table( uint64_t name, const clang::CXXRecordDecl* decl, bool force=false ) { - if (force || decl->isEosioTable() && abigen::is_eosio_contract(decl, get_contract_name())) { - abi_table t; - t.type = decl->getNameAsString(); - t.name = name_to_string(name); - _abi.tables.insert(t); - } + void add_table( uint64_t name, const clang::CXXRecordDecl* decl ) { + abi_table t; + t.type = decl->getNameAsString(); + t.name = name_to_string(name); + _abi.tables.insert(t); } void add_clauses( const std::vector>& clauses ) { @@ -801,24 +799,6 @@ namespace eosio { namespace cdt { return true; } - const clang::CXXRecordDecl* find_contract_class(const clang::ASTContext &ctx) const { - const auto* translation_unit = ctx.getTranslationUnitDecl(); - // scanning entire translation unit to find contract class - for (const clang::Decl* cur_decl : translation_unit->decls()) { - if (const auto* cxx_decl = llvm::dyn_cast(cur_decl)) { - - if (cxx_decl->isEosioContract()) { - auto attr_name = cxx_decl->getEosioContractAttr()->getName(); - auto name = attr_name.empty() ? cxx_decl->getName() : attr_name; - if (name == llvm::StringRef(ag.get_contract_name())) - return cxx_decl; - } - } - } - - return nullptr; - } - bool is_same_type(const clang::Decl* decl1, const clang::CXXRecordDecl* decl2) const { if (!decl1 || !decl2) return false; @@ -840,12 +820,9 @@ namespace eosio { namespace cdt { bool defined_in_contract(const clang::ClassTemplateSpecializationDecl* decl) { if (!contract_class) { - contract_class = find_contract_class(decl->getASTContext()); - if (!contract_class) { // currently this is unreachable as we do not traverse non-main file translation units - CDT_WARN("codegen_warning", decl->getLocation(), "contract class not found"); + CDT_WARN("codegen_warning", decl->getLocation(), "contract class not found: " + ag.get_contract_name()); return false; - } } for (const clang::Decl* cur_decl : contract_class->decls()) { @@ -858,16 +835,60 @@ namespace eosio { namespace cdt { virtual bool VisitDecl(clang::Decl* decl) { if (const auto* d = dyn_cast(decl)) { - bool is_singleton = d->getName() == "singleton"; - if ((d->getName() == "multi_index" || is_singleton) && defined_in_contract(d)) { - ag.add_table(d->getTemplateArgs()[0].getAsIntegral().getExtValue(), - d->getTemplateArgs()[1].getAsType().getTypePtr()->getAsCXXRecordDecl(), is_singleton); + if (d->getName() == "multi_index" || d->getName() == "singleton") { + // second template parameter is table type + const auto* table_type = d->getTemplateArgs()[1].getAsType().getTypePtr()->getAsCXXRecordDecl(); + if ((table_type->isEosioTable() && ag.is_eosio_contract(table_type, ag.get_contract_name())) || defined_in_contract(d)) { + // first parameter is table name + ag.add_table(d->getTemplateArgs()[0].getAsIntegral().getExtValue(), table_type); + } } } return true; } + void set_contract_class(const CXXRecordDecl* decl) { + contract_class = decl; + } }; + class contract_class_finder : public RecursiveASTVisitor { + private: + abigen& ag = abigen::get(); + const clang::CXXRecordDecl* contract_class = nullptr; + public: + virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl* cxx_decl) { + if (cxx_decl->isEosioContract()) { + bool is_eosio_contract = false; + // on this point it could be just an attribute so let's check base classes + for (const auto& base : cxx_decl->bases()) { + if (const clang::Type *base_type = base.getType().getTypePtrOrNull()) { + if (const auto* cur_cxx_decl = base_type->getAsCXXRecordDecl()) { + if (cur_cxx_decl->getQualifiedNameAsString() == "eosio::contract") { + is_eosio_contract = true; + break; + } + } + } + } + if (!is_eosio_contract) + return true; + + auto attr_name = cxx_decl->getEosioContractAttr()->getName(); + auto name = attr_name.empty() ? cxx_decl->getName() : attr_name; + if (name == llvm::StringRef(ag.get_contract_name())) { + contract_class = cxx_decl; + return false; + } + } + return true; + } + bool contract_found() const { + return contract_class != nullptr; + } + const clang::CXXRecordDecl* get_contract() const { + return contract_class; + } + }; class eosio_abigen_consumer : public ASTConsumer { private: eosio_abigen_visitor *visitor; @@ -883,6 +904,10 @@ namespace eosio { namespace cdt { auto& f_mgr = src_mgr.getFileManager(); auto main_fe = f_mgr.getFile(main_file); if (main_fe) { + contract_class_finder cf; + cf.TraverseDecl(Context.getTranslationUnitDecl()); + if (cf.contract_found()) + visitor->set_contract_class(cf.get_contract()); visitor->TraverseDecl(Context.getTranslationUnitDecl()); } } From d6721b2d66274c65bfa29998f91b9b1a3f42be7a Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Tue, 19 Sep 2023 00:00:38 -0400 Subject: [PATCH 069/158] fixing integration tests --- tests/toolchain/abigen-pass/singleton_contract.abi | 10 ++++++++++ tools/include/eosio/abigen.hpp | 8 +++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/toolchain/abigen-pass/singleton_contract.abi b/tests/toolchain/abigen-pass/singleton_contract.abi index 86c6fe4927..4adaedf6d8 100644 --- a/tests/toolchain/abigen-pass/singleton_contract.abi +++ b/tests/toolchain/abigen-pass/singleton_contract.abi @@ -3,6 +3,16 @@ "version": "eosio::abi/1.2", "types": [], "structs": [ + { + "name": "out_of_class", + "base": "", + "fields": [ + { + "name": "id", + "type": "uint64" + } + ] + }, { "name": "out_of_class3", "base": "", diff --git a/tools/include/eosio/abigen.hpp b/tools/include/eosio/abigen.hpp index 2dc4317e37..5836b04dcf 100644 --- a/tools/include/eosio/abigen.hpp +++ b/tools/include/eosio/abigen.hpp @@ -841,12 +841,14 @@ namespace eosio { namespace cdt { if ((table_type->isEosioTable() && ag.is_eosio_contract(table_type, ag.get_contract_name())) || defined_in_contract(d)) { // first parameter is table name ag.add_table(d->getTemplateArgs()[0].getAsIntegral().getExtValue(), table_type); + if (table_type->isEosioTable()) + ag.add_struct(table_type); } } } return true; } - void set_contract_class(const CXXRecordDecl* decl) { + inline void set_contract_class(const CXXRecordDecl* decl) { contract_class = decl; } }; @@ -882,10 +884,10 @@ namespace eosio { namespace cdt { return true; } - bool contract_found() const { + inline bool contract_found() const { return contract_class != nullptr; } - const clang::CXXRecordDecl* get_contract() const { + inline const clang::CXXRecordDecl* get_contract() const { return contract_class; } }; From f88520b2f126ac9defd6a2503898332468b0e71d Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Tue, 19 Sep 2023 15:09:45 -0700 Subject: [PATCH 070/158] updated wording on LLVM --- docs/roadmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/roadmap.md b/docs/roadmap.md index cbf3a0bc48..272c73d0e0 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -15,7 +15,7 @@ We lack the resources to complete the Antler project and support it going forwar Allow contracts to compile C++20. In addition to the benefits from the latest language features. Currently, ENF maintains an additional fork of one of our upstream dependencies for EOS EVM simply to get around the fact that the upstream assumes C++20 but we cannot build C++20 code in our contracts. ## Vanilla Clang/LLVM -Remove the custom extensions to Clang/LLVM. This will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. +Try eosio extension free llvm and if it works with no issues then remove extensions to Clang/LLVM. This hope we will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. ## Upgrade to LLVM 16 Upgrade to the latest From 6b55a4f119c384844c8278709fd5b6d3715e4341 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Tue, 19 Sep 2023 15:13:05 -0700 Subject: [PATCH 071/158] spelling error --- docs/roadmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/roadmap.md b/docs/roadmap.md index 272c73d0e0..f52d7547d4 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -15,7 +15,7 @@ We lack the resources to complete the Antler project and support it going forwar Allow contracts to compile C++20. In addition to the benefits from the latest language features. Currently, ENF maintains an additional fork of one of our upstream dependencies for EOS EVM simply to get around the fact that the upstream assumes C++20 but we cannot build C++20 code in our contracts. ## Vanilla Clang/LLVM -Try eosio extension free llvm and if it works with no issues then remove extensions to Clang/LLVM. This hope we will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. +Try eosio extension free llvm and if it works with no issues then remove extensions to Clang/LLVM. The hope we will enable us to use Vanilla versions of the packages. This will allow us to use the latest, and will lead to improvements in functionality and performance. ## Upgrade to LLVM 16 Upgrade to the latest From 4b6430ed3ad09e962a5b7b51a223f27b9ed5a0e1 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:49:50 -0400 Subject: [PATCH 072/158] Revert "added shared object option to CDTMacros" This reverts commit 7efd2580d1842ed594c9f1a9085e45cea034e9d4. --- modules/CDTMacros.cmake.in | 16 +------ modules/CDTWasmToolchain.cmake.in | 1 - tools/include/compiler_options.hpp.in | 64 ++++++--------------------- 3 files changed, 14 insertions(+), 67 deletions(-) diff --git a/modules/CDTMacros.cmake.in b/modules/CDTMacros.cmake.in index 99ddd0ee8b..f40559c093 100644 --- a/modules/CDTMacros.cmake.in +++ b/modules/CDTMacros.cmake.in @@ -20,7 +20,7 @@ endmacro() macro (add_native_library TARGET) add_library( ${TARGET} ${ARGN} ) - target_compile_options( ${TARGET} PUBLIC -fnative -fPIC ) + target_compile_options( ${TARGET} PUBLIC -fnative ) endmacro() macro (add_native_executable TARGET) @@ -37,17 +37,3 @@ macro (add_native_executable TARGET) endif() endmacro() -macro (add_native_shared_lib TARGET) - set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE) - add_library( ${TARGET} SHARED ${ARGN} ) - target_compile_options( ${TARGET} PUBLIC -fnative ) - get_target_property(BINOUTPUT ${TARGET} BINARY_DIR) - if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" AND APPLE) - target_compile_options( ${TARGET} PUBLIC -g ) - find_program ( name NAMES "dsymutil" ) - if ( name ) - add_custom_command( TARGET ${TARGET} POST_BUILD COMMAND dsymutil ${BINOUTPUT}/${TARGET} ) - endif() - endif() -endmacro() - diff --git a/modules/CDTWasmToolchain.cmake.in b/modules/CDTWasmToolchain.cmake.in index 4343fdb787..c22b1a7a85 100644 --- a/modules/CDTWasmToolchain.cmake.in +++ b/modules/CDTWasmToolchain.cmake.in @@ -21,7 +21,6 @@ set(WASM_LINKER "@CDT_ROOT_DIR@/bin/cdt-ld") set(CMAKE_C_LINK_EXECUTABLE "${WASM_LINKER} -o ") set(CMAKE_CXX_LINK_EXECUTABLE "${WASM_LINKER} -o ") -set(CMAKE_CXX_CREATE_SHARED_LIBRARY "${WASM_LINKER} -shared -o ") set(CMAKE_EXECUTABLE_SUFFIX_C ".wasm") set(CMAKE_EXECUTABLE_SUFFIX_CXX ".wasm") diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index 2556101970..ea744efd57 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -131,11 +131,6 @@ static cl::opt fnative_opt( cl::desc("Compile and link for x86-64"), cl::ZeroOrMore, cl::cat(LD_CAT)); -static cl::opt fshared_opt( - "shared", - cl::desc("Make shared object native library"), - cl::ZeroOrMore, - cl::cat(LD_CAT)); static cl::opt fuse_main_opt( "fuse-main", cl::desc("Use main as entry"), @@ -360,11 +355,6 @@ static cl::opt warn_action_read_only_opt( "warn-action-read-only", cl::desc("Issue a warning if a read-only action uses a write API and continue compilation"), cl::cat(EosioCompilerToolCategory)); -static cl::opt fPIC_opt( - "fPIC", - cl::desc("Generate position independent code. This option is used for shared libraries"), - cl::Optional, - cl::cat(LD_CAT)); /// end c/c++ options /// begin c++ options @@ -401,7 +391,6 @@ struct Options { std::vector abigen_resources; bool debug; bool native; - bool shared_lib; std::pair abi_version; bool has_o_opt; bool has_contract_opt; @@ -415,7 +404,7 @@ static void GetCompDefaults(std::vector& copts) { copts.emplace_back(std::string("-D__eosio_cdt_major__=")+"${VERSION_MAJOR}"); copts.emplace_back(std::string("-D__eosio_cdt_minor__=")+"${VERSION_MINOR}"); copts.emplace_back(std::string("-D__eosio_cdt_patchlevel__=")+"${VERSION_PATCH}"); - if (!fnative_opt && !fshared_opt) { + if (!fnative_opt) { copts.emplace_back("--target=wasm32"); copts.emplace_back("-ffreestanding"); copts.emplace_back("-nostdlib"); @@ -450,7 +439,7 @@ static void GetCompDefaults(std::vector& copts) { copts.emplace_back("-DBOOST_DISABLE_ASSERTS"); copts.emplace_back("-DBOOST_EXCEPTION_DISABLE"); copts.emplace_back("-U__APPLE__"); - if (!fnative_opt && !fshared_opt) { + if (!fnative_opt) { copts.emplace_back("-Xclang"); copts.emplace_back("-load"); copts.emplace_back("-Xclang"); @@ -477,7 +466,7 @@ static void GetCompDefaults(std::vector& copts) { #ifdef ONLY_LD static void GetLdDefaults(std::vector& ldopts) { - if (!fnative_opt && !fshared_opt) { + if (!fnative_opt) { if (!allow_names_opt) { ldopts.emplace_back("--gc-sections"); ldopts.emplace_back("--strip-all"); @@ -547,22 +536,13 @@ static void GetLdDefaults(std::vector& ldopts) { #ifdef __APPLE__ ldopts.insert(ldopts.end(), {"-arch", "x86_64", "-macosx_version_min", "10.13", "-framework", "Foundation", "-framework", "System"}); #endif - if (fshared_opt) { - ldopts.emplace_back("-shared"); - } - else { - ldopts.emplace_back("-static"); - } - ldopts.insert(ldopts.end(), {"-Bstatic", "-lnative_c++", "-lnative_c", "-lnative_eosio", "-lnative", "-lnative_rt"}); + ldopts.emplace_back("-static"); + ldopts.insert(ldopts.end(), {"-lnative_c++", "-lnative_c", "-lnative_eosio", "-lnative", "-lnative_rt"}); } } #endif static Options CreateOptions(bool add_defaults=true) { - if (fshared_opt && fnative_opt) { - throw std::runtime_error("Both -fnative and -fshared was specified. Only one of them should be present."); - } - std::string output_fn; std::vector inputs; std::vector agresources; @@ -742,7 +722,7 @@ static Options CreateOptions(bool add_defaults=true) { copts.emplace_back("-I"+sysroot_opt+"/include/libc"); // only allow capi for native builds and for eosio-cc - if (fnative_opt || fshared_opt) { + if (fnative_opt) { copts.emplace_back("-I"+sysroot_opt+"/include/eosiolib/capi"); copts.emplace_back("-I"+sysroot_opt+"/include/eosiolib/native"); } @@ -768,7 +748,7 @@ static Options CreateOptions(bool add_defaults=true) { agopts.emplace_back("--sysroot="+eosio::cdt::whereami::where()+"/../"); ldopts.emplace_back("-L"+eosio::cdt::whereami::where()+"/../lib"); - if (fnative_opt || fshared_opt) { + if (fnative_opt) { copts.emplace_back("-I"+eosio::cdt::whereami::where()+"/../include/eosiolib/capi"); copts.emplace_back("-I"+eosio::cdt::whereami::where()+"/../include/eosiolib/native"); } @@ -807,12 +787,8 @@ static Options CreateOptions(bool add_defaults=true) { copts.emplace_back("-W"+warn); } - if (fPIC_opt) { - copts.emplace_back("-fPIC"); - } - #endif - if (!fnative_opt && !fshared_opt) { + if (!fnative_opt) { #ifdef ONLY_LD if (stack_canary_opt) { ldopts.emplace_back("--stack-canary"); @@ -852,22 +828,11 @@ static Options CreateOptions(bool add_defaults=true) { for ( auto library : l_opt ) { ldopts.emplace_back("-l"+library); } - auto replace_extension = [&](auto& str){ - if (fnative_opt) { - llvm::sys::path::replace_extension(str, ""); - } - else if (fshared_opt) { - llvm::sys::path::replace_extension(str, ".so"); - } - else { - llvm::sys::path::replace_extension(str, ".wasm"); - } - }; if (o_opt.empty()) { #ifndef ONLY_LD if (inputs.size() == 1) { llvm::SmallString<256> fn = llvm::sys::path::filename(inputs[0]); - replace_extension(fn); + llvm::sys::path::replace_extension(fn, fnative_opt ? "" : ".wasm"); output_fn = fn.str(); } else { output_fn = "a.out"; @@ -878,7 +843,7 @@ static Options CreateOptions(bool add_defaults=true) { if (inputs.size() == 1) { llvm::SmallString<256> fn = llvm::sys::path::filename(inputs[0]); llvm::sys::path::replace_extension(fn, ""); - replace_extension(fn); + llvm::sys::path::replace_extension(fn, fnative_opt ? "" : ".wasm"); output_fn = fn.str(); ldopts.emplace_back("-o"+output_fn); } else { @@ -895,7 +860,7 @@ static Options CreateOptions(bool add_defaults=true) { has_o_opt = true; } - if (!fnative_opt && !fshared_opt) { + if (!fnative_opt) { #ifndef ONLY_LD if (!imports_opt.empty()) { ldopts.emplace_back("-imports="+imports_opt); @@ -952,9 +917,6 @@ static Options CreateOptions(bool add_defaults=true) { } if (fnative_opt) ldopts.emplace_back("-fnative"); - else if (fshared_opt) { - ldopts.emplace_back("-shared"); - } if (fuse_main_opt) ldopts.emplace_back("-fuse-main"); @@ -977,8 +939,8 @@ static Options CreateOptions(bool add_defaults=true) { } #ifndef ONLY_LD - return {output_fn, inputs, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, fshared_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only}; + return {output_fn, inputs, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only}; #else - return {output_fn, {}, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, fshared_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only}; + return {output_fn, {}, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only}; #endif } From 87672eedb5a2c50ea59fa421e18d22be6af8de2c Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:50:28 -0400 Subject: [PATCH 073/158] Revert "cdt-ld shared-object support added" This reverts commit 4d0173410a936bd53e9293219d73282353dff9aa. --- tools/ld/cdt-ld.cpp.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/ld/cdt-ld.cpp.in b/tools/ld/cdt-ld.cpp.in index 89fda6a7cd..6bb649c800 100644 --- a/tools/ld/cdt-ld.cpp.in +++ b/tools/ld/cdt-ld.cpp.in @@ -22,7 +22,7 @@ int main(int argc, const char **argv) { Options opts = CreateOptions(); std::string line; - if (opts.native || opts.shared_lib) { + if (opts.native) { #ifdef __APPLE__ if (!eosio::cdt::environment::exec_subprogram("ld", opts.ld_options, true)) #else @@ -41,7 +41,7 @@ int main(int argc, const char **argv) { } // finally any post processing - if (!fno_post_pass_opt && !opts.native && !opts.shared_lib) { + if (!fno_post_pass_opt && !opts.native) { if ( !llvm::sys::fs::exists( opts.eosio_pp_dir+"/eosio-pp" ) ) { std::cerr << "Error: eosio.pp not found! (Try reinstalling eosio.cdt)" << std::endl; return -1; From ea06f6cc826facde881a7302576a4d1ed7046cff Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:56:05 -0400 Subject: [PATCH 074/158] Revert "support of longjmp frpm shared object" This reverts commit d1d41aa5f761ddebcb31b2090177fae5dfc30655. --- libraries/native/crt.cpp | 3 --- libraries/native/crt_lib.cpp | 3 --- libraries/native/native/eosio/crt.hpp | 12 +++++------- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/libraries/native/crt.cpp b/libraries/native/crt.cpp index d75c6c44f6..112820eda0 100644 --- a/libraries/native/crt.cpp +++ b/libraries/native/crt.cpp @@ -64,9 +64,6 @@ extern "C" { } } - jmp_buf* __get_jmp_buf() { - return ___env_ptr; - } void __set_env_test() { ___env_ptr = &test_env; } diff --git a/libraries/native/crt_lib.cpp b/libraries/native/crt_lib.cpp index 1f5021b257..4201a0149e 100644 --- a/libraries/native/crt_lib.cpp +++ b/libraries/native/crt_lib.cpp @@ -12,9 +12,6 @@ static jmp_buf env; static jmp_buf test_env; jmp_buf* ___env_ptr = &env; -jmp_buf* __get_jmp_buf() { - return ___env_ptr; -} void __set_env_test() { ___env_ptr = &test_env; } diff --git a/libraries/native/native/eosio/crt.hpp b/libraries/native/native/eosio/crt.hpp index 9e9d23e3fb..7a08f1d863 100644 --- a/libraries/native/native/eosio/crt.hpp +++ b/libraries/native/native/eosio/crt.hpp @@ -26,15 +26,13 @@ namespace eosio { namespace cdt { extern eosio::cdt::output_stream std_out; extern eosio::cdt::output_stream std_err; +extern "C" jmp_buf* ___env_ptr; +extern "C" char* ___heap_ptr; +extern "C" bool ___disable_output; +extern "C" bool ___has_failed; +extern "C" bool ___earlier_unit_test_has_failed; extern "C" { - extern jmp_buf* ___env_ptr; - extern char* ___heap_ptr; - extern bool ___disable_output; - extern bool ___has_failed; - extern bool ___earlier_unit_test_has_failed; - - jmp_buf* __get_jmp_buf(); void __set_env_test(); void __reset_env(); void _prints_l(const char* cstr, uint32_t len, uint8_t which); From e2d7bfbf79370a2c2185c89951f1cbb0be5490cb Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:56:39 -0400 Subject: [PATCH 075/158] Revert "reverting exceptions to jumps" This reverts commit ea20dd01ce6c56620baaec15e824c7da1b2837ca. --- libraries/native/CMakeLists.txt | 1 + libraries/native/crt_lib.cpp | 10 ---------- libraries/native/intrinsics.cpp | 18 +++++++++++++++--- libraries/native/native/eosio/crt.hpp | 8 ++++++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/libraries/native/CMakeLists.txt b/libraries/native/CMakeLists.txt index 19ad02f238..60159e66c9 100644 --- a/libraries/native/CMakeLists.txt +++ b/libraries/native/CMakeLists.txt @@ -339,6 +339,7 @@ target_include_directories( sf PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/sou if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt_lib.cpp) + target_compile_definitions(${PROJECT_NAME} PUBLIC NATIVELIB_ENABLE_EXCEPTIONS) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/contracts ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/capi diff --git a/libraries/native/crt_lib.cpp b/libraries/native/crt_lib.cpp index 4201a0149e..75fc674567 100644 --- a/libraries/native/crt_lib.cpp +++ b/libraries/native/crt_lib.cpp @@ -8,16 +8,6 @@ eosio::cdt::output_stream std_err; bool ___disable_output = false; bool ___has_failed = false; bool ___earlier_unit_test_has_failed = false; -static jmp_buf env; -static jmp_buf test_env; -jmp_buf* ___env_ptr = &env; - -void __set_env_test() { - ___env_ptr = &test_env; -} -void __reset_env() { - ___env_ptr = &env; -} void _prints_l(const char* cstr, uint32_t len, uint8_t which) { for (int i=0; i < len; i++) { diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index a1db008f82..ab886df0f7 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -13,6 +13,18 @@ #include #include +#ifdef NATIVELIB_ENABLE_EXCEPTIONS +#define RAISE_ERROR_CHOOSE(_1,_2,NAME,...) NAME +#define RAISE_ERROR1(msg) \ + throw eosio::cdt::assert_exception((const char*)msg) +#define RAISE_ERROR2(msg, len) \ + throw eosio::cdt::assert_exception(std::string((const char*)msg, (size_t)len)) +#define RAISE_ERROR(...) RAISE_ERROR_CHOOSE(__VA_ARGS__, RAISE_ERROR2, RAISE_ERROR1)(__VA_ARGS__) +#else +#define RAISE_ERROR(...) \ + longjmp(*___env_ptr, 1) +#endif + // Boilerplate using namespace eosio::native; extern "C" { @@ -861,7 +873,7 @@ extern "C" { if (test == 0) { _prints(msg, eosio::cdt::output_stream_kind::std_err); _prints_l("\n", 1, eosio::cdt::output_stream_kind::none); - longjmp(*___env_ptr, 1); + RAISE_ERROR(msg); } } @@ -869,7 +881,7 @@ extern "C" { if (test == 0) { _prints_l(msg, len, eosio::cdt::output_stream_kind::std_err); _prints_l("\n", 1, eosio::cdt::output_stream_kind::none); - longjmp(*___env_ptr, 1); + RAISE_ERROR(msg, len); } } @@ -879,7 +891,7 @@ extern "C" { snprintf(buff, 32, "%llu", code); _prints(buff, eosio::cdt::output_stream_kind::std_err); _prints_l("\n", 1, eosio::cdt::output_stream_kind::none); - longjmp(*___env_ptr, 1); + RAISE_ERROR(buff); } } diff --git a/libraries/native/native/eosio/crt.hpp b/libraries/native/native/eosio/crt.hpp index 7a08f1d863..d47e441b63 100644 --- a/libraries/native/native/eosio/crt.hpp +++ b/libraries/native/native/eosio/crt.hpp @@ -3,6 +3,14 @@ #include + +#ifdef NATIVELIB_ENABLE_EXCEPTIONS +#include +namespace eosio { namespace cdt { + using assert_exception = std::runtime_error; +}} //ns eosio::cdt +#endif + namespace eosio { namespace cdt { enum output_stream_kind { From 59bb425a254fabea3bde620f25059d3b53a06bea Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:57:08 -0400 Subject: [PATCH 076/158] Revert "native library support of shared object" This reverts commit 0a53b4a2633e0d00f3a4078056312b009a96e8fe. --- libraries/native/intrinsics.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index ab886df0f7..a1db008f82 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -13,18 +13,6 @@ #include #include -#ifdef NATIVELIB_ENABLE_EXCEPTIONS -#define RAISE_ERROR_CHOOSE(_1,_2,NAME,...) NAME -#define RAISE_ERROR1(msg) \ - throw eosio::cdt::assert_exception((const char*)msg) -#define RAISE_ERROR2(msg, len) \ - throw eosio::cdt::assert_exception(std::string((const char*)msg, (size_t)len)) -#define RAISE_ERROR(...) RAISE_ERROR_CHOOSE(__VA_ARGS__, RAISE_ERROR2, RAISE_ERROR1)(__VA_ARGS__) -#else -#define RAISE_ERROR(...) \ - longjmp(*___env_ptr, 1) -#endif - // Boilerplate using namespace eosio::native; extern "C" { @@ -873,7 +861,7 @@ extern "C" { if (test == 0) { _prints(msg, eosio::cdt::output_stream_kind::std_err); _prints_l("\n", 1, eosio::cdt::output_stream_kind::none); - RAISE_ERROR(msg); + longjmp(*___env_ptr, 1); } } @@ -881,7 +869,7 @@ extern "C" { if (test == 0) { _prints_l(msg, len, eosio::cdt::output_stream_kind::std_err); _prints_l("\n", 1, eosio::cdt::output_stream_kind::none); - RAISE_ERROR(msg, len); + longjmp(*___env_ptr, 1); } } @@ -891,7 +879,7 @@ extern "C" { snprintf(buff, 32, "%llu", code); _prints(buff, eosio::cdt::output_stream_kind::std_err); _prints_l("\n", 1, eosio::cdt::output_stream_kind::none); - RAISE_ERROR(buff); + longjmp(*___env_ptr, 1); } } From 7e2efdafdac9e9d918a167ab55e03c70adcafcab Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:57:50 -0400 Subject: [PATCH 077/158] Revert "native library shared object support" This reverts commit 23659dce801aeca802798820b9a3952889fefaa6. --- libraries/native/CMakeLists.txt | 11 +++-- libraries/native/crt_lib.cpp | 32 ------------- libraries/native/elf_crt.s | 66 +-------------------------- libraries/native/mac_utils.s | 60 ++++++++++++++++++++++++ libraries/native/macho_crt.s | 62 +------------------------ libraries/native/native/eosio/crt.hpp | 12 ----- libraries/native/utils.s | 64 ++++++++++++++++++++++++++ 7 files changed, 134 insertions(+), 173 deletions(-) delete mode 100644 libraries/native/crt_lib.cpp create mode 100644 libraries/native/mac_utils.s create mode 100644 libraries/native/utils.s diff --git a/libraries/native/CMakeLists.txt b/libraries/native/CMakeLists.txt index 60159e66c9..be3a1fb9d4 100644 --- a/libraries/native/CMakeLists.txt +++ b/libraries/native/CMakeLists.txt @@ -4,8 +4,10 @@ project(native LANGUAGES CXX ASM) if (NOT __APPLE) set(CRT_ASM elf_crt.s) + set(UTILS_ASM utils.s) else() set(CRT_ASM macho_crt.s) + set(UTILS_ASM mac_utils.s) endif() if (NOT ${PARENT_PROJECT_NAME} MATCHES "cdt_tools") @@ -337,9 +339,12 @@ add_library ( sf STATIC ${softfloat_sources} ) target_include_directories( sf PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR}) if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") - add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt_lib.cpp) + # TODO: we need crt.cpp and assembly only for _prints_l and _prints + # we unlikely need those implementations for antler-run so + # we may need to add some code (macroses, etc) to crt.cpp and/or intrinsics.cpp so + # we can revert assembly changes and build only *.cpp without ${UTILS_ASM} + add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${UTILS_ASM}) - target_compile_definitions(${PROJECT_NAME} PUBLIC NATIVELIB_ENABLE_EXCEPTIONS) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/contracts ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/capi @@ -347,7 +352,7 @@ if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") # suppress executable stack warning. this is due to absence of .note.GNU-stack target_link_libraries(${PROJECT_NAME} PUBLIC "-Wl,-z,noexecstack") else() - add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM}) + add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM} ${UTILS_ASM}) endif() target_include_directories( native PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/eosiolib/capi ${CMAKE_SOURCE_DIR}/eosiolib/contracts ${CMAKE_SOURCE_DIR}/eosiolib/core) diff --git a/libraries/native/crt_lib.cpp b/libraries/native/crt_lib.cpp deleted file mode 100644 index 75fc674567..0000000000 --- a/libraries/native/crt_lib.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "native/eosio/crt.hpp" - -#include - -eosio::cdt::output_stream std_out; -eosio::cdt::output_stream std_err; - -bool ___disable_output = false; -bool ___has_failed = false; -bool ___earlier_unit_test_has_failed = false; - -void _prints_l(const char* cstr, uint32_t len, uint8_t which) { - for (int i=0; i < len; i++) { - if (which == eosio::cdt::output_stream_kind::std_out) - std_out.push(cstr[i]); - else if (which == eosio::cdt::output_stream_kind::std_err) - std_err.push(cstr[i]); - if (!___disable_output) { - std::putc(cstr[i], which == eosio::cdt::output_stream_kind::std_out ? stdout : stderr); - } - } -} -void _prints(const char* cstr, uint8_t which) { - for (int i=0; cstr[i] != '\0'; i++) { - if (which == eosio::cdt::output_stream_kind::std_out) - std_out.push(cstr[i]); - else if (which == eosio::cdt::output_stream_kind::std_err) - std_err.push(cstr[i]); - if (!___disable_output) - std::putc(cstr[i], which == eosio::cdt::output_stream_kind::std_out ? stdout : stderr); - } -} \ No newline at end of file diff --git a/libraries/native/elf_crt.s b/libraries/native/elf_crt.s index f5f5f41810..5a452c43a6 100644 --- a/libraries/native/elf_crt.s +++ b/libraries/native/elf_crt.s @@ -1,13 +1,5 @@ .global _start -.global ___putc -.global _mmap -.global setjmp -.global longjmp .type _start,@function -.type ___putc,@function -.type _mmap,@function -.type setjmp,@function -.type longjmp,@function _start: mov %rsp, %rbp @@ -16,60 +8,4 @@ _start: call _wrap_main mov %rax, %rdi mov $60, %rax - syscall - -___putc: - dec %rsp - mov %rbx, %r8 - mov %rdi, %rax - mov %al, 0(%rsp) - mov $1, %edi - mov %rsp, %rsi - mov $1, %edx - mov $1, %eax - syscall - inc %rsp - mov %r8, %rbx - ret - -_mmap: - mov $9, %eax - mov $0, %rdi - mov $0x6400000, %rsi # 100Mb - mov $3, %rdx - mov $0x22, %r10 - mov $-1, %r8 - mov $0, %r9 - syscall - ret - -setjmp: - mov %rbx, 0(%rdi) - mov %rbp, 8(%rdi) - mov %r12, 16(%rdi) - mov %r13, 24(%rdi) - mov %r14, 32(%rdi) - mov %r15, 40(%rdi) - lea 8(%rsp), %rdx - mov %rdx, 48(%rdi) - mov (%rsp), %rdx - mov %rdx, 56(%rdi) - xor %rax, %rax - ret - -longjmp: - mov %rsi, %rax - test %rax, %rax - jnz 1f - inc %rax -1: - mov 0(%rdi), %rbx - mov 8(%rdi), %rbp - mov 16(%rdi), %r12 - mov 24(%rdi), %r13 - mov 32(%rdi), %r14 - mov 40(%rdi), %r15 - mov 48(%rdi), %rdx - mov %rdx, %rsp - mov 56(%rdi), %rdx - jmp *%rdx + syscall \ No newline at end of file diff --git a/libraries/native/mac_utils.s b/libraries/native/mac_utils.s new file mode 100644 index 0000000000..4a7eb755ed --- /dev/null +++ b/libraries/native/mac_utils.s @@ -0,0 +1,60 @@ +.global ____putc +.global __mmap +.global _setjmp +.global _longjmp + +____putc: + dec %rsp + mov %rbx, %r8 + mov %rdi, %rax + mov %al, 0(%rsp) + mov $1, %edi # using stdout + mov %rsp, %rsi # point to the buffer + mov $1, %edx # buffer is only 1 char + mov $0x2000004, %eax # write syscall 0x4 + syscall + inc %rsp + mov %r8, %rbx + ret + +__mmap: + mov $0x20000C5, %eax # mmap syscall 0xC5 or 197 + mov $0, %rdi # don't map + mov $0x640000000, %rsi # size 100Mb + mov $3, %rdx + mov $0x1002, %r10 + mov $-1, %r8 + mov $0, %r9 + syscall + ret + +_setjmp: + mov %rbx, 0(%rdi) + mov %rbp, 8(%rdi) + mov %r12, 16(%rdi) + mov %r13, 24(%rdi) + mov %r14, 32(%rdi) + mov %r15, 40(%rdi) + lea 8(%rsp), %rdx + mov %rdx, 48(%rdi) + mov (%rsp), %rdx + mov %rdx, 56(%rdi) + xor %rax, %rax + ret + +_longjmp: + mov %rsi, %rax + test %rax, %rax + jnz 1f + inc %rax +1: + mov 0(%rdi), %rbx + mov 8(%rdi), %rbp + mov 16(%rdi), %r12 + mov 24(%rdi), %r13 + mov 32(%rdi), %r14 + mov 40(%rdi), %r15 + mov 48(%rdi), %rdx + mov %rdx, %rsp + mov 56(%rdi), %rdx + jmp *%rdx diff --git a/libraries/native/macho_crt.s b/libraries/native/macho_crt.s index 64fc6e405a..b6cf516a9d 100644 --- a/libraries/native/macho_crt.s +++ b/libraries/native/macho_crt.s @@ -1,8 +1,4 @@ .global start -.global ____putc -.global __mmap -.global _setjmp -.global _longjmp start: mov %rsp, %rbp @@ -11,60 +7,4 @@ start: call __wrap_main mov %rax, %rdi mov $0x2000001, %rax - syscall - -____putc: - dec %rsp - mov %rbx, %r8 - mov %rdi, %rax - mov %al, 0(%rsp) - mov $1, %edi # using stdout - mov %rsp, %rsi # point to the buffer - mov $1, %edx # buffer is only 1 char - mov $0x2000004, %eax # write syscall 0x4 - syscall - inc %rsp - mov %r8, %rbx - ret - -__mmap: - mov $0x20000C5, %eax # mmap syscall 0xC5 or 197 - mov $0, %rdi # don't map - mov $0x640000000, %rsi # size 100Mb - mov $3, %rdx - mov $0x1002, %r10 - mov $-1, %r8 - mov $0, %r9 - syscall - ret - -_setjmp: - mov %rbx, 0(%rdi) - mov %rbp, 8(%rdi) - mov %r12, 16(%rdi) - mov %r13, 24(%rdi) - mov %r14, 32(%rdi) - mov %r15, 40(%rdi) - lea 8(%rsp), %rdx - mov %rdx, 48(%rdi) - mov (%rsp), %rdx - mov %rdx, 56(%rdi) - xor %rax, %rax - ret - -_longjmp: - mov %rsi, %rax - test %rax, %rax - jnz 1f - inc %rax -1: - mov 0(%rdi), %rbx - mov 8(%rdi), %rbp - mov 16(%rdi), %r12 - mov 24(%rdi), %r13 - mov 32(%rdi), %r14 - mov 40(%rdi), %r15 - mov 48(%rdi), %rdx - mov %rdx, %rsp - mov 56(%rdi), %rdx - jmp *%rdx + syscall \ No newline at end of file diff --git a/libraries/native/native/eosio/crt.hpp b/libraries/native/native/eosio/crt.hpp index d47e441b63..5d81a60015 100644 --- a/libraries/native/native/eosio/crt.hpp +++ b/libraries/native/native/eosio/crt.hpp @@ -3,16 +3,7 @@ #include - -#ifdef NATIVELIB_ENABLE_EXCEPTIONS -#include -namespace eosio { namespace cdt { - using assert_exception = std::runtime_error; -}} //ns eosio::cdt -#endif - namespace eosio { namespace cdt { - enum output_stream_kind { std_out, std_err, @@ -36,9 +27,6 @@ extern eosio::cdt::output_stream std_out; extern eosio::cdt::output_stream std_err; extern "C" jmp_buf* ___env_ptr; extern "C" char* ___heap_ptr; -extern "C" bool ___disable_output; -extern "C" bool ___has_failed; -extern "C" bool ___earlier_unit_test_has_failed; extern "C" { void __set_env_test(); diff --git a/libraries/native/utils.s b/libraries/native/utils.s new file mode 100644 index 0000000000..a3ec67818c --- /dev/null +++ b/libraries/native/utils.s @@ -0,0 +1,64 @@ +.global ___putc +.global _mmap +.global setjmp +.global longjmp +.type ___putc,@function +.type _mmap,@function +.type setjmp,@function +.type longjmp,@function + +___putc: + dec %rsp + mov %rbx, %r8 + mov %rdi, %rax + mov %al, 0(%rsp) + mov $1, %edi + mov %rsp, %rsi + mov $1, %edx + mov $1, %eax + syscall + inc %rsp + mov %r8, %rbx + ret + +_mmap: + mov $9, %eax + mov $0, %rdi + mov $0x6400000, %rsi # 100Mb + mov $3, %rdx + mov $0x22, %r10 + mov $-1, %r8 + mov $0, %r9 + syscall + ret + +setjmp: + mov %rbx, 0(%rdi) + mov %rbp, 8(%rdi) + mov %r12, 16(%rdi) + mov %r13, 24(%rdi) + mov %r14, 32(%rdi) + mov %r15, 40(%rdi) + lea 8(%rsp), %rdx + mov %rdx, 48(%rdi) + mov (%rsp), %rdx + mov %rdx, 56(%rdi) + xor %rax, %rax + ret + +longjmp: + mov %rsi, %rax + test %rax, %rax + jnz 1f + inc %rax +1: + mov 0(%rdi), %rbx + mov 8(%rdi), %rbp + mov 16(%rdi), %r12 + mov 24(%rdi), %r13 + mov 32(%rdi), %r14 + mov 40(%rdi), %r15 + mov 48(%rdi), %rdx + mov %rdx, %rsp + mov 56(%rdi), %rdx + jmp *%rdx From 2c8868e46a457c8b4bd937a1eb251ad57b6f7768 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:58:26 -0400 Subject: [PATCH 078/158] Revert "#94 concerns addressed" This reverts commit 226f4975639b9b0ab95d06be44238d49e84adf43. --- libraries/eosiolib/core/eosio/datastream.hpp | 2 +- tools/antler-run/file-utils.hpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/eosiolib/core/eosio/datastream.hpp b/libraries/eosiolib/core/eosio/datastream.hpp index 571d7236cb..892dfd9516 100644 --- a/libraries/eosiolib/core/eosio/datastream.hpp +++ b/libraries/eosiolib/core/eosio/datastream.hpp @@ -17,7 +17,7 @@ #include #include -#include +#include namespace eosio { diff --git a/tools/antler-run/file-utils.hpp b/tools/antler-run/file-utils.hpp index b54f8924c9..ea27952ec2 100644 --- a/tools/antler-run/file-utils.hpp +++ b/tools/antler-run/file-utils.hpp @@ -20,7 +20,7 @@ enum class file_type { wasm }; -inline file_type get_file_type(const char* path) { +file_type get_file_type(const char* path) { std::fstream file; file.open(path, std::fstream::in | std::fstream::binary); assert(file.is_open()); @@ -55,7 +55,7 @@ inline file_type get_file_type(const char* path) { return file_type::uninitialized; } -inline std::string file_type_str(file_type t) { +std::string file_type_str(file_type t) { switch (t) { case file_type::non_elf_other: return "non elf"; From eff46f202092dcbba1537e40bc9a7d27824a9f76 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 22:58:54 -0400 Subject: [PATCH 079/158] Revert "Revert "gcc 7 build fix"" This reverts commit c47cb0f88454f29f298fe50ac2778a2f51cc0ec3. --- .../include/bluegrass/meta/function_traits.hpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp index 5f2ba12d24..5f31f4de39 100644 --- a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp +++ b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp @@ -18,7 +18,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t> && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::NAME)>(), int> = 0) constexpr { \ + decltype(&std::decay_t::type::NAME)>::value, int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -27,7 +27,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::NAME)>(), int> = 0) constexpr { \ + decltype(&std::decay_t::type::NAME)>::value, int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -36,7 +36,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t> && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::template NAME)>(), int> = 0) constexpr { \ + decltype(&std::decay_t::type::template NAME)>::value, int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -45,7 +45,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::template NAME)>(), int> = 0) constexpr { \ + decltype(&std::decay_t::type::template NAME)>::value, int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -63,7 +63,9 @@ namespace bluegrass { namespace meta { namespace detail { template - constexpr bool pass_type() { return true; } + struct pass_type { + enum { value = 1 }; + }; template struct wrapper_t { From f8b654855d5e69307fafffbf29b29ee08ff560f1 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 23:00:31 -0400 Subject: [PATCH 080/158] Revert "eos-vm submodule ssh -> https for build server" This reverts commit 4c7d1bea9a7327b0d902b0ac49a6528832415379. --- .gitmodules | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index f2c1eb62c5..8103cffb97 100644 --- a/.gitmodules +++ b/.gitmodules @@ -12,7 +12,7 @@ url = https://github.com/AntelopeIO/berkeley-softfloat-3 [submodule "tools/external/eos-vm"] path = tools/external/eos-vm - url = https://github.com/AntelopeIO/eos-vm + url = git@github.com:AntelopeIO/eos-vm [submodule "tools/external/antler-proj"] path = tools/external/antler-proj - url = https://github.com/AntelopeIO/antler-proj + url = https://github.com/AntelopeIO/antler-proj \ No newline at end of file From bb32acc3778a77019a83d04683473c180ed1fd32 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 23:02:41 -0400 Subject: [PATCH 081/158] Revert "gcc 7 build fix" This reverts commit f926d59c470e3f05dac92c5d08412c5ec19c92b8. --- .../include/bluegrass/meta/function_traits.hpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp index 5f31f4de39..5f2ba12d24 100644 --- a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp +++ b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp @@ -18,7 +18,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t> && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::NAME)>::value, int> = 0) constexpr { \ + decltype(&std::decay_t::type::NAME)>(), int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -27,7 +27,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::NAME)>::value, int> = 0) constexpr { \ + decltype(&std::decay_t::type::NAME)>(), int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -36,7 +36,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t> && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::template NAME)>::value, int> = 0) constexpr { \ + decltype(&std::decay_t::type::template NAME)>(), int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -45,7 +45,7 @@ bluegrass::meta::overloaded { \ [](auto&& f, std::enable_if_t && \ bluegrass::meta::detail::pass_type< \ - decltype(&std::decay_t::type::template NAME)>::value, int> = 0) constexpr { \ + decltype(&std::decay_t::type::template NAME)>(), int> = 0) constexpr { \ return true; \ }, [](...) constexpr { return false; } \ }(bluegrass::meta::detail::wrapper_t{}) @@ -63,9 +63,7 @@ namespace bluegrass { namespace meta { namespace detail { template - struct pass_type { - enum { value = 1 }; - }; + constexpr bool pass_type() { return true; } template struct wrapper_t { From c09fcff6135260f35a449990fee7700f9aacf25d Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 23:02:45 -0400 Subject: [PATCH 082/158] Revert "fix ubuntu 18 build" This reverts commit 204b16eb7b63e2edf0a3dd665ed890648cdef9f0. --- libraries/eosiolib/CMakeLists.txt | 10 +++------- .../include/bluegrass/meta/function_traits.hpp | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/libraries/eosiolib/CMakeLists.txt b/libraries/eosiolib/CMakeLists.txt index 805cccbc05..7e72b5bee2 100644 --- a/libraries/eosiolib/CMakeLists.txt +++ b/libraries/eosiolib/CMakeLists.txt @@ -14,13 +14,9 @@ if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core ${CMAKE_SOURCE_DIR}/../libraries/meta_refl/include) target_compile_options(${PROJECT_NAME} PUBLIC -fPIC -fexceptions -fno-rtti) - if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0") - target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes) - else() - target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=gnu::eosio_wasm_import) - target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=eosio::ignore) - endif() + if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) + target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=gnu::eosio_wasm_import) + target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=eosio::ignore) endif() else() add_library(eosio diff --git a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp index 5f2ba12d24..670153e96a 100644 --- a/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp +++ b/libraries/meta_refl/include/bluegrass/meta/function_traits.hpp @@ -34,7 +34,7 @@ #define BLUEGRASS_HAS_TEMPLATE_MEMBER(ARG, NAME) \ bluegrass::meta::overloaded { \ - [](auto&& f, std::enable_if_t> && \ + [&](auto&& f, std::enable_if_t> && \ bluegrass::meta::detail::pass_type< \ decltype(&std::decay_t::type::template NAME)>(), int> = 0) constexpr { \ return true; \ From 00a8c47ef76b2efaae700fceb58d84dd4cc00c7c Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 23:02:49 -0400 Subject: [PATCH 083/158] Revert "fix ubuntu 18 & 20 build" This reverts commit 90506ff790a7cdcf6e44e780ab6e5076e72d2b23. --- libraries/native/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/native/CMakeLists.txt b/libraries/native/CMakeLists.txt index be3a1fb9d4..9988d63c3e 100644 --- a/libraries/native/CMakeLists.txt +++ b/libraries/native/CMakeLists.txt @@ -350,7 +350,7 @@ if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/capi ${CMAKE_SOURCE_DIR}/../libraries/meta_refl/include) # suppress executable stack warning. this is due to absence of .note.GNU-stack - target_link_libraries(${PROJECT_NAME} PUBLIC "-Wl,-z,noexecstack") + target_link_libraries(${PROJECT_NAME} PUBLIC "-Wl,-z noexecstack") else() add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM} ${UTILS_ASM}) endif() From 734da3fd79e953c8dcd7f4f736736880a2be3dee Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 23:02:52 -0400 Subject: [PATCH 084/158] Revert "fixing ubuntu 18 & 20 builds" This reverts commit b7a0f32424b4a1e09ae55d6dc4de7a67a672a396. --- tools/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 06d53ee67c..fb731cf4aa 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -26,7 +26,6 @@ add_definitions(${LLVM_DEFINITIONS}) file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -set(CMAKE_CXX_STANDARD 17) set(EOSIO_STACK_SIZE 8192) set(ENABLE_NATIVE_COMPILER TRUE) From 70928133ee9f7ffb04a67511e6469f0815ebf568 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 23:02:55 -0400 Subject: [PATCH 085/158] Revert "fix ubuntu 18 & 20 build" This reverts commit 37f7e3659dee543f72034e23eef1033640797498. --- libraries/native/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/native/CMakeLists.txt b/libraries/native/CMakeLists.txt index 9988d63c3e..d2d4bfb415 100644 --- a/libraries/native/CMakeLists.txt +++ b/libraries/native/CMakeLists.txt @@ -349,8 +349,7 @@ if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/contracts ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/capi ${CMAKE_SOURCE_DIR}/../libraries/meta_refl/include) - # suppress executable stack warning. this is due to absence of .note.GNU-stack - target_link_libraries(${PROJECT_NAME} PUBLIC "-Wl,-z noexecstack") + target_link_options(${PROJECT_NAME} PUBLIC -z noexecstack) # suppress executable stack warning. this is due to absence of .note.GNU-stack else() add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM} ${UTILS_ASM}) endif() From 977d18802a53dee63965863a400c0af40b6080a5 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Wed, 20 Sep 2023 23:06:23 -0400 Subject: [PATCH 086/158] Revert "empty antler-run with eos-vm and eosiolib linked" This reverts commit e7bc222c14ac7153d4302d55d110fc8f7f7df7ef. --- .gitmodules | 3 - libraries/eosiolib/CMakeLists.txt | 91 ++++++++----------- libraries/eosiolib/capi/eosio/db.h | 2 - libraries/eosiolib/capi/eosio/types.h | 14 +-- libraries/eosiolib/contracts/eosio/action.hpp | 4 +- libraries/eosiolib/core/eosio/fixed_bytes.hpp | 1 - libraries/eosiolib/core/eosio/print.hpp | 2 +- libraries/eosiolib/core/eosio/reflect.hpp | 2 +- libraries/eosiolib/core/eosio/types.hpp | 10 -- libraries/eosiolib/malloc.cpp | 4 - libraries/native/CMakeLists.txt | 37 ++------ libraries/native/elf_crt.s | 66 +++++++++++++- libraries/native/mac_utils.s | 60 ------------ libraries/native/macho_crt.s | 62 ++++++++++++- libraries/native/utils.s | 64 ------------- tools/CMakeLists.txt | 5 - tools/antler-run/CMakeLists.txt | 22 ----- tools/antler-run/antler-run.cpp | 50 ---------- tools/antler-run/file-utils.hpp | 78 ---------------- tools/antler-run/options.hpp | 32 ------- tools/external/CMakeLists.txt | 1 - tools/external/eos-vm | 1 - 22 files changed, 175 insertions(+), 436 deletions(-) delete mode 100644 libraries/eosiolib/core/eosio/types.hpp delete mode 100644 libraries/native/mac_utils.s delete mode 100644 libraries/native/utils.s delete mode 100644 tools/antler-run/CMakeLists.txt delete mode 100644 tools/antler-run/antler-run.cpp delete mode 100644 tools/antler-run/file-utils.hpp delete mode 100644 tools/antler-run/options.hpp delete mode 160000 tools/external/eos-vm diff --git a/.gitmodules b/.gitmodules index 8103cffb97..6ef39354df 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,9 +10,6 @@ [submodule "libraries/native/softfloat"] path = libraries/native/softfloat url = https://github.com/AntelopeIO/berkeley-softfloat-3 -[submodule "tools/external/eos-vm"] - path = tools/external/eos-vm - url = git@github.com:AntelopeIO/eos-vm [submodule "tools/external/antler-proj"] path = tools/external/antler-proj url = https://github.com/AntelopeIO/antler-proj \ No newline at end of file diff --git a/libraries/eosiolib/CMakeLists.txt b/libraries/eosiolib/CMakeLists.txt index 7e72b5bee2..a716dde5bb 100644 --- a/libraries/eosiolib/CMakeLists.txt +++ b/libraries/eosiolib/CMakeLists.txt @@ -1,68 +1,49 @@ -set(PARENT_PROJECT_NAME ${PROJECT_NAME}) - file(GLOB HEADERS "*.hpp" "*.h") -if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") - project(native_eosio) - add_library( ${PROJECT_NAME} - eosiolib.cpp - crypto.cpp - malloc.cpp - ${HEADERS} ) - target_compile_definitions(${PROJECT_NAME} PUBLIC EOSIO_NATIVE EOSIOLIB_DISABLE_MALLOC) - target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core - ${CMAKE_SOURCE_DIR}/../libraries/meta_refl/include) - target_compile_options(${PROJECT_NAME} PUBLIC -fPIC -fexceptions -fno-rtti) - if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) - target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=gnu::eosio_wasm_import) - target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=eosio::ignore) - endif() -else() - add_library(eosio - eosiolib.cpp - crypto.cpp - ${HEADERS}) +add_library(eosio + eosiolib.cpp + crypto.cpp + ${HEADERS}) - add_library(eosio_malloc - malloc.cpp - ${HEADERS}) +add_library(eosio_malloc + malloc.cpp + ${HEADERS}) - add_library(eosio_dsm - simple_malloc.cpp - ${HEADERS}) +add_library(eosio_dsm + simple_malloc.cpp + ${HEADERS}) - add_library(eosio_cmem - memory.cpp - ${HEADERS}) +add_library(eosio_cmem + memory.cpp + ${HEADERS}) - set_target_properties(eosio_malloc PROPERTIES LINKER_LANGUAGE C) +set_target_properties(eosio_malloc PROPERTIES LINKER_LANGUAGE C) - target_include_directories(eosio PUBLIC - ${CMAKE_SOURCE_DIR}/libc/cdt-musl/include - ${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/internal - ${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/crypt - ${CMAKE_SOURCE_DIR}/libc/cdt-musl/arch/eos - ${CMAKE_SOURCE_DIR}/libc++/cdt-libcxx/include - ${CMAKE_SOURCE_DIR}) +target_include_directories(eosio PUBLIC + ${CMAKE_SOURCE_DIR}/libc/cdt-musl/include + ${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/internal + ${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/crypt + ${CMAKE_SOURCE_DIR}/libc/cdt-musl/arch/eos + ${CMAKE_SOURCE_DIR}/libc++/cdt-libcxx/include + ${CMAKE_SOURCE_DIR}) - target_link_libraries( eosio c c++ ) - add_custom_command( TARGET eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) - add_custom_command( TARGET eosio_malloc POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) - add_custom_command( TARGET eosio_dsm POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) - add_custom_command( TARGET eosio_cmem POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) +target_link_libraries( eosio c c++ ) +add_custom_command( TARGET eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) +add_custom_command( TARGET eosio_malloc POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) +add_custom_command( TARGET eosio_dsm POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) +add_custom_command( TARGET eosio_cmem POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) - if (ENABLE_NATIVE_COMPILER) - add_native_library(native_eosio - eosiolib.cpp - crypto.cpp - malloc.cpp - ${HEADERS}) +if (ENABLE_NATIVE_COMPILER) + add_native_library(native_eosio + eosiolib.cpp + crypto.cpp + malloc.cpp + ${HEADERS}) - add_dependencies( native_eosio eosio ) - add_custom_command( TARGET native_eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) - endif() + add_dependencies( native_eosio eosio ) + add_custom_command( TARGET native_eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) +endif() - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../eosiolib DESTINATION ${BASE_BINARY_DIR}/include FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp") -endif() \ No newline at end of file +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../eosiolib DESTINATION ${BASE_BINARY_DIR}/include FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp") diff --git a/libraries/eosiolib/capi/eosio/db.h b/libraries/eosiolib/capi/eosio/db.h index ae58579b46..e21f20caf7 100644 --- a/libraries/eosiolib/capi/eosio/db.h +++ b/libraries/eosiolib/capi/eosio/db.h @@ -6,8 +6,6 @@ #pragma once #include "types.h" -#include "eosio/types.hpp" - #ifdef __cplusplus extern "C" { #endif diff --git a/libraries/eosiolib/capi/eosio/types.h b/libraries/eosiolib/capi/eosio/types.h index b3f54915b9..9350b9b4d1 100644 --- a/libraries/eosiolib/capi/eosio/types.h +++ b/libraries/eosiolib/capi/eosio/types.h @@ -32,12 +32,7 @@ typedef uint64_t capi_name; /** * EOSIO Public Key. K1 and R1 keys are 34 bytes. Newer keys can be variable-sized */ -struct -#ifdef __GNUC__ -__attribute__((deprecated("newer public key types cannot be represented as a fixed size structure, use char[] instead"))) -#else -__attribute__((deprecated("newer public key types cannot be represented as a fixed size structure", "char[]"))) -#endif +struct __attribute__((deprecated("newer public key types cannot be represented as a fixed size structure", "char[]"))) capi_public_key { char data[34]; }; @@ -45,12 +40,7 @@ capi_public_key { /** * EOSIO Signature. K1 and R1 signatures are 66 bytes. Newer signatures can be variable-sized */ -struct -#ifdef __GNUC__ -__attribute__((deprecated("newer public key types cannot be represented as a fixed size structure, use char[] instead"))) -#else -__attribute__((deprecated("newer public key types cannot be represented as a fixed size structure", "char[]"))) -#endif +struct __attribute__((deprecated("newer signature types cannot be represented as a fixed size structure", "char[]"))) capi_signature { uint8_t data[66]; }; diff --git a/libraries/eosiolib/contracts/eosio/action.hpp b/libraries/eosiolib/contracts/eosio/action.hpp index ecd71a8b4d..d043931e25 100644 --- a/libraries/eosiolib/contracts/eosio/action.hpp +++ b/libraries/eosiolib/contracts/eosio/action.hpp @@ -323,12 +323,12 @@ namespace eosio { /** * Name of the account the action is intended for */ - eosio::name account; + name account; /** * Name of the action */ - eosio::name name; + name name; /** * List of permissions that authorize this action diff --git a/libraries/eosiolib/core/eosio/fixed_bytes.hpp b/libraries/eosiolib/core/eosio/fixed_bytes.hpp index 812117b4d6..97cbf40bb8 100644 --- a/libraries/eosiolib/core/eosio/fixed_bytes.hpp +++ b/libraries/eosiolib/core/eosio/fixed_bytes.hpp @@ -4,7 +4,6 @@ */ #pragma once #include "datastream.hpp" -#include "types.hpp" #include #include diff --git a/libraries/eosiolib/core/eosio/print.hpp b/libraries/eosiolib/core/eosio/print.hpp index de8db22ae6..c2ee216fdc 100644 --- a/libraries/eosiolib/core/eosio/print.hpp +++ b/libraries/eosiolib/core/eosio/print.hpp @@ -5,7 +5,7 @@ #pragma once #include #include -#include "types.hpp" + namespace eosio { diff --git a/libraries/eosiolib/core/eosio/reflect.hpp b/libraries/eosiolib/core/eosio/reflect.hpp index bd74557254..683342638b 100644 --- a/libraries/eosiolib/core/eosio/reflect.hpp +++ b/libraries/eosiolib/core/eosio/reflect.hpp @@ -3,4 +3,4 @@ #include #define CDT_REFLECT(...) \ - BLUEGRASS_META_REFL(__VA_ARGS__); + BLUEGRASS_META_REFL(__VA_ARGS__); \ diff --git a/libraries/eosiolib/core/eosio/types.hpp b/libraries/eosiolib/core/eosio/types.hpp deleted file mode 100644 index af5c5d7075..0000000000 --- a/libraries/eosiolib/core/eosio/types.hpp +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @file - * @copyright defined in eos/LICENSE - */ -#pragma once - -#ifdef __GNUC__ -typedef __int128 int128_t; -typedef unsigned __int128 uint128_t; -#endif \ No newline at end of file diff --git a/libraries/eosiolib/malloc.cpp b/libraries/eosiolib/malloc.cpp index 9f936b2933..2f3585baa9 100644 --- a/libraries/eosiolib/malloc.cpp +++ b/libraries/eosiolib/malloc.cpp @@ -1,6 +1,5 @@ #include #include -#include #include "core/eosio/check.hpp" #include "core/eosio/print.hpp" @@ -526,8 +525,6 @@ namespace eosio { } /// namespace eosio extern "C" { -#ifndef EOSIOLIB_DISABLE_MALLOC - void* malloc(size_t size) { return eosio::memory_heap.malloc(size); } @@ -545,6 +542,5 @@ void* realloc(void* ptr, size_t size) { void free(void* ptr) { return eosio::memory_heap.free(ptr); } -#endif } diff --git a/libraries/native/CMakeLists.txt b/libraries/native/CMakeLists.txt index d2d4bfb415..68ed5238ab 100644 --- a/libraries/native/CMakeLists.txt +++ b/libraries/native/CMakeLists.txt @@ -1,19 +1,13 @@ -set(PARENT_PROJECT_NAME ${PROJECT_NAME}) - project(native LANGUAGES CXX ASM) if (NOT __APPLE) set(CRT_ASM elf_crt.s) - set(UTILS_ASM utils.s) else() set(CRT_ASM macho_crt.s) - set(UTILS_ASM mac_utils.s) endif() -if (NOT ${PARENT_PROJECT_NAME} MATCHES "cdt_tools") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-everything -allow-sse") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-everything -allow-sse") -endif() +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-everything -allow-sse") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-everything -allow-sse") file(GLOB HEADERS "*.hpp" "*.h") @@ -338,32 +332,15 @@ list( APPEND softfloat_sources ${softfloat_headers} ) add_library ( sf STATIC ${softfloat_sources} ) target_include_directories( sf PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR}) -if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") - # TODO: we need crt.cpp and assembly only for _prints_l and _prints - # we unlikely need those implementations for antler-run so - # we may need to add some code (macroses, etc) to crt.cpp and/or intrinsics.cpp so - # we can revert assembly changes and build only *.cpp without ${UTILS_ASM} - add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${UTILS_ASM}) - - target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core - ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/contracts - ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/capi - ${CMAKE_SOURCE_DIR}/../libraries/meta_refl/include) - target_link_options(${PROJECT_NAME} PUBLIC -z noexecstack) # suppress executable stack warning. this is due to absence of .note.GNU-stack -else() - add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM} ${UTILS_ASM}) -endif() - +add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM} ) target_include_directories( native PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/eosiolib/capi ${CMAKE_SOURCE_DIR}/eosiolib/contracts ${CMAKE_SOURCE_DIR}/eosiolib/core) add_dependencies(native native_eosio) -if (NOT ${PARENT_PROJECT_NAME} MATCHES "cdt_tools") - add_custom_command( TARGET native POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) +add_custom_command( TARGET native POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) - add_custom_command( TARGET sf POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) +add_custom_command( TARGET sf POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${BASE_BINARY_DIR}/lib ) - file(COPY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${BASE_BINARY_DIR}/include/eosio FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "softfloat" EXCLUDE) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${BASE_BINARY_DIR}/include/eosio FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "softfloat" EXCLUDE) - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/native DESTINATION ${BASE_BINARY_DIR}/include/eosiolib FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "softfloat" EXCLUDE) -endif() \ No newline at end of file +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/native DESTINATION ${BASE_BINARY_DIR}/include/eosiolib FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "softfloat" EXCLUDE) diff --git a/libraries/native/elf_crt.s b/libraries/native/elf_crt.s index 5a452c43a6..f5f5f41810 100644 --- a/libraries/native/elf_crt.s +++ b/libraries/native/elf_crt.s @@ -1,5 +1,13 @@ .global _start +.global ___putc +.global _mmap +.global setjmp +.global longjmp .type _start,@function +.type ___putc,@function +.type _mmap,@function +.type setjmp,@function +.type longjmp,@function _start: mov %rsp, %rbp @@ -8,4 +16,60 @@ _start: call _wrap_main mov %rax, %rdi mov $60, %rax - syscall \ No newline at end of file + syscall + +___putc: + dec %rsp + mov %rbx, %r8 + mov %rdi, %rax + mov %al, 0(%rsp) + mov $1, %edi + mov %rsp, %rsi + mov $1, %edx + mov $1, %eax + syscall + inc %rsp + mov %r8, %rbx + ret + +_mmap: + mov $9, %eax + mov $0, %rdi + mov $0x6400000, %rsi # 100Mb + mov $3, %rdx + mov $0x22, %r10 + mov $-1, %r8 + mov $0, %r9 + syscall + ret + +setjmp: + mov %rbx, 0(%rdi) + mov %rbp, 8(%rdi) + mov %r12, 16(%rdi) + mov %r13, 24(%rdi) + mov %r14, 32(%rdi) + mov %r15, 40(%rdi) + lea 8(%rsp), %rdx + mov %rdx, 48(%rdi) + mov (%rsp), %rdx + mov %rdx, 56(%rdi) + xor %rax, %rax + ret + +longjmp: + mov %rsi, %rax + test %rax, %rax + jnz 1f + inc %rax +1: + mov 0(%rdi), %rbx + mov 8(%rdi), %rbp + mov 16(%rdi), %r12 + mov 24(%rdi), %r13 + mov 32(%rdi), %r14 + mov 40(%rdi), %r15 + mov 48(%rdi), %rdx + mov %rdx, %rsp + mov 56(%rdi), %rdx + jmp *%rdx diff --git a/libraries/native/mac_utils.s b/libraries/native/mac_utils.s deleted file mode 100644 index 4a7eb755ed..0000000000 --- a/libraries/native/mac_utils.s +++ /dev/null @@ -1,60 +0,0 @@ -.global ____putc -.global __mmap -.global _setjmp -.global _longjmp - -____putc: - dec %rsp - mov %rbx, %r8 - mov %rdi, %rax - mov %al, 0(%rsp) - mov $1, %edi # using stdout - mov %rsp, %rsi # point to the buffer - mov $1, %edx # buffer is only 1 char - mov $0x2000004, %eax # write syscall 0x4 - syscall - inc %rsp - mov %r8, %rbx - ret - -__mmap: - mov $0x20000C5, %eax # mmap syscall 0xC5 or 197 - mov $0, %rdi # don't map - mov $0x640000000, %rsi # size 100Mb - mov $3, %rdx - mov $0x1002, %r10 - mov $-1, %r8 - mov $0, %r9 - syscall - ret - -_setjmp: - mov %rbx, 0(%rdi) - mov %rbp, 8(%rdi) - mov %r12, 16(%rdi) - mov %r13, 24(%rdi) - mov %r14, 32(%rdi) - mov %r15, 40(%rdi) - lea 8(%rsp), %rdx - mov %rdx, 48(%rdi) - mov (%rsp), %rdx - mov %rdx, 56(%rdi) - xor %rax, %rax - ret - -_longjmp: - mov %rsi, %rax - test %rax, %rax - jnz 1f - inc %rax -1: - mov 0(%rdi), %rbx - mov 8(%rdi), %rbp - mov 16(%rdi), %r12 - mov 24(%rdi), %r13 - mov 32(%rdi), %r14 - mov 40(%rdi), %r15 - mov 48(%rdi), %rdx - mov %rdx, %rsp - mov 56(%rdi), %rdx - jmp *%rdx diff --git a/libraries/native/macho_crt.s b/libraries/native/macho_crt.s index b6cf516a9d..64fc6e405a 100644 --- a/libraries/native/macho_crt.s +++ b/libraries/native/macho_crt.s @@ -1,4 +1,8 @@ .global start +.global ____putc +.global __mmap +.global _setjmp +.global _longjmp start: mov %rsp, %rbp @@ -7,4 +11,60 @@ start: call __wrap_main mov %rax, %rdi mov $0x2000001, %rax - syscall \ No newline at end of file + syscall + +____putc: + dec %rsp + mov %rbx, %r8 + mov %rdi, %rax + mov %al, 0(%rsp) + mov $1, %edi # using stdout + mov %rsp, %rsi # point to the buffer + mov $1, %edx # buffer is only 1 char + mov $0x2000004, %eax # write syscall 0x4 + syscall + inc %rsp + mov %r8, %rbx + ret + +__mmap: + mov $0x20000C5, %eax # mmap syscall 0xC5 or 197 + mov $0, %rdi # don't map + mov $0x640000000, %rsi # size 100Mb + mov $3, %rdx + mov $0x1002, %r10 + mov $-1, %r8 + mov $0, %r9 + syscall + ret + +_setjmp: + mov %rbx, 0(%rdi) + mov %rbp, 8(%rdi) + mov %r12, 16(%rdi) + mov %r13, 24(%rdi) + mov %r14, 32(%rdi) + mov %r15, 40(%rdi) + lea 8(%rsp), %rdx + mov %rdx, 48(%rdi) + mov (%rsp), %rdx + mov %rdx, 56(%rdi) + xor %rax, %rax + ret + +_longjmp: + mov %rsi, %rax + test %rax, %rax + jnz 1f + inc %rax +1: + mov 0(%rdi), %rbx + mov 8(%rdi), %rbp + mov 16(%rdi), %r12 + mov 24(%rdi), %r13 + mov 32(%rdi), %r14 + mov 40(%rdi), %r15 + mov 48(%rdi), %rdx + mov %rdx, %rsp + mov 56(%rdi), %rdx + jmp *%rdx diff --git a/libraries/native/utils.s b/libraries/native/utils.s deleted file mode 100644 index a3ec67818c..0000000000 --- a/libraries/native/utils.s +++ /dev/null @@ -1,64 +0,0 @@ -.global ___putc -.global _mmap -.global setjmp -.global longjmp -.type ___putc,@function -.type _mmap,@function -.type setjmp,@function -.type longjmp,@function - -___putc: - dec %rsp - mov %rbx, %r8 - mov %rdi, %rax - mov %al, 0(%rsp) - mov $1, %edi - mov %rsp, %rsi - mov $1, %edx - mov $1, %eax - syscall - inc %rsp - mov %r8, %rbx - ret - -_mmap: - mov $9, %eax - mov $0, %rdi - mov $0x6400000, %rsi # 100Mb - mov $3, %rdx - mov $0x22, %r10 - mov $-1, %r8 - mov $0, %r9 - syscall - ret - -setjmp: - mov %rbx, 0(%rdi) - mov %rbp, 8(%rdi) - mov %r12, 16(%rdi) - mov %r13, 24(%rdi) - mov %r14, 32(%rdi) - mov %r15, 40(%rdi) - lea 8(%rsp), %rdx - mov %rdx, 48(%rdi) - mov (%rsp), %rdx - mov %rdx, 56(%rdi) - xor %rax, %rax - ret - -longjmp: - mov %rsi, %rax - test %rax, %rax - jnz 1f - inc %rax -1: - mov 0(%rdi), %rbx - mov 8(%rdi), %rbp - mov 16(%rdi), %r12 - mov 24(%rdi), %r13 - mov 32(%rdi), %r14 - mov 40(%rdi), %r15 - mov 48(%rdi), %rdx - mov %rdx, %rsp - mov 56(%rdi), %rdx - jmp *%rdx diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index fb731cf4aa..df4ebf1698 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,5 +1,4 @@ cmake_minimum_required(VERSION 3.5) -project(cdt_tools) find_package(LLVM REQUIRED CONFIG) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") @@ -27,7 +26,6 @@ file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(EOSIO_STACK_SIZE 8192) -set(ENABLE_NATIVE_COMPILER TRUE) macro (add_tool name) set(LLVM_LINK_COMPONENTS support) @@ -74,14 +72,11 @@ macro (add_tool name) add_custom_command( TARGET ${name} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${CMAKE_BINARY_DIR}/bin/ ) endmacro() -add_subdirectory(../libraries/eosiolib ${CMAKE_BINARY_DIR}/eosiolib) -add_subdirectory(../libraries/native ${CMAKE_BINARY_DIR}/native) add_subdirectory(abidiff) add_subdirectory(cc) add_subdirectory(ld) add_subdirectory(init) add_subdirectory(external) -#add_subdirectory(antler-run) # TODO: add back when it will be ready configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/compiler_options.hpp.in ${CMAKE_BINARY_DIR}/compiler_options.hpp) diff --git a/tools/antler-run/CMakeLists.txt b/tools/antler-run/CMakeLists.txt deleted file mode 100644 index c4e2071402..0000000000 --- a/tools/antler-run/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -project(antler-run) - -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/antler-run.cpp ${CMAKE_BINARY_DIR}/antler-run.cpp) -find_package(eos-vm) - -set(TOOLS_DIR ${CMAKE_SOURCE_DIR}) -set(LIB_DIR ${CMAKE_SOURCE_DIR}/../libraries) -set(CDT_NO_START TRUE) - -include_directories(${LIB_DIR}) - -add_tool(antler-run) -target_compile_options(${PROJECT_NAME} PUBLIC -ldl) -set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-rpath,\"\\$ORIGIN/../lib\"") -target_include_directories(${PROJECT_NAME} PUBLIC ${TOOLS_DIR}/external/eos-vm/include - ${LIB_DIR}/eosiolib/contracts - ${LIB_DIR}/eosiolib/core - ${LIB_DIR}/eosiolib/capi - ${LIB_DIR}/eosiolib/native - ${LIB_DIR}/native - ${LIB_DIR}/meta_refl/include) -target_link_libraries(${PROJECT_NAME} native_eosio native) \ No newline at end of file diff --git a/tools/antler-run/antler-run.cpp b/tools/antler-run/antler-run.cpp deleted file mode 100644 index 521f7617f8..0000000000 --- a/tools/antler-run/antler-run.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "options.hpp" -#include "file-utils.hpp" - -#include -#include -#include - -#include -#include - -#include -#include - -#include "llvm/Support/CommandLine.h" - -using namespace eosio; -using namespace eosio::cdt; -using namespace eosio::native; -using namespace eosio::vm; -using namespace llvm; - -int main(int argc, const char **argv) { - - cl::SetVersionPrinter([](llvm::raw_ostream& os) { - os << "Antler-run version ${VERSION_FULL}" << "\n"; - }); - - cl::ParseCommandLineOptions(argc, argv); - - const auto& contract_path = contract_path_opt.getValue(); - - if (test_so_opt) { - auto contract_type = utils::get_file_type(contract_path.c_str()); - if ( contract_type != utils::file_type::elf_shared_object ) { - fprintf(stderr, "not a shared object file: %s\n", file_type_str(contract_type).c_str()); - } - // TODO: add check for neccesary exports - // TODO: add unit test that generates shared object and executes runner with this flag - return 0; - } - - const auto& node_url = nodeos_url_opt.getValue(); - const auto& node_port = nodeos_port_opt.getValue(); - const auto& action = action_name_opt.getValue(); - const auto& account = register_opt.getValue(); - - //TODO add runner implementation here - - return 0; -} \ No newline at end of file diff --git a/tools/antler-run/file-utils.hpp b/tools/antler-run/file-utils.hpp deleted file mode 100644 index ea27952ec2..0000000000 --- a/tools/antler-run/file-utils.hpp +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once - -#include - -#include -#include -#include -#include -#include - -namespace eosio { namespace utils { - -enum class file_type { - uninitialized, - non_elf_other, - elf_object, - elf_executable, - elf_shared_object, - elf_core_dump, - wasm -}; - -file_type get_file_type(const char* path) { - std::fstream file; - file.open(path, std::fstream::in | std::fstream::binary); - assert(file.is_open()); - - std::vector buf(17); - file.read(buf.data(), buf.size()); - - if (buf[0] == 0x7F && - buf[1] == 'E' && - buf[2] == 'L' && - buf[3] == 'F') { - //ELF binary - switch (buf[16]) { - case 1: - return file_type::elf_object; - case 2: - return file_type::elf_executable; - case 3: - return file_type::elf_shared_object; - case 4: - return file_type::elf_core_dump; - } - } else { - uint32_t wasm_magic; - memcpy(&wasm_magic, buf.data(), sizeof(wasm_magic)); - if (wasm_magic == eosio::vm::magic) - return file_type::wasm; - - return file_type::non_elf_other; - } - - return file_type::uninitialized; -} - -std::string file_type_str(file_type t) { - switch (t) { - case file_type::non_elf_other: - return "non elf"; - case file_type::elf_object: - return "object"; - case file_type::elf_executable: - return "executable"; - case file_type::elf_shared_object: - return "shared object"; - case file_type::elf_core_dump: - return "core dump"; - case file_type::wasm: - return "wasm"; - case file_type::uninitialized: - default: - return "uninitialized"; - } -} - -}} // eosio::utils \ No newline at end of file diff --git a/tools/antler-run/options.hpp b/tools/antler-run/options.hpp deleted file mode 100644 index 3f65f18041..0000000000 --- a/tools/antler-run/options.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include "llvm/Support/CommandLine.h" - -#include - -static llvm::cl::OptionCategory AntlerRunOptions("Runner options"); - -llvm::cl::opt contract_path_opt ( "contract", - llvm::cl::desc("Smart contract path to debug. It could be either wasm or native shared object"), - llvm::cl::cat(AntlerRunOptions), - llvm::cl::Required ); -llvm::cl::opt test_so_opt ( "test", - llvm::cl::desc("Test that shared object is loadable and has neccesary exports"), - llvm::cl::cat(AntlerRunOptions), - llvm::cl::ZeroOrMore ); -llvm::cl::opt nodeos_url_opt ( "nodeos-url", - llvm::cl::desc("`nodeos` URL"), - llvm::cl::cat(AntlerRunOptions), - llvm::cl::ZeroOrMore ); -llvm::cl::opt nodeos_port_opt ( "nodeos-port", - llvm::cl::desc("`nodeos` port"), - llvm::cl::cat(AntlerRunOptions), - llvm::cl::ZeroOrMore ); -llvm::cl::opt action_name_opt ( "call-action", - llvm::cl::desc("Action that will be called"), - llvm::cl::cat(AntlerRunOptions), - llvm::cl::ZeroOrMore ); -llvm::cl::opt register_opt ( "register", - llvm::cl::desc("Register an account name to be debuggable through `nodeos`"), - llvm::cl::cat(AntlerRunOptions), - llvm::cl::ZeroOrMore ); diff --git a/tools/external/CMakeLists.txt b/tools/external/CMakeLists.txt index e486f2dcb0..4f5e28cc78 100644 --- a/tools/external/CMakeLists.txt +++ b/tools/external/CMakeLists.txt @@ -1,3 +1,2 @@ add_subdirectory(wabt) -add_subdirectory(eos-vm) add_subdirectory(antler-proj) \ No newline at end of file diff --git a/tools/external/eos-vm b/tools/external/eos-vm deleted file mode 160000 index 1592261e96..0000000000 --- a/tools/external/eos-vm +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1592261e96a5ebb4a5f261d7167c0723ca941b9b From 181595fd3885ddeef616abdc388fffea7374c837 Mon Sep 17 00:00:00 2001 From: Dmytro Sydorchenko Date: Thu, 21 Sep 2023 22:33:54 -0400 Subject: [PATCH 087/158] #221 review concerns addressed --- libraries/eosiolib/CMakeLists.txt | 2 + libraries/eosiolib/base64.cpp | 169 ++++++++++++ libraries/eosiolib/core/eosio/base64.hpp | 258 +----------------- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 76 +++--- tests/unit/base64_tests.cpp | 11 +- .../test_contracts/bls_primitives_tests.cpp | 10 +- 6 files changed, 232 insertions(+), 294 deletions(-) create mode 100644 libraries/eosiolib/base64.cpp diff --git a/libraries/eosiolib/CMakeLists.txt b/libraries/eosiolib/CMakeLists.txt index 805cccbc05..b15eb08ca1 100644 --- a/libraries/eosiolib/CMakeLists.txt +++ b/libraries/eosiolib/CMakeLists.txt @@ -26,6 +26,7 @@ else() add_library(eosio eosiolib.cpp crypto.cpp + base64.cpp ${HEADERS}) add_library(eosio_malloc @@ -62,6 +63,7 @@ else() eosiolib.cpp crypto.cpp malloc.cpp + base64.cpp ${HEADERS}) add_dependencies( native_eosio eosio ) diff --git a/libraries/eosiolib/base64.cpp b/libraries/eosiolib/base64.cpp new file mode 100644 index 0000000000..9a31dbdf52 --- /dev/null +++ b/libraries/eosiolib/base64.cpp @@ -0,0 +1,169 @@ +#include "core/eosio/base64.hpp" + +namespace eosio::detail { + +unsigned int pos_of_char(const unsigned char chr) { + // Return the position of chr within base64_encode() + const unsigned char from_base64_chars[256] = { + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 62, 64, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, + 64, 0, 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, 64, 64, 64, 64, 63, + 64, 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, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 + }; + auto c = from_base64_chars[chr]; + eosio::check(c != 64, "encountered non-base64 character"); + + return c; +} + +std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) { + + const size_t len_encoded = (in_len +2) / 3 * 4; + const unsigned char trailing_char = url ? '.' : '='; + + // Includes performance improvement from unmerged PR: https://github.com/ReneNyffenegger/cpp-base64/pull/27 + + // Depending on the url parameter in base64_chars, one of + // two sets of base64 characters needs to be chosen. + // They differ in their last two characters. + const char* base64_chars[2] = { + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "-_"}; + + // Choose set of base64 characters. They differ + // for the last two positions, depending on the url + // parameter. + // A bool (as is the parameter url) is guaranteed + // to evaluate to either 0 or 1 in C++ therefore, + // the correct character set is chosen by subscripting + // base64_chars with url. + const char* base64_chars_ = base64_chars[url]; + + std::string ret; + ret.reserve(len_encoded); + + unsigned int pos = 0; + + while (pos < in_len) { + ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0xfc) >> 2]); + + if (pos+1 < in_len) { + ret.push_back(base64_chars_[((bytes_to_encode[pos + 0] & 0x03) << 4) + ((bytes_to_encode[pos + 1] & 0xf0) >> 4)]); + + if (pos+2 < in_len) { + ret.push_back(base64_chars_[((bytes_to_encode[pos + 1] & 0x0f) << 2) + ((bytes_to_encode[pos + 2] & 0xc0) >> 6)]); + ret.push_back(base64_chars_[ bytes_to_encode[pos + 2] & 0x3f]); + } + else { + ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]); + ret.push_back(trailing_char); + } + } + else { + + ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]); + ret.push_back(trailing_char); + ret.push_back(trailing_char); + } + + pos += 3; + } + + + return ret; +} + +std::string base64_decode(std::string_view encoded_string, bool remove_linebreaks) { + if (encoded_string.empty()) return std::string{}; + + if (remove_linebreaks) { + + std::string copy(encoded_string); + + copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end()); + + return base64_decode(copy, false); + } + + size_t length_of_string = encoded_string.length(); + size_t pos = 0; + + // + // The approximate length (bytes) of the decoded string might be one or + // two bytes smaller, depending on the amount of trailing equal signs + // in the encoded string. This approximation is needed to reserve + // enough space in the string to be returned. + // + size_t approx_length_of_decoded_string = length_of_string / 4 * 3; + std::string ret; + ret.reserve(approx_length_of_decoded_string); + + while (pos < length_of_string) { + // + // Iterate over encoded input string in chunks. The size of all + // chunks except the last one is 4 bytes. + // + // The last chunk might be padded with equal signs or dots + // in order to make it 4 bytes in size as well, but this + // is not required as per RFC 2045. + // + // All chunks except the last one produce three output bytes. + // + // The last chunk produces at least one and up to three bytes. + // + eosio::check(pos+1 < length_of_string, "wrong encoded string size"); + size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1) ); + + // + // Emit the first output byte that is produced in each chunk: + // + ret.push_back(static_cast( ( (pos_of_char(encoded_string.at(pos+0)) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4))); + + if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045) + encoded_string.at(pos+2) != '=' && + encoded_string.at(pos+2) != '.' // accept URL-safe base 64 strings, too, so check for '.' also. + ) + { + // + // Emit a chunk's second byte (which might not be produced in the last chunk). + // + unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2) ); + ret.push_back(static_cast( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2))); + + if ( ( pos + 3 < length_of_string ) && + encoded_string.at(pos+3) != '=' && + encoded_string.at(pos+3) != '.' + ) + { + // + // Emit a chunk's third byte (which might not be produced in the last chunk). + // + ret.push_back(static_cast( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3)) )); + } + } + + pos += 4; + } + + return ret; +} + +}//eosio::detail \ No newline at end of file diff --git a/libraries/eosiolib/core/eosio/base64.hpp b/libraries/eosiolib/core/eosio/base64.hpp index 4bd754c2e2..f17f4a94f1 100644 --- a/libraries/eosiolib/core/eosio/base64.hpp +++ b/libraries/eosiolib/core/eosio/base64.hpp @@ -33,270 +33,26 @@ #include "check.hpp" #include -#include -#if __cplusplus >= 201703L #include -#endif // __cplusplus >= 201703L namespace eosio { -std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len); -inline std::string base64_encode(char const* bytes_to_encode, unsigned int in_len) { return base64_encode( (unsigned char const*)bytes_to_encode, in_len); } -std::string base64_encode( std::string_view enc ); -std::string base64_decode( std::string_view encoded_string); - namespace detail { -std::string base64_encode (std::string const& s, bool url = false); - -std::string base64_decode(std::string const& s, bool remove_linebreaks = false); std::string base64_encode(unsigned char const*, size_t len, bool url = false); - -std::string base64_encode (std::string_view s, bool url = false); - std::string base64_decode(std::string_view s, bool remove_linebreaks = false); -unsigned int pos_of_char(const unsigned char chr) { - // Return the position of chr within base64_encode() - const unsigned char from_base64_chars[256] = { - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 62, 64, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, - 64, 0, 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, 64, 64, 64, 64, 63, - 64, 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, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 - }; - auto c = from_base64_chars[chr]; - eosio::check(c != 64, "encountered non-base64 character"); - - return c; -} - -std::string insert_linebreaks(std::string str, size_t distance) { - // Provided by https://github.com/JomaCorpFX - if (!str.length()) { - return std::string{}; - } - - size_t pos = distance; - - while (pos < str.size()) { - str.insert(pos, "\n"); - pos += distance + 1; - } - - return str; -} - -template -std::string encode_with_line_breaks(String s) { - return insert_linebreaks(base64_encode(s, false), line_length); -} - -template -std::string encode_pem(String s) { - return encode_with_line_breaks(s); -} - -template -std::string encode_mime(String s) { - return encode_with_line_breaks(s); -} - -template -std::string encode(String s, bool url) { - return base64_encode(reinterpret_cast(s.data()), s.length(), url); -} - -std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) { - - const size_t len_encoded = (in_len +2) / 3 * 4; - const unsigned char trailing_char = url ? '.' : '='; - - // Includes performance improvement from unmerged PR: https://github.com/ReneNyffenegger/cpp-base64/pull/27 - - // Depending on the url parameter in base64_chars, one of - // two sets of base64 characters needs to be chosen. - // They differ in their last two characters. - const char* base64_chars[2] = { - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789" - "+/", - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789" - "-_"}; - - // Choose set of base64 characters. They differ - // for the last two positions, depending on the url - // parameter. - // A bool (as is the parameter url) is guaranteed - // to evaluate to either 0 or 1 in C++ therefore, - // the correct character set is chosen by subscripting - // base64_chars with url. - const char* base64_chars_ = base64_chars[url]; - - std::string ret; - ret.reserve(len_encoded); - - unsigned int pos = 0; - - while (pos < in_len) { - ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0xfc) >> 2]); - - if (pos+1 < in_len) { - ret.push_back(base64_chars_[((bytes_to_encode[pos + 0] & 0x03) << 4) + ((bytes_to_encode[pos + 1] & 0xf0) >> 4)]); - - if (pos+2 < in_len) { - ret.push_back(base64_chars_[((bytes_to_encode[pos + 1] & 0x0f) << 2) + ((bytes_to_encode[pos + 2] & 0xc0) >> 6)]); - ret.push_back(base64_chars_[ bytes_to_encode[pos + 2] & 0x3f]); - } - else { - ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]); - ret.push_back(trailing_char); - } - } - else { - - ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]); - ret.push_back(trailing_char); - ret.push_back(trailing_char); - } - - pos += 3; - } - +} // detail namespace - return ret; +inline std::string base64_encode(std::string_view enc) { + return detail::base64_encode(reinterpret_cast(enc.data()), enc.size(), false); } - -template -static std::string decode(const String& encoded_string, bool remove_linebreaks) { - // - // decode(…) is templated so that it can be used with String = const std::string& - // or std::string_view (requires at least C++17) - // - - if (encoded_string.empty()) return std::string{}; - - if (remove_linebreaks) { - - std::string copy(encoded_string); - - copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end()); - - return base64_decode(copy, false); - } - - size_t length_of_string = encoded_string.length(); - size_t pos = 0; - - // - // The approximate length (bytes) of the decoded string might be one or - // two bytes smaller, depending on the amount of trailing equal signs - // in the encoded string. This approximation is needed to reserve - // enough space in the string to be returned. - // - size_t approx_length_of_decoded_string = length_of_string / 4 * 3; - std::string ret; - ret.reserve(approx_length_of_decoded_string); - - while (pos < length_of_string) { - // - // Iterate over encoded input string in chunks. The size of all - // chunks except the last one is 4 bytes. - // - // The last chunk might be padded with equal signs or dots - // in order to make it 4 bytes in size as well, but this - // is not required as per RFC 2045. - // - // All chunks except the last one produce three output bytes. - // - // The last chunk produces at least one and up to three bytes. - // - eosio::check(pos+1 < length_of_string, "wrong encoded string size"); - size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1) ); - - // - // Emit the first output byte that is produced in each chunk: - // - ret.push_back(static_cast( ( (pos_of_char(encoded_string.at(pos+0)) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4))); - - if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045) - encoded_string.at(pos+2) != '=' && - encoded_string.at(pos+2) != '.' // accept URL-safe base 64 strings, too, so check for '.' also. - ) - { - // - // Emit a chunk's second byte (which might not be produced in the last chunk). - // - unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2) ); - ret.push_back(static_cast( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2))); - - if ( ( pos + 3 < length_of_string ) && - encoded_string.at(pos+3) != '=' && - encoded_string.at(pos+3) != '.' - ) - { - // - // Emit a chunk's third byte (which might not be produced in the last chunk). - // - ret.push_back(static_cast( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3)) )); - } - } - - pos += 4; - } - - return ret; -} - -std::string base64_decode(std::string const& s, bool remove_linebreaks) { - return decode(s, remove_linebreaks); -} - -std::string base64_encode(std::string const& s, bool url) { - return encode(s, url); -} - -std::string base64_encode(std::string_view s, bool url) { - return encode(s, url); -} - -std::string base64_decode(std::string_view s, bool remove_linebreaks) { - return decode(s, remove_linebreaks); -} - -} // anonymous namespace - - -std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { - return detail::base64_encode(bytes_to_encode, in_len, false); -} -std::string base64_encode(std::string_view enc) { - return detail::base64_encode(enc, false); -} -std::string base64_decode(std::string_view encoded_string) { +inline std::string base64_decode(std::string_view encoded_string) { return detail::base64_decode(encoded_string, false); } - -std::string base64url_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { - return detail::base64_encode(bytes_to_encode, in_len, true); -} -std::string base64url_encode(std::string_view enc) { - return detail::base64_encode(enc, true); +inline std::string base64url_encode(std::string_view enc) { + return detail::base64_encode(reinterpret_cast(enc.data()), enc.size(), true); } -std::string base64url_decode(std::string_view encoded_string) { +inline std::string base64url_decode(std::string_view encoded_string) { return detail::base64_decode(encoded_string, true); } diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 1bce39544c..4bd8e000bf 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -10,8 +10,6 @@ #include #include -#include - namespace bls12_381 { class sha256 { public: @@ -337,20 +335,20 @@ namespace eosio { namespace detail { const inline std::string bls_public_key_prefix = "PUB_BLS_"; - const inline uint32_t bls_publick_key_checksum_size = sizeof(uint32_t); + constexpr inline uint32_t bls_checksum_size = sizeof(uint32_t); const inline std::string bls_signature_prefix = "SIG_BLS_"; template std::string bls_type_to_base64(const T& g1) { - std::array g1_with_checksum; + std::array g1_with_checksum; auto it = std::copy(g1.begin(), g1.end(), g1_with_checksum.begin()); auto csum = ripemd160(g1.data(), g1.size()).extract_as_byte_array(); std::copy(reinterpret_cast(csum.data()), - reinterpret_cast(csum.data())+bls_publick_key_checksum_size, + reinterpret_cast(csum.data())+bls_checksum_size, it); - return Prefix + eosio::base64_encode(g1_with_checksum.data(), g1_with_checksum.size()); + return Prefix + eosio::base64_encode({g1_with_checksum.data(), g1_with_checksum.size()}); } template T bls_base64_to_type(const char* data, size_t size) { @@ -359,16 +357,16 @@ namespace detail { std::string decoded = eosio::base64_decode({data+Prefix.size(), size - Prefix.size()}); T ret; - eosio::check(decoded.size() == ret.size() + bls_publick_key_checksum_size, "decoded size " + std::to_string(decoded.size()) + + eosio::check(decoded.size() == ret.size() + bls_checksum_size, "decoded size " + std::to_string(decoded.size()) + " doesn't match structure size " + std::to_string(ret.size()) + - " + checksum " + std::to_string(bls_publick_key_checksum_size)); + " + checksum " + std::to_string(bls_checksum_size)); auto it = decoded.end(); - std::advance(it, -bls_publick_key_checksum_size); + std::advance(it, -bls_checksum_size); std::copy(decoded.begin(), it, ret.begin()); auto csum = ripemd160(ret.data(), ret.size()).extract_as_byte_array(); - eosio::check(0 == memcmp(&*it, csum.data(), bls_publick_key_checksum_size), "checksum of structure doesn't match"); + eosio::check(0 == memcmp(&*it, csum.data(), bls_checksum_size), "checksum of structure doesn't match"); return ret; } @@ -464,7 +462,7 @@ namespace detail { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 73x8 = 584 bytes - const inline std::vector R1 = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, + const inline std::array R1 = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, @@ -519,13 +517,13 @@ namespace detail { } } bls_s scalar_fromBE(const bls_s& in) { - std::array out; - for(uint64_t i = 0; i < 8; i++) { - uint64_t temp; - memcpy(&temp, &in[in.size() - i*8 - 8], sizeof(uint64_t)); - out[i] = htobe64(temp); + bls_s out; + constexpr size_t last_index = sizeof(out) - 1; + for(size_t i = 0; i <= last_index; ++i) + { + out[i] = in[last_index - i]; } - return reinterpret_cast(std::move(out)); + return out; } void g2_fromMessage(const bls_g1_affine& msg, const std::string& dst, bls_g2& res) { @@ -553,17 +551,39 @@ namespace detail { } } - inline std::string bls_g1_affine_to_base64(const bls_g1_affine& g1) { + inline std::string encode_g1_to_bls_public_key(const bls_g1_affine& g1) { return detail::bls_type_to_base64(g1); } - inline bls_g1_affine bls_base64_to_g1_affine(const char* data, size_t size) { - return detail::bls_base64_to_type(data, size); + inline bls_g1_affine decode_bls_public_key_to_g1(std::string_view public_key) { + return detail::bls_base64_to_type(public_key.data(), public_key.size()); } - inline std::string bls_sig_to_base64_affine(const bls_g2_affine& g2) { + inline std::string encode_g2_to_bls_signature(const bls_g2_affine& g2) { return detail::bls_type_to_base64(g2); } - inline bls_g2_affine bls_base64_to_sig_affine(const char* data, size_t size) { - return detail::bls_base64_to_type(data, size); + inline bls_g2_affine decode_bls_signature_to_g2(std::string_view public_key) { + return detail::bls_base64_to_type(public_key.data(), public_key.size()); + } + + inline bls_g1 g1_affine_to_jacobian(const bls_g1_affine& pubkey) { + using namespace detail; + + static_assert(sizeof(bls_g1_affine) + sizeof(R1) == sizeof(bls_g1)); + // add z coordinate (R1) to pubkey and signature_proof + bls_g1 pubkey_jacobian; + auto insert_it = std::copy(pubkey.begin(), pubkey.end(), pubkey_jacobian.begin()); + std::copy(R1.begin(), R1.end(), insert_it); + + return pubkey_jacobian; + } + + inline bls_g2 g2_affine_to_jacobian(const bls_g2_affine& signature_proof) { + using namespace detail; + + bls_g2 sig_ex = {0}; + auto insert_it = std::copy(signature_proof.begin(), signature_proof.end(), sig_ex.begin()); + std::copy(R1.begin(), R1.end(), insert_it); + + return sig_ex; } // pubkey and signature are assumed to be in RAW affine little-endian bytes @@ -573,14 +593,8 @@ namespace detail { bls_g1 g1_points[2] = {0}; bls_g2 g2_points[2] = {0}; - // add z coordinate (R1) to pubkey and signature_proof - std::vector pubkey_ex(sizeof(bls_g1_affine) + R1.size(), 0); - auto insert_it = std::copy(pubkey.begin(), pubkey.end(), pubkey_ex.begin()); - std::copy(R1.begin(), R1.end(), insert_it); - - std::vector sig_ex(sizeof(bls_g2), 0); - insert_it = std::copy(signature_proof.begin(), signature_proof.end(), sig_ex.begin()); - std::copy(R1.begin(), R1.end(), insert_it); + bls_g1 pubkey_ex = g1_affine_to_jacobian(pubkey); + bls_g2 sig_ex = g2_affine_to_jacobian(signature_proof); memcpy(&g1_points[0], G1_ONE_NEG.data(), G1_ONE_NEG.size()); memcpy(&g2_points[0], sig_ex.data(), sig_ex.size()); diff --git a/tests/unit/base64_tests.cpp b/tests/unit/base64_tests.cpp index ac8cd8a92a..a875eced47 100644 --- a/tests/unit/base64_tests.cpp +++ b/tests/unit/base64_tests.cpp @@ -63,7 +63,7 @@ EOSIO_TEST_BEGIN(base64_cpp_base64_tests) "http://www.renenyffenegger.ch\n" "passion for data\n"; - std::string encoded = base64_encode(reinterpret_cast(orig.c_str()), orig.length()); + std::string encoded = base64_encode({orig.c_str(), orig.length()}); std::string decoded = base64_decode(encoded); CHECK_EQUAL(encoded, "UmVuw6kgTnlmZmVuZWdnZXIKaHR0cDovL3d3dy5yZW5lbnlmZmVuZWdnZXIuY2gKcGFzc2lvbiBmb3IgZGF0YQo="); @@ -75,8 +75,7 @@ EOSIO_TEST_BEGIN(base64_cpp_base64_tests) std::string rest0_original = "abc"; std::string rest0_reference = "YWJj"; - std::string rest0_encoded = base64_encode(reinterpret_cast(rest0_original.c_str()), - rest0_original.length()); + std::string rest0_encoded = base64_encode({rest0_original.c_str(),rest0_original.length()}); std::string rest0_decoded = base64_decode(rest0_encoded); CHECK_EQUAL(rest0_decoded, rest0_original); @@ -85,8 +84,7 @@ EOSIO_TEST_BEGIN(base64_cpp_base64_tests) std::string rest1_original = "abcd"; std::string rest1_reference = "YWJjZA=="; - std::string rest1_encoded = base64_encode(reinterpret_cast(rest1_original.c_str()), - rest1_original.length()); + std::string rest1_encoded = base64_encode({rest1_original.c_str(), rest1_original.length()}); std::string rest1_decoded = base64_decode(rest1_encoded); CHECK_EQUAL(rest1_decoded, rest1_original); @@ -95,8 +93,7 @@ EOSIO_TEST_BEGIN(base64_cpp_base64_tests) std::string rest2_original = "abcde"; std::string rest2_reference = "YWJjZGU="; - std::string rest2_encoded = base64_encode(reinterpret_cast(rest2_original.c_str()), - rest2_original.length()); + std::string rest2_encoded = base64_encode({rest2_original.c_str(),rest2_original.length()}); std::string rest2_decoded = base64_decode(rest2_encoded); CHECK_EQUAL(rest2_decoded, rest2_original); diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 03d17f4abd..9b05e5e57c 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -89,22 +89,22 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ [[eosio::action]] void popverify(const std::string& pk, const std::string& sig) { - check(bls_pop_verify(bls_base64_to_g1_affine(pk.data(), pk.size()), bls_base64_to_sig_affine(sig.data(), sig.size())), "pop verify failed"); + check(bls_pop_verify(decode_bls_public_key_to_g1(pk), decode_bls_signature_to_g2(sig)), "pop verify failed"); } [[eosio::action]] void g1baseb4enc(const std::vector& g1, const std::string& base64 ) { check(g1.size() == std::tuple_size::value, "wrong g1 size passed"); - check(bls_g1_affine_to_base64(*reinterpret_cast(g1.data())) == base64, "g1 to base64 encoding doesn't match" ); - check(bls_base64_to_g1_affine(base64.data(), base64.size()) == *reinterpret_cast(g1.data()), "base64 to g1 decoding doesn't match" ); + check(encode_g1_to_bls_public_key(*reinterpret_cast(g1.data())) == base64, "g1 to base64 encoding doesn't match" ); + check(decode_bls_public_key_to_g1(base64) == *reinterpret_cast(g1.data()), "base64 to g1 decoding doesn't match" ); } [[eosio::action]] void sigbaseb4enc(const std::vector& g2, const std::string& base64) { check(g2.size() == std::tuple_size::value, "wrong g2 size passed"); - check(bls_sig_to_base64_affine(*reinterpret_cast(g2.data())) == base64, "g2 to base64 encoding doesn't match" ); - check(bls_base64_to_sig_affine(base64.data(), base64.size()) == *reinterpret_cast(g2.data()), "base64 to g2 decoding doesn't match" ); + check(encode_g2_to_bls_signature(*reinterpret_cast(g2.data())) == base64, "g2 to base64 encoding doesn't match" ); + check(decode_bls_signature_to_g2(base64) == *reinterpret_cast(g2.data()), "base64 to g2 decoding doesn't match" ); } }; From dd0887c4d191f843c10b1059ea5e5dbb7836762a Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:07:56 -0500 Subject: [PATCH 088/158] on release, upload .deb as release asset automatically --- .github/workflows/release.yaml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/release.yaml diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000000..d5e26e02bc --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,33 @@ +name: Upload Release .deb + +on: + release: + types: [published] + +jobs: + eb: + name: Upload Release .deb + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + actions: read + steps: + - name: Get cdt.deb + id: getter + uses: AntelopeIO/asset-artifact-download-action@v3 + with: + owner: ${{github.repository_owner}} + repo: ${{github.event.repository.name}} + file: cdt_.*_amd64.deb + target: ${{github.sha}} + artifact-name: cdt_ubuntu_package_amd64 + wait-for-exact-target: true + - run: | + curl -LsSf \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${{github.token}}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + -H "Content-Type: application/octet-stream" \ + --data-binary "@${{steps.getter.outputs.downloaded-file}}" \ + "https://uploads.github.com/repos/${{github.repository}}/releases/${{github.event.release.id}}/assets?name=${{steps.getter.outputs.downloaded-file}}" From 481d28a54d21be62afa2d919ce67f4faac23dc2d Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 1 Dec 2023 15:12:14 -0600 Subject: [PATCH 089/158] GH-251 Add new BLS host function support and switch to affine little-endian format --- .../eosiolib/capi/eosio/crypto_bls_ext.h | 16 +- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 143 +++++++----------- libraries/native/intrinsics.cpp | 28 ++-- .../native/native/eosio/intrinsics_def.hpp | 10 +- tests/integration/bls_tests.cpp | 85 ++++++----- .../test_contracts/bls_primitives_tests.cpp | 78 ++++++---- 6 files changed, 181 insertions(+), 179 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/crypto_bls_ext.h b/libraries/eosiolib/capi/eosio/crypto_bls_ext.h index 8071a16f91..323dbbe6c2 100644 --- a/libraries/eosiolib/capi/eosio/crypto_bls_ext.h +++ b/libraries/eosiolib/capi/eosio/crypto_bls_ext.h @@ -11,16 +11,10 @@ __attribute__((eosio_wasm_import)) int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); +int32_t bls_g1_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) -int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); - -__attribute__((eosio_wasm_import)) -int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); - -__attribute__((eosio_wasm_import)) -int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); +int32_t bls_g2_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); @@ -34,6 +28,12 @@ int32_t bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); +__attribute__((eosio_wasm_import)) +int32_t bls_fp_mul(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + +__attribute__((eosio_wasm_import)) +int32_t bls_fp_exp(const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, char* res, uint32_t res_len); + #ifdef __cplusplus } #endif diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 7a62b57b1c..7c3a85f497 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -18,16 +18,10 @@ namespace eosio { int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); + int32_t bls_g1_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) - int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len); - - __attribute__((eosio_wasm_import)) - int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); - - __attribute__((eosio_wasm_import)) - int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); + int32_t bls_g2_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len); __attribute__((eosio_wasm_import)) int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len); @@ -40,6 +34,12 @@ namespace eosio { __attribute__((eosio_wasm_import)) int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + int32_t bls_fp_mul(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len); + + __attribute__((eosio_wasm_import)) + int32_t bls_fp_exp(const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, char* res, uint32_t res_len); } } @@ -47,114 +47,87 @@ namespace eosio { using bls_fp = std::array; using bls_s = std::array; using bls_fp2 = std::array; - using bls_g1 = std::array; - using bls_g2 = std::array; - using bls_gt = std::array; + using bls_g1 = std::array; + using bls_g2 = std::array; + using bls_gt = std::array; // group fp12 - int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { + inline int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { return internal_use_do_not_use::bls_g1_add( - op1.data(), - sizeof(bls_g1), - op2.data(), - sizeof(bls_g1), - res.data(), - sizeof(bls_g1) + op1.data(), sizeof(bls_g1), + op2.data(), sizeof(bls_g1), + res.data(), sizeof(bls_g1) ); } - int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) { + inline int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) { return internal_use_do_not_use::bls_g2_add( - op1.data(), - sizeof(bls_g2), - op2.data(), - sizeof(bls_g2), - res.data(), - sizeof(bls_g2) - ); - } - - int32_t bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) { - return internal_use_do_not_use::bls_g1_mul( - point.data(), - sizeof(bls_g1), - scalar.data(), - sizeof(bls_scalar), - res.data(), - sizeof(bls_g1) + op1.data(), sizeof(bls_g2), + op2.data(), sizeof(bls_g2), + res.data(), sizeof(bls_g2) ); } - int32_t bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) { - return internal_use_do_not_use::bls_g2_mul( - point.data(), - sizeof(bls_g2), - scalar.data(), - sizeof(bls_scalar), - res.data(), - sizeof(bls_g2) - ); - } - - int32_t bls_g1_exp(const bls_g1 points[], const bls_scalar scalars[], const uint32_t num, bls_g1& res) { - return internal_use_do_not_use::bls_g1_exp( - num ? points[0].data() : nullptr, - num * sizeof(bls_g1), - num ? scalars[0].data() : nullptr, - num * sizeof(bls_scalar), + inline int32_t bls_g1_weighted_sum(const bls_g1 g1_points[], const bls_scalar scalars[], uint32_t num, bls_g1& res) { + return internal_use_do_not_use::bls_g1_weighted_sum( + num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1), + num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar), num, - res.data(), - sizeof(bls_g1) + res.data(), sizeof(bls_g1) ); } - int32_t bls_g2_exp(const bls_g2 points[], const bls_scalar scalars[], const uint32_t num, bls_g2& res) { - return internal_use_do_not_use::bls_g2_exp( - num ? points[0].data() : nullptr, - num * sizeof(bls_g2), - num ? scalars[0].data() : nullptr, - num * sizeof(bls_scalar), + inline int32_t bls_g2_weighted_sum(const bls_g2 g2_points[], const bls_scalar scalars[], uint32_t num, bls_g2& res) { + return internal_use_do_not_use::bls_g2_weighted_sum( + num ? g2_points[0].data() : nullptr, num * sizeof(bls_g2), + num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar), num, - res.data(), - sizeof(bls_g2) + res.data(), sizeof(bls_g2) ); } - int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { + inline int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { return internal_use_do_not_use::bls_pairing( - num ? g1_points[0].data() : nullptr, - num * sizeof(bls_g1), - num ? g2_points[0].data() : nullptr, - num * sizeof(bls_g2), + num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1), + num ? g2_points[0].data() : nullptr, num * sizeof(bls_g2), num, - res.data(), - sizeof(bls_gt) + res.data(), sizeof(bls_gt) ); } - int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { + inline int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { return internal_use_do_not_use::bls_g1_map( - e.data(), - sizeof(bls_fp), - res.data(), - sizeof(bls_g1) + e.data(), sizeof(bls_fp), + res.data(), sizeof(bls_g1) ); } - int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) { + inline int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) { return internal_use_do_not_use::bls_g2_map( - e[0].data(), - sizeof(bls_fp2), - res.data(), - sizeof(bls_g2) + e[0].data(), sizeof(bls_fp2), + res.data(), sizeof(bls_g2) ); } - int32_t bls_fp_mod(const bls_s& s, bls_fp& res) { + inline int32_t bls_fp_mod(const bls_s& s, bls_fp& res) { return internal_use_do_not_use::bls_fp_mod( - s.data(), - sizeof(bls_s), - res.data(), - sizeof(bls_fp) + s.data(), sizeof(bls_s), + res.data(), sizeof(bls_fp) + ); + } + + inline int32_t bls_fp_mul(const bls_fp& op1, const bls_fp& op2, bls_fp& res) { + return internal_use_do_not_use::bls_fp_mul( + op1.data(), sizeof(bls_fp), + op2.data(), sizeof(bls_fp), + res.data(), sizeof(bls_fp) + ); + } + + inline int32_t bls_fp_exp(const bls_fp& base, const bls_s& exp, bls_fp& res) { + return internal_use_do_not_use::bls_fp_exp( + base.data(), sizeof(bls_fp), + exp.data(), sizeof(bls_s), + res.data(), sizeof(bls_fp) ); } } diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index a1db008f82..2eea8ab397 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -947,24 +947,14 @@ int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); } -int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) +int32_t bls_g1_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) { - return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); + return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); } -int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len) +int32_t bls_g2_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) { - return intrinsics::get().call(point, point_len, scalar, scalar_len, res, res_len); -} - -int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) -{ - return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); -} - -int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len) -{ - return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); + return intrinsics::get().call(points, points_len, scalars, scalars_len, n, res, res_len); } int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len) @@ -986,3 +976,13 @@ int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len) { return intrinsics::get().call(s, s_len, res, res_len); } + +int32_t bls_fp_mul(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len) +{ + return intrinsics::get().call(op1, op1_len, op2, op2_len, res, res_len); +} + +int32_t bls_fp_exp(const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, char* res, uint32_t res_len) +{ + return intrinsics::get().call(base, base_len, exp, exp_len, res, res_len); +} diff --git a/libraries/native/native/eosio/intrinsics_def.hpp b/libraries/native/native/eosio/intrinsics_def.hpp index 7affd102e0..4aac210997 100644 --- a/libraries/native/native/eosio/intrinsics_def.hpp +++ b/libraries/native/native/eosio/intrinsics_def.hpp @@ -176,14 +176,14 @@ intrinsic_macro(alt_bn128_pair) \ intrinsic_macro(mod_exp) \ intrinsic_macro(bls_g1_add) \ intrinsic_macro(bls_g2_add) \ -intrinsic_macro(bls_g1_mul) \ -intrinsic_macro(bls_g2_mul) \ -intrinsic_macro(bls_g1_exp) \ -intrinsic_macro(bls_g2_exp) \ +intrinsic_macro(bls_g1_weighted_sum) \ +intrinsic_macro(bls_g2_weighted_sum) \ intrinsic_macro(bls_pairing) \ intrinsic_macro(bls_g1_map) \ intrinsic_macro(bls_g2_map) \ -intrinsic_macro(bls_fp_mod) +intrinsic_macro(bls_fp_mod) \ +intrinsic_macro(bls_fp_mul) \ +intrinsic_macro(bls_fp_exp) diff --git a/tests/integration/bls_tests.cpp b/tests/integration/bls_tests.cpp index 91bc18ca34..293f57a4f3 100644 --- a/tests/integration/bls_tests.cpp +++ b/tests/integration/bls_tests.cpp @@ -30,78 +30,81 @@ BOOST_AUTO_TEST_SUITE(bls_primitives_tests) BOOST_FIXTURE_TEST_CASE( g1_add_test, bls_primitives_tester ) try { push_action("eosio"_n, "testg1add"_n, "test"_n, mvo() - ("op1", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") - ("op2", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") - ("res", "2b90dabdf4613e10d269d70050e61bdc53c6d01ac517ad33cd8e82799d5515dfba05bc172fd280e2a73ff01aca77e30cbf82182b9005141106ef83a6d33dcda8bece738c9f9d6313f7e05945fd92c23a208efbe7a2048c6250f7f14df9f5c215e244ce19aa2759751dfb31f234c644189d4b0eae26beb2ba29a380a052b058a380b3005a7f18391cd44411a0f87d7817")); + ("op1", "aa83970268d80b50a68535d920954bd993217b9114cbe255e5c0b64e748c5683b6518071552148ec555cdb8fed0f07001e76e62761e8d828a74b81f216a0c5ebeb923550cafd110512cec20e33ec9b272c0ff2b88215953945153fc926691506") + ("op2", "e810c1d6ac7cbae0157a0fc958ef607c33e03dee20e72a97ae72de0dd9a0ef20d768ab4b659429255b87feda0be194062a77cd664976e6560b33c921d5968c19a5d5ae9e38c909690975c084c0a4e82481b01e4023b83d6a8ddcb9d38892f50f") + ("res", "145cae227562a69bbf270b8ec8d1783bed4ad0fd43e9c81ac5caa7d3e7feaa3acda07d6033ece1f7f0649d6b75009a03e83ab8f9b44f94c4ab762611266b7a530ce05092ad38b7554a95caa2b63ad0969a902d69f34531dbccf8a4f2e12cf60d")); } FC_LOG_AND_RETHROW() BOOST_FIXTURE_TEST_CASE( g2_add_test, bls_primitives_tester ) try { push_action("eosio"_n, "testg2add"_n, "test"_n, mvo() - ("op1", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") - ("op2", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") - ("res", "1987cff592d8ae5c83ba9e9a1a016afd3a4e80d646e10a1274ba259b60a405c189945d07b3608d4d339a96429d60f80d8290d66f4e963c0e8c00e35db541ab46e93148fd7e41449f0be0a1883e36e4b56cf87121991d6d4778d499fdf1501c09a9220f11cdfe60560b15d7e6ec33825a8d9ce209fe8e20391d32210ba83dbd77ce4cd6019ca50465f8f5fed4a8a631048739c8d9b8fdc26a962b24f0306f8293a00d72d37fb2fb1b0643d9a8453cbf6a520463a54e25e8c42134d6798d7cce00949892c0f015e698b4386dbc3ef4f9b2b4c61454d90acdcfbf921adcd26bdf77454bdee1eb52a70fcab501fd1cfb0701ba60c9be25f9815bb9c32856144e543140d7c977d4585b0d75467b929db892f2da957948a1b02ecee537bcc742855716")); + ("op1", "a75a447bc0dd806bcdaee2cb429498193e409a4d370dfe0e9605c5a9ec4a09c0314c0321f2f918b72e5f37cf10ea7e0b69ac1a7fb3200e63e5cef9929cd2d9afde2fb957989e610c5b44649af8649d968baa0424b873b501be32c66ef040240b469127753f17ca28a072c2151b69d51fecff1ed0f4786b4e6fd4e52ec72f95ef73cf19fc2d6a28267bdf11a262b403110c2e901fb32d0e900082e4934a20c1c06c262533488fd7f9f47e4e2fde102fe98fb48344de25712d5e2e2887bc04d207") + ("op2", "11cc3115ad08c43ed0b7fc3945ca83c69d0a6663f05b25373ea3fdff14c722a7fddd7a1b4f1cf44290e1c480f50b6208419f6a208ea48a48cef5a345a35c052cf0db4a76c719eb90a5829a8f606e99c807a6ae71493f079c364d57f02cba7c124f344978bb539bea7c448eb96ccea9b6c7b7d2495212b88227fc269ac88391f4a6d23e2d47d4fe47e63f8e709b61450644ba13c08e29770bc397da41466cb26c9fa7a1ffd9b5dd5c51c3ddbfb88412998e41f72e63b7972dfd856b9f11530c01") + ("res", "d9458632a7cfcf59e72cf6d1ad70ec8d65d9e81edd0da77c2b10ec4d2a4169f1b0ac46419796d3ed6ef66b34eb76651288d916a5e5b44e75cc849ecd257a223398f92683f2318bf5fe7aa2b5d45e97dad6c415dac4b9cdc1c72071a4c9845c15096b203deb2e071d3856983cb26a101eb193cbfe914000ade94a62339b2e418c4a53578fb177af1986cc9dee3ce9ff0f9598925cb7e5a29af31404fa8cd9c22e17eb1a44b6b461dd9b7dc4d384ad60b82d94e431a389a244b1724a5441ac6009")); } FC_LOG_AND_RETHROW() -BOOST_FIXTURE_TEST_CASE( g1_mul_test, bls_primitives_tester ) try { - push_action("eosio"_n, "testg1mul"_n, "test"_n, mvo() - ("point", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") - ("scalar", "2a000000000000002a000000000000002a000000000000002a00000000000000") - ("res", "de3e7eeee055abe12a480e58411508d51a356ff6692b14b43426d22cc354cd5d7469c41e0f1f5e40503c91e11419a30285cb057a62c93e2caaaff6c9c1dbc8f88c0a122157f51a617ce0e2890442cd9ce004a8ba972442e61bce9dabf1c6780c191984ae3c11ef21884a536f0d3450974df37295e9579d16cdb8dfdf9252091ca3cd9d05f4c6e645535add05ac197b08")); +BOOST_FIXTURE_TEST_CASE( g1_weighted_sum_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg1wsum"_n, "test"_n, mvo() + ("points", "7a63674c35f9f2be4f0ec092110b82fad24a283316a846d960843b9a6a8a98bfdabd7e897b7da774fbbb05d7ad95fb18940a5f7a7345e1821e0203e156d677e88445385bc0779d25f51b772be3c3a6ba0b09b2533a8ee66053b2f3c853994112e74a97324685e515c86d44ff5a3ef5218e53c6f29e7df49a546c79cd1fb9d14e63f68aaf6b2df1f6f56e6250fdd6f20dcc36499a5aea69f4c77a18774eccf8460e77ab6b0236ce2d85083faeacf0f3378a3dcc95e1d3b0ae282b9bbec1ecee1793e63ca4730ec63df496546c07094e848625610a514d99dd0a6dd9f58d15c4e4ed5aca65025562177ebd0a4c2c3ae8139d24be936dd5dbec6a54897ba2a98a68a9c43ea3199695896789c958bc289a7275550b5670a351b262c10166a48baa161f841df4255408abb7186f78647422313fbe3a0a35699a487dd71482b76b8adc20622f44bb32e04a78b98e4f2cd42f16a6d35ec2463a484906dbb655969b900a15cc8a47694b1961bd4b2e9f074afd79e4c04344d31dd8fcd56d5147a9a47712") + ("scalars", "2be3984dc7a5c9e6172df42c905be929ce5bb29d9aa31a03f3ed8816e7d1265a0669a674fef9254f0efffd3e89c30f1f05587472251d362e1acea03a76918d137b322778b8188c1bf68c36ef8e0ce933290c67f981611c13a286e42235f89855e49966a4e9ad8c64122f4f5e720986193e59771fa91aef113d8739642bdd6613") + ("num", 4) + ("res", "25038aea2d2d1c15dad2fa4c6e31162d1b79ca1b1bca608190a05dea451701e9fc95a8c441a25083df7fa56d54eae51241a8c9489719b9cc35852ce3d7e04b5ada5c65f45d1818f80ea161264b70ab6fa86e694691281b8110000d209aea5c17")); } FC_LOG_AND_RETHROW() -BOOST_FIXTURE_TEST_CASE( g2_mul_test, bls_primitives_tester ) try { - push_action("eosio"_n, "testg2mul"_n, "test"_n, mvo() - ("point", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") - ("scalar", "2a000000000000002a000000000000002a000000000000002a00000000000000") - ("res", "aa25f3f539b6f3215318d81b22d14bf9108294790e8a50545a404ae2057278304d8c3b5844271202b757767d1555a106ede90a9967cdc27b527d4a5720efda79a68b17072ad9402ed373ce9a2d28f5496106fc4cd23234b083181e8325734417f8330d2ced14040b815a006f7f905361f654d483c45abb90b4a958b4ca20ee2bb97cc1c9b6ff45644539abb32149610f33858c88450dad3d2adb82df72f9ed9c42dc2ef78e17f5a2a1abd0468853d55f05f6458179fdf5671db784795a686c0ffae139afaed0212d18d615b0ff90a9deba090f723190521dc8c822621b0a7e70a03b9f3faaeb862846dccd418855d70406fc57e73783c2da92433c1a3873640217539ec7c01f3d354506d86db49fddad225e82421506d99c19b749170aa4f805")); - -} FC_LOG_AND_RETHROW() - -BOOST_FIXTURE_TEST_CASE( g1_exp_test, bls_primitives_tester ) try { - push_action("eosio"_n, "testg1exp"_n, "test"_n, mvo() - ("points", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") - ("scalars", "2a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a00000000000000") - ("res", "9e80ed609e62978a3a7f0d6bf57e1df4070725c1bf4dab43a6b710adef7450fb41f0cf14a7895ec26fd8c830c327cc0e2c8fe7687d23ff84647b47bbad04cf77625dee1049e53ad5162fe772278e5fe3ceb0bdc472f31952343da7b75532f7016613af892092131ad77d13afc8c9192487ac19f8454ad932653145f06e25790d26b23ac6b83025326f3397efbd845511")); - -} FC_LOG_AND_RETHROW() - -BOOST_FIXTURE_TEST_CASE( g2_exp_test, bls_primitives_tester ) try { - push_action("eosio"_n, "testg2exp"_n, "test"_n, mvo() - ("points", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") - ("scalars", "2a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a000000000000002a00000000000000") - ("res", "595a2cbaa4315ecc0dd9838a81712e0cfb88cb2c4468640ff382abb321b3a9bedebb0aad985a9057f664fc52eb52cd194b1c9611b25ef4281e439a52d1832813decd66c22440821f0288dcb7a82151386aa0240e943b6905e61619be2e11c208fa03df16e11a1b1368abac90598bb237f785701d5d1d5cb0af6934ed633d366de28703431b8d70899d92797689207c0cd35c345460f971dff5d648d9ddec8f5fabef99b15ea7c4440ac1564d6e0326076f32a4ec9cf0d10059593d64afd35c03d10f16794821628b565c9319f4af3c96b98c4fb11bc0c04172bb57372531f6e76798142d4b00488adbea850a2649a305b71fa389c3c226f2df4a58c8bce5d90698452f4126046be0c82d8817b64162a53787f1cb73af27d969adc626c1392716")); +BOOST_FIXTURE_TEST_CASE( g2_weighted_sum_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testg2wsum"_n, "test"_n, mvo() + ("points", "556524e3e80fcf448c0befd659ad744c409cdd1111291a81949324a02688999ab88e6ccb3e9dcecf57ccfe9aa7ada21540ba5407c0700ce81578bcee892ed385dd1ed476c465fe48cea2dd7e37784d8ae795fba842ce0f88baa76857ec1c7c137163beed84ff2c6a159d486d10c7128b2cbb6f10c547d202d0f6e68fb43a9a6a18a01823c4e70b5dc25d38b9c33d4916510de467f10922f3b284855c4accc816f542fe74fd0b331b1884dac6918d3c38860b0725809a19b6101e5d3942b9f40eee6901e17a0c50187e6385767b7d1e54eaacbb3c1939e2e995ec79569dd0200583a937891035475e1e5a733a0446f7063e77f9d564c60dfc6dc25572369bf7fe51b2607f73fd0fa9eee9d27b127a81c63bbd7f08dd64237d082e9735018d96024c56d32b55e991e84f39c6ffc0d56ef17307e82fc200df9259f83b7bdae719124f7151d855b8f51f703e087ea1540900c4eba1592de051db895689aeb5233dec52ccb74f93acc8fc14bcde4413a14dceb0ec386623021ddf9ad1fce429e1780c5a6877b00b17af384fd49b7077434387672218967f4a7647174162c07e99d9007380be10a7bef367181ba886a0e40c160a4b36548826c30710ea8238b0c37ec06e331fe1d3aaf1b5a73ae0880794d0c0a9623b8498d9bbd4754e1a8c1e2346091fec42aaeddc40f2ae06456e8ad9059c61db437089a855af3d1bc2a6d68e9c01474bdbaabfa185a58dba9808f8e0b20a3c51c51c5bca629aa061dfa5ff238a43e1e90242df403635869743c723276c0eca711c6c91b94a65310b6b869ee556179ed113a2b406a52de2fe54f61f4589f510924b2e3f8b14ead1541af2e1cc9dd35a91f6cd4bbc02c58a844d9e4906a106197b8ab39a539561aeecd265762bcf5106344ae2fe0a62c7af1bbc846e9b39ebe38a046461d7c0c4901caddb0fd7c70b86246d53bf3124af629596f6367e86f3456cc954e3581bfea600b6d2818259332fedeb88ece673626ffd4d5935d0310e5cf2462055d5fa4b6cc4531f11cf0722c8647dbfc7364c9b2e24e0066ef5b0e3497ce38537a7230e02f7edc2f53e980f") + ("scalars", "e5a6afde5d144ed43bd1742b7ec7f5385a7d392f65465927e37afe7b9f0a9e368449cf52e36ca432b096f8562c98f318216d21a426b2d51f0a4db8fcafcd687326b548cd973f30d4fc47d79d853ec94239c620229ea0632b2570a1f8df08fe1ce323ef2cad610917a13d1157e25fac05ef6bafaa68e8ce265594d207a5d13c1e") + ("num", 4) + ("res", "889d8b750987cbce4aa4b872b29f22011752354e672b24ad4b229ca575ce14a8c081366db5cfd0f7a85ff68559bd4216b3cecfb6a2618a29c6c95b17147d083f28cc1fae5959fd7de74d986e7fd543ce1060cbdd8d4ae136910d8a15a1b3ee16d955657630d5f50aae107d13aa0c46040616e2d3018e5d07f9551e2a4c34565c9b59b8fd7751e025f473772120b4a511946e09fc93dcf9732399244c1fbb864fa09c2dd5d29543e794e64b6e1a2df39289245ff4b65dff0357a6621d151e4514")); } FC_LOG_AND_RETHROW() BOOST_FIXTURE_TEST_CASE( pairing_test, bls_primitives_tester ) try { push_action("eosio"_n, "testpairing"_n, "test"_n, mvo() - ("g1_points", "160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615160c53fd9087b35cf5ff769967fc1778c1a13b14c7954f1547e7d0f3cd6aaef040f4db21cc6eceed75fb0b9e417701127122e70cd593acba8efd18791a63228cce250757135f59dd945140502958ac51c05900ad3f8c1c0e6aa20850fc3ebc0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") - ("g2_points", "100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100a9402a28ff2f51a96b48726fbf5b380e52a3eb593a8a1e9ae3c1a9d9994986b36631863b7676fd7bc50439291810506f6239e75c0a9a5c360cdbc9dc5a0aa067886e2187eb13b67b34185ccb61a1b478515f20eedb6c2f3ed6073092a92114a4c4960f80a734c5a9c365e1ffa7c595a630aaa6c85e6e75f490d6ee9b5efbba225eff075a9d307e5da807e8efd83005db064df92fcc0addc61142b0a27aa18a0ebe43b6aacad863aa33dc94e5c4979edca3ca4505817e7f21bde63a1c22b0bfdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") - ("res", "af587ca8f5f0760b888e4ec66748623d1b13986547b5de2042ce89233c2c837bbb080329a505285c948d8bb4c88a5914ae216a64db582ba084178db4364a7b3843ef7fb9056a90526d27dc9e252b38600fc54ce7a5ec15771c555816edc26d0166e75d3315789c846387c8c234b1c98b50baf233b7312aa317ae56a2bfa170b1bc43f0e046b4a1f45cb7aacea7d0ae0320496e3960d4dbc2039027ba8cfe1ca120ea98fa94cf25034ccb5e74033f447b837a78b03affd44b87f865f3d713000de78ab5629dbde8d0c8b7a28941717be3ebd23924cf16897144bb69730f68bff5a109fc2e6d563b15e2eb883cf4becd11edcbc2ec4402c93249389ed612fa0396ed6eadcbe4d703667d46b0150ee3a5158caef956791e4f527e1312c8402f8509acf7001dc0dc311549d76398c247e79b737b614d0a6663f7dbdb314e5b51eace14457ee7b1f3f54d5987c16c8f89d1022d91d4649f5f6a204047077e5791a654cd2277506cd0af77f9ea789278b364c115ec07ba14390a9c22d59aa9c97a9c09ce025b5e14443f3c4e4cd602ef34105fadd82837fc5ce60a461e6ea11b13ae67b82e3366a2b2d1bbe78b2579173b3c0c5aed88b2949403060c3e065782bcb742c55c559e75e373293d80dec54120773d80144b21b353ead58dc8427e5b9cbd0c1431f1a74caf5f57c4b55b89810029cdd24ef4a029797ced68ff882492a8f55f31fe52217356366983e35d2a5640e818cc8bddf9274de0100e624a6bf03e2b9ff22f8dda09a46b50b1b305cb6adfa2ae775febbce4a2b507cd2390db968ceb13")); + ("g1_points", "af7d28fa69e9ff7cf98da7614f02faf91f8ae2888d522db1a225724394bedd8547ae93cb07b41f5f21092846c6c2950e43328c2e6aaa18cf735a703f4ee43a60658167b9c1bf895ddbc898dae5269a335699c595aa9dc4137103d5480320270f756a4c35700b847a02d4933be43e508139affb438b7bcf547debcf9904f1056d197ec8484283e2f194430e16443d250f0b4734d5380cac631d6287e2b45a6a12fb5fea3eed96a7b2cd3bac9a532e4bb6176b1e792964444798e5bf151e14c314277fa052e05911619c457335e708633aca11239a33686e56dd949e6f50e5fe79f1d069bbb01822566f992299184ddc06d08c662a5d2b37668c7821bef5177ff4df977799dc81ae15d0444be9a54db2195d41e316f7ebdb7d879f8f7b355cb416137401a36684f0ccbce51fd4d8b32f45d81f2c33bc95687ed777e7fcb181367e163d94356c96d239f193dabf01616d13936ba746ed9a64578a5a4b191238fe8472e4135525afcac4ed818c13e518864ce82de26a6bb1066b9c0771c021716603") + ("g2_points", "5528fc322669dcf2757583835b5ba5cc84f629db47e1d5e3ec78523fff567ec26100b7977ba038586c9e465abbaeb603554afef9cfe21ac33847b1d11796088cc6b18c7544355da3f996ddb0c0a15c8b1eeba2ec3a5cc8c7b4a8ecc7131e4609582cc42caa57f68017f95af50757bfc0d3c4d4327d1d40012af8f664da428f1984bf17c26d43b6fd65a8ce6c3d039d03f1247385a3f565108de722d4e08b0e9d23bba036646668167bdd41709ebe8bf5aadbd88ccc2af0c6d5940531c9fd50053e786ffe9011554db15fc7e130ea07cd254f21a86911fe758517274f96b89cb67e30c104186c4a6cb0d92cb82e39260245b68e74bdb828992697a1957be0564a9863f56eaf36bb5fc4d0869468b6ad4e36a1e7c550303c80c4ca0cbae773b309a61c6d4d6a4d2f17a4424c1987d294f195275581c8b8f88b7ce7fec5bc67e7bfd58bb747f39aa4dbc77d79cd18dd09142b10a45d49abc8fd544281f02a0e045062b72b25ead1a376e4f705ac83ad2385c84a8fd914d2f6a71d7eeb7126d677001013a0df759ff906a09fe4a3da307dd6cc17ee1fd72af8f7d953662be427cda2985cb1073563bbea492d9e6a96ca8a05ca5d45d349cb3e2f09cff6022a9916324eaa4fe5c543d46cafe5f82e81c5baddc2cc58100b03a20c2feaeed58cbd63019b8e3586d14193470d4ee9c1a0fc5e0a736e1ba94d7ad20ead3a3dacc4b2f3530ad751daffa944be4cb1969b3d2595058007d28d49e2fdebab68607c119b77d766785ed9e209582c737fce93b6ad0d4d795f86415238e0d3b70b9d8954d6a20ce85e7128588a849c505d5d2c81ff63bf12ea3ad52a316316ce49b2014dfbc486c9934dccb98db06089c03dc8f76ad70323ada12fe7e5b9f552c5b6cc073f72409a6e44f6634ca58bed30649b9840f177ea0039932f81574ea22b6da18b702f0e367f633e41d2b585d5166669f4c48d389f003eeecf06b5aa119d0df46de26adcb938d362e4410fdaa990a6fc4c2eca0f7bffee724789a0a47ba1b531bb4a3c95c129585ba5c9a6fe978a6915f4afedeb710ef6a55f153a5b21f943de6531680d") + ("res", "01cc207c7c76c65afc059d073106d545705af7c4e4ed78e6169c599bae1c9bf6358d5a2cdfeeae389cc64436dfe7b7192c733fe9e2d105c0eaec21589d8bb5ecd5c71a5fee083af47a1cbc3af2df8a13603a78956c39f55fd0663ed482846a047280f87426ecde4c535c0d30bdf4b95ff5aff45782129ee6c7a0fa20a632bc98e95bfb663685eb2443e4b1f518c40f00502303b3db49be61f36ef6af46b9097c83835a864ae6337ef07e40df617af1aacea98c0d75e96a104d41a3617e7a4f05fc1cdcec57ea40a0237ff387a5d9130b34c92cb57a3450080bbaa2f6f9e895851b05c05eec03abd2d5083c81c402a500d2bec4895cf0f58a9953e43ee9039a38ae73cc3ec325fa636777e1f597b69a2b6a62bd49fba5b163bc03ce73f277f818fad1c6681fa59be891c2baf85b6a5e908d96b6a6c4cf3d53b3de23fdade91b3dde7199648aca0fce1d2307253bf57b054a4c4dc276034f8d57017cddc0a84975c643f9ea808652f2cefea32f767fe8e6df58a837c47c3136ced7107e7dbd9711399605212e6a1b8da806a12636076f7da0629ca8173f0411ba2a1050d1bca61994e28f69f274ec94e016a3ba6f9a6b06cd17f570928b5e4b8c75a5d62a0a67bd54249702923b440355db775599ea7be7cd3c6d88ab8da7694e43352837b0080814880a1460bc61fae141d36f961db8393313f74a65c3eabcc38793b84062999364bb0c36a415445ec4318b59c16e850260a53e7aaaf72bcaa568b57b69b552bf827c3e1c116565122322d6c26e14354fd96dc4bb1161ede0ee36efeeae1a5503")); } FC_LOG_AND_RETHROW() BOOST_FIXTURE_TEST_CASE( g1_map_test, bls_primitives_tester ) try { push_action("eosio"_n, "testg1map"_n, "test"_n, mvo() - ("e", "c93f817b159bdf8404dc378514f845192bbae4faac7f4a568924f2d9725125000489408fd796461c288900add00d4618") - ("res", "d8e09005badb694b1e5dba8476ba3012cd2bf0c472ee1811ac9b34c869638b47e669bce16933e98ce9b129f83d93d71890d6fd13b77d4ad8df1a5f1c6b81df2b3f305721b7b7874cd81eba23ca05e8bc974b14f7e4c2c77cbdf321c340ba4903a2a46af290abe078e426f913e492b8d9b997232dbe6198aea719a31e73b40e110c4efa9e42d737a42883e0ca5f344d02")); + ("e", "6de1c03023f2b4e454559b97266ebc1f96cd5510c4f48e2acafbe73a536a1477f2c8d1c9b5f6615d01968ea59ef4fd07") + ("res", "2c7390d4bfbe0ccbaae9699aab5458d62f6d1aa91938b0ab0717249ad89209cb14ee8ed764a8952415382dbbfdef2312d5b2988b1ed7e6907d73ef2335cba7f9263b63003effa44ab8d6805d6ed0f08d87919230f0cb45d9ec35b2e0615d920f")); } FC_LOG_AND_RETHROW() BOOST_FIXTURE_TEST_CASE( g2_map_test, bls_primitives_tester ) try { push_action("eosio"_n, "testg2map"_n, "test"_n, mvo() - ("e", "d4f2cfec99387809574fcc2dba105603d950d490e2bebe0c212c05e16b784745ef4fe8e70b554d0a52fe0bed5ea6690ade2348eb8972a96740a430df162d920e175f5923a76d18650ea24a8ec06d414c6d1d218d673dac3619a1a5c142785708") - ("res", "9968ae9e9bf3d22ec6c9670efa64ea23d4966b41bb25f76ea2ef71b96fa35e031adb866b3df234065b9aa72d0b12ab14978678279eb944f05b5af53d91e700b57aa87076727def2e9f2fba3cf6784a25591ae1669c2cf15cdcf038b826d1e81178bd7b59b7e911e0c2d11d6805756222201e3364f8010fb651939eca63f7e77709042ee1030cd938f53905a714e815112a7dfeed207757d30382f69617014bc683a175d0dfbd74f2684de26c771f3f55a538e6d2f969eb282bddfec4fc08dd18f37df0889292f288dff631b02f07b88134410fd86526d234d75b9a329bc9a8b6e71c7ad516b87af9717d962cba5d2b19fd1b77746f1e484a54e6aec81ede148f01e2c8283c598a4976182d2ce287fe6888e23996ce03b03ce6709e4aa8e66416")); + ("e", "b4108d552274d175fd4e52dd766ff396a4f365c102e0ef7b4d266f220ec67eefab48ac15d1db0d98ba31cda435616e0bdeb51952b044ce0f64b5d42199d9560ed0175f3d2c58af07909d29db4480c1b417dfbf7f1af274648adb54b029e38f08") + ("res", "f74a6a357dc6a1fb33344f60898ab9eb29c68170727136b26d943249fbbd9393bd893846ea0d43250d4fd37737e49f1463d11827561e80cda0f6ead2cd3c84d4be6d99d61fa4cbc41cce534af2cf3d1b3fc31ccecc1259cfc7530a93c58e801951454f38f693d2d763e0f993c202151d873eefec3e90507cea6232cb3c0061f254a2f4b5872098b23362763a79d6c004333e0e02ad077c8a3683d63827becf20c88a9b7a63bbac739bc3a4f659d119df2f5853e371a25c803fc8301c393e7804")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( fp_mul_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testfpmul"_n, "test"_n, mvo() + ("op1", "AAAAFFFFFFFFFEB9FFFF53B1FEFFAB1E24F6B0F6A0D23067BF1285F3844B7764D7AC4B43B6A71B4B9AE67F39EA11011A") + ("op2", "AAAAFFFFFFFFFEB9FFFF53B1FEFFAB1E24F6B0F6A0D23067BF1285F3844B7764D7AC4B43B6A71B4B9AE67F39EA11011A") + ("res", "010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( fp_exp_test, bls_primitives_tester ) try { + push_action("eosio"_n, "testfpexp"_n, "test"_n, mvo() + ("base", "AAAAFFFFFFFFFEB9FFFF53B1FEFFAB1E24F6B0F6A0D23067BF1285F3844B7764D7AC4B43B6A71B4B9AE67F39EA11011A") + ("exp", "AAAAFFFFFFFFFEB9FFFF53B1FEFFAB1E24F6B0F6A0D23067BF1285F3844B7764D7AC4B43B6A71B4B9AE67F39EA11011A00000000000000000000000000000000") + ("res", "010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")); } FC_LOG_AND_RETHROW() BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { + // assumes signed message of std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; push_action("eosio"_n, "verify"_n, "test"_n, mvo() - ("pk", "90ace8f520a5e056eab099d4e6a6f761a8f51301e1e15910908453007d94e33f8e86b834808dcb5323313153be066d01d49660f594a805ad9a4249d0af4190407e3e221e1d6fdad866e022308357776ed5e396777c7f7dfd020e7d736511ac06fdff02000000097602000cc40b00f4ebba58c7535798485f455752705358ce776dec56a2971a075c93e480fac35ef615") - ("sig", "3c38a59a6c016fb598d8da531e205e4d8e0953868675760f8333c7fee725d81ddf246db75792eb7cf0ab8aeba051800babf407ddd8ec721db83fe1b1098b9334bf45ad63f10cf5a6b56b60f74b391615d7373088d182711358a6032f8c31fe1694ff3f3254a7e02e3e2811ce0becdec6031b193b5541d577b49cdb6cf09f919917b09776b1f05c977ffa17c2de876205f402f7e4fb00896cb9b3df1a25f761239e5625ddaab4bfacce8e6c505c13eb73632b12cedd30b0804fe29f317015c307ec2dfe48cb5060d84fd2c2756f41ae79f4c0a3a37ba62b22877b23042bb30a063d4ad3255de844545e89f0886041b513410df52d380fac23c31277997381115651c02014a9aeeaaec85c34b81a93ee73cd712ce241f394dc860ea5e792b0f50e")); + ("pk", "c27efc440ed89d739ff80f1c7178d4672d5f71e8c8926a85785d0f84074e79c1662c522fdbd6fcaebd640d816dea6c0dc36c5d7e7a297afc960369e306d845fcad0e5fe2a88c0d3bbe854d4f9e3e6365c9c1003662f0e81b35fd9c6a11d0a10c") + ("sig", "e13a38c18dfad440a9af8d720bfb9ce0f388a25a5789a8cdc34b87204fe29db5cc16658a5d9f4fa4a7cc16b8ef8d2d0d1a5984e6b69ca228692f1920e9340f149024b4d856711656f11bb1e0c7d081e5bb80692638a9bace2d8f4d796b4887129c59fbb7901997cbbb681f709291a45315979f55b719dfc8757d3a878cf01e4a48a0e6647b837d0ea8aea5d41e121b0dd37fa20007aef4f51740d846cc8d6ab151dbd88964e7d19890a500d5537be81b881451b75f928a66f546441d45028603")); } FC_LOG_AND_RETHROW() diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 1b97e8a338..2ccc67b870 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -198,35 +198,35 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ } [[eosio::action]] - void testg1mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { + void testg1wsum(const std::vector& points, const std::vector& scalars, const uint32_t num, const std::vector& res) + { + check(points.size() == std::tuple_size::value * num, "wrong points size passed"); + check(scalars.size() == std::tuple_size::value * num, "wrong scalars size passed"); + check(res.size() == std::tuple_size::value, "wrong res size passed"); bls_g1 r; - bls_g1_mul(*reinterpret_cast(point.data()), *reinterpret_cast(scalar.data()), r); - check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_mul test failed"); + int32_t error = bls_g1_weighted_sum( + reinterpret_cast(points.data()), + reinterpret_cast(scalars.data()), + num, + r + ); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_weighted_sum: Result does not match"); } [[eosio::action]] - void testg2mul(const std::vector& point, const std::vector& scalar, const std::vector& res) { + void testg2wsum(const std::vector& points, const std::vector& scalars, const uint32_t num, const std::vector& res) + { + check(points.size() == std::tuple_size::value * num, "wrong points size passed"); + check(scalars.size() == std::tuple_size::value * num, "wrong scalars size passed"); + check(res.size() == std::tuple_size::value, "wrong res size passed"); bls_g2 r; - bls_g2_mul(*reinterpret_cast(point.data()), *reinterpret_cast(scalar.data()), r); - check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_mul test failed"); - } - - [[eosio::action]] - void testg1exp(const std::vector& points, const std::vector& scalars, const std::vector& res) { - auto num = scalars.size()/sizeof(bls_scalar); - check(points.size()/sizeof(bls_g1) == num, "number of elements in points and scalars must be equal"); - bls_g1 r; - bls_g1_exp(reinterpret_cast(points.data()), reinterpret_cast(scalars.data()), num, r); - check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_exp test failed"); - } - - [[eosio::action]] - void testg2exp(const std::vector& points, const std::vector& scalars, const std::vector& res) { - auto num = scalars.size()/sizeof(bls_scalar); - check(points.size()/sizeof(bls_g2) == num, "number of elements in points and scalars must be equal"); - bls_g2 r; - bls_g2_exp(reinterpret_cast(points.data()), reinterpret_cast(scalars.data()), num, r); - check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_exp test failed"); + int32_t error = bls_g2_weighted_sum( + reinterpret_cast(points.data()), + reinterpret_cast(scalars.data()), + num, + r + ); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_g2_weighted_sum: Result does not match"); } [[eosio::action]] @@ -245,6 +245,30 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ check(std::equal(res.begin(), res.end(), r.begin()), "bls_g1_map test failed"); } + [[eosio::action]] + void testfpmul(const std::vector& op1, const std::vector& op2, const std::vector& res) + { + bls_fp r; + int32_t error = bls_fp_mul( + *reinterpret_cast(op1.data()), + *reinterpret_cast(op2.data()), + r + ); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_fp_mul: Result does not match"); + } + + [[eosio::action]] + void testfpexp(const std::vector& base, const std::vector& exp, const std::vector& res) + { + bls_fp r; + int32_t error = bls_fp_exp( + *reinterpret_cast(base.data()), + *reinterpret_cast(exp.data()), + r + ); + check(std::equal(res.begin(), res.end(), r.begin()), "bls_fp_exp: Result does not match"); + } + [[eosio::action]] void testg2map(const std::vector& e, const std::vector& res) { bls_g2 r; @@ -336,13 +360,15 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ } const std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; - const std::vector G1_ONE_NEG = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; - const std::vector GT_ONE = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + const std::vector G1_ONE_NEG = {0xbb, 0xc6, 0x22, 0xdb, 0xa, 0xf0, 0x3a, 0xfb, 0xef, 0x1a, 0x7a, 0xf9, 0x3f, 0xe8, 0x55, 0x6c, 0x58, 0xac, 0x1b, 0x17, 0x3f, 0x3a, 0x4e, 0xa1, 0x5, 0xb9, 0x74, 0x97, 0x4f, 0x8c, 0x68, 0xc3, 0xf, 0xac, 0xa9, 0x4f, 0x8c, 0x63, 0x95, 0x26, 0x94, 0xd7, 0x97, 0x31, 0xa7, 0xd3, 0xf1, 0x17, 0xca, 0xc2, 0x39, 0xb9, 0xd6, 0xdc, 0x54, 0xad, 0x1b, 0x75, 0xcb, 0xe, 0xba, 0x38, 0x6f, 0x4e, 0x36, 0x42, 0xac, 0xca, 0xd5, 0xb9, 0x55, 0x66, 0xc9, 0x7, 0xb5, 0x1d, 0xef, 0x6a, 0x81, 0x67, 0xf2, 0x21, 0x2e, 0xcf, 0xc8, 0x76, 0x7d, 0xaa, 0xa8, 0x45, 0xd5, 0x55, 0x68, 0x1d, 0x4d, 0x11}; + const std::vector GT_ONE = {0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; [[eosio::action]] void verify(const std::vector& pk, const std::vector& sig) { + check(pk.size() == std::tuple_size::value, "wrong pk size passed"); + check(sig.size() == std::tuple_size::value, "wrong sig size passed"); bls_g1 g1_points[2]; bls_g2 g2_points[2]; From 260c2a27e11cc0293d5a13c0e68699ec83b5ab97 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 1 Dec 2023 15:43:37 -0600 Subject: [PATCH 090/158] GH-251 Add comments on how values created --- tests/unit/test_contracts/bls_primitives_tests.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 2ccc67b870..fecb8fad7f 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -360,9 +360,11 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ } const std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; + // g1::one().negate().toAffineBytesLE() const std::vector G1_ONE_NEG = {0xbb, 0xc6, 0x22, 0xdb, 0xa, 0xf0, 0x3a, 0xfb, 0xef, 0x1a, 0x7a, 0xf9, 0x3f, 0xe8, 0x55, 0x6c, 0x58, 0xac, 0x1b, 0x17, 0x3f, 0x3a, 0x4e, 0xa1, 0x5, 0xb9, 0x74, 0x97, 0x4f, 0x8c, 0x68, 0xc3, 0xf, 0xac, 0xa9, 0x4f, 0x8c, 0x63, 0x95, 0x26, 0x94, 0xd7, 0x97, 0x31, 0xa7, 0xd3, 0xf1, 0x17, 0xca, 0xc2, 0x39, 0xb9, 0xd6, 0xdc, 0x54, 0xad, 0x1b, 0x75, 0xcb, 0xe, 0xba, 0x38, 0x6f, 0x4e, 0x36, 0x42, 0xac, 0xca, 0xd5, 0xb9, 0x55, 0x66, 0xc9, 0x7, 0xb5, 0x1d, 0xef, 0x6a, 0x81, 0x67, 0xf2, 0x21, 0x2e, 0xcf, 0xc8, 0x76, 0x7d, 0xaa, 0xa8, 0x45, 0xd5, 0x55, 0x68, 0x1d, 0x4d, 0x11}; + // fp12::one().toBytesLE(); const std::vector GT_ONE = {0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; - + // caller of verify() must use this msg std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; [[eosio::action]] From 677e739462566ba873f6913a4288dc958e28d1bd Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 1 Dec 2023 16:03:00 -0600 Subject: [PATCH 091/158] GH-251 Add comment on how public key and signature strings were created --- tests/integration/bls_tests.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/integration/bls_tests.cpp b/tests/integration/bls_tests.cpp index 293f57a4f3..446887c041 100644 --- a/tests/integration/bls_tests.cpp +++ b/tests/integration/bls_tests.cpp @@ -102,6 +102,8 @@ BOOST_FIXTURE_TEST_CASE( fp_exp_test, bls_primitives_tester ) try { BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { // assumes signed message of std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; + // vector seed(32, 0x30); array sk = secret_key(seed); g1 pk = g1::one().scale(sk).affine(); string pk_hex = to_hex(pk.toAffineBytesLE()); + // g2 sig = sign(sk, msg); string sig_hex = to_hex(sig.toAffineBytesLE()); push_action("eosio"_n, "verify"_n, "test"_n, mvo() ("pk", "c27efc440ed89d739ff80f1c7178d4672d5f71e8c8926a85785d0f84074e79c1662c522fdbd6fcaebd640d816dea6c0dc36c5d7e7a297afc960369e306d845fcad0e5fe2a88c0d3bbe854d4f9e3e6365c9c1003662f0e81b35fd9c6a11d0a10c") ("sig", "e13a38c18dfad440a9af8d720bfb9ce0f388a25a5789a8cdc34b87204fe29db5cc16658a5d9f4fa4a7cc16b8ef8d2d0d1a5984e6b69ca228692f1920e9340f149024b4d856711656f11bb1e0c7d081e5bb80692638a9bace2d8f4d796b4887129c59fbb7901997cbbb681f709291a45315979f55b719dfc8757d3a878cf01e4a48a0e6647b837d0ea8aea5d41e121b0dd37fa20007aef4f51740d846cc8d6ab151dbd88964e7d19890a500d5537be81b881451b75f928a66f546441d45028603")); From c076567d0e6e124824cdc73b7f855f513c9f44ef Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 1 Dec 2023 16:22:42 -0600 Subject: [PATCH 092/158] GH-251 Use size() instead of sizeof --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 7c3a85f497..fe64e3a1a8 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -53,81 +53,81 @@ namespace eosio { inline int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) { return internal_use_do_not_use::bls_g1_add( - op1.data(), sizeof(bls_g1), - op2.data(), sizeof(bls_g1), - res.data(), sizeof(bls_g1) + op1.data(), op1.size(), + op2.data(), op2.size(), + res.data(), res.size() ); } inline int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) { return internal_use_do_not_use::bls_g2_add( - op1.data(), sizeof(bls_g2), - op2.data(), sizeof(bls_g2), - res.data(), sizeof(bls_g2) + op1.data(), op1.size(), + op2.data(), op2.size(), + res.data(), res.size() ); } inline int32_t bls_g1_weighted_sum(const bls_g1 g1_points[], const bls_scalar scalars[], uint32_t num, bls_g1& res) { return internal_use_do_not_use::bls_g1_weighted_sum( - num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1), - num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar), + num ? g1_points[0].data() : nullptr, num * g1_points[0].size(), + num ? scalars[0].data() : nullptr, num * scalars[0].size(), num, - res.data(), sizeof(bls_g1) + res.data(), res.size() ); } inline int32_t bls_g2_weighted_sum(const bls_g2 g2_points[], const bls_scalar scalars[], uint32_t num, bls_g2& res) { return internal_use_do_not_use::bls_g2_weighted_sum( - num ? g2_points[0].data() : nullptr, num * sizeof(bls_g2), - num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar), + num ? g2_points[0].data() : nullptr, num * g2_points[0].size(), + num ? scalars[0].data() : nullptr, num * scalars[0].size(), num, - res.data(), sizeof(bls_g2) + res.data(), res.size() ); } inline int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { return internal_use_do_not_use::bls_pairing( - num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1), - num ? g2_points[0].data() : nullptr, num * sizeof(bls_g2), + num ? g1_points[0].data() : nullptr, num * g1_points[0].size(), + num ? g2_points[0].data() : nullptr, num * g2_points[0].size(), num, - res.data(), sizeof(bls_gt) + res.data(), res.size() ); } inline int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { return internal_use_do_not_use::bls_g1_map( - e.data(), sizeof(bls_fp), - res.data(), sizeof(bls_g1) + e.data(), e.size(), + res.data(), res.size() ); } inline int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) { return internal_use_do_not_use::bls_g2_map( - e[0].data(), sizeof(bls_fp2), - res.data(), sizeof(bls_g2) + e[0].data(), 2 * e[0].size(), + res.data(), res.size() ); } inline int32_t bls_fp_mod(const bls_s& s, bls_fp& res) { return internal_use_do_not_use::bls_fp_mod( - s.data(), sizeof(bls_s), - res.data(), sizeof(bls_fp) + s.data(), s.size(), + res.data(), res.size() ); } inline int32_t bls_fp_mul(const bls_fp& op1, const bls_fp& op2, bls_fp& res) { return internal_use_do_not_use::bls_fp_mul( - op1.data(), sizeof(bls_fp), - op2.data(), sizeof(bls_fp), - res.data(), sizeof(bls_fp) + op1.data(), op1.size(), + op2.data(), op2.size(), + res.data(), res.size() ); } inline int32_t bls_fp_exp(const bls_fp& base, const bls_s& exp, bls_fp& res) { return internal_use_do_not_use::bls_fp_exp( - base.data(), sizeof(bls_fp), - exp.data(), sizeof(bls_s), - res.data(), sizeof(bls_fp) + base.data(), base.size(), + exp.data(), exp.size(), + res.data(), res.size() ); } } From 6beeb6c92cd163e6bfd467d2bf68169f2105e6f3 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 1 Dec 2023 16:26:08 -0600 Subject: [PATCH 093/158] GH-251 Simplify GT_ONE declaration --- tests/unit/test_contracts/bls_primitives_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index fecb8fad7f..12ef0b7ad6 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -363,7 +363,7 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ // g1::one().negate().toAffineBytesLE() const std::vector G1_ONE_NEG = {0xbb, 0xc6, 0x22, 0xdb, 0xa, 0xf0, 0x3a, 0xfb, 0xef, 0x1a, 0x7a, 0xf9, 0x3f, 0xe8, 0x55, 0x6c, 0x58, 0xac, 0x1b, 0x17, 0x3f, 0x3a, 0x4e, 0xa1, 0x5, 0xb9, 0x74, 0x97, 0x4f, 0x8c, 0x68, 0xc3, 0xf, 0xac, 0xa9, 0x4f, 0x8c, 0x63, 0x95, 0x26, 0x94, 0xd7, 0x97, 0x31, 0xa7, 0xd3, 0xf1, 0x17, 0xca, 0xc2, 0x39, 0xb9, 0xd6, 0xdc, 0x54, 0xad, 0x1b, 0x75, 0xcb, 0xe, 0xba, 0x38, 0x6f, 0x4e, 0x36, 0x42, 0xac, 0xca, 0xd5, 0xb9, 0x55, 0x66, 0xc9, 0x7, 0xb5, 0x1d, 0xef, 0x6a, 0x81, 0x67, 0xf2, 0x21, 0x2e, 0xcf, 0xc8, 0x76, 0x7d, 0xaa, 0xa8, 0x45, 0xd5, 0x55, 0x68, 0x1d, 0x4d, 0x11}; // fp12::one().toBytesLE(); - const std::vector GT_ONE = {0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; + const std::vector GT_ONE = []{ std::vector r(576, 0); r[0] = 1; return r; }(); // caller of verify() must use this msg std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; From 34c7f6991370e5a1db400a1ad25a920cd212f6f6 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 1 Dec 2023 16:41:07 -0600 Subject: [PATCH 094/158] GH-251 Correctly handle num==0 --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 63 +++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index fe64e3a1a8..3676a1e829 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -68,30 +68,57 @@ namespace eosio { } inline int32_t bls_g1_weighted_sum(const bls_g1 g1_points[], const bls_scalar scalars[], uint32_t num, bls_g1& res) { - return internal_use_do_not_use::bls_g1_weighted_sum( - num ? g1_points[0].data() : nullptr, num * g1_points[0].size(), - num ? scalars[0].data() : nullptr, num * scalars[0].size(), - num, - res.data(), res.size() - ); + if (num > 0) { + return internal_use_do_not_use::bls_g1_weighted_sum( + g1_points[0].data(), num * g1_points[0].size(), + scalars[0].data(), num * scalars[0].size(), + num, + res.data(), res.size() + ); + } else { + return internal_use_do_not_use::bls_g1_weighted_sum( + nullptr, 0, + nullptr, 0, + 0, + res.data(), res.size() + ); + } } inline int32_t bls_g2_weighted_sum(const bls_g2 g2_points[], const bls_scalar scalars[], uint32_t num, bls_g2& res) { - return internal_use_do_not_use::bls_g2_weighted_sum( - num ? g2_points[0].data() : nullptr, num * g2_points[0].size(), - num ? scalars[0].data() : nullptr, num * scalars[0].size(), - num, - res.data(), res.size() - ); + if (num > 0) { + return internal_use_do_not_use::bls_g2_weighted_sum( + g2_points[0].data(), num * g2_points[0].size(), + scalars[0].data(), num * scalars[0].size(), + num, + res.data(), res.size() + ); + } else { + return internal_use_do_not_use::bls_g2_weighted_sum( + nullptr, 0, + nullptr, 0, + 0, + res.data(), res.size() + ); + } } inline int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { - return internal_use_do_not_use::bls_pairing( - num ? g1_points[0].data() : nullptr, num * g1_points[0].size(), - num ? g2_points[0].data() : nullptr, num * g2_points[0].size(), - num, - res.data(), res.size() - ); + if (num > 0) { + return internal_use_do_not_use::bls_pairing( + g1_points[0].data(), num * g1_points[0].size(), + g2_points[0].data(), num * g2_points[0].size(), + num, + res.data(), res.size() + ); + } else { + return internal_use_do_not_use::bls_pairing( + nullptr, 0, + nullptr, 0, + 0, + res.data(), res.size() + ); + } } inline int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { From 8cdc0f4596a1ee3800037f74b8c76c750ca323eb Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 5 Dec 2023 12:52:05 -0600 Subject: [PATCH 095/158] GH-215 Use affine non-montgomery form for proof of possesion --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 165 ++++-------------- tests/integration/bls_tests.cpp | 9 +- .../test_contracts/bls_primitives_tests.cpp | 33 ++-- 3 files changed, 55 insertions(+), 152 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 2369ee4eb1..1e73f53865 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace bls12_381 { class sha256 { @@ -348,6 +349,7 @@ namespace detail { return Prefix + eosio::base64_encode({g1_with_checksum.data(), g1_with_checksum.size()}); } + template T bls_base64_to_type(const char* data, size_t size) { eosio::check(size > Prefix.size(), "encoded base64 key is too short"); @@ -370,102 +372,19 @@ namespace detail { } const inline std::string POP_CIPHERSUITE_ID = "BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; - const inline std::vector G1_ONE_NEG = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c, - 0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78, - 0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15, - 0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0, - 0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed, - 0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12, - 0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff, - 0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92, - 0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89, - 0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12, - 0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c, - 0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e, - 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, - 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, - 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, - 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, - 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, - 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; //18x8 = 144 bytes - const inline std::vector GT_ONE = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, - 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, - 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, - 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, - 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, - 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 73x8 = 584 bytes - const inline std::array R1 = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76, - 0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb, - 0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f, - 0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77, - 0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c, - 0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; // 6x8 = 48 bytes + + // g1::one().negate().toAffineBytesLE(), 96 bytes + const inline std::vector G1_ONE_NEG = {0xbb, 0xc6, 0x22, 0xdb, 0xa, 0xf0, 0x3a, 0xfb, 0xef, 0x1a, 0x7a, 0xf9, + 0x3f, 0xe8, 0x55, 0x6c, 0x58, 0xac, 0x1b, 0x17, 0x3f, 0x3a, 0x4e, 0xa1, + 0x5, 0xb9, 0x74, 0x97, 0x4f, 0x8c, 0x68, 0xc3, 0xf, 0xac, 0xa9, 0x4f, + 0x8c, 0x63, 0x95, 0x26, 0x94, 0xd7, 0x97, 0x31, 0xa7, 0xd3, 0xf1, 0x17, + 0xca, 0xc2, 0x39, 0xb9, 0xd6, 0xdc, 0x54, 0xad, 0x1b, 0x75, 0xcb, 0xe, + 0xba, 0x38, 0x6f, 0x4e, 0x36, 0x42, 0xac, 0xca, 0xd5, 0xb9, 0x55, 0x66, + 0xc9, 0x7, 0xb5, 0x1d, 0xef, 0x6a, 0x81, 0x67, 0xf2, 0x21, 0x2e, 0xcf, + 0xc8, 0x76, 0x7d, 0xaa, 0xa8, 0x45, 0xd5, 0x55, 0x68, 0x1d, 0x4d, 0x11}; + + // fp12::one().toBytesLE(), 576 bytes + const inline std::vector GT_ONE = []{ std::vector r(576, 0); r[0] = 1; return r; }(); // Construct an extensible-output function based on SHA256 void xmd_sh256( @@ -514,6 +433,7 @@ namespace detail { memcpy(buf + (i - 1) * SHA256HashSize, b_i, copy_len); } } + bls_s scalar_fromBE(const bls_s& in) { bls_s out; constexpr size_t last_index = sizeof(out) - 1; @@ -523,7 +443,8 @@ namespace detail { } return out; } - void g2_fromMessage(const bls_g1_affine& msg, const std::string& dst, bls_g2& res) { + + void g2_fromMessage(std::span msg, const std::string& dst, bls_g2& res) { std::array buf; xmd_sh256(buf.data()->data(), sizeof(buf), msg.data(), msg.size(), dst.data(), dst.length()); @@ -549,59 +470,35 @@ namespace detail { } } - inline std::string encode_g1_to_bls_public_key(const bls_g1_affine& g1) { - return detail::bls_type_to_base64(g1); + inline std::string encode_g1_to_bls_public_key(const bls_g1& g1) { + return detail::bls_type_to_base64(g1); } - inline bls_g1_affine decode_bls_public_key_to_g1(std::string_view public_key) { - return detail::bls_base64_to_type(public_key.data(), public_key.size()); + inline bls_g1 decode_bls_public_key_to_g1(std::string_view public_key) { + return detail::bls_base64_to_type(public_key.data(), public_key.size()); } - inline std::string encode_g2_to_bls_signature(const bls_g2_affine& g2) { - return detail::bls_type_to_base64(g2); + inline std::string encode_g2_to_bls_signature(const bls_g2& g2) { + return detail::bls_type_to_base64(g2); } - inline bls_g2_affine decode_bls_signature_to_g2(std::string_view public_key) { - return detail::bls_base64_to_type(public_key.data(), public_key.size()); - } - - inline bls_g1 g1_affine_to_jacobian(const bls_g1_affine& pubkey) { - using namespace detail; - - static_assert(sizeof(bls_g1_affine) + sizeof(R1) == sizeof(bls_g1)); - // add z coordinate (R1) to pubkey and signature_proof - bls_g1 pubkey_jacobian; - auto insert_it = std::copy(pubkey.begin(), pubkey.end(), pubkey_jacobian.begin()); - std::copy(R1.begin(), R1.end(), insert_it); - - return pubkey_jacobian; - } - - inline bls_g2 g2_affine_to_jacobian(const bls_g2_affine& signature_proof) { - using namespace detail; - - bls_g2 sig_ex = {0}; - auto insert_it = std::copy(signature_proof.begin(), signature_proof.end(), sig_ex.begin()); - std::copy(R1.begin(), R1.end(), insert_it); - - return sig_ex; + inline bls_g2 decode_bls_signature_to_g2(std::string_view public_key) { + return detail::bls_base64_to_type(public_key.data(), public_key.size()); } // pubkey and signature are assumed to be in RAW affine little-endian bytes - bool bls_pop_verify(const bls_g1_affine& pubkey, const bls_g2_affine& signature_proof) { + bool bls_pop_verify(const bls_g1& pubkey, const bls_g2& signature_proof) { using namespace detail; bls_g1 g1_points[2] = {0}; bls_g2 g2_points[2] = {0}; - bls_g1 pubkey_ex = g1_affine_to_jacobian(pubkey); - bls_g2 sig_ex = g2_affine_to_jacobian(signature_proof); + memcpy(g1_points[0].data(), G1_ONE_NEG.data(), G1_ONE_NEG.size()); + memcpy(g2_points[0].data(), signature_proof.data(), signature_proof.size()); - memcpy(&g1_points[0], G1_ONE_NEG.data(), G1_ONE_NEG.size()); - memcpy(&g2_points[0], sig_ex.data(), sig_ex.size()); - - memcpy(&g1_points[1], pubkey_ex.data(), pubkey_ex.size()); + memcpy(g1_points[1].data(), pubkey.data(), pubkey.size()); g2_fromMessage(pubkey, POP_CIPHERSUITE_ID, g2_points[1]); bls_gt r; bls_pairing(g1_points, g2_points, 2, r); + return 0 == std::memcmp(r.data(), GT_ONE.data(), sizeof(bls_gt)); } } diff --git a/tests/integration/bls_tests.cpp b/tests/integration/bls_tests.cpp index 0c689e3e18..08a366ae0a 100644 --- a/tests/integration/bls_tests.cpp +++ b/tests/integration/bls_tests.cpp @@ -122,11 +122,12 @@ BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { BOOST_FIXTURE_TEST_CASE( sig_pop_verify_test, bls_primitives_tester ) try { push_action("eosio"_n, "popverifyraw"_n, "test"_n, mvo() - ("pk", "220ef5c49c1868e85b82658df9766fc2cee4bd0b5d9880d4cdee9139edd3c4ebdae4074f1d3db3f3c9213962942eef091b00d9f2a9b837015c8dbe507f242aee459272589b6b2973bcf33eb4608722c70c1ac3d1ade40c845b9e15ea6380080a") - ("sig", "2f6bc758f0eec23b9218b665e72b522804c2c941dd3a18c4a6686f1f329bb4f6a0cc75ce532f366fc50962d0f58ce40f4ce009e581fe0dde6a13eff94eb041f266e8e386b1036dc866e941345b96dad688039d3859dcb767e261462c9e6d0300598567f1d3e47e3d01a81588cff6a0a8fb24aeb3d725cdb83effd1483c12780e75702508692ae64083358f42fab19d10410ccd774c25d38b4fc9066f7fc2ba00c312ef35b9ddbe4aa9b932d4a516291e2da04da856c3216d852336a672bf2a06")); + ("pk", "3b08e73bbda9c3eff597fc60fbb1bd8c81af3659024f62829082438ef52a02a71f99658087ea58108e12543c08980a0f1950c7f3085ab4b49e3137e8d35adfcbed616dd37cd011435e940a74f53f99ab1d1fd220a089d4e8517df0267ecb0802") + ("sig", "3a1e5689abddbd0ed54c48c88dcef7ac9bba70abdd7a8e965fb807a188cfc8a7e21ca5c9ecc58df7681d158cc6aced13d50bbe35a5aa7848c32a290b096e1f7a61b2817660bd6e6d5686180a1c00716d47ee996ced081fdb6c4417d6cc8dbd06f26f037331c1b4703e94454374ba04e71fb7571159299b9020c124e9ecee777c2b5c16a51ca883b716082fdf2e6c150551a82eaca5efaf761053d6998a439cc696366fe82eb93f19aac34893610698b37d11f0d608fdc1befc50a7e565ea4813")); + push_action("eosio"_n, "popverify"_n, "test"_n, mvo() - ("pk", "PUB_BLS_2/RLYxVb3RsST5FV7kCwCsYm1ciqP4jp2l3YkN8AbaFCgzhG/yM+E6QMkLWWqgcZC7TeY6JluYSAeMrno9f8p3hsFg0S1wUyOnWISUVer4ilomRvr7uBHaRQ3QdpXxsAwOc0YQ==") - ("sig", "SIG_BLS_a9pjyTpvY4/aKbOTzsi86lE23S4nzsUEJUY/IesfLwmlNHAkpCfjAtSGRlZRCfQYHIYSme3CQBW2LMFG7XsX1EUQ/L+TPvDq52TceT9o9z/iA6TBjs582dMQOmKZ8hUZUUZgzPlPDqIh0DNbTT2vVX3es6iBnHE291Yl4p4brCMLiKKQ/3kA+xvjFHykouQP5ostyMk48j4FTxdgOVNlnizUYmWmst+G65ngEqtTTUIZqc3I0OUDixGIhTNQPC8FRe+RNQ==")); + ("pk", "PUB_BLS_OwjnO72pw+/1l/xg+7G9jIGvNlkCT2KCkIJDjvUqAqcfmWWAh+pYEI4SVDwImAoPGVDH8whatLSeMTfo01rfy+1hbdN80BFDXpQKdPU/masdH9IgoInU6FF98CZ+ywgCD2yuNg==") + ("sig", "SIG_BLS_Oh5WiavdvQ7VTEjIjc73rJu6cKvdeo6WX7gHoYjPyKfiHKXJ7MWN92gdFYzGrO0T1Qu+NaWqeEjDKikLCW4femGygXZgvW5tVoYYChwAcW1H7pls7Qgf22xEF9bMjb0G8m8DczHBtHA+lEVDdLoE5x+3VxFZKZuQIMEk6ezud3wrXBalHKiDtxYIL98ubBUFUagurKXvr3YQU9aZikOcxpY2b+guuT8ZqsNIk2EGmLN9EfDWCP3BvvxQp+Vl6kgTpacydw==")); } FC_LOG_AND_RETHROW() BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index cb81257b8d..eb350a76f0 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -106,9 +106,9 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ [[eosio::action]] void popverifyraw(const std::vector& pk, const std::vector& sig) { - check(pk.size() == std::tuple_size::value, "wrong public key size passed"); - check(sig.size() == std::tuple_size::value, "wrong signature size passed"); - check(bls_pop_verify(*reinterpret_cast(pk.data()), *reinterpret_cast(sig.data())), "raw pop verify failed"); + check(pk.size() == std::tuple_size::value, "wrong public key size passed"); + check(sig.size() == std::tuple_size::value, "wrong signature size passed"); + check(bls_pop_verify(*reinterpret_cast(pk.data()), *reinterpret_cast(sig.data())), "raw pop verify failed"); } [[eosio::action]] @@ -118,24 +118,20 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ [[eosio::action]] void g1baseb4enc(const std::vector& g1, const std::string& base64 ) { - check(g1.size() == std::tuple_size::value, "wrong g1 size passed"); + check(g1.size() == std::tuple_size::value, "wrong g1 size passed"); - check(encode_g1_to_bls_public_key(*reinterpret_cast(g1.data())) == base64, "g1 to base64 encoding doesn't match" ); - check(decode_bls_public_key_to_g1(base64) == *reinterpret_cast(g1.data()), "base64 to g1 decoding doesn't match" ); + check(encode_g1_to_bls_public_key(*reinterpret_cast(g1.data())) == base64, "g1 to base64 encoding doesn't match" ); + check(decode_bls_public_key_to_g1(base64) == *reinterpret_cast(g1.data()), "base64 to g1 decoding doesn't match" ); } [[eosio::action]] void sigbaseb4enc(const std::vector& g2, const std::string& base64) { - check(g2.size() == std::tuple_size::value, "wrong g2 size passed"); - check(encode_g2_to_bls_signature(*reinterpret_cast(g2.data())) == base64, "g2 to base64 encoding doesn't match" ); - check(decode_bls_signature_to_g2(base64) == *reinterpret_cast(g2.data()), "base64 to g2 decoding doesn't match" ); + check(g2.size() == std::tuple_size::value, "wrong g2 size passed"); + check(encode_g2_to_bls_signature(*reinterpret_cast(g2.data())) == base64, "g2 to base64 encoding doesn't match" ); + check(decode_bls_signature_to_g2(base64) == *reinterpret_cast(g2.data()), "base64 to g2 decoding doesn't match" ); } const std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; - // g1::one().negate().toAffineBytesLE() - const std::vector G1_ONE_NEG = {0xbb, 0xc6, 0x22, 0xdb, 0xa, 0xf0, 0x3a, 0xfb, 0xef, 0x1a, 0x7a, 0xf9, 0x3f, 0xe8, 0x55, 0x6c, 0x58, 0xac, 0x1b, 0x17, 0x3f, 0x3a, 0x4e, 0xa1, 0x5, 0xb9, 0x74, 0x97, 0x4f, 0x8c, 0x68, 0xc3, 0xf, 0xac, 0xa9, 0x4f, 0x8c, 0x63, 0x95, 0x26, 0x94, 0xd7, 0x97, 0x31, 0xa7, 0xd3, 0xf1, 0x17, 0xca, 0xc2, 0x39, 0xb9, 0xd6, 0xdc, 0x54, 0xad, 0x1b, 0x75, 0xcb, 0xe, 0xba, 0x38, 0x6f, 0x4e, 0x36, 0x42, 0xac, 0xca, 0xd5, 0xb9, 0x55, 0x66, 0xc9, 0x7, 0xb5, 0x1d, 0xef, 0x6a, 0x81, 0x67, 0xf2, 0x21, 0x2e, 0xcf, 0xc8, 0x76, 0x7d, 0xaa, 0xa8, 0x45, 0xd5, 0x55, 0x68, 0x1d, 0x4d, 0x11}; - // fp12::one().toBytesLE(); - const std::vector GT_ONE = []{ std::vector r(576, 0); r[0] = 1; return r; }(); // caller of verify() must use this msg std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; @@ -146,8 +142,17 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ bls_g1 g1_points[2]; bls_g2 g2_points[2]; - memcpy(g1_points[0].data(), G1_ONE_NEG.data(), sizeof(bls_g1)); + memcpy(g1_points[0].data(), detail::G1_ONE_NEG.data(), sizeof(bls_g1)); memcpy(g2_points[0].data(), sig.data(), sizeof(bls_g2)); + + bls_g2 p_msg; + detail::g2_fromMessage(std::span(reinterpret_cast(msg.data()), msg.size()), CIPHERSUITE_ID, p_msg); + memcpy(g1_points[1].data(), pk.data(), sizeof(bls_g1)); + memcpy(g2_points[1].data(), p_msg.data(), sizeof(bls_g2)); + + bls_gt r; + bls_pairing(g1_points, g2_points, 2, r); + check(0 == memcmp(r.data(), detail::GT_ONE.data(), sizeof(bls_gt)), "bls signature verify failed"); } }; From 3588c2c3c781781581d7cf516d7e5b5b11ad1464 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 5 Dec 2023 13:46:32 -0600 Subject: [PATCH 096/158] GH-215 Add a bls_signature_verify method --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 28 +++++++++++++++---- .../test_contracts/bls_primitives_tests.cpp | 17 ++--------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 1e73f53865..b5f5d9bb13 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -371,7 +371,8 @@ namespace detail { return ret; } - const inline std::string POP_CIPHERSUITE_ID = "BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; + const inline std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; // basic DST + const inline std::string POP_CIPHERSUITE_ID = "BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; // proof of possession DST // g1::one().negate().toAffineBytesLE(), 96 bytes const inline std::vector G1_ONE_NEG = {0xbb, 0xc6, 0x22, 0xdb, 0xa, 0xf0, 0x3a, 0xfb, 0xef, 0x1a, 0x7a, 0xf9, @@ -490,16 +491,33 @@ namespace detail { bls_g1 g1_points[2] = {0}; bls_g2 g2_points[2] = {0}; - memcpy(g1_points[0].data(), G1_ONE_NEG.data(), G1_ONE_NEG.size()); - memcpy(g2_points[0].data(), signature_proof.data(), signature_proof.size()); + std::memcpy(g1_points[0].data(), G1_ONE_NEG.data(), G1_ONE_NEG.size()); + std::memcpy(g2_points[0].data(), signature_proof.data(), signature_proof.size()); - memcpy(g1_points[1].data(), pubkey.data(), pubkey.size()); + std::memcpy(g1_points[1].data(), pubkey.data(), pubkey.size()); g2_fromMessage(pubkey, POP_CIPHERSUITE_ID, g2_points[1]); bls_gt r; bls_pairing(g1_points, g2_points, 2, r); - return 0 == std::memcmp(r.data(), GT_ONE.data(), sizeof(bls_gt)); + return 0 == std::memcmp(r.data(), GT_ONE.data(), GT_ONE.size()); + } + + // pubkey and signature are assumed to be in RAW affine little-endian bytes + bool bls_signature_verify(const bls_g1& pubkey, const bls_g2& signature_proof, const std::string& msg) { + bls_g1 g1_points[2]; + bls_g2 g2_points[2]; + + std::memcpy(g1_points[0].data(), detail::G1_ONE_NEG.data(), detail::G1_ONE_NEG.size()); + std::memcpy(g2_points[0].data(), signature_proof.data(), signature_proof.size()); + + std::memcpy(g1_points[1].data(), pubkey.data(), pubkey.size()); + detail::g2_fromMessage(msg, detail::CIPHERSUITE_ID, g2_points[1]); + + bls_gt r; + bls_pairing(g1_points, g2_points, 2, r); + + return 0 == std::memcmp(r.data(), detail::GT_ONE.data(), detail::GT_ONE.size()); } } diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index eb350a76f0..1f0e4e7a38 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -131,7 +131,6 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ check(decode_bls_signature_to_g2(base64) == *reinterpret_cast(g2.data()), "base64 to g2 decoding doesn't match" ); } - const std::string CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"; // caller of verify() must use this msg std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; @@ -139,20 +138,10 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ void verify(const std::vector& pk, const std::vector& sig) { check(pk.size() == std::tuple_size::value, "wrong pk size passed"); check(sig.size() == std::tuple_size::value, "wrong sig size passed"); - bls_g1 g1_points[2]; - bls_g2 g2_points[2]; - memcpy(g1_points[0].data(), detail::G1_ONE_NEG.data(), sizeof(bls_g1)); - memcpy(g2_points[0].data(), sig.data(), sizeof(bls_g2)); - - bls_g2 p_msg; - detail::g2_fromMessage(std::span(reinterpret_cast(msg.data()), msg.size()), CIPHERSUITE_ID, p_msg); - memcpy(g1_points[1].data(), pk.data(), sizeof(bls_g1)); - memcpy(g2_points[1].data(), p_msg.data(), sizeof(bls_g2)); - - bls_gt r; - bls_pairing(g1_points, g2_points, 2, r); - check(0 == memcmp(r.data(), detail::GT_ONE.data(), sizeof(bls_gt)), "bls signature verify failed"); + std::string msg_str; + std::copy(msg.begin(), msg.end(), std::back_inserter(msg_str)); + check(bls_signature_verify(*reinterpret_cast(pk.data()), *reinterpret_cast(sig.data()), msg_str), "raw pop verify failed"); } }; From 6e8e98a94943525668329b5f0e14f086d2865707 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 5 Dec 2023 13:48:16 -0600 Subject: [PATCH 097/158] GH-215 fix check message --- tests/unit/test_contracts/bls_primitives_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 1f0e4e7a38..1326e0b0a4 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -141,7 +141,7 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ std::string msg_str; std::copy(msg.begin(), msg.end(), std::back_inserter(msg_str)); - check(bls_signature_verify(*reinterpret_cast(pk.data()), *reinterpret_cast(sig.data()), msg_str), "raw pop verify failed"); + check(bls_signature_verify(*reinterpret_cast(pk.data()), *reinterpret_cast(sig.data()), msg_str), "signature verify failed"); } }; From 5bdd337b614e8340347f6e16fe72fbc811f20e17 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 5 Dec 2023 14:15:33 -0600 Subject: [PATCH 098/158] GH-215 Add test for bls_signature_verify --- libraries/eosiolib/core/eosio/crypto_bls_ext.hpp | 2 +- tests/integration/bls_tests.cpp | 13 ++++++++++++- tests/unit/test_contracts/bls_primitives_tests.cpp | 9 ++++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index b5f5d9bb13..eeaa3778e9 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -469,7 +469,7 @@ namespace detail { bls_g2_map(t, q); bls_g2_add(p, q, res); } -} +} // namespace detail inline std::string encode_g1_to_bls_public_key(const bls_g1& g1) { return detail::bls_type_to_base64(g1); diff --git a/tests/integration/bls_tests.cpp b/tests/integration/bls_tests.cpp index 08a366ae0a..17ecff1bac 100644 --- a/tests/integration/bls_tests.cpp +++ b/tests/integration/bls_tests.cpp @@ -114,10 +114,21 @@ BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { // assumes signed message of std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; // vector seed(32, 0x30); array sk = secret_key(seed); g1 pk = g1::one().scale(sk).affine(); string pk_hex = to_hex(pk.toAffineBytesLE()); // g2 sig = sign(sk, msg); string sig_hex = to_hex(sig.toAffineBytesLE()); - push_action("eosio"_n, "verify"_n, "test"_n, mvo() + push_action("eosio"_n, "verifyraw"_n, "test"_n, mvo() ("pk", "c27efc440ed89d739ff80f1c7178d4672d5f71e8c8926a85785d0f84074e79c1662c522fdbd6fcaebd640d816dea6c0dc36c5d7e7a297afc960369e306d845fcad0e5fe2a88c0d3bbe854d4f9e3e6365c9c1003662f0e81b35fd9c6a11d0a10c") ("sig", "e13a38c18dfad440a9af8d720bfb9ce0f388a25a5789a8cdc34b87204fe29db5cc16658a5d9f4fa4a7cc16b8ef8d2d0d1a5984e6b69ca228692f1920e9340f149024b4d856711656f11bb1e0c7d081e5bb80692638a9bace2d8f4d796b4887129c59fbb7901997cbbb681f709291a45315979f55b719dfc8757d3a878cf01e4a48a0e6647b837d0ea8aea5d41e121b0dd37fa20007aef4f51740d846cc8d6ab151dbd88964e7d19890a500d5537be81b881451b75f928a66f546441d45028603")); + // bls_private_key sk = bls_private_key(seed_1); + // bls_public_key pk = sk.get_public_key(); + // std::string msg = "this is a message string"; + // std::vector msg_v(msg.begin(), msg.end()); + // bls_signature signature = sk.sign(msg_v); + // pk.to_string(); + // signature.to_string(); + push_action("eosio"_n, "verify"_n, "test"_n, mvo() + ("pk", "PUB_BLS_sS66EwY8bNx7vkDn3mKhwPhhqa1V6STN1QSb6bWOIFBTloF5zt5b55r9y7uQMiQGrvt6XOZO3CpEgthlba7R7qz7Qob2YcD5EX3Ng/rUUdMBsjEJRuXNWICPe0QbKAoCQOw9vw==") + ("sig", "SIG_BLS_07NCCTekrxhDFurhRhl3b8m4T+xmTixSl63TYcee/xJONi2/qy9+8o5vyetMefsL4hhDMfy1yIeHERqxUvGguRBBncpabp84b3P2bNpY18A+PVs7qKFqlld1gEUqt+cDOcwkyUe6HqqFXR5wtXoSHE4lHauV3CzR0lD91gnr2c49aUQPAC7SD+ZcnZyahwURje+Db26zhTXIO1WWIx6vwKnZUuvZEbzYrijjBQKeZsqp0eoAzjrByx9gOmN2d/AR9PhRvg==") + ("msg", "this is a message string")); } FC_LOG_AND_RETHROW() BOOST_FIXTURE_TEST_CASE( sig_pop_verify_test, bls_primitives_tester ) try { diff --git a/tests/unit/test_contracts/bls_primitives_tests.cpp b/tests/unit/test_contracts/bls_primitives_tests.cpp index 1326e0b0a4..fb866b50d0 100644 --- a/tests/unit/test_contracts/bls_primitives_tests.cpp +++ b/tests/unit/test_contracts/bls_primitives_tests.cpp @@ -135,13 +135,16 @@ class [[eosio::contract]] bls_primitives_tests : public contract{ std::vector msg = {51, 23, 56, 93, 212, 129, 128, 27, 251, 12, 42, 129, 210, 9, 34, 98}; [[eosio::action]] - void verify(const std::vector& pk, const std::vector& sig) { + void verifyraw(const std::vector& pk, const std::vector& sig) { check(pk.size() == std::tuple_size::value, "wrong pk size passed"); check(sig.size() == std::tuple_size::value, "wrong sig size passed"); - std::string msg_str; - std::copy(msg.begin(), msg.end(), std::back_inserter(msg_str)); + std::string msg_str(msg.begin(), msg.end()); check(bls_signature_verify(*reinterpret_cast(pk.data()), *reinterpret_cast(sig.data()), msg_str), "signature verify failed"); } + [[eosio::action]] + void verify(const std::string& pk, const std::string& sig, const std::string& msg) { + check(bls_signature_verify(decode_bls_public_key_to_g1(pk), decode_bls_signature_to_g2(sig), msg), "signature verify failed"); + } }; From f9331b180d5eb26770e6c5459d90bf8b038e5167 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 6 Dec 2023 07:49:05 -0600 Subject: [PATCH 099/158] GH-253 Return -1 on n==0 for bls_g1_weighted_sum, bls_g2_weighted_sum, and bls_pairing. Add return_code type. --- libraries/eosiolib/core/eosio/check.hpp | 9 +++ .../eosiolib/core/eosio/crypto_bls_ext.hpp | 69 +++++++------------ 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/libraries/eosiolib/core/eosio/check.hpp b/libraries/eosiolib/core/eosio/check.hpp index 2d5d560e57..243707a576 100644 --- a/libraries/eosiolib/core/eosio/check.hpp +++ b/libraries/eosiolib/core/eosio/check.hpp @@ -22,6 +22,15 @@ namespace eosio { } } + + /** + * Return codes returned by host functions + */ + enum return_code : int32_t { + failure = -1, + success = 0 + }; + /** * @defgroup system System * @ingroup core diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 3676a1e829..19c15a1955 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -68,57 +68,36 @@ namespace eosio { } inline int32_t bls_g1_weighted_sum(const bls_g1 g1_points[], const bls_scalar scalars[], uint32_t num, bls_g1& res) { - if (num > 0) { - return internal_use_do_not_use::bls_g1_weighted_sum( - g1_points[0].data(), num * g1_points[0].size(), - scalars[0].data(), num * scalars[0].size(), - num, - res.data(), res.size() - ); - } else { - return internal_use_do_not_use::bls_g1_weighted_sum( - nullptr, 0, - nullptr, 0, - 0, - res.data(), res.size() - ); - } + if (num == 0) + return return_code::failure; + return internal_use_do_not_use::bls_g1_weighted_sum( + g1_points[0].data(), num * g1_points[0].size(), + scalars[0].data(), num * scalars[0].size(), + num, + res.data(), res.size() + ); } inline int32_t bls_g2_weighted_sum(const bls_g2 g2_points[], const bls_scalar scalars[], uint32_t num, bls_g2& res) { - if (num > 0) { - return internal_use_do_not_use::bls_g2_weighted_sum( - g2_points[0].data(), num * g2_points[0].size(), - scalars[0].data(), num * scalars[0].size(), - num, - res.data(), res.size() - ); - } else { - return internal_use_do_not_use::bls_g2_weighted_sum( - nullptr, 0, - nullptr, 0, - 0, - res.data(), res.size() - ); - } + if (num == 0) + return return_code::failure; + return internal_use_do_not_use::bls_g2_weighted_sum( + g2_points[0].data(), num * g2_points[0].size(), + scalars[0].data(), num * scalars[0].size(), + num, + res.data(), res.size() + ); } inline int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) { - if (num > 0) { - return internal_use_do_not_use::bls_pairing( - g1_points[0].data(), num * g1_points[0].size(), - g2_points[0].data(), num * g2_points[0].size(), - num, - res.data(), res.size() - ); - } else { - return internal_use_do_not_use::bls_pairing( - nullptr, 0, - nullptr, 0, - 0, - res.data(), res.size() - ); - } + if (num == 0) + return return_code::failure; + return internal_use_do_not_use::bls_pairing( + g1_points[0].data(), num * g1_points[0].size(), + g2_points[0].data(), num * g2_points[0].size(), + num, + res.data(), res.size() + ); } inline int32_t bls_g1_map(const bls_fp& e, bls_g1& res) { From 290e80681a6a64ac1e8501ae4b2e1ee4e32ad3a7 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Thu, 7 Dec 2023 11:01:42 -0500 Subject: [PATCH 100/158] fix a compile error due to type alias bls_g1_affine changed to bls_g1 --- libraries/eosiolib/contracts/eosio/finalizer_set.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/finalizer_set.hpp b/libraries/eosiolib/contracts/eosio/finalizer_set.hpp index 450d85e3cc..e60f5450ec 100644 --- a/libraries/eosiolib/contracts/eosio/finalizer_set.hpp +++ b/libraries/eosiolib/contracts/eosio/finalizer_set.hpp @@ -31,9 +31,9 @@ namespace eosio { void set_finalizers( const abi_finalizer_set& fin_set ) { for (const auto& finalizer : fin_set.finalizers) - eosio::check(finalizer.public_key_g1_affine_le.size() == sizeof(bls_g1_affine), "public key has a wrong size" ); + eosio::check(finalizer.public_key_g1_affine_le.size() == sizeof(bls_g1), "public key has a wrong size" ); auto packed = eosio::pack(fin_set); internal_use_do_not_use::set_finalizers(packed.data(), packed.size()); } -} //eosio \ No newline at end of file +} //eosio From 5e504d3e620c86424d3ccc8ee7e5770ebc053381 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Thu, 7 Dec 2023 16:04:19 -0500 Subject: [PATCH 101/158] Fix set_finalizers_tests * Use BOOST_REQUIRE_EQUAL and BOOST_REQUIRE; existing BOOST_ASSERT does not run during tests * Update to Leap changes of proposed_finalizer_set to proposed_finalizer_policy --- tests/integration/set_finalizers_tests.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp index 81fdebf421..bb8ab4e229 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/set_finalizers_tests.cpp @@ -45,12 +45,12 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { signed_block_ptr cur_block = produce_block(); fc::variant pretty_output; abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); - BOOST_ASSERT(pretty_output.get_object().contains("proposed_finalizer_set")); - BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["generation"] == 1); - BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["fthreshold"] == 1); - BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["finalizers"].size() == 1); - BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["finalizers"][size_t(0)]["description"] == "test_desc"); - BOOST_ASSERT(pretty_output["proposed_finalizer_set"]["finalizers"][size_t(0)]["fweight"] == 1); + BOOST_REQUIRE(pretty_output.get_object().contains("proposed_finalizer_policy")); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["generation"], 1); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["fthreshold"], 1); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"].size(), 1u); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["description"], "test_desc"); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["fweight"], 1); //TODO: add key check here after base64 support will be added // testing wrong public key size @@ -63,4 +63,4 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { } FC_LOG_AND_RETHROW() -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file +BOOST_AUTO_TEST_SUITE_END() From cb73eae8450b6c4cceed2aee1db6cd6740f1f2c9 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Thu, 7 Dec 2023 16:33:09 -0500 Subject: [PATCH 102/158] Update set_finalizers_tests * Fixed breaking strict-aliasing rules * Added public key check --- tests/integration/set_finalizers_tests.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp index bb8ab4e229..871e352809 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/set_finalizers_tests.cpp @@ -23,25 +23,12 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { produce_block(); - const std::vector G1 = {0x22, 0x0e, 0xf5, 0xc4, 0x9c, 0x18, 0x68, 0xe8, - 0x5b, 0x82, 0x65, 0x8d, 0xf9, 0x76, 0x6f, 0xc2, - 0xce, 0xe4, 0xbd, 0x0b, 0x5d, 0x98, 0x80, 0xd4, - 0xcd, 0xee, 0x91, 0x39, 0xed, 0xd3, 0xc4, 0xeb, - 0xda, 0xe4, 0x07, 0x4f, 0x1d, 0x3d, 0xb3, 0xf3, - 0xc9, 0x21, 0x39, 0x62, 0x94, 0x2e, 0xef, 0x09, - 0x1b, 0x00, 0xd9, 0xf2, 0xa9, 0xb8, 0x37, 0x01, - 0x5c, 0x8d, 0xbe, 0x50, 0x7f, 0x24, 0x2a, 0xee, - 0x45, 0x92, 0x72, 0x58, 0x9b, 0x6b, 0x29, 0x73, - 0xbc, 0xf3, 0x3e, 0xb4, 0x60, 0x87, 0x22, 0xc7, - 0x0c, 0x1a, 0xc3, 0xd1, 0xad, 0xe4, 0x0c, 0x84, - 0x5b, 0x9e, 0x15, 0xea, 0x63, 0x80, 0x08, 0x0a}; // 12x8 = 96 bytes - push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() ("fin_set", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() ("description", "test_desc") ("fweight", 1) - ("public_key_g1_affine_le", *reinterpret_cast*>(&G1))}))); + ("public_key_g1_affine_le", "220ef5c49c1868e85b82658df9766fc2cee4bd0b5d9880d4cdee9139edd3c4ebdae4074f1d3db3f3c9213962942eef091b00d9f2a9b837015c8dbe507f242aee459272589b6b2973bcf33eb4608722c70c1ac3d1ade40c845b9e15ea6380080a")}))); signed_block_ptr cur_block = produce_block(); fc::variant pretty_output; abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); @@ -51,7 +38,7 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"].size(), 1u); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["description"], "test_desc"); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["fweight"], 1); - //TODO: add key check here after base64 support will be added + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_Ig71xJwYaOhbgmWN+XZvws7kvQtdmIDUze6ROe3TxOva5AdPHT2z88khOWKULu8JGwDZ8qm4NwFcjb5QfyQq7kWSclibaylzvPM+tGCHIscMGsPRreQMhFueFepjgAgKXjOb8g=="); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() From 30786faac59a5a64b3405ce7c762d12acd2cc121 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Thu, 7 Dec 2023 16:54:17 -0500 Subject: [PATCH 103/158] rename finalizer_set.hpp -> finalizer_policy.hpp --- .../contracts/eosio/{finalizer_set.hpp => finalizer_policy.hpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libraries/eosiolib/contracts/eosio/{finalizer_set.hpp => finalizer_policy.hpp} (100%) diff --git a/libraries/eosiolib/contracts/eosio/finalizer_set.hpp b/libraries/eosiolib/contracts/eosio/finalizer_policy.hpp similarity index 100% rename from libraries/eosiolib/contracts/eosio/finalizer_set.hpp rename to libraries/eosiolib/contracts/eosio/finalizer_policy.hpp From 8ab3b6b5b3788ad12d944f16fc45b759a13ce855 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Thu, 7 Dec 2023 20:21:39 -0500 Subject: [PATCH 104/158] update set_finalizers_tests.cpp to include finalizer_policy.hpp --- tests/unit/test_contracts/set_finalizers_tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_contracts/set_finalizers_tests.cpp b/tests/unit/test_contracts/set_finalizers_tests.cpp index 32c4982884..ad0895b8a1 100644 --- a/tests/unit/test_contracts/set_finalizers_tests.cpp +++ b/tests/unit/test_contracts/set_finalizers_tests.cpp @@ -1,5 +1,5 @@ #include -#include +#include class [[eosio::contract]] set_finalizers_tests : public eosio::contract{ public: @@ -9,4 +9,4 @@ class [[eosio::contract]] set_finalizers_tests : public eosio::contract{ void setfinal(const eosio::abi_finalizer_set& fin_set) { eosio::set_finalizers(fin_set); } -}; \ No newline at end of file +}; From f43d6efc7984ca7253eeecd2499fb4e6428a4add Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Thu, 7 Dec 2023 21:11:09 -0500 Subject: [PATCH 105/158] change abi_finalizer_set to abi_finalizer_policy --- .../eosiolib/contracts/eosio/finalizer_policy.hpp | 10 +++++----- tests/unit/test_contracts/set_finalizers_tests.cpp | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/finalizer_policy.hpp b/libraries/eosiolib/contracts/eosio/finalizer_policy.hpp index e60f5450ec..69f9e5f717 100644 --- a/libraries/eosiolib/contracts/eosio/finalizer_policy.hpp +++ b/libraries/eosiolib/contracts/eosio/finalizer_policy.hpp @@ -22,17 +22,17 @@ namespace eosio { EOSLIB_SERIALIZE(abi_finalizer_authority, (description)(fweight)(public_key_g1_affine_le)); }; - struct abi_finalizer_set { + struct abi_finalizer_policy { uint64_t fthreshold = 0; std::vector finalizers; - EOSLIB_SERIALIZE(abi_finalizer_set, (fthreshold)(finalizers)); + EOSLIB_SERIALIZE(abi_finalizer_policy, (fthreshold)(finalizers)); }; - void set_finalizers( const abi_finalizer_set& fin_set ) { - for (const auto& finalizer : fin_set.finalizers) + void set_finalizers( const abi_finalizer_policy& finalizer_policy ) { + for (const auto& finalizer : finalizer_policy.finalizers) eosio::check(finalizer.public_key_g1_affine_le.size() == sizeof(bls_g1), "public key has a wrong size" ); - auto packed = eosio::pack(fin_set); + auto packed = eosio::pack(finalizer_policy); internal_use_do_not_use::set_finalizers(packed.data(), packed.size()); } diff --git a/tests/unit/test_contracts/set_finalizers_tests.cpp b/tests/unit/test_contracts/set_finalizers_tests.cpp index ad0895b8a1..1b4fb7d8ca 100644 --- a/tests/unit/test_contracts/set_finalizers_tests.cpp +++ b/tests/unit/test_contracts/set_finalizers_tests.cpp @@ -6,7 +6,7 @@ class [[eosio::contract]] set_finalizers_tests : public eosio::contract{ using contract::contract; [[eosio::action]] - void setfinal(const eosio::abi_finalizer_set& fin_set) { - eosio::set_finalizers(fin_set); + void setfinal(const eosio::abi_finalizer_policy& finalizer_policy) { + eosio::set_finalizers(finalizer_policy); } }; From c82af4eb22d5499f0225dad7f3c06d0a68a1847b Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Fri, 8 Dec 2023 08:30:44 -0500 Subject: [PATCH 106/158] update tests to use finalizer_policy --- tests/integration/set_finalizers_tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/set_finalizers_tests.cpp index 871e352809..82e041f2ce 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/set_finalizers_tests.cpp @@ -24,7 +24,7 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { produce_block(); push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() - ("fin_set", mvo()("fthreshold", 1) + ("finalizer_policy", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() ("description", "test_desc") ("fweight", 1) @@ -42,7 +42,7 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() - ("fin_set", mvo()("fthreshold", 1) + ("finalizer_policy", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() ("description", "test_desc") ("fweight", 1) From a532a6dec4d54624b2a7c31705930e295c3385bb Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Fri, 8 Dec 2023 21:29:56 -0500 Subject: [PATCH 107/158] rename files and tests from set_finalizers to instant_finality --- .../eosiolib/capi/eosio/instant_finality.h | 26 +++++++++++++++++++ libraries/eosiolib/capi/eosio/privileged.h | 11 -------- ...alizer_policy.hpp => instant_finality.hpp} | 15 +++++++++-- .../native/native/eosio/intrinsics_def.hpp | 1 + tests/integration/contracts.hpp.in | 4 +-- ...s_tests.cpp => instant_finality_tests.cpp} | 12 ++++----- tests/unit/test_contracts/CMakeLists.txt | 2 +- tests/unit/test_contracts/capi/privileged.c | 1 + .../test_contracts/instant_finality_tests.cpp | 12 +++++++++ .../test_contracts/set_finalizers_tests.cpp | 12 --------- 10 files changed, 62 insertions(+), 34 deletions(-) create mode 100644 libraries/eosiolib/capi/eosio/instant_finality.h rename libraries/eosiolib/contracts/eosio/{finalizer_policy.hpp => instant_finality.hpp} (78%) rename tests/integration/{set_finalizers_tests.cpp => instant_finality_tests.cpp} (85%) create mode 100644 tests/unit/test_contracts/instant_finality_tests.cpp delete mode 100644 tests/unit/test_contracts/set_finalizers_tests.cpp diff --git a/libraries/eosiolib/capi/eosio/instant_finality.h b/libraries/eosiolib/capi/eosio/instant_finality.h new file mode 100644 index 0000000000..9c6494514c --- /dev/null +++ b/libraries/eosiolib/capi/eosio/instant_finality.h @@ -0,0 +1,26 @@ +#pragma once +#include "types.h" +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup instant_finality_c Instant_finality C API + * @ingroup c_api + * @brief Defines %C Instant_finality API + */ + +/** + * Submits a finalizer policy change to Instant Finality + * + * @param data - pointer finalizer_set object packed as bytes + * @param len - size of packed finalazer_set object + * @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_policy data + * abi_finalizer_policy structure is defined in instant_finality.hpp + */ +__attribute__((eosio_wasm_import)) +void set_finalizers( const char* data, uint32_t len ); + +#ifdef __cplusplus +} +#endif diff --git a/libraries/eosiolib/capi/eosio/privileged.h b/libraries/eosiolib/capi/eosio/privileged.h index edd3dd9047..8628c00769 100644 --- a/libraries/eosiolib/capi/eosio/privileged.h +++ b/libraries/eosiolib/capi/eosio/privileged.h @@ -123,17 +123,6 @@ void set_kv_parameters_packed( const char* data, uint32_t datalen ); __attribute__((eosio_wasm_import)) void preactivate_feature( const struct capi_checksum256* feature_digest ); -/** - * Submits a finalizer set change to Hotstuff - * - * @param data - pointer finalizer_set object packed as bytes - * @param len - size of packed finalazer_set object - * @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_set data - * abi_finalizer_set structure is defined in finalizer_set.hpp - */ -__attribute__((eosio_wasm_import)) -void set_finalizers( const char* data, uint32_t len ); - #ifdef __cplusplus } #endif diff --git a/libraries/eosiolib/contracts/eosio/finalizer_policy.hpp b/libraries/eosiolib/contracts/eosio/instant_finality.hpp similarity index 78% rename from libraries/eosiolib/contracts/eosio/finalizer_policy.hpp rename to libraries/eosiolib/contracts/eosio/instant_finality.hpp index 69f9e5f717..0976a0ceda 100644 --- a/libraries/eosiolib/contracts/eosio/finalizer_policy.hpp +++ b/libraries/eosiolib/contracts/eosio/instant_finality.hpp @@ -5,7 +5,13 @@ #include #include -#include + +/** + * @defgroup instant_finality Instant_finality + * @ingroup instant_finality + * @ingroup contracts + * @brief Defines C++ Instant Finality API + */ namespace eosio { namespace internal_use_do_not_use { @@ -29,7 +35,12 @@ namespace eosio { EOSLIB_SERIALIZE(abi_finalizer_policy, (fthreshold)(finalizers)); }; - void set_finalizers( const abi_finalizer_policy& finalizer_policy ) { +/** + * Submits a finalizer policy change to Instant Finality + * + * @param finalizer_policy - finalizer policy to be set + */ + inline void set_finalizers( const abi_finalizer_policy& finalizer_policy ) { for (const auto& finalizer : finalizer_policy.finalizers) eosio::check(finalizer.public_key_g1_affine_le.size() == sizeof(bls_g1), "public key has a wrong size" ); auto packed = eosio::pack(finalizer_policy); diff --git a/libraries/native/native/eosio/intrinsics_def.hpp b/libraries/native/native/eosio/intrinsics_def.hpp index e6740fdc87..5121017dda 100644 --- a/libraries/native/native/eosio/intrinsics_def.hpp +++ b/libraries/native/native/eosio/intrinsics_def.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include diff --git a/tests/integration/contracts.hpp.in b/tests/integration/contracts.hpp.in index d8dc4cf017..b4be50e834 100644 --- a/tests/integration/contracts.hpp.in +++ b/tests/integration/contracts.hpp.in @@ -26,8 +26,8 @@ namespace eosio::testing { static std::vector bls_primitives_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/bls_primitives_tests.wasm"); } static std::vector bls_primitives_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/bls_primitives_tests.abi"); } - static std::vector set_finalizers_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/set_finalizers_tests.wasm"); } - static std::vector set_finalizers_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/set_finalizers_tests.abi"); } + static std::vector instant_finality_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/instant_finality_tests.wasm"); } + static std::vector instant_finality_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/instant_finality_tests.abi"); } static std::vector get_code_hash_write_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.wasm"); } static std::vector get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); } diff --git a/tests/integration/set_finalizers_tests.cpp b/tests/integration/instant_finality_tests.cpp similarity index 85% rename from tests/integration/set_finalizers_tests.cpp rename to tests/integration/instant_finality_tests.cpp index 82e041f2ce..009a1c9464 100644 --- a/tests/integration/set_finalizers_tests.cpp +++ b/tests/integration/instant_finality_tests.cpp @@ -12,18 +12,18 @@ using namespace fc; using mvo = fc::mutable_variant_object; -BOOST_AUTO_TEST_SUITE(set_finalizers_tests) +BOOST_AUTO_TEST_SUITE(instant_finality_tests) -BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { +BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { create_accounts( { "test"_n } ); produce_block(); - set_code( config::system_account_name, contracts::set_finalizers_test_wasm() ); - set_abi( config::system_account_name, contracts::set_finalizers_test_abi().data() ); + set_code( config::system_account_name, contracts::instant_finality_test_wasm() ); + set_abi( config::system_account_name, contracts::instant_finality_test_abi().data() ); produce_block(); - push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() + push_action(config::system_account_name, "setfinalizer"_n, "test"_n, mvo() ("finalizer_policy", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() ("description", "test_desc") @@ -41,7 +41,7 @@ BOOST_FIXTURE_TEST_CASE(set_finalizers_test, tester) try { BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_Ig71xJwYaOhbgmWN+XZvws7kvQtdmIDUze6ROe3TxOva5AdPHT2z88khOWKULu8JGwDZ8qm4NwFcjb5QfyQq7kWSclibaylzvPM+tGCHIscMGsPRreQMhFueFepjgAgKXjOb8g=="); // testing wrong public key size - BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinal"_n, "test"_n, mvo() + BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinalizer"_n, "test"_n, mvo() ("finalizer_policy", mvo()("fthreshold", 1) ("finalizers", std::vector{mvo() ("description", "test_desc") diff --git a/tests/unit/test_contracts/CMakeLists.txt b/tests/unit/test_contracts/CMakeLists.txt index 3728039199..1cc1107231 100644 --- a/tests/unit/test_contracts/CMakeLists.txt +++ b/tests/unit/test_contracts/CMakeLists.txt @@ -8,7 +8,7 @@ add_contract(transfer_contract transfer_contract transfer.cpp) add_contract(minimal_tests minimal_tests minimal_tests.cpp) add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp) add_contract(bls_primitives_tests bls_primitives_tests bls_primitives_tests.cpp) -add_contract(set_finalizers_tests set_finalizers_tests set_finalizers_tests.cpp) +add_contract(instant_finality_tests instant_finality_tests instant_finality_tests.cpp) add_contract(get_code_hash_tests get_code_hash_write get_code_hash_write.cpp) add_contract(get_code_hash_tests get_code_hash_read get_code_hash_read.cpp) add_contract(name_pk_tests name_pk_tests name_pk_tests.cpp) diff --git a/tests/unit/test_contracts/capi/privileged.c b/tests/unit/test_contracts/capi/privileged.c index 3bc040cf50..e1131c918a 100644 --- a/tests/unit/test_contracts/capi/privileged.c +++ b/tests/unit/test_contracts/capi/privileged.c @@ -1,4 +1,5 @@ #include +#include #include void test_privileged( void ) { diff --git a/tests/unit/test_contracts/instant_finality_tests.cpp b/tests/unit/test_contracts/instant_finality_tests.cpp new file mode 100644 index 0000000000..ca5f1851f4 --- /dev/null +++ b/tests/unit/test_contracts/instant_finality_tests.cpp @@ -0,0 +1,12 @@ +#include +#include + +class [[eosio::contract]] instant_finality_tests : public eosio::contract{ +public: + using contract::contract; + + [[eosio::action]] + void setfinalizer(const eosio::abi_finalizer_policy& finalizer_policy) { + eosio::set_finalizers(finalizer_policy); + } +}; diff --git a/tests/unit/test_contracts/set_finalizers_tests.cpp b/tests/unit/test_contracts/set_finalizers_tests.cpp deleted file mode 100644 index 1b4fb7d8ca..0000000000 --- a/tests/unit/test_contracts/set_finalizers_tests.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include - -class [[eosio::contract]] set_finalizers_tests : public eosio::contract{ -public: - using contract::contract; - - [[eosio::action]] - void setfinal(const eosio::abi_finalizer_policy& finalizer_policy) { - eosio::set_finalizers(finalizer_policy); - } -}; From 7eeaba0761a36b963517b66a9c521d3790c17e73 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Sun, 10 Dec 2023 09:34:34 -0500 Subject: [PATCH 108/158] change finalazer_set to finalazer_policy in ia couple of comments --- libraries/eosiolib/capi/eosio/instant_finality.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/instant_finality.h b/libraries/eosiolib/capi/eosio/instant_finality.h index 9c6494514c..1ed3d2c05a 100644 --- a/libraries/eosiolib/capi/eosio/instant_finality.h +++ b/libraries/eosiolib/capi/eosio/instant_finality.h @@ -13,8 +13,8 @@ extern "C" { /** * Submits a finalizer policy change to Instant Finality * - * @param data - pointer finalizer_set object packed as bytes - * @param len - size of packed finalazer_set object + * @param data - pointer finalizer_policy object packed as bytes + * @param len - size of packed finalazer_policy object * @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_policy data * abi_finalizer_policy structure is defined in instant_finality.hpp */ From a2e3f3f240387297620112de1dd371c0596371fc Mon Sep 17 00:00:00 2001 From: Douglas James Butner Date: Wed, 13 Dec 2023 12:22:48 -0500 Subject: [PATCH 109/158] UPDATE Link to latest patch of CDT (4.0.1) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1a4ac3ee58..b226330e54 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ CDT currently supports Linux x86_64 Debian packages. Visit the [release page](ht Download the appropriate version of the Debian package and then install it. To download and install the latest version, run the following: ```sh -wget https://github.com/AntelopeIO/cdt/releases/download/v4.0.0/cdt_4.0.0_amd64.deb -sudo apt install ./cdt_4.0.0_amd64.deb +wget https://github.com/AntelopeIO/cdt/releases/download/v4.0.1/cdt_4.0.1_amd64.deb +sudo apt install ./cdt_4.0.1_amd64.deb ``` ### Debian package uninstall From d572004e4db319557a5eeea72e76a458b527de56 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 19 Dec 2023 14:53:02 -0600 Subject: [PATCH 110/158] GH-257 Rename set_finalizers types to match eosio.bios and leap names --- .../contracts/eosio/instant_finality.hpp | 20 +++++++++---------- .../test_contracts/instant_finality_tests.cpp | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/instant_finality.hpp b/libraries/eosiolib/contracts/eosio/instant_finality.hpp index 0976a0ceda..07a74f9a2a 100644 --- a/libraries/eosiolib/contracts/eosio/instant_finality.hpp +++ b/libraries/eosiolib/contracts/eosio/instant_finality.hpp @@ -21,18 +21,18 @@ namespace eosio { } // extern "C" } //internal_use_do_not_use - struct abi_finalizer_authority { + struct finalizer_authority { std::string description; - uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold - std::vector public_key_g1_affine_le; // Affine little endian + uint64_t weight = 0; // weight that this finalizer's vote has for meeting threshold + std::vector public_key_g1; // Affine little endian non-montgomery g1 - EOSLIB_SERIALIZE(abi_finalizer_authority, (description)(fweight)(public_key_g1_affine_le)); + EOSLIB_SERIALIZE(finalizer_authority, (description)(weight)(public_key_g1)); }; - struct abi_finalizer_policy { - uint64_t fthreshold = 0; - std::vector finalizers; + struct finalizer_policy { + uint64_t threshold = 0; + std::vector finalizers; - EOSLIB_SERIALIZE(abi_finalizer_policy, (fthreshold)(finalizers)); + EOSLIB_SERIALIZE(finalizer_policy, (threshold)(finalizers)); }; /** @@ -40,9 +40,9 @@ namespace eosio { * * @param finalizer_policy - finalizer policy to be set */ - inline void set_finalizers( const abi_finalizer_policy& finalizer_policy ) { + inline void set_finalizers( const finalizer_policy& finalizer_policy ) { for (const auto& finalizer : finalizer_policy.finalizers) - eosio::check(finalizer.public_key_g1_affine_le.size() == sizeof(bls_g1), "public key has a wrong size" ); + eosio::check(finalizer.public_key_g1.size() == sizeof(bls_g1), "public key has a wrong size" ); auto packed = eosio::pack(finalizer_policy); internal_use_do_not_use::set_finalizers(packed.data(), packed.size()); } diff --git a/tests/unit/test_contracts/instant_finality_tests.cpp b/tests/unit/test_contracts/instant_finality_tests.cpp index ca5f1851f4..b613cfa92c 100644 --- a/tests/unit/test_contracts/instant_finality_tests.cpp +++ b/tests/unit/test_contracts/instant_finality_tests.cpp @@ -6,7 +6,7 @@ class [[eosio::contract]] instant_finality_tests : public eosio::contract{ using contract::contract; [[eosio::action]] - void setfinalizer(const eosio::abi_finalizer_policy& finalizer_policy) { + void setfinalizer(const eosio::finalizer_policy& finalizer_policy) { eosio::set_finalizers(finalizer_policy); } }; From da082d4d47751ed390b8a8096608a1f89c11e566 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 19 Dec 2023 15:26:51 -0600 Subject: [PATCH 111/158] GH-257 Use public_key instead of public_key_g1. Update tests. --- .../contracts/eosio/instant_finality.hpp | 6 ++--- tests/integration/instant_finality_tests.cpp | 24 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/instant_finality.hpp b/libraries/eosiolib/contracts/eosio/instant_finality.hpp index 07a74f9a2a..004efdca75 100644 --- a/libraries/eosiolib/contracts/eosio/instant_finality.hpp +++ b/libraries/eosiolib/contracts/eosio/instant_finality.hpp @@ -24,9 +24,9 @@ namespace eosio { struct finalizer_authority { std::string description; uint64_t weight = 0; // weight that this finalizer's vote has for meeting threshold - std::vector public_key_g1; // Affine little endian non-montgomery g1 + std::vector public_key; // Affine little endian non-montgomery g1 - EOSLIB_SERIALIZE(finalizer_authority, (description)(weight)(public_key_g1)); + EOSLIB_SERIALIZE(finalizer_authority, (description)(weight)(public_key)); }; struct finalizer_policy { uint64_t threshold = 0; @@ -42,7 +42,7 @@ namespace eosio { */ inline void set_finalizers( const finalizer_policy& finalizer_policy ) { for (const auto& finalizer : finalizer_policy.finalizers) - eosio::check(finalizer.public_key_g1.size() == sizeof(bls_g1), "public key has a wrong size" ); + eosio::check(finalizer.public_key.size() == sizeof(bls_g1), "public key has a wrong size" ); auto packed = eosio::pack(finalizer_policy); internal_use_do_not_use::set_finalizers(packed.data(), packed.size()); } diff --git a/tests/integration/instant_finality_tests.cpp b/tests/integration/instant_finality_tests.cpp index 009a1c9464..b00e3c9488 100644 --- a/tests/integration/instant_finality_tests.cpp +++ b/tests/integration/instant_finality_tests.cpp @@ -24,29 +24,29 @@ BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { produce_block(); push_action(config::system_account_name, "setfinalizer"_n, "test"_n, mvo() - ("finalizer_policy", mvo()("fthreshold", 1) - ("finalizers", std::vector{mvo() - ("description", "test_desc") - ("fweight", 1) - ("public_key_g1_affine_le", "220ef5c49c1868e85b82658df9766fc2cee4bd0b5d9880d4cdee9139edd3c4ebdae4074f1d3db3f3c9213962942eef091b00d9f2a9b837015c8dbe507f242aee459272589b6b2973bcf33eb4608722c70c1ac3d1ade40c845b9e15ea6380080a")}))); + ("finalizer_policy", mvo()("threshold", 1) + ("finalizers", std::vector{mvo() + ("description", "test_desc") + ("weight", 1) + ("public_key", "220ef5c49c1868e85b82658df9766fc2cee4bd0b5d9880d4cdee9139edd3c4ebdae4074f1d3db3f3c9213962942eef091b00d9f2a9b837015c8dbe507f242aee459272589b6b2973bcf33eb4608722c70c1ac3d1ade40c845b9e15ea6380080a")}))); signed_block_ptr cur_block = produce_block(); fc::variant pretty_output; abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); BOOST_REQUIRE(pretty_output.get_object().contains("proposed_finalizer_policy")); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["generation"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["fthreshold"], 1); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["threshold"], 1); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"].size(), 1u); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["description"], "test_desc"); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["fweight"], 1); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["weight"], 1); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_Ig71xJwYaOhbgmWN+XZvws7kvQtdmIDUze6ROe3TxOva5AdPHT2z88khOWKULu8JGwDZ8qm4NwFcjb5QfyQq7kWSclibaylzvPM+tGCHIscMGsPRreQMhFueFepjgAgKXjOb8g=="); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinalizer"_n, "test"_n, mvo() - ("finalizer_policy", mvo()("fthreshold", 1) - ("finalizers", std::vector{mvo() - ("description", "test_desc") - ("fweight", 1) - ("public_key_g1_affine_le", std::vector{'a', 'b', 'c'})}))), fc::exception); + ("finalizer_policy", mvo()("threshold", 1) + ("finalizers", std::vector{mvo() + ("description", "test_desc") + ("weight", 1) + ("public_key", std::vector{'a', 'b', 'c'})}))), fc::exception); } FC_LOG_AND_RETHROW() From e87d621b0ca058b9898773fd5b63c359e11a950b Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 19 Dec 2023 16:22:11 -0600 Subject: [PATCH 112/158] GH-257 Update with valid public key --- tests/integration/instant_finality_tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/instant_finality_tests.cpp b/tests/integration/instant_finality_tests.cpp index b00e3c9488..b99bcbcae2 100644 --- a/tests/integration/instant_finality_tests.cpp +++ b/tests/integration/instant_finality_tests.cpp @@ -28,7 +28,7 @@ BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { ("finalizers", std::vector{mvo() ("description", "test_desc") ("weight", 1) - ("public_key", "220ef5c49c1868e85b82658df9766fc2cee4bd0b5d9880d4cdee9139edd3c4ebdae4074f1d3db3f3c9213962942eef091b00d9f2a9b837015c8dbe507f242aee459272589b6b2973bcf33eb4608722c70c1ac3d1ade40c845b9e15ea6380080a")}))); + ("public_key", "744beeb74c9d1debc318fe847f73b822ae905dff6351c3144f59c22515fe251625158acefea2adff1e37f6f509d83919df639de8074967e4bd756444f52cbeed0e9b363a6820e3f4716ce4282d43aa685f137a5a5be293840de7a0b915f12b08")}))); signed_block_ptr cur_block = produce_block(); fc::variant pretty_output; abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); @@ -38,7 +38,7 @@ BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"].size(), 1u); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["description"], "test_desc"); BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["weight"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_Ig71xJwYaOhbgmWN+XZvws7kvQtdmIDUze6ROe3TxOva5AdPHT2z88khOWKULu8JGwDZ8qm4NwFcjb5QfyQq7kWSclibaylzvPM+tGCHIscMGsPRreQMhFueFepjgAgKXjOb8g=="); + BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_dEvut0ydHevDGP6Ef3O4Iq6QXf9jUcMUT1nCJRX+JRYlFYrO/qKt/x439vUJ2DkZ32Od6AdJZ+S9dWRE9Sy+7Q6bNjpoIOP0cWzkKC1DqmhfE3paW+KThA3noLkV8SsILcfxpQ=="); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinalizer"_n, "test"_n, mvo() From 589faab1d0bd0e113a3bedf93cdd9c8596c9c1b5 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Wed, 20 Dec 2023 18:22:55 -0800 Subject: [PATCH 113/158] Reverts operations to multi-index that created alias for existing operations This reverts commit 65f928fcde87eeaddb20ae8f684bceac74a3e902. --- libraries/eosiolib/contracts/eosio/multi_index.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/multi_index.hpp b/libraries/eosiolib/contracts/eosio/multi_index.hpp index 64dd85e227..5d2eb457d3 100644 --- a/libraries/eosiolib/contracts/eosio/multi_index.hpp +++ b/libraries/eosiolib/contracts/eosio/multi_index.hpp @@ -866,11 +866,11 @@ class multi_index auto secondary_keys = make_extractor_tuple::get_extractor_tuple(indices_type{}, obj); - uint64_t pk = to_raw_key(obj.primary_key()); + uint64_t pk = _multi_index_detail::to_raw_key(obj.primary_key()); updater( obj ); - eosio::check( pk == to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); + eosio::check( pk == _multi_index_detail::to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); size_t size = pack_size( obj ); //using malloc/free here potentially is not exception-safe, although WASM doesn't support exceptions From 5634784cd033b2bc9d35025f2f85e04251de911b Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Wed, 20 Dec 2023 18:24:47 -0800 Subject: [PATCH 114/158] Revert "Update tests to cover both const and non-const version of remove()" This reverts commit a1aae5f8d2d2c4e9de6f8344e22e46067cd03c52. --- .../unit/test_contracts/multi_index_tests.cpp | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/tests/unit/test_contracts/multi_index_tests.cpp b/tests/unit/test_contracts/multi_index_tests.cpp index 155d753a1b..8fa99e7f69 100644 --- a/tests/unit/test_contracts/multi_index_tests.cpp +++ b/tests/unit/test_contracts/multi_index_tests.cpp @@ -205,7 +205,7 @@ namespace _test_multi_index eosio::check(itr2 == table.end(), "idx64_general - table.erase()"); } - // insert, update and remove by iterator + // insert, update and delete by iterator { const uint64_t ssn = 421; auto new_person = table.insert(payer, [&](auto &r) @@ -224,7 +224,7 @@ namespace _test_multi_index eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); } - // insert, update and remove by object + // insert, update and delete by object { const uint64_t ssn = 421; auto new_person = table.insert(payer, [&](auto &r) @@ -235,26 +235,7 @@ namespace _test_multi_index table.update(new_person, payer, [&](auto &r) { r.sec = "billy"_n.value; }); - const auto& person_object = table.get(ssn); - auto& mutable_person = const_cast(person_object); - table.remove(mutable_person); - auto itr2 = table.find(ssn); - eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); - } - - // insert, update and remove by const object - { - const uint64_t ssn = 421; - auto new_person = table.insert(payer, [&](auto &r) - { - r.id = ssn; - r.sec = "bob"_n.value; }); - - table.update(new_person, payer, [&](auto &r) - { r.sec = "billy"_n.value; }); - - const auto& person_object = table.get(ssn); - table.remove(person_object); + table.remove(new_person); auto itr2 = table.find(ssn); eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); } From 47e1b5175af541e67c3de5d1bb72f42d57be41de Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Wed, 20 Dec 2023 18:24:57 -0800 Subject: [PATCH 115/158] Revert "Get rid of const obj for new modify() method." This reverts commit 2b0fd5c55bd8ff32400ff0d65a8d461e226b8db0. --- .../eosiolib/contracts/eosio/multi_index.hpp | 106 +++++++++--------- 1 file changed, 50 insertions(+), 56 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/multi_index.hpp b/libraries/eosiolib/contracts/eosio/multi_index.hpp index 5d2eb457d3..2ae6ae0e0d 100644 --- a/libraries/eosiolib/contracts/eosio/multi_index.hpp +++ b/libraries/eosiolib/contracts/eosio/multi_index.hpp @@ -743,8 +743,8 @@ class multi_index } template - void update( T& obj, eosio::name payer, Lambda&& updater ) { - _multidx->update( obj, payer, std::forward(updater) ); + void update( const T& obj, eosio::name payer, Lambda&& updater ) { + modify( obj, payer, std::forward(updater) ); } const_iterator erase( const_iterator itr ) { @@ -855,56 +855,6 @@ class multi_index return *ptr; } /// load_object_by_primary_iterator - template - void modify_object(T& obj, name payer, Lambda&& updater) { - using namespace _multi_index_detail; - - auto& objitem = static_cast(obj); - eosio::check( objitem.__idx == this, "object passed to modify is not in multi_index" ); - - eosio::check( _code == current_receiver(), "cannot modify objects in table of another contract" ); // Quick fix for mutating db using multi_index that shouldn't allow mutation. Real fix can come in RC2. - - auto secondary_keys = make_extractor_tuple::get_extractor_tuple(indices_type{}, obj); - - uint64_t pk = _multi_index_detail::to_raw_key(obj.primary_key()); - - updater( obj ); - - eosio::check( pk == _multi_index_detail::to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); - - size_t size = pack_size( obj ); - //using malloc/free here potentially is not exception-safe, although WASM doesn't support exceptions - void* buffer = max_stack_buffer_size < size ? malloc(size) : alloca(size); - - datastream ds( (char*)buffer, size ); - ds << obj; - - internal_use_do_not_use::db_update_i64( objitem.__primary_itr, payer.value, buffer, size ); - - if ( max_stack_buffer_size < size ) { - free( buffer ); - } - - if( pk >= _next_primary_key ) - _next_primary_key = (pk >= no_available_primary_key) ? no_available_primary_key : (pk + 1); - - bluegrass::meta::for_each(indices_type{}, [&](auto idx){ - typedef std::tuple_element_t index_type; - auto secondary = index_type::extract_secondary_key( obj ); - if( memcmp( &std::get(secondary_keys), &secondary, sizeof(secondary) ) != 0 ) { - auto indexitr = objitem.__iters[index_type::number()]; - - if( indexitr < 0 ) { - typename index_type::secondary_key_type temp_secondary_key; - indexitr = objitem.__iters[index_type::number()] - = secondary_index_db_functions::db_idx_find_primary( _code.value, _scope, index_type::name(), pk, temp_secondary_key ); - } - - secondary_index_db_functions::db_idx_update( indexitr, payer.value, secondary ); - } - } ); - } - public: /** * Constructs an instance of a Multi-Index table. @@ -1855,9 +1805,53 @@ class multi_index */ template void modify( const T& obj, name payer, Lambda&& updater ) { - T& mutable_obj = const_cast(obj); + using namespace _multi_index_detail; + + const auto& objitem = static_cast(obj); + eosio::check( objitem.__idx == this, "object passed to modify is not in multi_index" ); + auto& mutableitem = const_cast(objitem); + eosio::check( _code == current_receiver(), "cannot modify objects in table of another contract" ); // Quick fix for mutating db using multi_index that shouldn't allow mutation. Real fix can come in RC2. + + auto secondary_keys = make_extractor_tuple::get_extractor_tuple(indices_type{}, obj); + + uint64_t pk = _multi_index_detail::to_raw_key(obj.primary_key()); + + auto& mutableobj = const_cast(obj); // Do not forget the auto& otherwise it would make a copy and thus not update at all. + updater( mutableobj ); + + eosio::check( pk == _multi_index_detail::to_raw_key(obj.primary_key()), "updater cannot change primary key when modifying an object" ); + + size_t size = pack_size( obj ); + //using malloc/free here potentially is not exception-safe, although WASM doesn't support exceptions + void* buffer = max_stack_buffer_size < size ? malloc(size) : alloca(size); + + datastream ds( (char*)buffer, size ); + ds << obj; + + internal_use_do_not_use::db_update_i64( objitem.__primary_itr, payer.value, buffer, size ); + + if ( max_stack_buffer_size < size ) { + free( buffer ); + } - modify_object(mutable_obj, payer, std::forward(updater)); + if( pk >= _next_primary_key ) + _next_primary_key = (pk >= no_available_primary_key) ? no_available_primary_key : (pk + 1); + + bluegrass::meta::for_each(indices_type{}, [&](auto idx){ + typedef std::tuple_element_t index_type; + auto secondary = index_type::extract_secondary_key( obj ); + if( memcmp( &std::get(secondary_keys), &secondary, sizeof(secondary) ) != 0 ) { + auto indexitr = mutableitem.__iters[index_type::number()]; + + if( indexitr < 0 ) { + typename index_type::secondary_key_type temp_secondary_key; + indexitr = mutableitem.__iters[index_type::number()] + = secondary_index_db_functions::db_idx_find_primary( _code.value, _scope, index_type::name(), pk, temp_secondary_key ); + } + + secondary_index_db_functions::db_idx_update( indexitr, payer.value, secondary ); + } + } ); } /** @@ -1902,8 +1896,8 @@ class multi_index * @endcode */ template - void update( T& obj, name payer, Lambda&& updater ) { - modify_object(obj, payer, std::forward(updater)); + void update( const T& obj, name payer, Lambda&& updater ) { + modify(obj, payer, std::forward(updater)); } /** From d31c73a65b91d531647840bf585d5f8afefa14f5 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Wed, 20 Dec 2023 18:25:02 -0800 Subject: [PATCH 116/158] Revert "Add insert, remove and update methods to multi_index (and index)" This reverts commit 94ca0508dddd1d1d0d93847088e3b49ca0ce00f2. --- .../eosiolib/contracts/eosio/multi_index.hpp | 224 ------------------ .../unit/test_contracts/multi_index_tests.cpp | 35 --- 2 files changed, 259 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/multi_index.hpp b/libraries/eosiolib/contracts/eosio/multi_index.hpp index 2ae6ae0e0d..2b06384aac 100644 --- a/libraries/eosiolib/contracts/eosio/multi_index.hpp +++ b/libraries/eosiolib/contracts/eosio/multi_index.hpp @@ -732,21 +732,11 @@ class multi_index _multidx->modify( *itr, payer, std::forward(updater) ); } - template - void update( const_iterator itr, eosio::name payer, Lambda&& updater ) { - modify( itr, payer, std::forward(updater) ); - } - template void modify( const T& obj, eosio::name payer, Lambda&& updater ) { _multidx->modify( obj, payer, std::forward(updater) ); } - template - void update( const T& obj, eosio::name payer, Lambda&& updater ) { - modify( obj, payer, std::forward(updater) ); - } - const_iterator erase( const_iterator itr ) { eosio::check( itr != cend(), "cannot pass end iterator to erase" ); @@ -758,10 +748,6 @@ class multi_index return itr; } - const_iterator remove( const_iterator itr ) { - return erase(itr); - } - eosio::name get_code()const { return _multidx->get_code(); } uint64_t get_scope()const { return _multidx->get_scope(); } @@ -1545,7 +1531,6 @@ class multi_index eosio::check( objitem.__idx == this, "object passed to iterator_to is not in multi_index" ); return {this, &objitem}; } - /** * Adds a new object (i.e., row) to the table. * @ingroup multiindex @@ -1628,48 +1613,6 @@ class multi_index return {this, ptr}; } - /** - * Adds a new object (i.e., row) to the table, alias for emplace() - * @ingroup multiindex - * - * @param payer - Account name of the payer for the Storage usage of the new object - * @param constructor - Lambda function that does an in-place initialization of the object to be created in the table - * - * @pre A multi index table has been instantiated - * @post A new object is created in the Multi-Index table, with a unique primary key (as specified in the object). The object is serialized and written to the table. If the table does not exist, it is created. - * @post Secondary indices are updated to refer to the newly added object. If the secondary index tables do not exist, they are created. - * @post The payer is charged for the storage usage of the new object and, if the table (and secondary index tables) must be created, for the overhead of the table creation. - * - * @return A primary key iterator to the newly created object - * - * Exception - The account is not authorized to write to the table. - * - * Example: - * - * @code - * // This assumes the code from the constructor example. Replace myaction() {...} - * - * void myaction() { - * address_index addresses(_self, _self.value); // code, scope - * // add to table, first argument is account to bill for storage - * addresses.insert(_self, [&](auto& address) { - * address.account_name = "dan"_n; - * address.first_name = "Daniel"; - * address.last_name = "Larimer"; - * address.street = "1 EOS Way"; - * address.city = "Blacksburg"; - * address.state = "VA"; - * }); - * } - * } - * EOSIO_DISPATCH( addressbook, (myaction) ) - * @endcode - */ - template - const_iterator insert( name payer, Lambda&& constructor ) { - return emplace(payer, std::forward(constructor)); - } - /** * Modifies an existing object in a table. * @ingroup multiindex @@ -1717,51 +1660,6 @@ class multi_index modify( *itr, payer, std::forward(updater) ); } - /** - * Updates an existing object in a table, alias for modify() - * @ingroup multiindex - * - * @param itr - an iterator pointing to the object to be updated - * @param payer - account name of the payer for the storage usage of the updated row - * @param updater - lambda function that updates the target object - * - * @pre itr points to an existing element - * @pre payer is a valid account that is authorized to execute the action and be billed for storage usage. - * - * @post The updated object is serialized, then replaces the existing object in the table. - * @post Secondary indices are updated; the primary key of the updated object is not changed. - * @post The payer is charged for the storage usage of the updated object. - * @post If payer is the same as the existing payer, payer only pays for the usage difference between existing and updated object (and is refunded if this difference is negative). - * @post If payer is different from the existing payer, the existing payer is refunded for the storage usage of the existing object. - * - * Exceptions: - * If called with an invalid precondition, execution is aborted. - * - * Example: - * - * @code - * // This assumes the code from the constructor example. Replace myaction() {...} - * - * void myaction() { - * // create reference to address_index - see emplace example - * // add dan account to table - see emplace example - * - * auto itr = addresses.find("dan"_n); - * eosio::check(itr != addresses.end(), "Address for account not found"); - * addresses.update( itr, account payer, [&]( auto& address ) { - * address.city = "San Luis Obispo"; - * address.state = "CA"; - * }); - * } - * } - * EOSIO_DISPATCH( addressbook, (myaction) ) - * @endcode - */ - template - void update( const_iterator itr, name payer, Lambda&& updater ) { - modify( itr, payer, std::forward(updater) ); - } - /** * Modifies an existing object in a table. * @ingroup multiindex @@ -1854,52 +1752,6 @@ class multi_index } ); } - /** - * Updates an existing object in a table, alias for modify() - * @ingroup multiindex - * - * @param obj - a reference to the object to be updated - * @param payer - account name of the payer for the storage usage of the updated row - * @param updater - lambda function that updates the target object - * - * @pre obj is an existing object in the table - * @pre payer is a valid account that is authorized to execute the action and be billed for storage usage. - * - * @post The modified object is serialized, then replaces the existing object in the table. - * @post Secondary indices are updated; the primary key of the updated object is not changed. - * @post The payer is charged for the storage usage of the updated object. - * @post If payer is the same as the existing payer, payer only pays for the usage difference between existing and updated object (and is refunded if this difference is negative). - * @post If payer is different from the existing payer, the existing payer is refunded for the storage usage of the existing object. - * - * Exceptions: - * If called with an invalid precondition, execution is aborted. - * - * Example: - * - * @code - * // This assumes the code from the constructor example. Replace myaction() {...} - * - * void myaction() { - * // create reference to address_index - see emplace example - * // add dan account to table - see emplace example - * - * auto itr = addresses.find("dan"_n); - * eosio::check(itr != addresses.end(), "Address for account not found"); - * addresses.update( *itr, payer, [&]( auto& address ) { - * address.city = "San Luis Obispo"; - * address.state = "CA"; - * }); - * eosio::check(itr->city == "San Luis Obispo", "Lock arf, Address not modified"); - * } - * } - * EOSIO_DISPATCH( addressbook, (myaction) ) - * @endcode - */ - template - void update( const T& obj, name payer, Lambda&& updater ) { - modify(obj, payer, std::forward(updater)); - } - /** * Retrieves an existing object from a table using its primary key. * @ingroup multiindex @@ -2050,46 +1902,6 @@ class multi_index return itr; } - /** - * Deletes an existing object from a table using its primary key, alias for erase() - * @ingroup multiindex - * - * @param itr - An iterator pointing to the object to be removed - * - * @pre itr points to an existing element - * @post The object is removed from the table and all associated storage is reclaimed. - * @post Secondary indices associated with the table are updated. - * @post The existing payer for storage usage of the object is refunded for the table and secondary indices usage of the removed object, and if the table and indices are removed, for the associated overhead. - * - * @return For the signature with `const_iterator`, returns a pointer to the object following the removed object. - * - * Exceptions: - * The object to be removed is not in the table. - * The action is not authorized to modify the table. - * The given iterator is invalid. - * - * Example: - * - * @code - * // This assumes the code from the constructor example. Replace myaction() {...} - * - * void myaction() { - * // create reference to address_index - see emplace example - * // add dan account to table - see emplace example - * - * auto itr = addresses.find("dan"_n); - * eosio::check(itr != addresses.end(), "Address for account not found"); - * addresses.delete( itr ); - * eosio::check(itr != addresses.end(), "Everting lock arf, Address not erased properly"); - * } - * } - * EOSIO_ABI( addressbook, (myaction) ) - * @endcode - */ - const_iterator remove( const_iterator itr ) { - return erase(itr); - } - /** * Remove an existing object from a table using its primary key. * @ingroup multiindex @@ -2153,41 +1965,5 @@ class multi_index _items_vector.erase(--(itr2.base())); } -/** - * Remove an existing object from a table using its primary key, alias for erase() - * @ingroup multiindex - * - * @param obj - Object to be removed - * - * @pre obj is an existing object in the table - * @post The object is removed from the table and all associated storage is reclaimed. - * @post Secondary indices associated with the table are updated. - * @post The existing payer for storage usage of the object is refunded for the table and secondary indices usage of the removed object, and if the table and indices are removed, for the associated overhead. - * - * Exceptions: - * The object to be removed is not in the table. - * The action is not authorized to modify the table. - * The given iterator is invalid. - * - * Example: - * - * @code - * // This assumes the code from the constructor example. Replace myaction() {...} - * - * void myaction() { - * auto itr = addresses.find("dan"_n); - * eosio::check(itr != addresses.end(), "Record is not found"); - * addresses.remove(*itr); - * itr = addresses.find("dan"_n); - * eosio::check(itr == addresses.end(), "Record is not deleted"); - * } - * } - * EOSIO_DISPATCH( addressbook, (myaction) ) - * @endcode - */ - void remove( const T& obj ) { - erase(obj); - } - }; } /// eosio diff --git a/tests/unit/test_contracts/multi_index_tests.cpp b/tests/unit/test_contracts/multi_index_tests.cpp index 8fa99e7f69..8c62358081 100644 --- a/tests/unit/test_contracts/multi_index_tests.cpp +++ b/tests/unit/test_contracts/multi_index_tests.cpp @@ -204,41 +204,6 @@ namespace _test_multi_index auto itr2 = table.find(ssn); eosio::check(itr2 == table.end(), "idx64_general - table.erase()"); } - - // insert, update and delete by iterator - { - const uint64_t ssn = 421; - auto new_person = table.insert(payer, [&](auto &r) - { - r.id = ssn; - r.sec = "bob"_n.value; }); - - table.update(new_person, payer, [&](auto &r) - { r.sec = "billy"_n.value; }); - - auto itr1 = table.find(ssn); - eosio::check(itr1 != table.end() && itr1->sec == "billy"_n.value, "idx64_general - table.update()"); - - table.remove(itr1); - auto itr2 = table.find(ssn); - eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); - } - - // insert, update and delete by object - { - const uint64_t ssn = 421; - auto new_person = table.insert(payer, [&](auto &r) - { - r.id = ssn; - r.sec = "bob"_n.value; }); - - table.update(new_person, payer, [&](auto &r) - { r.sec = "billy"_n.value; }); - - table.remove(new_person); - auto itr2 = table.find(ssn); - eosio::check(itr2 == table.end(), "idx64_general - table.remove()"); - } } template From 3b07180665ff8ed726439b7069bcc5c3a3c02228 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 8 Jan 2024 20:44:19 -0500 Subject: [PATCH 117/158] export wasm memory --- tools/include/compiler_options.hpp.in | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index e94e8d57e2..0547701ce1 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -508,6 +508,7 @@ static void GetLdDefaults(std::vector& ldopts) { else ldopts.insert(ldopts.end(), { "-e", "apply" }); ldopts.insert(ldopts.end(), { "--only-export", "apply:function" }); + ldopts.insert(ldopts.end(), { "--only-export", "*:memory" }); } ldopts.emplace_back("-lc++"); ldopts.emplace_back("-lc"); From fd49c7b08cb08eedfef3c329b488b67f92ea7d57 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Thu, 15 Feb 2024 08:55:56 -0800 Subject: [PATCH 118/158] updated build&test to use ubuntu22 leap --- .github/workflows/build.yaml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 7f70ff46a8..cadb8b2815 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -102,8 +102,8 @@ jobs: - uses: actions/checkout@v3 with: submodules: recursive - - name: Download leap-dev.deb (Ubuntu 20 only) - if: matrix.platform == 'ubuntu20' + - name: Download leap-dev.deb (Ubuntu 22 only) + if: matrix.platform == 'ubuntu22' uses: AntelopeIO/asset-artifact-download-action@v3 with: owner: AntelopeIO @@ -111,10 +111,10 @@ jobs: file: 'leap-dev.*(x86_64|amd64).deb' target: '${{needs.versions.outputs.leap-dev-target}}' prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} - artifact-name: leap-dev-ubuntu20-amd64 + artifact-name: leap-dev-ubuntu22-amd64 container-package: experimental-binaries - - name: Install leap-dev.deb (Ubuntu 20 only) - if: matrix.platform == 'ubuntu20' + - name: Install leap-dev.deb (Ubuntu 22 only) + if: matrix.platform == 'ubuntu22' run: | apt-get update && apt-get upgrade -y apt install -y ./leap-dev*.deb @@ -127,13 +127,13 @@ jobs: make -j $(nproc) cd tests ctest -j $(nproc) --output-on-failure - - name: Package (Ubuntu 20 only) - if: matrix.platform == 'ubuntu20' + - name: Package (Ubuntu 22 only) + if: matrix.platform == 'ubuntu22' run: | cd build/packages bash generate_package.sh deb ubuntu amd64 - - name: Upload (Ubuntu 20 only) - if: matrix.platform == 'ubuntu20' + - name: Upload (Ubuntu 22 only) + if: matrix.platform == 'ubuntu22' uses: actions/upload-artifact@v3 with: name: cdt_ubuntu_package_amd64 From b8b9318784d37134597228ea63ad9a5bd54038bd Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Fri, 16 Feb 2024 19:03:47 -0800 Subject: [PATCH 119/158] specify os version in leap dev download file --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index cadb8b2815..0e0f0581d5 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -108,7 +108,7 @@ jobs: with: owner: AntelopeIO repo: leap - file: 'leap-dev.*(x86_64|amd64).deb' + file: 'file: leap-dev.*ubuntu22\.04_amd64.deb' target: '${{needs.versions.outputs.leap-dev-target}}' prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} artifact-name: leap-dev-ubuntu22-amd64 From 0d42f9bc77c4c8468d62ba2c670fdb27219d668a Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Fri, 16 Feb 2024 19:04:19 -0800 Subject: [PATCH 120/158] specify os version in leap dev download file --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 0e0f0581d5..c40b699527 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -108,7 +108,7 @@ jobs: with: owner: AntelopeIO repo: leap - file: 'file: leap-dev.*ubuntu22\.04_amd64.deb' + file: 'leap-dev.*ubuntu22\.04_amd64.deb' target: '${{needs.versions.outputs.leap-dev-target}}' prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} artifact-name: leap-dev-ubuntu22-amd64 From b067f1c76bdbae9a021b2c5be1675fe7abd1532a Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Sun, 10 Mar 2024 19:13:04 -0400 Subject: [PATCH 121/158] do not append trailing = to base64url encoding --- libraries/eosiolib/base64.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/eosiolib/base64.cpp b/libraries/eosiolib/base64.cpp index 9a31dbdf52..d08957db1b 100644 --- a/libraries/eosiolib/base64.cpp +++ b/libraries/eosiolib/base64.cpp @@ -31,7 +31,7 @@ unsigned int pos_of_char(const unsigned char chr) { std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) { const size_t len_encoded = (in_len +2) / 3 * 4; - const unsigned char trailing_char = url ? '.' : '='; + const unsigned char trailing_char = '='; // Includes performance improvement from unmerged PR: https://github.com/ReneNyffenegger/cpp-base64/pull/27 @@ -74,14 +74,14 @@ std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, b } else { ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]); - ret.push_back(trailing_char); + if (!url) ret.push_back(trailing_char); } } else { ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]); - ret.push_back(trailing_char); - ret.push_back(trailing_char); + if (!url) ret.push_back(trailing_char); + if (!url) ret.push_back(trailing_char); } pos += 3; @@ -166,4 +166,4 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak return ret; } -}//eosio::detail \ No newline at end of file +}//eosio::detail From 42c0a9d1584179a7afab772a299db16f31f3d82d Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Sun, 10 Mar 2024 21:32:14 -0400 Subject: [PATCH 122/158] Do not allow base64 chars (+, /, and =) when decoding base64url, to match with Leap base64url decode behavior --- libraries/eosiolib/base64.cpp | 72 +++++++++++++++--------- libraries/eosiolib/core/eosio/base64.hpp | 4 +- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/libraries/eosiolib/base64.cpp b/libraries/eosiolib/base64.cpp index d08957db1b..4dc5e2a7b2 100644 --- a/libraries/eosiolib/base64.cpp +++ b/libraries/eosiolib/base64.cpp @@ -2,27 +2,47 @@ namespace eosio::detail { -unsigned int pos_of_char(const unsigned char chr) { +unsigned int pos_of_char(const unsigned char chr, bool url) { // Return the position of chr within base64_encode() - const unsigned char from_base64_chars[256] = { - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 62, 64, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, - 64, 0, 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, 64, 64, 64, 64, 63, - 64, 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, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 - }; - auto c = from_base64_chars[chr]; + constexpr unsigned char from_base64_chars[2][256] = { + { // for base64 + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, + 64, 0, 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, 64, 64, 64, 64, 64, + 64, 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, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 + }, + { // for base64url + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, + 64, 0, 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, 64, 64, 64, 64, 63, + 64, 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, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 + }}; + + auto c = from_base64_chars[url][chr]; eosio::check(c != 64, "encountered non-base64 character"); return c; @@ -91,7 +111,7 @@ std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, b return ret; } -std::string base64_decode(std::string_view encoded_string, bool remove_linebreaks) { +std::string base64_decode(std::string_view encoded_string, bool remove_linebreaks, bool url) { if (encoded_string.empty()) return std::string{}; if (remove_linebreaks) { @@ -100,7 +120,7 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end()); - return base64_decode(copy, false); + return base64_decode(copy, false, url); } size_t length_of_string = encoded_string.length(); @@ -130,12 +150,12 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak // The last chunk produces at least one and up to three bytes. // eosio::check(pos+1 < length_of_string, "wrong encoded string size"); - size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1) ); + size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1), url); // // Emit the first output byte that is produced in each chunk: // - ret.push_back(static_cast( ( (pos_of_char(encoded_string.at(pos+0)) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4))); + ret.push_back(static_cast( ( (pos_of_char(encoded_string.at(pos+0), url) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4))); if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045) encoded_string.at(pos+2) != '=' && @@ -145,7 +165,7 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak // // Emit a chunk's second byte (which might not be produced in the last chunk). // - unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2) ); + unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2), url); ret.push_back(static_cast( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2))); if ( ( pos + 3 < length_of_string ) && @@ -156,7 +176,7 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak // // Emit a chunk's third byte (which might not be produced in the last chunk). // - ret.push_back(static_cast( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3)) )); + ret.push_back(static_cast( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3), url))); } } diff --git a/libraries/eosiolib/core/eosio/base64.hpp b/libraries/eosiolib/core/eosio/base64.hpp index f17f4a94f1..0f800e8eee 100644 --- a/libraries/eosiolib/core/eosio/base64.hpp +++ b/libraries/eosiolib/core/eosio/base64.hpp @@ -39,7 +39,7 @@ namespace eosio { namespace detail { std::string base64_encode(unsigned char const*, size_t len, bool url = false); -std::string base64_decode(std::string_view s, bool remove_linebreaks = false); +std::string base64_decode(std::string_view s, bool remove_linebreaks = false, bool url = false); } // detail namespace @@ -53,7 +53,7 @@ inline std::string base64url_encode(std::string_view enc) { return detail::base64_encode(reinterpret_cast(enc.data()), enc.size(), true); } inline std::string base64url_decode(std::string_view encoded_string) { - return detail::base64_decode(encoded_string, true); + return detail::base64_decode(encoded_string, true, true); } } // namespace eosio From 28794f472e73b7ecfd6ebf3dbb2cbc6c59edfac0 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Mon, 11 Mar 2024 10:55:34 -0400 Subject: [PATCH 123/158] add tests to verify base64url_decode reject invalid chars and update existing tests --- tests/unit/base64_tests.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/tests/unit/base64_tests.cpp b/tests/unit/base64_tests.cpp index a875eced47..7ec2f655d5 100644 --- a/tests/unit/base64_tests.cpp +++ b/tests/unit/base64_tests.cpp @@ -16,6 +16,7 @@ EOSIO_TEST_BEGIN(base64enc) CHECK_EQUAL(expected_output, base64_encode(input)); EOSIO_TEST_END +// No trailing sequence of = added EOSIO_TEST_BEGIN(base64urlenc) auto input = "abc123$&()'?\xb4\xf5\x01\xfa~a"s; auto expected_output = "YWJjMTIzJCYoKSc_tPUB-n5h"s; @@ -51,6 +52,30 @@ EOSIO_TEST_BEGIN(base64dec_bad_stuff) })); EOSIO_TEST_END +// `+` not valid in base64 url +EOSIO_TEST_BEGIN(base64urldec_plus_not_allowed) + CHECK_ASSERT( "encountered non-base64 character", + ([](){ + base64_decode("YWJjMTIzJCYoKSc+tPUB-n5h"s); + })); +EOSIO_TEST_END + +// `/` not valid in base64 url +EOSIO_TEST_BEGIN(base64urldec_slash_not_allowed) + CHECK_ASSERT( "encountered non-base64 character", + ([](){ + base64_decode("YWJjMTIzJCYoKSc/tPUB-n5h"s); + })); +EOSIO_TEST_END + +// trailing not allowed in base64 url +EOSIO_TEST_BEGIN(base64urldec_trailing_not_allowed) + CHECK_ASSERT( "encountered non-base64 character", + ([](){ + base64_decode("YWJjMTIzJCYoKSc_tPUB-n5h=="s); + })); +EOSIO_TEST_END + // tests from https://github.com/ReneNyffenegger/cpp-base64/blob/master/test.cpp EOSIO_TEST_BEGIN(base64_cpp_base64_tests) // @@ -112,7 +137,7 @@ EOSIO_TEST_BEGIN(base64_cpp_base64_tests) std::string a17_encoded_url = base64url_encode(a17_orig); CHECK_EQUAL(a17_encoded, "YWFhYWFhYWFhYWFhYWFhYWE="); - CHECK_EQUAL(a17_encoded_url, "YWFhYWFhYWFhYWFhYWFhYWE."); + CHECK_EQUAL(a17_encoded_url, "YWFhYWFhYWFhYWFhYWFhYWE"); CHECK_EQUAL(base64_decode(a17_encoded_url), a17_orig); CHECK_EQUAL(base64_decode(a17_encoded), a17_orig); @@ -126,9 +151,9 @@ EOSIO_TEST_BEGIN(base64_cpp_base64_tests) std::string s_6364_encoded_url = base64url_encode(s_6364); CHECK_EQUAL(s_6364_encoded, "A+//+Q=="); - CHECK_EQUAL(s_6364_encoded_url, "A-__-Q.."); + CHECK_EQUAL(s_6364_encoded_url, "A-__-Q"); CHECK_EQUAL(base64_decode(s_6364_encoded), s_6364); - CHECK_EQUAL(base64_decode(s_6364_encoded_url), s_6364); + CHECK_EQUAL(base64url_decode(s_6364_encoded_url), s_6364); // ---------------------------------------------- From 75eb06ea0396e3be29fe499738824477f9d28a60 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Mon, 11 Mar 2024 12:35:59 -0400 Subject: [PATCH 124/158] change BLS library to use base64 URL for encoding and decoding --- .../eosiolib/core/eosio/crypto_bls_ext.hpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 42d950319b..9c143e60b7 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -317,7 +317,7 @@ namespace detail { const inline std::string bls_signature_prefix = "SIG_BLS_"; template - std::string bls_type_to_base64(const T& g1) { + std::string bls_type_to_base64url(const T& g1) { std::array g1_with_checksum; auto it = std::copy(g1.begin(), g1.end(), g1_with_checksum.begin()); @@ -326,15 +326,15 @@ namespace detail { reinterpret_cast(csum.data())+bls_checksum_size, it); - return Prefix + eosio::base64_encode({g1_with_checksum.data(), g1_with_checksum.size()}); + return Prefix + eosio::base64url_encode({g1_with_checksum.data(), g1_with_checksum.size()}); } template - T bls_base64_to_type(const char* data, size_t size) { + T bls_base64url_to_type(const char* data, size_t size) { eosio::check(size > Prefix.size(), "encoded base64 key is too short"); eosio::check(0 == memcmp(data, Prefix.data(), Prefix.size()), "base64 encoded type must begin from corresponding prefix"); - std::string decoded = eosio::base64_decode({data+Prefix.size(), size - Prefix.size()}); + std::string decoded = eosio::base64url_decode({data+Prefix.size(), size - Prefix.size()}); T ret; eosio::check(decoded.size() == ret.size() + bls_checksum_size, "decoded size " + std::to_string(decoded.size()) + " doesn't match structure size " + std::to_string(ret.size()) + @@ -346,6 +346,7 @@ namespace detail { auto csum = ripemd160(ret.data(), ret.size()).extract_as_byte_array(); eosio::check(0 == memcmp(&*it, csum.data(), bls_checksum_size), "checksum of structure doesn't match"); + printhex(static_cast(ret.data()), ret.size()); return ret; } @@ -451,16 +452,16 @@ namespace detail { } // namespace detail inline std::string encode_g1_to_bls_public_key(const bls_g1& g1) { - return detail::bls_type_to_base64(g1); + return detail::bls_type_to_base64url(g1); } inline bls_g1 decode_bls_public_key_to_g1(std::string_view public_key) { - return detail::bls_base64_to_type(public_key.data(), public_key.size()); + return detail::bls_base64url_to_type(public_key.data(), public_key.size()); } inline std::string encode_g2_to_bls_signature(const bls_g2& g2) { - return detail::bls_type_to_base64(g2); + return detail::bls_type_to_base64url(g2); } inline bls_g2 decode_bls_signature_to_g2(std::string_view public_key) { - return detail::bls_base64_to_type(public_key.data(), public_key.size()); + return detail::bls_base64url_to_type(public_key.data(), public_key.size()); } // pubkey and signature are assumed to be in RAW affine little-endian bytes From 26414ada06e942cfb52f30cc73bad6cb8462af5c Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Mon, 11 Mar 2024 13:10:42 -0400 Subject: [PATCH 125/158] Adapt BLS and instant_finality tests to base64 URL --- tests/integration/bls_tests.cpp | 17 ++++++++--------- tests/integration/instant_finality_tests.cpp | 15 ++++++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/integration/bls_tests.cpp b/tests/integration/bls_tests.cpp index 17ecff1bac..b2880a6609 100644 --- a/tests/integration/bls_tests.cpp +++ b/tests/integration/bls_tests.cpp @@ -102,12 +102,11 @@ BOOST_FIXTURE_TEST_CASE( fp_exp_test, bls_primitives_tester ) try { BOOST_FIXTURE_TEST_CASE( base64_test, bls_primitives_tester ) try { push_action("eosio"_n, "g1baseb4enc"_n, "test"_n, mvo() - ("g1", "dbf44b63155bdd1b124f9155ee40b00ac626d5c8aa3f88e9da5dd890df006da142833846ff233e13a40c90b596aa07190bb4de63a265b9848078cae7a3d7fca7786c160d12d705323a758849455eaf88a5a2646fafbb811da450dd07695f1b00") - ("base64", "PUB_BLS_2/RLYxVb3RsST5FV7kCwCsYm1ciqP4jp2l3YkN8AbaFCgzhG/yM+E6QMkLWWqgcZC7TeY6JluYSAeMrno9f8p3hsFg0S1wUyOnWISUVer4ilomRvr7uBHaRQ3QdpXxsAwOc0YQ==")); + ("g1", "4783d417e555ba2fe4a6a9068e3c212bc43d5c895a83dc15bfdcbb65357c4caafbf6a6884c36a41c68587a77caba3f1105feb50023b4806b003c913c3ecca1ede21ff4cf322576520571977958ec1f6b8d483ae505a1eb93675ecf856ae0dc0e") + ("base64", "PUB_BLS_R4PUF-VVui_kpqkGjjwhK8Q9XIlag9wVv9y7ZTV8TKr79qaITDakHGhYenfKuj8RBf61ACO0gGsAPJE8Psyh7eIf9M8yJXZSBXGXeVjsH2uNSDrlBaHrk2dez4Vq4NwOf9VEMA")); push_action("eosio"_n, "sigbaseb4enc"_n, "test"_n, mvo() - ("g2", "6bda63c93a6f638fda29b393cec8bcea5136dd2e27cec50425463f21eb1f2f09a5347024a427e302d48646565109f4181c861299edc24015b62cc146ed7b17d44510fcbf933ef0eae764dc793f68f73fe203a4c18ece7cd9d3103a6299f21519514660ccf94f0ea221d0335b4d3daf557ddeb3a8819c7136f75625e29e1bac230b88a290ff7900fb1be3147ca4a2e40fe68b2dc8c938f23e054f17603953659e2cd46265a6b2df86eb99e012ab534d4219a9cdc8d0e5038b11888533503c2f05") - ("base64", "SIG_BLS_a9pjyTpvY4/aKbOTzsi86lE23S4nzsUEJUY/IesfLwmlNHAkpCfjAtSGRlZRCfQYHIYSme3CQBW2LMFG7XsX1EUQ/L+TPvDq52TceT9o9z/iA6TBjs582dMQOmKZ8hUZUUZgzPlPDqIh0DNbTT2vVX3es6iBnHE291Yl4p4brCMLiKKQ/3kA+xvjFHykouQP5ostyMk48j4FTxdgOVNlnizUYmWmst+G65ngEqtTTUIZqc3I0OUDixGIhTNQPC8FRe+RNQ==")); - + ("g2", "ddb6db1f52a1eb191dfa57656645689fbd08e76170f08c9674c20c243c71bda00054d06862623083abe2a738da032000f1d2784eefacb8696a48f185ecc225f2b0cc26d3e59d84cb6bafba976785002446cd2921cb954daa124058a368ba270ef6e7a57e41f3c651f2ca6e70982330b5ab3acc65887f24e9f5c63d3550d2c6fa643fd6ea061c7e24ecdc41a8c55850089ab3cf40b0d986d283d065f1e7b66047008596d721db450b0e8b8eada95a09ca258a651d0d19b4651e2b38cbae308218") + ("base64", "SIG_BLS_3bbbH1Kh6xkd-ldlZkVon70I52Fw8IyWdMIMJDxxvaAAVNBoYmIwg6vipzjaAyAA8dJ4Tu-suGlqSPGF7MIl8rDMJtPlnYTLa6-6l2eFACRGzSkhy5VNqhJAWKNouicO9uelfkHzxlHyym5wmCMwtas6zGWIfyTp9cY9NVDSxvpkP9bqBhx-JOzcQajFWFAImrPPQLDZhtKD0GXx57ZgRwCFltch20ULDouOralaCcolimUdDRm0ZR4rOMuuMIIYOJG0NA")); } FC_LOG_AND_RETHROW() BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { @@ -126,8 +125,8 @@ BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try { // pk.to_string(); // signature.to_string(); push_action("eosio"_n, "verify"_n, "test"_n, mvo() - ("pk", "PUB_BLS_sS66EwY8bNx7vkDn3mKhwPhhqa1V6STN1QSb6bWOIFBTloF5zt5b55r9y7uQMiQGrvt6XOZO3CpEgthlba7R7qz7Qob2YcD5EX3Ng/rUUdMBsjEJRuXNWICPe0QbKAoCQOw9vw==") - ("sig", "SIG_BLS_07NCCTekrxhDFurhRhl3b8m4T+xmTixSl63TYcee/xJONi2/qy9+8o5vyetMefsL4hhDMfy1yIeHERqxUvGguRBBncpabp84b3P2bNpY18A+PVs7qKFqlld1gEUqt+cDOcwkyUe6HqqFXR5wtXoSHE4lHauV3CzR0lD91gnr2c49aUQPAC7SD+ZcnZyahwURje+Db26zhTXIO1WWIx6vwKnZUuvZEbzYrijjBQKeZsqp0eoAzjrByx9gOmN2d/AR9PhRvg==") + ("pk", "PUB_BLS_sS66EwY8bNx7vkDn3mKhwPhhqa1V6STN1QSb6bWOIFBTloF5zt5b55r9y7uQMiQGrvt6XOZO3CpEgthlba7R7qz7Qob2YcD5EX3Ng_rUUdMBsjEJRuXNWICPe0QbKAoCQOw9vw") + ("sig", "SIG_BLS_07NCCTekrxhDFurhRhl3b8m4T-xmTixSl63TYcee_xJONi2_qy9-8o5vyetMefsL4hhDMfy1yIeHERqxUvGguRBBncpabp84b3P2bNpY18A-PVs7qKFqlld1gEUqt-cDOcwkyUe6HqqFXR5wtXoSHE4lHauV3CzR0lD91gnr2c49aUQPAC7SD-ZcnZyahwURje-Db26zhTXIO1WWIx6vwKnZUuvZEbzYrijjBQKeZsqp0eoAzjrByx9gOmN2d_AR9PhRvg") ("msg", "this is a message string")); } FC_LOG_AND_RETHROW() @@ -137,8 +136,8 @@ BOOST_FIXTURE_TEST_CASE( sig_pop_verify_test, bls_primitives_tester ) try { ("sig", "3a1e5689abddbd0ed54c48c88dcef7ac9bba70abdd7a8e965fb807a188cfc8a7e21ca5c9ecc58df7681d158cc6aced13d50bbe35a5aa7848c32a290b096e1f7a61b2817660bd6e6d5686180a1c00716d47ee996ced081fdb6c4417d6cc8dbd06f26f037331c1b4703e94454374ba04e71fb7571159299b9020c124e9ecee777c2b5c16a51ca883b716082fdf2e6c150551a82eaca5efaf761053d6998a439cc696366fe82eb93f19aac34893610698b37d11f0d608fdc1befc50a7e565ea4813")); push_action("eosio"_n, "popverify"_n, "test"_n, mvo() - ("pk", "PUB_BLS_OwjnO72pw+/1l/xg+7G9jIGvNlkCT2KCkIJDjvUqAqcfmWWAh+pYEI4SVDwImAoPGVDH8whatLSeMTfo01rfy+1hbdN80BFDXpQKdPU/masdH9IgoInU6FF98CZ+ywgCD2yuNg==") - ("sig", "SIG_BLS_Oh5WiavdvQ7VTEjIjc73rJu6cKvdeo6WX7gHoYjPyKfiHKXJ7MWN92gdFYzGrO0T1Qu+NaWqeEjDKikLCW4femGygXZgvW5tVoYYChwAcW1H7pls7Qgf22xEF9bMjb0G8m8DczHBtHA+lEVDdLoE5x+3VxFZKZuQIMEk6ezud3wrXBalHKiDtxYIL98ubBUFUagurKXvr3YQU9aZikOcxpY2b+guuT8ZqsNIk2EGmLN9EfDWCP3BvvxQp+Vl6kgTpacydw==")); + ("pk", "PUB_BLS_82P3oM1u0IEv64u9i4vSzvg1-QDl4Fb2n50Mp8Sk7Fr1Tz0MJypzL39nSd5VPFgFC9WqrjopRbBm1Pf0RkP018fo1k2rXaJY7Wtzd9RKlE8PoQ6XhDm4PyZlIupQg_gOuiMhcg") + ("sig", "SIG_BLS_RrwvP79LxfahskX-ceZpbgrJ1aUkSSIzE2sMFj0twuhK8QwjcGMvT2tZ_-QMHvAV83tWZYOs7SEvoyteCKGD_Tk6YySkw1HONgvVeNWM8ZwuNgonOHkegNNPIXSIvWMTczfkg2lEtEh-ngBa5t9-4CvZ6aOjg29XPVvu6dimzHix-9E0M53YkWZ-gW5GDkkOLoN2FMxjXaELmhuI64xSeSlcWLFfZa6TMVTctBFWsHDXm1ZMkURoB83dokKHEi4OQTbJtg")); } FC_LOG_AND_RETHROW() BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/integration/instant_finality_tests.cpp b/tests/integration/instant_finality_tests.cpp index b99bcbcae2..b5367da24d 100644 --- a/tests/integration/instant_finality_tests.cpp +++ b/tests/integration/instant_finality_tests.cpp @@ -32,13 +32,14 @@ BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { signed_block_ptr cur_block = produce_block(); fc::variant pretty_output; abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); - BOOST_REQUIRE(pretty_output.get_object().contains("proposed_finalizer_policy")); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["generation"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["threshold"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"].size(), 1u); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["description"], "test_desc"); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["weight"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["proposed_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_dEvut0ydHevDGP6Ef3O4Iq6QXf9jUcMUT1nCJRX+JRYlFYrO/qKt/x439vUJ2DkZ32Od6AdJZ+S9dWRE9Sy+7Q6bNjpoIOP0cWzkKC1DqmhfE3paW+KThA3noLkV8SsILcfxpQ=="); + std::cout << fc::json::to_string(pretty_output, fc::time_point::now() + abi_serializer_max_time) << std::endl; + BOOST_REQUIRE(pretty_output.get_object().contains("instant_finality_extension")); + BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["generation"], 1); + BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["threshold"], 1); + BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"].size(), 1u); + BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"][size_t(0)]["description"], "test_desc"); + BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"][size_t(0)]["weight"], 1); + BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_dEvut0ydHevDGP6Ef3O4Iq6QXf9jUcMUT1nCJRX-JRYlFYrO_qKt_x439vUJ2DkZ32Od6AdJZ-S9dWRE9Sy-7Q6bNjpoIOP0cWzkKC1DqmhfE3paW-KThA3noLkV8SsILcfxpQ"); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinalizer"_n, "test"_n, mvo() From 921e850e656a1f68870db9e8ae92d5d588d8405a Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Mon, 1 Apr 2024 13:33:20 -0400 Subject: [PATCH 126/158] Add missing inline specifier to several methonds in crypto_bls_ext.hpp --- libraries/eosiolib/core/eosio/crypto_bls_ext.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 9c143e60b7..26f9fc82f5 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -368,7 +368,7 @@ namespace detail { const inline std::vector GT_ONE = []{ std::vector r(576, 0); r[0] = 1; return r; }(); // Construct an extensible-output function based on SHA256 - void xmd_sh256( + inline void xmd_sh256( char *buf, int buf_len, const char *in, @@ -415,7 +415,7 @@ namespace detail { } } - bls_s scalar_fromBE(const bls_s& in) { + inline bls_s scalar_fromBE(const bls_s& in) { bls_s out; constexpr size_t last_index = sizeof(out) - 1; for(size_t i = 0; i <= last_index; ++i) @@ -425,7 +425,7 @@ namespace detail { return out; } - void g2_fromMessage(std::span msg, const std::string& dst, bls_g2& res) { + inline void g2_fromMessage(std::span msg, const std::string& dst, bls_g2& res) { std::array buf; xmd_sh256(buf.data()->data(), sizeof(buf), msg.data(), msg.size(), dst.data(), dst.length()); @@ -465,7 +465,7 @@ namespace detail { } // pubkey and signature are assumed to be in RAW affine little-endian bytes - bool bls_pop_verify(const bls_g1& pubkey, const bls_g2& signature_proof) { + inline bool bls_pop_verify(const bls_g1& pubkey, const bls_g2& signature_proof) { using namespace detail; bls_g1 g1_points[2] = {0}; @@ -484,7 +484,7 @@ namespace detail { } // pubkey and signature are assumed to be in RAW affine little-endian bytes - bool bls_signature_verify(const bls_g1& pubkey, const bls_g2& signature_proof, const std::string& msg) { + inline bool bls_signature_verify(const bls_g1& pubkey, const bls_g2& signature_proof, const std::string& msg) { bls_g1 g1_points[2]; bls_g2 g2_points[2]; From 77190299319e162193bbe11b4a06af8c94cebc00 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 1 May 2024 17:45:26 -0500 Subject: [PATCH 127/158] GH-276 Use spring instead of leap for integration tests --- .cicd/defaults.json | 4 +-- .github/workflows/build.yaml | 40 ++++++++++++------------- README.md | 10 +++---- modules/TestsExternalProject.txt | 6 ++-- tests/CMakeLists.txt | 2 +- tests/integration/CMakeLists.txt | 10 +++---- tests/integration/multi_index_tests.cpp | 2 +- 7 files changed, 37 insertions(+), 37 deletions(-) diff --git a/.cicd/defaults.json b/.cicd/defaults.json index 49400aa35c..caf5c75be9 100644 --- a/.cicd/defaults.json +++ b/.cicd/defaults.json @@ -1,6 +1,6 @@ { - "leap-dev":{ - "target":"hotstuff_integration", + "spring-dev":{ + "target":"main", "prerelease":false } } \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index c40b699527..a294427ff0 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -8,12 +8,12 @@ on: pull_request: workflow_dispatch: inputs: - override-leap-dev: - description: Override leap-dev target + override-spring-dev: + description: Override spring-dev target type: string - override-leap-dev-prerelease: + override-spring-dev-prerelease: type: choice - description: Override leap-dev prelease + description: Override spring-dev prelease options: - default - true @@ -69,8 +69,8 @@ jobs: name: Determine Versions runs-on: ubuntu-latest outputs: - leap-dev-target: ${{steps.versions.outputs.leap-dev-target}} - leap-dev-prerelease: ${{steps.versions.outputs.leap-dev-prerelease}} + spring-dev-target: ${{steps.versions.outputs.spring-dev-target}} + spring-dev-prerelease: ${{steps.versions.outputs.spring-dev-prerelease}} steps: - name: Setup versions from input or defaults id: versions @@ -78,14 +78,14 @@ jobs: GH_TOKEN: ${{github.token}} run: | DEFAULTS_JSON=$(curl -sSfL $(gh api https://api.github.com/repos/${{github.repository}}/contents/.cicd/defaults.json?ref=${{github.sha}} --jq .download_url)) - echo leap-dev-target=$(echo "$DEFAULTS_JSON" | jq -r '."leap-dev".target') >> $GITHUB_OUTPUT - echo leap-dev-prerelease=$(echo "$DEFAULTS_JSON" | jq -r '."leap-dev".prerelease') >> $GITHUB_OUTPUT + echo spring-dev-target=$(echo "$DEFAULTS_JSON" | jq -r '."spring-dev".target') >> $GITHUB_OUTPUT + echo spring-dev-prerelease=$(echo "$DEFAULTS_JSON" | jq -r '."spring-dev".prerelease') >> $GITHUB_OUTPUT - if [[ "${{inputs.override-leap-dev}}" != "" ]]; then - echo leap-dev-target=${{inputs.override-leap-dev}} >> $GITHUB_OUTPUT + if [[ "${{inputs.override-spring-dev}}" != "" ]]; then + echo spring-dev-target=${{inputs.override-spring-dev}} >> $GITHUB_OUTPUT fi - if [[ "${{inputs.override-leap-dev-prerelease}}" == +(true|false) ]]; then - echo leap-dev-prerelease=${{inputs.override-leap-dev-prerelease}} >> $GITHUB_OUTPUT + if [[ "${{inputs.override-spring-dev-prerelease}}" == +(true|false) ]]; then + echo spring-dev-prerelease=${{inputs.override-spring-dev-prerelease}} >> $GITHUB_OUTPUT fi Build: @@ -102,22 +102,22 @@ jobs: - uses: actions/checkout@v3 with: submodules: recursive - - name: Download leap-dev.deb (Ubuntu 22 only) + - name: Download spring-dev.deb (Ubuntu 22 only) if: matrix.platform == 'ubuntu22' uses: AntelopeIO/asset-artifact-download-action@v3 with: owner: AntelopeIO - repo: leap - file: 'leap-dev.*ubuntu22\.04_amd64.deb' - target: '${{needs.versions.outputs.leap-dev-target}}' - prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}} - artifact-name: leap-dev-ubuntu22-amd64 + repo: spring + file: 'spring-dev.*ubuntu22\.04_amd64.deb' + target: '${{needs.versions.outputs.spring-dev-target}}' + prereleases: ${{fromJSON(needs.versions.outputs.spring-dev-prerelease)}} + artifact-name: spring-dev-ubuntu22-amd64 container-package: experimental-binaries - - name: Install leap-dev.deb (Ubuntu 22 only) + - name: Install spring-dev.deb (Ubuntu 22 only) if: matrix.platform == 'ubuntu22' run: | apt-get update && apt-get upgrade -y - apt install -y ./leap-dev*.deb + apt install -y ./spring-dev*.deb rm ./leap-dev*.deb - name: Build & Test run: | diff --git a/README.md b/README.md index b226330e54..52158283d2 100644 --- a/README.md +++ b/README.md @@ -54,19 +54,19 @@ python3 -m pip install pygments ### Allowing integration tests to build -Integration tests require access to a build of [Leap](https://github.com/AntelopeIO/leap), a C++ implementation of the Antelope protocol. Simply installing Leap from a binary package will not be sufficient. +Integration tests require access to a build of [Spring](https://github.com/AntelopeIO/spring), a C++ implementation of the Antelope protocol. Simply installing Spring from a binary package will not be sufficient. -If you do not wish to build Leap, you can continue with building CDT but without building the integration tests. Otherwise, follow the instructions below before running `cmake`. +If you do not wish to build Spring, you can continue with building CDT but without building the integration tests. Otherwise, follow the instructions below before running `cmake`. -First, ensure that Leap has been built from source (see Leap's [README](https://github.com/AntelopeIO/leap#building-from-source) for details) and identify the build path, e.g. `/path/to/leap/build/`. +First, ensure that Spring has been built from source (see Spring's [README](https://github.com/AntelopeIO/spring#building-from-source) for details) and identify the build path, e.g. `/path/to/spring/build/`. Then, execute the following command in the same terminal session that you will use to build CDT: ```sh -export leap_DIR=/path/to/leap/build/lib/cmake/leap +export spring_DIR=/path/to/spring/build/lib/cmake/spring ``` -Now you can continue with the steps to build CDT as described. When you run `cmake` make sure that it does not report `leap package not found`. If it does, this means CDT was not able to find a build of Leap at the specified path in `leap_DIR` and will therefore continue without building the integration tests. +Now you can continue with the steps to build CDT as described. When you run `cmake` make sure that it does not report `spring package not found`. If it does, this means CDT was not able to find a build of Leap at the specified path in `spring_DIR` and will therefore continue without building the integration tests. ### ccache diff --git a/modules/TestsExternalProject.txt b/modules/TestsExternalProject.txt index f22a55564e..8b6d479df3 100644 --- a/modules/TestsExternalProject.txt +++ b/modules/TestsExternalProject.txt @@ -16,9 +16,9 @@ ExternalProject_Add( ) -find_package(leap QUIET) +find_package(spring QUIET) -if (leap_FOUND) +if (spring_FOUND) if(CMAKE_BUILD_TYPE STREQUAL "Debug") set(TEST_BUILD_TYPE "Debug") else() @@ -40,5 +40,5 @@ if (leap_FOUND) BUILD_ALWAYS 1 ) else() - message(STATUS "leap package not found, skipping building integration tests") + message(STATUS "spring package not found, skipping building integration tests") endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bcb4e052b0..e699d9afd3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,7 +29,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/unit/version_tests.sh ${CMAKE_BINARY_ add_test(NAME version_tests COMMAND ${CMAKE_BINARY_DIR}/tests/unit/version_tests.sh "${VERSION_FULL}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) set_property(TEST version_tests PROPERTY LABELS unit_tests) -if (leap_FOUND) +if (spring_FOUND) add_test(integration_tests ${CMAKE_BINARY_DIR}/tests/integration/integration_tests) set_property(TEST integration_tests PROPERTY LABELS integration_tests) endif() diff --git a/tests/integration/CMakeLists.txt b/tests/integration/CMakeLists.txt index cd2b3fec8b..be19469465 100644 --- a/tests/integration/CMakeLists.txt +++ b/tests/integration/CMakeLists.txt @@ -1,10 +1,10 @@ cmake_minimum_required( VERSION 3.5 ) -set(EOSIO_VERSION_MIN "3.1") +set(EOSIO_VERSION_MIN "1.0") set(EOSIO_VERSION_SOFT_MAX "5.0") #set(EOSIO_VERSION_HARD_MAX "") -find_package(leap) +find_package(spring) find_path(GMP_INCLUDE_DIR NAMES gmp.h) find_library(GMP_LIBRARY gmp) @@ -17,11 +17,11 @@ EOSIO_CHECK_VERSION(VERSION_OUTPUT "${EOSIO_VERSION}" "${EOSIO_VERSION_HARD_MAX}" VERSION_MATCH_ERROR_MSG) if(VERSION_OUTPUT STREQUAL "MATCH") - message(STATUS "Using Leap version ${EOSIO_VERSION}") + message(STATUS "Using Spring version ${EOSIO_VERSION}") elseif(VERSION_OUTPUT STREQUAL "WARN") - message(WARNING "Using Leap version ${EOSIO_VERSION} even though it exceeds the maximum supported version of ${EOSIO_VERSION_SOFT_MAX}; continuing with configuration, however build may fail.\nIt is recommended to use Leap version ${EOSIO_VERSION_SOFT_MAX}.x") + message(WARNING "Using Spring version ${EOSIO_VERSION} even though it exceeds the maximum supported version of ${EOSIO_VERSION_SOFT_MAX}; continuing with configuration, however build may fail.\nIt is recommended to use Spring version ${EOSIO_VERSION_SOFT_MAX}.x") else() # INVALID OR MISMATCH - message(FATAL_ERROR "Found Leap version ${EOSIO_VERSION} but it does not satisfy version requirements: ${VERSION_MATCH_ERROR_MSG}\nPlease use Leap version ${EOSIO_VERSION_SOFT_MAX}.x") + message(FATAL_ERROR "Found Spring version ${EOSIO_VERSION} but it does not satisfy version requirements: ${VERSION_MATCH_ERROR_MSG}\nPlease use Spring version ${EOSIO_VERSION_SOFT_MAX}.x") endif(VERSION_OUTPUT STREQUAL "MATCH") diff --git a/tests/integration/multi_index_tests.cpp b/tests/integration/multi_index_tests.cpp index c5420a77ff..e53fbdecfd 100644 --- a/tests/integration/multi_index_tests.cpp +++ b/tests/integration/multi_index_tests.cpp @@ -18,7 +18,7 @@ using namespace eosio::testing; BOOST_AUTO_TEST_SUITE(multi_index_tests) -// this test is copy from leap test_api_multi_index +// this test is copy from spring test_api_multi_index BOOST_FIXTURE_TEST_CASE(main_multi_index_tests, TESTER) { try { produce_blocks(1); create_account( "testapi"_n ); From 7906f2a2fbfb754cdff6b48201a6b5a31ae94803 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 1 May 2024 17:55:33 -0500 Subject: [PATCH 128/158] GH-276 Use spring instead of leap for integration tests --- .github/workflows/build.yaml | 2 +- README.md | 2 +- docs/07_best-practices/08_abi/00_understanding-abi-files.md | 2 +- docs/09_tutorials/03_create-an-abi-file.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a294427ff0..dcfd22ab9b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -118,7 +118,7 @@ jobs: run: | apt-get update && apt-get upgrade -y apt install -y ./spring-dev*.deb - rm ./leap-dev*.deb + rm ./spring-dev*.deb - name: Build & Test run: | mkdir build diff --git a/README.md b/README.md index 52158283d2..a21fbcd0db 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Then, execute the following command in the same terminal session that you will u export spring_DIR=/path/to/spring/build/lib/cmake/spring ``` -Now you can continue with the steps to build CDT as described. When you run `cmake` make sure that it does not report `spring package not found`. If it does, this means CDT was not able to find a build of Leap at the specified path in `spring_DIR` and will therefore continue without building the integration tests. +Now you can continue with the steps to build CDT as described. When you run `cmake` make sure that it does not report `spring package not found`. If it does, this means CDT was not able to find a build of Spring at the specified path in `spring_DIR` and will therefore continue without building the integration tests. ### ccache diff --git a/docs/07_best-practices/08_abi/00_understanding-abi-files.md b/docs/07_best-practices/08_abi/00_understanding-abi-files.md index a5b33165a1..e63c794c1d 100644 --- a/docs/07_best-practices/08_abi/00_understanding-abi-files.md +++ b/docs/07_best-practices/08_abi/00_understanding-abi-files.md @@ -35,7 +35,7 @@ Start with an empty ABI, for exemplification we will work based on the `eosio.to An ABI enables any client or interface to interpret and even generate a GUI for your contract. For this to work consistently, describe the custom types that are used as a parameter in any public action or struct that needs to be described in the ABI. [[info | Built-in Types]] -| Antelope implements a number of custom built-ins. Built-in types don't need to be described in an ABI file. If you would like to familiarize yourself with Antelope's built-ins, they are defined [here](https://github.com/AntelopeIO/leap/blob/6817911900a088c60f91563995cf482d6b380b2d/libraries/chain/abi_serializer.cpp#L88-L129) +| Antelope implements a number of custom built-ins. Built-in types don't need to be described in an ABI file. If you would like to familiarize yourself with Antelope's built-ins, they are defined [here](https://github.com/AntelopeIO/spring/blob/5a3550a6fec4c1865e8aca07aa97693f720afe72/libraries/chain/abi_serializer.cpp#L92-L130) ```json diff --git a/docs/09_tutorials/03_create-an-abi-file.md b/docs/09_tutorials/03_create-an-abi-file.md index d82a57eb3e..199e78ecff 100644 --- a/docs/09_tutorials/03_create-an-abi-file.md +++ b/docs/09_tutorials/03_create-an-abi-file.md @@ -35,7 +35,7 @@ An ABI enables any client or interface to interpret and even generate an GUI for [[info]] |Built-in Types -Antelope implements a number of custom built-ins. Built-in types don't need to be described in an ABI file. If you would like to familiarize yourself with Antelope's built-ins, they are defined [here](https://github.com/AntelopeIO/leap/blob/6817911900a088c60f91563995cf482d6b380b2d/libraries/chain/abi_serializer.cpp#L88-L129). +Antelope implements a number of custom built-ins. Built-in types don't need to be described in an ABI file. If you would like to familiarize yourself with Antelope's built-ins, they are defined [here](https://github.com/AntelopeIO/spring/blob/5a3550a6fec4c1865e8aca07aa97693f720afe72/libraries/chain/abi_serializer.cpp#L92-L130). Using **eosio.token** as an example, the only type that requires a description in the ABI file is `account_name`. The ABI uses "new_type_name" to describe explicit types, in this case `account_name`, and `account_name` is an alias of `name` type. From e610d1b14ef553afad767f7457450082f79908a3 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 1 May 2024 19:52:20 -0500 Subject: [PATCH 129/158] Update for set_finalizers host function now takes a packed format --- libraries/eosiolib/contracts/eosio/instant_finality.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/instant_finality.hpp b/libraries/eosiolib/contracts/eosio/instant_finality.hpp index 004efdca75..af9a4bf275 100644 --- a/libraries/eosiolib/contracts/eosio/instant_finality.hpp +++ b/libraries/eosiolib/contracts/eosio/instant_finality.hpp @@ -17,7 +17,7 @@ namespace eosio { namespace internal_use_do_not_use { extern "C" { __attribute__((eosio_wasm_import)) - void set_finalizers( const char* data, uint32_t len ); + void set_finalizers( uint64_t packed_finalizer_format, const char* data, uint32_t len ); } // extern "C" } //internal_use_do_not_use @@ -44,7 +44,7 @@ namespace eosio { for (const auto& finalizer : finalizer_policy.finalizers) eosio::check(finalizer.public_key.size() == sizeof(bls_g1), "public key has a wrong size" ); auto packed = eosio::pack(finalizer_policy); - internal_use_do_not_use::set_finalizers(packed.data(), packed.size()); + internal_use_do_not_use::set_finalizers(0, packed.data(), packed.size()); } } //eosio From 01b3b6e7da21fe2eb88b586b599afa811da34064 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Thu, 2 May 2024 08:10:56 -0500 Subject: [PATCH 130/158] GH-276 Update comment --- tests/integration/multi_index_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/multi_index_tests.cpp b/tests/integration/multi_index_tests.cpp index e53fbdecfd..919ced1194 100644 --- a/tests/integration/multi_index_tests.cpp +++ b/tests/integration/multi_index_tests.cpp @@ -18,7 +18,7 @@ using namespace eosio::testing; BOOST_AUTO_TEST_SUITE(multi_index_tests) -// this test is copy from spring test_api_multi_index +// this test is copied from Spring test_api_multi_index BOOST_FIXTURE_TEST_CASE(main_multi_index_tests, TESTER) { try { produce_blocks(1); create_account( "testapi"_n ); From 99600bb8d6ef8901e67f29eea711fc27b3170573 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Thu, 2 May 2024 08:12:40 -0500 Subject: [PATCH 131/158] Add comment --- libraries/eosiolib/contracts/eosio/instant_finality.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/eosiolib/contracts/eosio/instant_finality.hpp b/libraries/eosiolib/contracts/eosio/instant_finality.hpp index af9a4bf275..f1bd97a3fa 100644 --- a/libraries/eosiolib/contracts/eosio/instant_finality.hpp +++ b/libraries/eosiolib/contracts/eosio/instant_finality.hpp @@ -44,6 +44,7 @@ namespace eosio { for (const auto& finalizer : finalizer_policy.finalizers) eosio::check(finalizer.public_key.size() == sizeof(bls_g1), "public key has a wrong size" ); auto packed = eosio::pack(finalizer_policy); + // 0 is packed format, currently only 0 is supported internal_use_do_not_use::set_finalizers(0, packed.data(), packed.size()); } From 7ef58b4c6511400656a5f8ac9c23f5bf9dfda8a7 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Thu, 2 May 2024 11:07:16 -0700 Subject: [PATCH 132/158] version bump supporting Savanna and Spring release --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c53deefe9..875d85e600 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,9 +14,9 @@ endif() project(cdt) set(VERSION_MAJOR 4) -set(VERSION_MINOR 0) -set(VERSION_PATCH 1) -set(VERSION_SUFFIX "") +set(VERSION_MINOR 1) +set(VERSION_PATCH 0) +set(VERSION_SUFFIX "rc1") if (VERSION_SUFFIX) set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}") From e40b7d35ebb63b5809cbd3aa30f53266294b9bee Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Thu, 2 May 2024 11:10:20 -0700 Subject: [PATCH 133/158] version bump next version after spring release --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 875d85e600..71cda7a167 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,8 @@ project(cdt) set(VERSION_MAJOR 4) set(VERSION_MINOR 1) -set(VERSION_PATCH 0) -set(VERSION_SUFFIX "rc1") +set(VERSION_PATCH 1) +set(VERSION_SUFFIX "") if (VERSION_SUFFIX) set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}") From c538d947a5012dfcd418047c9b99d4488271adec Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Thu, 2 May 2024 13:15:03 -0500 Subject: [PATCH 134/158] Update c interface of set_finalizers --- libraries/eosiolib/capi/eosio/instant_finality.h | 5 +++-- libraries/native/intrinsics.cpp | 4 ++-- tests/unit/test_contracts/capi/privileged.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/instant_finality.h b/libraries/eosiolib/capi/eosio/instant_finality.h index 1ed3d2c05a..9cabc260b3 100644 --- a/libraries/eosiolib/capi/eosio/instant_finality.h +++ b/libraries/eosiolib/capi/eosio/instant_finality.h @@ -12,14 +12,15 @@ extern "C" { /** * Submits a finalizer policy change to Instant Finality - * + * + * @param packed_finalizer_format - format of the finalizer_policy object, currently only supports 0 * @param data - pointer finalizer_policy object packed as bytes * @param len - size of packed finalazer_policy object * @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_policy data * abi_finalizer_policy structure is defined in instant_finality.hpp */ __attribute__((eosio_wasm_import)) -void set_finalizers( const char* data, uint32_t len ); +void set_finalizers( uint64_t packed_finalizer_format, const char* data, uint32_t len ); #ifdef __cplusplus } diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index 1d4c8570de..3f9ed470ea 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -906,8 +906,8 @@ extern "C" { return intrinsics::get().call(data, datalen); } - void set_finalizers(const char* data, uint32_t len) { - intrinsics::get().call(data, len); + void set_finalizers(uint64_t packed_finalizer_format, const char* data, uint32_t len) { + intrinsics::get().call(packed_finalizer_format, data, len); } } // extern "C" diff --git a/tests/unit/test_contracts/capi/privileged.c b/tests/unit/test_contracts/capi/privileged.c index e1131c918a..de780e982e 100644 --- a/tests/unit/test_contracts/capi/privileged.c +++ b/tests/unit/test_contracts/capi/privileged.c @@ -12,5 +12,5 @@ void test_privileged( void ) { set_blockchain_parameters_packed(NULL, 0); get_blockchain_parameters_packed(NULL, 0); preactivate_feature(NULL); - set_finalizers(NULL, 0); + set_finalizers(0, NULL, 0); } From bdba2fa7ce7a182acbe059fb73c767812e80f58b Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Mon, 6 May 2024 09:59:16 -0700 Subject: [PATCH 135/158] updated artifact download links --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a21fbcd0db..63b7744754 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ CDT currently supports Linux x86_64 Debian packages. Visit the [release page](ht Download the appropriate version of the Debian package and then install it. To download and install the latest version, run the following: ```sh -wget https://github.com/AntelopeIO/cdt/releases/download/v4.0.1/cdt_4.0.1_amd64.deb -sudo apt install ./cdt_4.0.1_amd64.deb +wget https://github.com/AntelopeIO/cdt/releases/download/v4.1.0-rc1/cdt_4.1.0-rc1_amd64.deb +sudo apt install ./cdt_4.1.0_amd64-rc1.deb ``` ### Debian package uninstall @@ -28,9 +28,9 @@ sudo apt remove cdt ## Building from source -Recent Ubuntu LTS releases are the only Linux distributions that we fully support. Other Linux distros and other POSIX operating systems (such as macOS) are tended to on a best-effort basis and may not be full featured. +Recent Ubuntu LTS releases are the only Linux distributions that we fully support. Other Linux distros and other POSIX operating systems (such as macOS) are tended to on a best-effort basis and may not be full featured. -The instructions below assume that you are building on Ubuntu 20.04. +The instructions below assume that you are building on Ubuntu 20.04. ### Install dependencies @@ -136,7 +136,7 @@ Installing CDT globally on your system will install the following tools in a loc * cdt-strip * eosio-pp * eosio-wasm2wast -* eosio-wast2wasm +* eosio-wast2wasm It will also install CMake files for CDT accessible within a `cmake/cdt` directory located within your system's `lib` directory. #### Manual installation From a306d507f10d798d7adf6a0bee2d871fa925317e Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Mon, 6 May 2024 10:33:17 -0700 Subject: [PATCH 136/158] corrected version fixed dangling rc1 reference --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 63b7744754..5557c5ec5a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Download the appropriate version of the Debian package and then install it. To d ```sh wget https://github.com/AntelopeIO/cdt/releases/download/v4.1.0-rc1/cdt_4.1.0-rc1_amd64.deb -sudo apt install ./cdt_4.1.0_amd64-rc1.deb +sudo apt install ./cdt_4.1.0-rc1_amd64.deb ``` ### Debian package uninstall From a95b1d4936911a63abaa6c61d8590e1fd643bc49 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Thu, 2 May 2024 13:15:03 -0500 Subject: [PATCH 137/158] Update c interface of set_finalizers --- libraries/eosiolib/capi/eosio/instant_finality.h | 5 +++-- libraries/native/intrinsics.cpp | 4 ++-- tests/unit/test_contracts/capi/privileged.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/instant_finality.h b/libraries/eosiolib/capi/eosio/instant_finality.h index 1ed3d2c05a..9cabc260b3 100644 --- a/libraries/eosiolib/capi/eosio/instant_finality.h +++ b/libraries/eosiolib/capi/eosio/instant_finality.h @@ -12,14 +12,15 @@ extern "C" { /** * Submits a finalizer policy change to Instant Finality - * + * + * @param packed_finalizer_format - format of the finalizer_policy object, currently only supports 0 * @param data - pointer finalizer_policy object packed as bytes * @param len - size of packed finalazer_policy object * @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_policy data * abi_finalizer_policy structure is defined in instant_finality.hpp */ __attribute__((eosio_wasm_import)) -void set_finalizers( const char* data, uint32_t len ); +void set_finalizers( uint64_t packed_finalizer_format, const char* data, uint32_t len ); #ifdef __cplusplus } diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index 1d4c8570de..3f9ed470ea 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -906,8 +906,8 @@ extern "C" { return intrinsics::get().call(data, datalen); } - void set_finalizers(const char* data, uint32_t len) { - intrinsics::get().call(data, len); + void set_finalizers(uint64_t packed_finalizer_format, const char* data, uint32_t len) { + intrinsics::get().call(packed_finalizer_format, data, len); } } // extern "C" diff --git a/tests/unit/test_contracts/capi/privileged.c b/tests/unit/test_contracts/capi/privileged.c index e1131c918a..de780e982e 100644 --- a/tests/unit/test_contracts/capi/privileged.c +++ b/tests/unit/test_contracts/capi/privileged.c @@ -12,5 +12,5 @@ void test_privileged( void ) { set_blockchain_parameters_packed(NULL, 0); get_blockchain_parameters_packed(NULL, 0); preactivate_feature(NULL); - set_finalizers(NULL, 0); + set_finalizers(0, NULL, 0); } From f5e7976a9d95800fe40b0425f0e24b00f12e6a25 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 6 May 2024 13:43:38 -0400 Subject: [PATCH 138/158] upload ubuntu20 package --- .github/workflows/build.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index dcfd22ab9b..52698c1574 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -127,13 +127,13 @@ jobs: make -j $(nproc) cd tests ctest -j $(nproc) --output-on-failure - - name: Package (Ubuntu 22 only) - if: matrix.platform == 'ubuntu22' + - name: Package (Ubuntu 20 only) + if: matrix.platform == 'ubuntu20' run: | cd build/packages bash generate_package.sh deb ubuntu amd64 - - name: Upload (Ubuntu 22 only) - if: matrix.platform == 'ubuntu22' + - name: Upload (Ubuntu 20 only) + if: matrix.platform == 'ubuntu20' uses: actions/upload-artifact@v3 with: name: cdt_ubuntu_package_amd64 From 0ab129eb93addcfcc6203f0bbed6d71351c9afb3 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 6 May 2024 13:47:30 -0400 Subject: [PATCH 139/158] ubuntu24 build --- .cicd/platforms.json | 3 +++ .cicd/platforms/ubuntu24.Dockerfile | 12 ++++++++++++ .github/workflows/build.yaml | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .cicd/platforms/ubuntu24.Dockerfile diff --git a/.cicd/platforms.json b/.cicd/platforms.json index c1528f30f0..a58fb76146 100644 --- a/.cicd/platforms.json +++ b/.cicd/platforms.json @@ -7,5 +7,8 @@ }, "ubuntu22-llvm": { "dockerfile": ".cicd/platforms/ubuntu22-llvm.Dockerfile" + }, + "ubuntu24": { + "dockerfile": ".cicd/platforms/ubuntu24.Dockerfile" } } diff --git a/.cicd/platforms/ubuntu24.Dockerfile b/.cicd/platforms/ubuntu24.Dockerfile new file mode 100644 index 0000000000..de1681197c --- /dev/null +++ b/.cicd/platforms/ubuntu24.Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:noble + +RUN apt-get update && apt-get upgrade -y && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential \ + cmake \ + git \ + ninja-build \ + python3 \ + pkg-config \ + libboost-all-dev \ + libcurl4-gnutls-dev \ + clang-tidy diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index dcfd22ab9b..c47d14967e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -95,7 +95,7 @@ jobs: strategy: fail-fast: false matrix: - platform: [ubuntu20, ubuntu22, ubuntu22-llvm] + platform: [ubuntu20, ubuntu22, ubuntu22-llvm, ubuntu24] runs-on: ["self-hosted", "enf-x86-beefy"] container: ${{fromJSON(needs.d.outputs.p)[matrix.platform].image}} steps: From 698c54c00e245cf0654e2efcddfed450b2e2186f Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:42:15 -0400 Subject: [PATCH 140/158] use antelope-spring-dev.deb --- .cicd/defaults.json | 2 +- .github/workflows/build.yaml | 42 ++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/.cicd/defaults.json b/.cicd/defaults.json index caf5c75be9..9e15d6be2d 100644 --- a/.cicd/defaults.json +++ b/.cicd/defaults.json @@ -1,5 +1,5 @@ { - "spring-dev":{ + "antelope-spring-dev":{ "target":"main", "prerelease":false } diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 8629bb5285..40d96477a4 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -8,12 +8,12 @@ on: pull_request: workflow_dispatch: inputs: - override-spring-dev: - description: Override spring-dev target + override-antelope-spring-dev: + description: Override antelope-spring-dev target type: string - override-spring-dev-prerelease: + override-antelope-spring-dev-prerelease: type: choice - description: Override spring-dev prelease + description: Override antelope-spring-dev prelease options: - default - true @@ -69,8 +69,8 @@ jobs: name: Determine Versions runs-on: ubuntu-latest outputs: - spring-dev-target: ${{steps.versions.outputs.spring-dev-target}} - spring-dev-prerelease: ${{steps.versions.outputs.spring-dev-prerelease}} + antelope-spring-dev-target: ${{steps.versions.outputs.antelope-spring-dev-target}} + antelope-spring-dev-prerelease: ${{steps.versions.outputs.antelope-spring-dev-prerelease}} steps: - name: Setup versions from input or defaults id: versions @@ -78,14 +78,14 @@ jobs: GH_TOKEN: ${{github.token}} run: | DEFAULTS_JSON=$(curl -sSfL $(gh api https://api.github.com/repos/${{github.repository}}/contents/.cicd/defaults.json?ref=${{github.sha}} --jq .download_url)) - echo spring-dev-target=$(echo "$DEFAULTS_JSON" | jq -r '."spring-dev".target') >> $GITHUB_OUTPUT - echo spring-dev-prerelease=$(echo "$DEFAULTS_JSON" | jq -r '."spring-dev".prerelease') >> $GITHUB_OUTPUT + echo antelope-spring-dev-target=$(echo "$DEFAULTS_JSON" | jq -r '."antelope-spring-dev".target') >> $GITHUB_OUTPUT + echo antelope-spring-dev-prerelease=$(echo "$DEFAULTS_JSON" | jq -r '."antelope-spring-dev".prerelease') >> $GITHUB_OUTPUT - if [[ "${{inputs.override-spring-dev}}" != "" ]]; then - echo spring-dev-target=${{inputs.override-spring-dev}} >> $GITHUB_OUTPUT + if [[ "${{inputs.override-antelope-spring-dev}}" != "" ]]; then + echo antelope-spring-dev-target=${{inputs.override-antelope-spring-dev}} >> $GITHUB_OUTPUT fi - if [[ "${{inputs.override-spring-dev-prerelease}}" == +(true|false) ]]; then - echo spring-dev-prerelease=${{inputs.override-spring-dev-prerelease}} >> $GITHUB_OUTPUT + if [[ "${{inputs.override-antelope-spring-dev-prerelease}}" == +(true|false) ]]; then + echo antelope-spring-dev-prerelease=${{inputs.override-antelope-spring-dev-prerelease}} >> $GITHUB_OUTPUT fi Build: @@ -102,23 +102,23 @@ jobs: - uses: actions/checkout@v3 with: submodules: recursive - - name: Download spring-dev.deb (Ubuntu 22 only) + - name: Download antelope-spring-dev.deb (Ubuntu 22 only) if: matrix.platform == 'ubuntu22' uses: AntelopeIO/asset-artifact-download-action@v3 with: owner: AntelopeIO repo: spring - file: 'spring-dev.*ubuntu22\.04_amd64.deb' - target: '${{needs.versions.outputs.spring-dev-target}}' - prereleases: ${{fromJSON(needs.versions.outputs.spring-dev-prerelease)}} - artifact-name: spring-dev-ubuntu22-amd64 - container-package: experimental-binaries - - name: Install spring-dev.deb (Ubuntu 22 only) + file: 'antelope-spring-dev.*ubuntu22\.04_amd64.deb' + target: '${{needs.versions.outputs.antelope-spring-dev-target}}' + prereleases: ${{fromJSON(needs.versions.outputs.antelope-spring-dev-prerelease)}} + artifact-name: antelope-spring-dev-ubuntu22-amd64 + container-package: antelope-spring-experimental-binaries + - name: Install antelope-spring-dev.deb (Ubuntu 22 only) if: matrix.platform == 'ubuntu22' run: | apt-get update && apt-get upgrade -y - apt install -y ./spring-dev*.deb - rm ./spring-dev*.deb + apt install -y ./antelope-spring-dev*.deb + rm ./antelope-spring-dev*.deb - name: Build & Test run: | mkdir build From 0b2c5eb90924cf5a1ce3f8330f196d0f6e2e0f0a Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Fri, 14 Jun 2024 17:57:10 -0400 Subject: [PATCH 141/158] Update instant_finality_tests to match Spring's change of instant_finality_extension format and make the tests less flaky --- tests/integration/instant_finality_tests.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/integration/instant_finality_tests.cpp b/tests/integration/instant_finality_tests.cpp index b5367da24d..4b07792db1 100644 --- a/tests/integration/instant_finality_tests.cpp +++ b/tests/integration/instant_finality_tests.cpp @@ -34,12 +34,13 @@ BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); std::cout << fc::json::to_string(pretty_output, fc::time_point::now() + abi_serializer_max_time) << std::endl; BOOST_REQUIRE(pretty_output.get_object().contains("instant_finality_extension")); - BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["generation"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["threshold"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"].size(), 1u); - BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"][size_t(0)]["description"], "test_desc"); - BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"][size_t(0)]["weight"], 1); - BOOST_REQUIRE_EQUAL(pretty_output["instant_finality_extension"]["new_finalizer_policy"]["finalizers"][size_t(0)]["public_key"], "PUB_BLS_dEvut0ydHevDGP6Ef3O4Iq6QXf9jUcMUT1nCJRX-JRYlFYrO_qKt_x439vUJ2DkZ32Od6AdJZ-S9dWRE9Sy-7Q6bNjpoIOP0cWzkKC1DqmhfE3paW-KThA3noLkV8SsILcfxpQ"); + + std::string output_json = fc::json::to_pretty_string(pretty_output); + BOOST_TEST(output_json.find("\"generation\": 2") != std::string::npos); + BOOST_TEST(output_json.find("\"threshold\": 1") != std::string::npos); + BOOST_TEST(output_json.find("\"description\": \"test_desc\"") != std::string::npos); + BOOST_TEST(output_json.find("\"weight\": 1") != std::string::npos); + BOOST_TEST(output_json.find("PUB_BLS_dEvut0ydHevDGP6Ef3O4Iq6QXf9jUcMUT1nCJRX-JRYlFYrO_qKt_x439vUJ2DkZ32Od6AdJZ-S9dWRE9Sy-7Q6bNjpoIOP0cWzkKC1DqmhfE3paW-KThA3noLkV8SsILcfxpQ") != std::string::npos); // testing wrong public key size BOOST_CHECK_THROW(push_action(config::system_account_name, "setfinalizer"_n, "test"_n, mvo() From c250347804e03a5f689bdf69b4c731e83c4edb98 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Fri, 14 Jun 2024 20:12:07 -0400 Subject: [PATCH 142/158] keep README's package link at 4.0.1 until 4.1.0 is released --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5557c5ec5a..9eb44d2d27 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ CDT currently supports Linux x86_64 Debian packages. Visit the [release page](ht Download the appropriate version of the Debian package and then install it. To download and install the latest version, run the following: ```sh -wget https://github.com/AntelopeIO/cdt/releases/download/v4.1.0-rc1/cdt_4.1.0-rc1_amd64.deb -sudo apt install ./cdt_4.1.0-rc1_amd64.deb +wget https://github.com/AntelopeIO/cdt/releases/download/v4.0.1/cdt_4.0.1_amd64.deb +sudo apt install ./cdt_4.0.1_amd64.deb ``` ### Debian package uninstall From a88c7b8f56f6410b7310d146400fb42bafdd4bb6 Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 20 Jun 2024 13:15:58 +0300 Subject: [PATCH 143/158] Appropriate methods were marked as const --- libraries/eosiolib/contracts/eosio/singleton.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/eosiolib/contracts/eosio/singleton.hpp b/libraries/eosiolib/contracts/eosio/singleton.hpp index 17f70aa08c..a9625b43ef 100644 --- a/libraries/eosiolib/contracts/eosio/singleton.hpp +++ b/libraries/eosiolib/contracts/eosio/singleton.hpp @@ -62,7 +62,7 @@ namespace eosio { * @return true - if exists * @return false - otherwise */ - bool exists() { + bool exists() const { return _t.find( pk_value ) != _t.end(); } @@ -72,7 +72,7 @@ namespace eosio { * @brief Get the value stored inside the singleton table * @return T - The value stored */ - T get() { + T get() const { auto itr = _t.find( pk_value ); eosio::check( itr != _t.end(), "singleton does not exist" ); return itr->value; @@ -84,7 +84,7 @@ namespace eosio { * @param def - The default value to be returned in case the data doesn't exist * @return T - The value stored */ - T get_or_default( const T& def = T() ) { + T get_or_default( const T& def = T() ) const { auto itr = _t.find( pk_value ); return itr != _t.end() ? itr->value : def; } From 40681c728347ae5fd1a6a2252ee1e4de4db5a1f4 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:53:46 -0400 Subject: [PATCH 144/158] remove security group stuff --- .../eosiolib/capi/eosio/security_group.h | 56 ------------ .../contracts/eosio/security_group.hpp | 86 ------------------- libraries/native/intrinsics.cpp | 16 ---- .../native/native/eosio/intrinsics_def.hpp | 5 -- 4 files changed, 163 deletions(-) delete mode 100644 libraries/eosiolib/capi/eosio/security_group.h delete mode 100644 libraries/eosiolib/contracts/eosio/security_group.hpp diff --git a/libraries/eosiolib/capi/eosio/security_group.h b/libraries/eosiolib/capi/eosio/security_group.h deleted file mode 100644 index 9e362c63e6..0000000000 --- a/libraries/eosiolib/capi/eosio/security_group.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once -#include "types.h" -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Propose new participants to the security group. - * - * @param data - the buffer containing the packed participants. - * @param datalen - size of the packed participants - * @pre `data` is a valid pointer to a range of memory at least `datalen` bytes long that contains packed participants data - * - * @return -1 if proposing a new security group was unsuccessful, otherwise returns 0. -*/ -__attribute__((eosio_wasm_import)) -int64_t add_security_group_participants(const char* data, uint32_t datalen); - -/** - * Propose to remove participants from the security group. - * - * @param data - the buffer containing the packed participants. - * @param datalen - size of the packed participants - * @pre `data` is a valid pointer to a range of memory at least `datalen` bytes long that contains packed participants data - * - * @return -1 if proposing a new security group was unsuccessful, otherwise returns 0. -*/ -__attribute__((eosio_wasm_import)) -int64_t remove_security_group_participants(const char* data, uint32_t datalen); - -/** - * Check if the specified accounts are all in the active security group. - * - * @param data - the buffer containing the packed participants. - * @param datalen - size of the packed participants - * - * @return Returns true if the specified accounts are all in the active security group. -*/ -__attribute__((eosio_wasm_import)) -bool in_active_security_group(const char* data, uint32_t datalen); - -/** - * Gets the active security group - * - * @param[out] data - the output buffer containing the packed security group. - * @param datalen - size of the `data` buffer - * - * @return Returns the size required in the buffer (if the buffer is too small, nothing is written). - * -*/ -__attribute__((eosio_wasm_import)) -uint32_t get_active_security_group(char* data, uint32_t datalen); - -#ifdef __cplusplus -} -#endif diff --git a/libraries/eosiolib/contracts/eosio/security_group.hpp b/libraries/eosiolib/contracts/eosio/security_group.hpp deleted file mode 100644 index b5575b8835..0000000000 --- a/libraries/eosiolib/contracts/eosio/security_group.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once -#include -#include "../../core/eosio/name.hpp" -#include "../../core/eosio/serialize.hpp" - -namespace eosio { - -namespace internal_use_do_not_use { -extern "C" { -__attribute__((eosio_wasm_import)) int64_t add_security_group_participants(const char* data, uint32_t datalen); - -__attribute__((eosio_wasm_import)) int64_t remove_security_group_participants(const char* data, uint32_t datalen); - -__attribute__((eosio_wasm_import)) bool in_active_security_group(const char* data, uint32_t datalen); - -__attribute__((eosio_wasm_import)) uint32_t get_active_security_group(char* data, uint32_t datalen); -} -} // namespace internal_use_do_not_use - -/** - * @defgroup security_group Security Group - * @ingroup contracts - * @brief Defines C++ security group API - */ - -struct security_group { - uint32_t version; - std::set participants; - CDT_REFLECT(version, participants); -}; - -/** - * Propose new participants to the security group. - * - * @ingroup security_group - * @param participants - the participants. - * - * @return -1 if proposing a new security group was unsuccessful, otherwise returns 0. - */ -inline int64_t add_security_group_participants(const std::set& participants) { - auto packed_participants = eosio::pack( participants ); - return internal_use_do_not_use::add_security_group_participants( packed_participants.data(), packed_participants.size() ); -} - -/** - * Propose to remove participants from the security group. - *å - * @ingroup security_group - * @param participants - the participants. - *å - * @return -1 if proposing a new security group was unsuccessful, otherwise returns 0. - */ -inline int64_t remove_security_group_participants(const std::set& participants){ - auto packed_participants = eosio::pack( participants ); - return internal_use_do_not_use::remove_security_group_participants( packed_participants.data(), packed_participants.size() ); -} - -/** - * Check if the specified accounts are all in the active security group. - * - * @ingroup security_group - * @param participants - the participants. - * - * @return Returns true if the specified accounts are all in the active security group. - */ -inline bool in_active_security_group(const std::set& participants){ - auto packed_participants = eosio::pack( participants ); - return internal_use_do_not_use::in_active_security_group( packed_participants.data(), packed_participants.size() ); -} - -/** - * Gets the active security group - * - * @ingroup security_group - * @param[out] packed_security_group - the buffer containing the packed security_group. - * - * @return Returns the size required in the buffer (if the buffer is too small, nothing is written). - * - */ -inline security_group get_active_security_group() { - size_t buffer_size = internal_use_do_not_use::get_active_security_group(0, 0); - std::vector buffer(buffer_size); - internal_use_do_not_use::get_active_security_group(buffer.data(), buffer_size); - return eosio::unpack(buffer); -} -} // namespace eosio \ No newline at end of file diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index 3f9ed470ea..c28f7c574f 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -890,22 +890,6 @@ extern "C" { } #pragma clang diagnostic pop - int64_t add_security_group_participants(const char* data, uint32_t datalen) { - return intrinsics::get().call(data, datalen); - } - - int64_t remove_security_group_participants(const char* data, uint32_t datalen){ - return intrinsics::get().call(data, datalen); - } - - bool in_active_security_group(const char* data, uint32_t datalen){ - return intrinsics::get().call(data, datalen); - } - - uint32_t get_active_security_group(char* data, uint32_t datalen){ - return intrinsics::get().call(data, datalen); - } - void set_finalizers(uint64_t packed_finalizer_format, const char* data, uint32_t len) { intrinsics::get().call(packed_finalizer_format, data, len); } diff --git a/libraries/native/native/eosio/intrinsics_def.hpp b/libraries/native/native/eosio/intrinsics_def.hpp index 5121017dda..27b0aa3300 100644 --- a/libraries/native/native/eosio/intrinsics_def.hpp +++ b/libraries/native/native/eosio/intrinsics_def.hpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -164,10 +163,6 @@ intrinsic_macro(cancel_deferred) \ intrinsic_macro(get_context_free_data) \ intrinsic_macro(get_sender) \ intrinsic_macro(set_action_return_value) \ -intrinsic_macro(add_security_group_participants) \ -intrinsic_macro(remove_security_group_participants) \ -intrinsic_macro(in_active_security_group) \ -intrinsic_macro(get_active_security_group) \ intrinsic_macro(blake2_f) \ intrinsic_macro(sha3) \ intrinsic_macro(k1_recover) \ From 93ddaaa5e3659388676ac5261a7fe79ebc811e46 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 1 Jul 2024 11:48:29 -0400 Subject: [PATCH 145/158] update various CI workflow action versions --- .cicd/platforms.json | 14 ---------- .github/workflows/build.yaml | 54 +++++++++--------------------------- 2 files changed, 13 insertions(+), 55 deletions(-) delete mode 100644 .cicd/platforms.json diff --git a/.cicd/platforms.json b/.cicd/platforms.json deleted file mode 100644 index a58fb76146..0000000000 --- a/.cicd/platforms.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "ubuntu20": { - "dockerfile": ".cicd/platforms/ubuntu20.Dockerfile" - }, - "ubuntu22": { - "dockerfile": ".cicd/platforms/ubuntu22.Dockerfile" - }, - "ubuntu22-llvm": { - "dockerfile": ".cicd/platforms/ubuntu22-llvm.Dockerfile" - }, - "ubuntu24": { - "dockerfile": ".cicd/platforms/ubuntu24.Dockerfile" - } -} diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 40d96477a4..88068cea7c 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -28,42 +28,15 @@ defaults: shell: bash jobs: - d: - name: Discover Platforms - runs-on: ubuntu-latest - outputs: - missing-platforms: ${{steps.discover.outputs.missing-platforms}} - p: ${{steps.discover.outputs.platforms}} - steps: - - name: Discover Platforms - id: discover - uses: AntelopeIO/discover-platforms-action@v1 - with: - platform-file: .cicd/platforms.json - password: ${{secrets.GITHUB_TOKEN}} - package-name: builders - build-platforms: - name: Build Platforms - needs: d - if: needs.d.outputs.missing-platforms != '[]' - strategy: - fail-fast: false - matrix: - platform: ${{fromJSON(needs.d.outputs.missing-platforms)}} - runs-on: ["self-hosted", "enf-x86-beefy"] - steps: - - name: Login to Container Registry - uses: docker/login-action@v2 - with: - registry: ghcr.io - username: ${{github.repository_owner}} - password: ${{secrets.GITHUB_TOKEN}} - - name: Build and push - uses: docker/build-push-action@v3 - with: - push: true - tags: ${{fromJSON(needs.d.outputs.p)[matrix.platform].image}} - file: ${{fromJSON(needs.d.outputs.p)[matrix.platform].dockerfile}} + platform-cache: + name: Platform Cache + uses: AntelopeIO/platform-cache-workflow/.github/workflows/platformcache.yaml@v1 + permissions: + packages: write + contents: read + with: + runs-on: '["self-hosted", "enf-x86-beefy"]' + platform-files: .cicd/platforms versions: name: Determine Versions @@ -90,16 +63,15 @@ jobs: Build: name: Build & Test - needs: [d, build-platforms, versions] - if: always() && needs.d.result == 'success' && (needs.build-platforms.result == 'success' || needs.build-platforms.result == 'skipped') + needs: [platform-cache, versions] strategy: fail-fast: false matrix: platform: [ubuntu20, ubuntu22, ubuntu22-llvm, ubuntu24] runs-on: ["self-hosted", "enf-x86-beefy"] - container: ${{fromJSON(needs.d.outputs.p)[matrix.platform].image}} + container: ${{fromJSON(needs.platform-cache.outputs.platforms)[matrix.platform].image}} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive - name: Download antelope-spring-dev.deb (Ubuntu 22 only) @@ -134,7 +106,7 @@ jobs: bash generate_package.sh deb ubuntu amd64 - name: Upload (Ubuntu 20 only) if: matrix.platform == 'ubuntu20' - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: cdt_ubuntu_package_amd64 path: build/packages/cdt*amd64.deb From bc90b7170a53cd82e6ee4038758e8e833c0da037 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Mon, 15 Jul 2024 07:26:52 -0500 Subject: [PATCH 146/158] instant_finality_extension renamed to finality_extension --- tests/integration/instant_finality_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/instant_finality_tests.cpp b/tests/integration/instant_finality_tests.cpp index 4b07792db1..f79ef71125 100644 --- a/tests/integration/instant_finality_tests.cpp +++ b/tests/integration/instant_finality_tests.cpp @@ -33,9 +33,9 @@ BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { fc::variant pretty_output; abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); std::cout << fc::json::to_string(pretty_output, fc::time_point::now() + abi_serializer_max_time) << std::endl; - BOOST_REQUIRE(pretty_output.get_object().contains("instant_finality_extension")); std::string output_json = fc::json::to_pretty_string(pretty_output); + BOOST_TEST(output_json.find("finality_extension") != std::string::npos); BOOST_TEST(output_json.find("\"generation\": 2") != std::string::npos); BOOST_TEST(output_json.find("\"threshold\": 1") != std::string::npos); BOOST_TEST(output_json.find("\"description\": \"test_desc\"") != std::string::npos); From 8a164d06bf39282ad0f38db5fc2ad0797460c314 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Mon, 15 Jul 2024 07:26:52 -0500 Subject: [PATCH 147/158] instant_finality_extension renamed to finality_extension --- tests/integration/instant_finality_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/instant_finality_tests.cpp b/tests/integration/instant_finality_tests.cpp index 4b07792db1..f79ef71125 100644 --- a/tests/integration/instant_finality_tests.cpp +++ b/tests/integration/instant_finality_tests.cpp @@ -33,9 +33,9 @@ BOOST_FIXTURE_TEST_CASE(instant_finality_test, tester) try { fc::variant pretty_output; abi_serializer::to_variant( *cur_block, pretty_output, get_resolver(), fc::microseconds::maximum() ); std::cout << fc::json::to_string(pretty_output, fc::time_point::now() + abi_serializer_max_time) << std::endl; - BOOST_REQUIRE(pretty_output.get_object().contains("instant_finality_extension")); std::string output_json = fc::json::to_pretty_string(pretty_output); + BOOST_TEST(output_json.find("finality_extension") != std::string::npos); BOOST_TEST(output_json.find("\"generation\": 2") != std::string::npos); BOOST_TEST(output_json.find("\"threshold\": 1") != std::string::npos); BOOST_TEST(output_json.find("\"description\": \"test_desc\"") != std::string::npos); From 6ffc9c985af28b6883e3aa2e9f9c09646f26810f Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Fri, 16 Aug 2024 14:22:16 -0700 Subject: [PATCH 148/158] bump 4.1 branch to rc2 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 875d85e600..c759618115 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ project(cdt) set(VERSION_MAJOR 4) set(VERSION_MINOR 1) set(VERSION_PATCH 0) -set(VERSION_SUFFIX "rc1") +set(VERSION_SUFFIX "rc2") if (VERSION_SUFFIX) set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}") From afdcd5269fe094eec2e43ae47c8f4a052e002a5b Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Fri, 16 Aug 2024 14:24:46 -0700 Subject: [PATCH 149/158] merge version bump commit and reset version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71cda7a167..cd7ac04cf5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ project(cdt) set(VERSION_MAJOR 4) set(VERSION_MINOR 1) set(VERSION_PATCH 1) -set(VERSION_SUFFIX "") +set(VERSION_SUFFIX "dev") if (VERSION_SUFFIX) set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}") From 76de7e8b9af5d40ba2835dab98782b92be1526b8 Mon Sep 17 00:00:00 2001 From: Guillaume Babin-Tremblay Date: Thu, 22 Aug 2024 06:59:42 -0600 Subject: [PATCH 150/158] Update crypto_bls_ext.hpp --- libraries/eosiolib/core/eosio/crypto_bls_ext.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp index 26f9fc82f5..87236f7a92 100644 --- a/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp +++ b/libraries/eosiolib/core/eosio/crypto_bls_ext.hpp @@ -346,7 +346,6 @@ namespace detail { auto csum = ripemd160(ret.data(), ret.size()).extract_as_byte_array(); eosio::check(0 == memcmp(&*it, csum.data(), bls_checksum_size), "checksum of structure doesn't match"); - printhex(static_cast(ret.data()), ret.size()); return ret; } From 387c2b862446e1d1390d67b3e6d2962af5901382 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:24:00 -0400 Subject: [PATCH 151/158] disable CFL Alias Analysis by default --- tools/include/compiler_options.hpp.in | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index 0547701ce1..fa6772c93c 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -80,9 +80,9 @@ static cl::opt fno_lto_opt( "fno-lto", cl::desc("Disable LTO"), cl::cat(LD_CAT)); -static cl::opt fno_cfl_aa_opt( - "fno-cfl-aa", - cl::desc("Disable CFL Alias Analysis"), +static cl::opt fcfl_aa_opt( + "fcfl-aa", + cl::desc("Enable CFL Alias Analysis"), cl::cat(LD_CAT)); static cl::opt fno_stack_first_opt( "fno-stack-first", @@ -617,7 +617,7 @@ static Options CreateOptions(bool add_defaults=true) { else pp_dir = eosio::cdt::whereami::where(); - if (!fno_cfl_aa_opt) { + if (fcfl_aa_opt) { copts.emplace_back("-mllvm"); copts.emplace_back("-use-cfl-aa-in-codegen=both"); agopts.emplace_back("-mllvm"); From bec0d6cac97b29dd464326abff396624f2350baa Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Wed, 21 Aug 2024 21:08:37 -0400 Subject: [PATCH 152/158] remove now removed -fno-cfl-aa from test builds --- tests/unit/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 4407c51898..450c3ed4d0 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -5,7 +5,6 @@ include( CDTMacros ) macro(add_cdt_unit_test TEST_NAME) add_native_executable(${TEST_NAME} ${TEST_NAME}.cpp) - target_compile_options(${TEST_NAME} PRIVATE -fno-cfl-aa) if(CMAKE_BUILD_TYPE STREQUAL "Release") target_compile_options(${TEST_NAME} PRIVATE -O2) elseif(CMAKE_BUILD_TYPE STREQUAL "Debug") From 29f4232238b85d0831c805583f199bffffe20aee Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Thu, 22 Aug 2024 13:50:14 -0400 Subject: [PATCH 153/158] change docs for no-cfl-aa removal --- docs/03_command-reference/cdt-cc.md | 2 +- docs/03_command-reference/cdt-cpp.md | 2 +- docs/03_command-reference/cdt-ld.md | 2 +- docs/man/cdt-cc.1.md | 4 ++-- docs/man/cdt-cpp.1.md | 4 ++-- docs/man/cdt-ld.1.md | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/03_command-reference/cdt-cc.md b/docs/03_command-reference/cdt-cc.md index 913e940067..49bcbfe22e 100644 --- a/docs/03_command-reference/cdt-cc.md +++ b/docs/03_command-reference/cdt-cc.md @@ -47,7 +47,7 @@ compiler options: -finline-hint-functions - Inline functions which are (explicitly or implicitly) marked inline -fmerge-all-constants - Allow merging of constants -fnative - Compile and link for x86-64 - -fno-cfl-aa - Disable CFL Alias Analysis + -fcfl-aa - Enable CFL Alias Analysis -fno-elide-constructors - Disable C++ copy constructor elision -fno-lto - Disable LTO -fno-post-pass - Don't run post processing pass diff --git a/docs/03_command-reference/cdt-cpp.md b/docs/03_command-reference/cdt-cpp.md index 382f4f4857..3942513cd1 100644 --- a/docs/03_command-reference/cdt-cpp.md +++ b/docs/03_command-reference/cdt-cpp.md @@ -49,7 +49,7 @@ compiler options: -finline-hint-functions - Inline functions which are (explicitly or implicitly) marked inline -fmerge-all-constants - Allow merging of constants -fnative - Compile and link for x86-64 - -fno-cfl-aa - Disable CFL Alias Analysis + -cfl-aa - Enable CFL Alias Analysis -fno-elide-constructors - Disable C++ copy constructor elision -fno-lto - Disable LTO -fno-post-pass - Don't run post processing pass diff --git a/docs/03_command-reference/cdt-ld.md b/docs/03_command-reference/cdt-ld.md index f4339e91d2..8e3b2daa98 100644 --- a/docs/03_command-reference/cdt-ld.md +++ b/docs/03_command-reference/cdt-ld.md @@ -21,7 +21,7 @@ ld options: -L= - Add directory to library search path -fasm - Assemble file for x86-64 -fnative - Compile and link for x86-64 - -fno-cfl-aa - Disable CFL Alias Analysis + -fcfl-aa - Enable CFL Alias Analysis -fno-lto - Disable LTO -fno-post-pass - Don't run post processing pass -fno-stack-first - Don't set the stack first in memory diff --git a/docs/man/cdt-cc.1.md b/docs/man/cdt-cc.1.md index 96d4b21563..e9c670a906 100644 --- a/docs/man/cdt-cc.1.md +++ b/docs/man/cdt-cc.1.md @@ -167,9 +167,9 @@ execution in Antelope block chain virtual machines. Compile and link for x86-64 -**`--fno-cfl-aa`** +**`--fcfl-aa`** - Disable CFL Alias Analysis + Enable CFL Alias Analysis **`--fno-elide-constructors`** diff --git a/docs/man/cdt-cpp.1.md b/docs/man/cdt-cpp.1.md index 95587afd0a..8a2a07f396 100644 --- a/docs/man/cdt-cpp.1.md +++ b/docs/man/cdt-cpp.1.md @@ -187,9 +187,9 @@ execution in Antelope block chain virtual machines. Compile and link for x86-64 -**`--fno-cfl-aa`** +**`--fcfl-aa`** - Disable CFL Alias Analysis + Enable CFL Alias Analysis **`--fno-elide-constructors`** diff --git a/docs/man/cdt-ld.1.md b/docs/man/cdt-ld.1.md index 7063e871bc..ad5ec25ca9 100644 --- a/docs/man/cdt-ld.1.md +++ b/docs/man/cdt-ld.1.md @@ -39,9 +39,9 @@ execution in Antelope block chain virtual machines. Compile and link for x86-64 -**`--fno-cfl-aa`** +**`--fcfl-aa`** - Disable CFL Alias Analysis + Enable CFL Alias Analysis **`--fno-lto`** From 14684af1c10519138523705ef4c89461add07252 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Thu, 22 Aug 2024 12:01:39 -0700 Subject: [PATCH 154/158] bump cmake to v4.1.0-rc3 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c759618115..896bbfdfbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ project(cdt) set(VERSION_MAJOR 4) set(VERSION_MINOR 1) set(VERSION_PATCH 0) -set(VERSION_SUFFIX "rc2") +set(VERSION_SUFFIX "rc3") if (VERSION_SUFFIX) set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}") From 551179192c408be214d3124ada699f0295fc3f23 Mon Sep 17 00:00:00 2001 From: Eric Passmore Date: Tue, 3 Sep 2024 11:32:04 -0700 Subject: [PATCH 155/158] version bump v4.1.0 --- CMakeLists.txt | 2 +- README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 896bbfdfbb..d88b27bae9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ project(cdt) set(VERSION_MAJOR 4) set(VERSION_MINOR 1) set(VERSION_PATCH 0) -set(VERSION_SUFFIX "rc3") +# set(VERSION_SUFFIX "") if (VERSION_SUFFIX) set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}") diff --git a/README.md b/README.md index 5557c5ec5a..d150e3eccc 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ CDT currently supports Linux x86_64 Debian packages. Visit the [release page](ht Download the appropriate version of the Debian package and then install it. To download and install the latest version, run the following: ```sh -wget https://github.com/AntelopeIO/cdt/releases/download/v4.1.0-rc1/cdt_4.1.0-rc1_amd64.deb -sudo apt install ./cdt_4.1.0-rc1_amd64.deb +wget https://github.com/AntelopeIO/cdt/releases/download/v4.1.0/cdt_4.1.0_amd64.deb +sudo apt install ./cdt_4.1.0_amd64.deb ``` ### Debian package uninstall From cf1123f0d4d9cd3fdfa9b48c149e7e39808c2f07 Mon Sep 17 00:00:00 2001 From: Quoc Le Date: Mon, 23 Dec 2024 08:36:05 +0000 Subject: [PATCH 156/158] add rsa sha256 verify signature --- libraries/eosiolib/capi/eosio/crypto.h | 23 +++++++++++++++++++++++ libraries/eosiolib/crypto.cpp | 18 ++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/libraries/eosiolib/capi/eosio/crypto.h b/libraries/eosiolib/capi/eosio/crypto.h index fff817b79b..122bf60c6e 100644 --- a/libraries/eosiolib/capi/eosio/crypto.h +++ b/libraries/eosiolib/capi/eosio/crypto.h @@ -186,6 +186,29 @@ void sha512( const char* data, uint32_t length, struct capi_checksum512* hash ); __attribute__((eosio_wasm_import)) void ripemd160( const char* data, uint32_t length, struct capi_checksum160* hash ); +/** + * Verifies the RSA SHA-256 signature for a given message using the provided public key components (exponent and modulus). + * + * @param message - The original message that was signed. + * @param message_len - Length of the message in bytes. + * @param signature - The RSA signature to verify. + * @param signature_len - Length of the signature in bytes. + * @param exponent - The public key exponent. + * @param exponent_len - Length of the exponent in bytes. + * @param modulus - The public key modulus. + * @param modulus_len - Length of the modulus in bytes. + * @return int - Returns 1 if the signature is valid, 0 if invalid. + * + * Example: + * + * @code + * @endcode + */ +__attribute__((eosio_wasm_import)) +int verify_rsa_sha256_sig( const void* message, uint32_t message_len, + const char* signature, uint32_t signature_len, + const char* exponent, uint32_t exponent_len, + const char* modulus, uint32_t modulus_len); /** * Calculates the public key used for a given signature and hash used to create a message. * diff --git a/libraries/eosiolib/crypto.cpp b/libraries/eosiolib/crypto.cpp index bc96fbd0db..8ede6312de 100644 --- a/libraries/eosiolib/crypto.cpp +++ b/libraries/eosiolib/crypto.cpp @@ -35,6 +35,12 @@ extern "C" { __attribute__((eosio_wasm_import)) void ripemd160( const char* data, uint32_t length, capi_checksum160* hash ); + __attribute__((eosio_wasm_import)) + int verify_rsa_sha256_sig( const void* message, uint32_t message_len, + const char* signature, uint32_t signature_len, + const char* exponent, uint32_t exponent_len, + const char* modulus, uint32_t modulus_len); + __attribute__((eosio_wasm_import)) int recover_key( const capi_checksum256* digest, const char* sig, size_t siglen, char* pub, size_t publen ); @@ -90,6 +96,18 @@ namespace eosio { return {hash.hash}; } + bool verify_rsa_sha256_sig( const void* message, + uint32_t message_len, + std::string_view signature, + std::string_view exponent, + std::string_view modulus) { + return ::verify_rsa_sha256_sig( + message, message_len, + signature.data(), signature.size(), + exponent.data(), exponent.size(), + modulus.data(), modulus.size()); + } + eosio::public_key recover_key( const eosio::checksum256& digest, const eosio::signature& sig ) { auto digest_data = digest.extract_as_byte_array(); From 1fc04fd8a5a4f6091bf0797f0a393852d8262d7a Mon Sep 17 00:00:00 2001 From: Quoc Le Date: Mon, 23 Dec 2024 10:25:34 +0000 Subject: [PATCH 157/158] add to crypto hpp interface --- libraries/eosiolib/core/eosio/crypto.hpp | 17 +++++++++++++++++ tests/unit/test_contracts/capi/crypto.c | 1 + 2 files changed, 18 insertions(+) diff --git a/libraries/eosiolib/core/eosio/crypto.hpp b/libraries/eosiolib/core/eosio/crypto.hpp index b7ca46a980..58053e2097 100644 --- a/libraries/eosiolib/core/eosio/crypto.hpp +++ b/libraries/eosiolib/core/eosio/crypto.hpp @@ -317,6 +317,23 @@ namespace eosio { */ eosio::checksum160 ripemd160( const char* data, uint32_t length ); + /** + * Verifies an RSA signature using SHA-256 hashing. + * + * @ingroup crypto + * @param message - Pointer to the message data to be verified + * @param message_len - Length of the message data + * @param signature - RSA signature to verify + * @param exponent - RSA public key exponent + * @param modulus - RSA public key modulus + * @return bool - `true` if the signature is valid, `false` otherwise + */ + bool verify_rsa_sha256_sig( const void* message, + uint32_t message_len, + std::string_view signature, + std::string_view exponent, + std::string_view modulus); + /** * Calculates the public key used for a given signature on a given digest. * diff --git a/tests/unit/test_contracts/capi/crypto.c b/tests/unit/test_contracts/capi/crypto.c index cbe7784c9b..f09f920f17 100644 --- a/tests/unit/test_contracts/capi/crypto.c +++ b/tests/unit/test_contracts/capi/crypto.c @@ -10,6 +10,7 @@ void test_crypto( void ) { sha1( NULL, 0, NULL ); sha512( NULL, 0, NULL ); ripemd160( NULL, 0, NULL ); + verify_rsa_sha256_sig( NULL, 0, NULL, 0, NULL, 0, NULL, 0); recover_key( NULL, NULL, 0, NULL, 0 ); assert_recover_key( NULL, NULL, 0, NULL, 0 ); } From 6804532a3c75cba8c5ceffe32ddf4f9b8cb2c94a Mon Sep 17 00:00:00 2001 From: Quoc Le Date: Tue, 24 Dec 2024 03:17:26 +0000 Subject: [PATCH 158/158] add to intrisics --- libraries/eosiolib/capi/eosio/crypto.h | 2 +- libraries/eosiolib/crypto.cpp | 2 +- libraries/native/intrinsics.cpp | 6 ++++++ libraries/native/native/eosio/intrinsics_def.hpp | 3 ++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/libraries/eosiolib/capi/eosio/crypto.h b/libraries/eosiolib/capi/eosio/crypto.h index 122bf60c6e..6ace6c1b74 100644 --- a/libraries/eosiolib/capi/eosio/crypto.h +++ b/libraries/eosiolib/capi/eosio/crypto.h @@ -205,7 +205,7 @@ void ripemd160( const char* data, uint32_t length, struct capi_checksum160* hash * @endcode */ __attribute__((eosio_wasm_import)) -int verify_rsa_sha256_sig( const void* message, uint32_t message_len, +int32_t verify_rsa_sha256_sig( const void* message, uint32_t message_len, const char* signature, uint32_t signature_len, const char* exponent, uint32_t exponent_len, const char* modulus, uint32_t modulus_len); diff --git a/libraries/eosiolib/crypto.cpp b/libraries/eosiolib/crypto.cpp index 8ede6312de..3acdf903a4 100644 --- a/libraries/eosiolib/crypto.cpp +++ b/libraries/eosiolib/crypto.cpp @@ -36,7 +36,7 @@ extern "C" { void ripemd160( const char* data, uint32_t length, capi_checksum160* hash ); __attribute__((eosio_wasm_import)) - int verify_rsa_sha256_sig( const void* message, uint32_t message_len, + int32_t verify_rsa_sha256_sig( const void* message, uint32_t message_len, const char* signature, uint32_t signature_len, const char* exponent, uint32_t exponent_len, const char* modulus, uint32_t modulus_len); diff --git a/libraries/native/intrinsics.cpp b/libraries/native/intrinsics.cpp index c28f7c574f..9d847b25fc 100644 --- a/libraries/native/intrinsics.cpp +++ b/libraries/native/intrinsics.cpp @@ -229,6 +229,12 @@ extern "C" { int32_t db_end_i64(capi_name code, uint64_t scope, capi_name table) { return intrinsics::get().call(code, scope, table); } + int32_t verify_rsa_sha256_sig( const void* message, uint32_t message_len, + const char* signature, uint32_t signature_len, + const char* exponent, uint32_t exponent_len, + const char* modulus, uint32_t modulus_len){ + return intrinsics::get().call(message, message_len, signature, signature_len, exponent, exponent_len, modulus, modulus_len); + } void assert_recover_key( const capi_checksum256* digest, const char* sig, size_t siglen, const char* pub, size_t publen ) { return intrinsics::get().call(digest, sig, siglen, pub, publen); } diff --git a/libraries/native/native/eosio/intrinsics_def.hpp b/libraries/native/native/eosio/intrinsics_def.hpp index 27b0aa3300..f4aee787ea 100644 --- a/libraries/native/native/eosio/intrinsics_def.hpp +++ b/libraries/native/native/eosio/intrinsics_def.hpp @@ -180,7 +180,8 @@ intrinsic_macro(bls_g2_map) \ intrinsic_macro(bls_fp_mod) \ intrinsic_macro(bls_fp_mul) \ intrinsic_macro(bls_fp_exp) \ -intrinsic_macro(set_finalizers) +intrinsic_macro(set_finalizers) \ +intrinsic_macro(verify_rsa_sha256_sig) #define CREATE_ENUM(name) \ name,