Skip to content

Commit bc52b10

Browse files
Merge branch 'main' into patch-1
2 parents 12c33d2 + c4384f4 commit bc52b10

File tree

32 files changed

+1739
-158
lines changed

32 files changed

+1739
-158
lines changed

.cicd/platforms/ubuntu18.Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FROM ubuntu:bionic
33
RUN apt-get update && apt-get upgrade -y && \
44
DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential \
55
cmake \
6+
g++-8 \
67
curl \
78
ninja-build \
89
software-properties-common \
@@ -15,4 +16,7 @@ RUN curl -L https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tgz | tar zx
1516
./configure --enable-optimizations --prefix=/usr && \
1617
make -j$(nproc) install && \
1718
cd .. && \
18-
rm -rf Python*
19+
rm -rf Python*
20+
21+
ENV CC=gcc-8
22+
ENV CXX=g++-8

.gitmodules

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

libraries/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ set(CMAKE_CXX_EXTENSIONS ON)
2121
add_subdirectory(libc)
2222
add_subdirectory(libc++)
2323
add_subdirectory(eosiolib)
24-
add_subdirectory(boost)
2524
add_subdirectory(rt)
2625

2726
if (ENABLE_NATIVE_COMPILER)

libraries/eosiolib/CMakeLists.txt

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,72 @@
1+
set(PARENT_PROJECT_NAME ${PROJECT_NAME})
2+
13
file(GLOB HEADERS "*.hpp"
24
"*.h")
35

4-
add_library(eosio
5-
eosiolib.cpp
6-
crypto.cpp
7-
${HEADERS})
8-
9-
add_library(eosio_malloc
10-
malloc.cpp
11-
${HEADERS})
12-
13-
add_library(eosio_dsm
14-
simple_malloc.cpp
15-
${HEADERS})
16-
17-
add_library(eosio_cmem
18-
memory.cpp
19-
${HEADERS})
20-
21-
22-
set_target_properties(eosio_malloc PROPERTIES LINKER_LANGUAGE C)
23-
24-
target_include_directories(eosio PUBLIC
25-
${CMAKE_SOURCE_DIR}/libc/cdt-musl/include
26-
${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/internal
27-
${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/crypt
28-
${CMAKE_SOURCE_DIR}/libc/cdt-musl/arch/eos
29-
${CMAKE_SOURCE_DIR}/libc++/cdt-libcxx/include
30-
${CMAKE_SOURCE_DIR}
31-
${CMAKE_SOURCE_DIR}/boost/include)
32-
33-
target_link_libraries( eosio c c++ )
34-
add_custom_command( TARGET eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio> ${BASE_BINARY_DIR}/lib )
35-
add_custom_command( TARGET eosio_malloc POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio_malloc> ${BASE_BINARY_DIR}/lib )
36-
add_custom_command( TARGET eosio_dsm POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio_dsm> ${BASE_BINARY_DIR}/lib )
37-
add_custom_command( TARGET eosio_cmem POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio_cmem> ${BASE_BINARY_DIR}/lib )
38-
39-
if (ENABLE_NATIVE_COMPILER)
40-
add_native_library(native_eosio
41-
eosiolib.cpp
42-
crypto.cpp
43-
malloc.cpp
44-
${HEADERS})
45-
46-
add_dependencies( native_eosio eosio )
47-
add_custom_command( TARGET native_eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:native_eosio> ${BASE_BINARY_DIR}/lib )
48-
endif()
49-
50-
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../eosiolib DESTINATION ${BASE_BINARY_DIR}/include FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
6+
if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools")
7+
project(native_eosio)
8+
add_library( ${PROJECT_NAME}
9+
eosiolib.cpp
10+
crypto.cpp
11+
malloc.cpp
12+
${HEADERS} )
13+
target_compile_definitions(${PROJECT_NAME} PUBLIC EOSIO_NATIVE EOSIOLIB_DISABLE_MALLOC)
14+
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core
15+
${CMAKE_SOURCE_DIR}/../libraries/meta_refl/include)
16+
target_compile_options(${PROJECT_NAME} PUBLIC -fPIC -fexceptions -fno-rtti)
17+
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
18+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
19+
target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes)
20+
else()
21+
target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=gnu::eosio_wasm_import)
22+
target_compile_options(${PROJECT_NAME} PUBLIC -Wno-attributes=eosio::ignore)
23+
endif()
24+
endif()
25+
else()
26+
add_library(eosio
27+
eosiolib.cpp
28+
crypto.cpp
29+
${HEADERS})
30+
31+
add_library(eosio_malloc
32+
malloc.cpp
33+
${HEADERS})
34+
35+
add_library(eosio_dsm
36+
simple_malloc.cpp
37+
${HEADERS})
38+
39+
add_library(eosio_cmem
40+
memory.cpp
41+
${HEADERS})
42+
43+
44+
set_target_properties(eosio_malloc PROPERTIES LINKER_LANGUAGE C)
45+
46+
target_include_directories(eosio PUBLIC
47+
${CMAKE_SOURCE_DIR}/libc/cdt-musl/include
48+
${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/internal
49+
${CMAKE_SOURCE_DIR}/libc/cdt-musl/src/crypt
50+
${CMAKE_SOURCE_DIR}/libc/cdt-musl/arch/eos
51+
${CMAKE_SOURCE_DIR}/libc++/cdt-libcxx/include
52+
${CMAKE_SOURCE_DIR})
53+
54+
target_link_libraries( eosio c c++ )
55+
add_custom_command( TARGET eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio> ${BASE_BINARY_DIR}/lib )
56+
add_custom_command( TARGET eosio_malloc POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio_malloc> ${BASE_BINARY_DIR}/lib )
57+
add_custom_command( TARGET eosio_dsm POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio_dsm> ${BASE_BINARY_DIR}/lib )
58+
add_custom_command( TARGET eosio_cmem POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio_cmem> ${BASE_BINARY_DIR}/lib )
59+
60+
if (ENABLE_NATIVE_COMPILER)
61+
add_native_library(native_eosio
62+
eosiolib.cpp
63+
crypto.cpp
64+
malloc.cpp
65+
${HEADERS})
66+
67+
add_dependencies( native_eosio eosio )
68+
add_custom_command( TARGET native_eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:native_eosio> ${BASE_BINARY_DIR}/lib )
69+
endif()
70+
71+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../eosiolib DESTINATION ${BASE_BINARY_DIR}/include FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
72+
endif()

0 commit comments

Comments
 (0)