diff --git a/Makefile b/Makefile index ad38a9ac..3a39d2fb 100644 --- a/Makefile +++ b/Makefile @@ -115,6 +115,9 @@ build/godwoken.h: ${PROTOCOL_SCHEMA_DIR}/godwoken.mol mkdir -p build ${MOLC} --language c --schema-file $< > $@ +fmt: + clang-format -i -style=Google c/**/*.* + clean: rm -rf build/* cd $(SECP_DIR) && [ -f "Makefile" ] && make distclean && make clean diff --git a/c/contracts.h b/c/contracts.h index 92fef881..3386d627 100644 --- a/c/contracts.h +++ b/c/contracts.h @@ -1,39 +1,40 @@ #ifndef CONTRACTS_H_ #define CONTRACTS_H_ -#include "sha256.h" -#include "ripemd160.h" #include "mbedtls/bignum.h" +#include "ripemd160.h" +#include "sha256.h" /* Protocol Params: - [Referenced]: https://github.com/ethereum/go-ethereum/blob/master/params/protocol_params.go + [Referenced]: + https://github.com/ethereum/go-ethereum/blob/master/params/protocol_params.go */ -#define SHA256_BASE_GAS 60 // Base price for a SHA256 operation -#define SHA256_PERWORD_GAS 12 // Per-word price for a SHA256 operation -#define RIPEMD160_BASE_GAS 600 // Base price for a RIPEMD160 operation -#define RIPEMD160_PERWORD_GAS 120 // Per-word price for a RIPEMD160 operation -#define IDENTITY_BASE_GAS 15 // Base price for a data copy operation -#define IDENTITY_PERWORD_GAS 3 // Per-work price for a data copy operation - -#define BLAKE2F_INPUT_LENGTH 213 -#define BLAKE2F_FINAL_BLOCK_BYTES 0x1 +#define SHA256_BASE_GAS 60 // Base price for a SHA256 operation +#define SHA256_PERWORD_GAS 12 // Per-word price for a SHA256 operation +#define RIPEMD160_BASE_GAS 600 // Base price for a RIPEMD160 operation +#define RIPEMD160_PERWORD_GAS 120 // Per-word price for a RIPEMD160 operation +#define IDENTITY_BASE_GAS 15 // Base price for a data copy operation +#define IDENTITY_PERWORD_GAS 3 // Per-work price for a data copy operation + +#define BLAKE2F_INPUT_LENGTH 213 +#define BLAKE2F_FINAL_BLOCK_BYTES 0x1 #define BLAKE2F_NON_FINAL_BLOCK_BYTES 0x0 -#define ERROR_MOD_EXP -23 -#define ERROR_BLAKE2F -24 +#define ERROR_MOD_EXP -23 +#define ERROR_BLAKE2F -24 /* pre-compiled Ethereum contracts */ -typedef int (*precompiled_contract_gas_fn)(const uint8_t *input_src, +typedef int (*precompiled_contract_gas_fn)(const uint8_t* input_src, const size_t input_size, - uint64_t *gas); -typedef int (*precompiled_contract_fn)(gw_context_t *ctx, - const uint8_t *input_src, + uint64_t* gas); +typedef int (*precompiled_contract_fn)(gw_context_t* ctx, + const uint8_t* input_src, const size_t input_size, - uint8_t **output, - size_t *output_size); + uint8_t** output, size_t* output_size); -int ecrecover_required_gas(const uint8_t *input, const size_t input_size, uint64_t *gas) { +int ecrecover_required_gas(const uint8_t* input, const size_t input_size, + uint64_t* gas) { // Elliptic curve sender recovery gas price *gas = 3000; return 0; @@ -49,11 +50,8 @@ int ecrecover_required_gas(const uint8_t *input, const size_t input_size, uint64 [64..96 ] => r (u256) [96..128] => s (u256) */ -int ecrecover(gw_context_t *ctx, - const uint8_t *input_src, - const size_t input_size, - uint8_t **output, size_t *output_size) { - +int ecrecover(gw_context_t* ctx, const uint8_t* input_src, + const size_t input_size, uint8_t** output, size_t* output_size) { int ret; secp256k1_context context; uint8_t secp_data[CKB_SECP256K1_DATA_SIZE]; @@ -72,7 +70,7 @@ int ecrecover(gw_context_t *ctx, for (size_t i = input_size; i < 128; i++) { input[i] = 0; } - for (int i = 32; i < 63; i ++) { + for (int i = 32; i < 63; i++) { if (input[i] != 0) { ckb_debug("input[32:63] not all zero!"); return -1; @@ -85,7 +83,8 @@ int ecrecover(gw_context_t *ctx, memcpy(signature_data, input + 64, 32); memcpy(signature_data + 32, input + 96, 32); secp256k1_ecdsa_recoverable_signature signature; - if (secp256k1_ecdsa_recoverable_signature_parse_compact(&context, &signature, signature_data, recid) == 0) { + if (secp256k1_ecdsa_recoverable_signature_parse_compact( + &context, &signature, signature_data, recid) == 0) { ckb_debug("parse signature failed"); return -1; } @@ -99,15 +98,14 @@ int ecrecover(gw_context_t *ctx, /* Check pubkey hash */ uint8_t temp[65]; size_t pubkey_size = 33; - if (secp256k1_ec_pubkey_serialize(&context, temp, - &pubkey_size, &pubkey, + if (secp256k1_ec_pubkey_serialize(&context, temp, &pubkey_size, &pubkey, SECP256K1_EC_COMPRESSED) != 1) { ckb_debug("public key serialize failed"); return -1; } union ethash_hash256 hash_result = ethash::keccak256(temp + 1, 64); - *output = (uint8_t *)malloc(32); + *output = (uint8_t*)malloc(32); if (*output == NULL) { return -1; } @@ -117,17 +115,16 @@ int ecrecover(gw_context_t *ctx, return 0; } -int sha256hash_required_gas(const uint8_t *input, const size_t input_size, uint64_t *gas) { - *gas = (uint64_t)(input_size + 31) / 32 * SHA256_PERWORD_GAS + SHA256_BASE_GAS; +int sha256hash_required_gas(const uint8_t* input, const size_t input_size, + uint64_t* gas) { + *gas = + (uint64_t)(input_size + 31) / 32 * SHA256_PERWORD_GAS + SHA256_BASE_GAS; return 0; } - -int sha256hash(gw_context_t *ctx, - const uint8_t *input_src, - const size_t input_size, - uint8_t **output, size_t *output_size) { - *output = (uint8_t *)malloc(32); +int sha256hash(gw_context_t* ctx, const uint8_t* input_src, + const size_t input_size, uint8_t** output, size_t* output_size) { + *output = (uint8_t*)malloc(32); if (*output == NULL) { return -1; } @@ -139,18 +136,17 @@ int sha256hash(gw_context_t *ctx, return 0; } - -int ripemd160hash_required_gas(const uint8_t *input, const size_t input_size, uint64_t *gas) { - *gas = (uint64_t)(input_size + 31) / 32 * RIPEMD160_PERWORD_GAS + RIPEMD160_BASE_GAS; +int ripemd160hash_required_gas(const uint8_t* input, const size_t input_size, + uint64_t* gas) { + *gas = (uint64_t)(input_size + 31) / 32 * RIPEMD160_PERWORD_GAS + + RIPEMD160_BASE_GAS; return 0; } - -int ripemd160hash(gw_context_t *ctx, - const uint8_t *input_src, - const size_t input_size, - uint8_t **output, size_t *output_size) { - *output = (uint8_t *)malloc(20); +int ripemd160hash(gw_context_t* ctx, const uint8_t* input_src, + const size_t input_size, uint8_t** output, + size_t* output_size) { + *output = (uint8_t*)malloc(20); if (*output == NULL) { return -1; } @@ -159,17 +155,16 @@ int ripemd160hash(gw_context_t *ctx, return 0; } -int data_copy_required_gas(const uint8_t *input, const size_t input_size, uint64_t *gas) { - *gas = (uint64_t)(input_size + 31) / 32 * IDENTITY_PERWORD_GAS + IDENTITY_BASE_GAS; +int data_copy_required_gas(const uint8_t* input, const size_t input_size, + uint64_t* gas) { + *gas = (uint64_t)(input_size + 31) / 32 * IDENTITY_PERWORD_GAS + + IDENTITY_BASE_GAS; return 0; } - -int data_copy(gw_context_t *ctx, - const uint8_t *input_src, - const size_t input_size, - uint8_t **output, size_t *output_size) { - *output = (uint8_t *)malloc(input_size); +int data_copy(gw_context_t* ctx, const uint8_t* input_src, + const size_t input_size, uint8_t** output, size_t* output_size) { + *output = (uint8_t*)malloc(input_size); if (*output == NULL) { return -1; } @@ -178,14 +173,9 @@ int data_copy(gw_context_t *ctx, return 0; } -int read_lens(const uint8_t *input, - const size_t input_size, - mbedtls_mpi *base_len, - mbedtls_mpi *exp_len, - mbedtls_mpi *mod_len, - size_t *base_size, - size_t *exp_size, - size_t *mod_size) { +int read_lens(const uint8_t* input, const size_t input_size, + mbedtls_mpi* base_len, mbedtls_mpi* exp_len, mbedtls_mpi* mod_len, + size_t* base_size, size_t* exp_size, size_t* mod_size) { int ret; mbedtls_mpi_init(base_len); mbedtls_mpi_init(exp_len); @@ -203,22 +193,26 @@ int read_lens(const uint8_t *input, return ERROR_MOD_EXP; } - ret = mbedtls_mpi_write_binary_le(base_len, (unsigned char *)(base_size), sizeof(size_t)); + ret = mbedtls_mpi_write_binary_le(base_len, (unsigned char*)(base_size), + sizeof(size_t)); if (ret != 0) { return ERROR_MOD_EXP; } - ret = mbedtls_mpi_write_binary_le(exp_len, (unsigned char *)(exp_size), sizeof(size_t)); + ret = mbedtls_mpi_write_binary_le(exp_len, (unsigned char*)(exp_size), + sizeof(size_t)); if (ret != 0) { return ERROR_MOD_EXP; } - ret = mbedtls_mpi_write_binary_le(mod_len, (unsigned char *)(mod_size), sizeof(size_t)); + ret = mbedtls_mpi_write_binary_le(mod_len, (unsigned char*)(mod_size), + sizeof(size_t)); if (ret != 0) { return ERROR_MOD_EXP; } return 0; } -// modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198 +// modexpMultComplexity implements bigModexp multComplexity formula, as defined +// in EIP-198 // // def mult_complexity(x): // if x <= 64: return x ** 2 @@ -236,7 +230,8 @@ uint128_t modexp_mult_complexity(uint128_t x) { } } -int big_mod_exp_required_gas(const uint8_t *input, const size_t input_size, uint64_t *target_gas) { +int big_mod_exp_required_gas(const uint8_t* input, const size_t input_size, + uint64_t* target_gas) { int ret; mbedtls_mpi base_len; mbedtls_mpi exp_len; @@ -244,20 +239,19 @@ int big_mod_exp_required_gas(const uint8_t *input, const size_t input_size, uint size_t base_size; size_t exp_size; size_t mod_size; - ret = read_lens(input, input_size, - &base_len, &exp_len, &mod_len, - &base_size, &exp_size, &mod_size); + ret = read_lens(input, input_size, &base_len, &exp_len, &mod_len, &base_size, + &exp_size, &mod_size); if (ret != 0) { return ERROR_MOD_EXP; } - const uint8_t *content = input_size > 96 ? input + 96 : NULL; + const uint8_t* content = input_size > 96 ? input + 96 : NULL; const size_t content_size = content != NULL ? input_size - 96 : 0; if (content_size < (base_size + exp_size + mod_size)) { return ERROR_MOD_EXP; } - // Retrieve the head 32 bytes of exp for the adjusted exponent length + // Retrieve the head 32 bytes of exp for the adjusted exponent length mbedtls_mpi exp_head; mbedtls_mpi_init(&exp_head); size_t exp_head_size = exp_size > 32 ? 32 : exp_size; @@ -265,7 +259,7 @@ int big_mod_exp_required_gas(const uint8_t *input, const size_t input_size, uint if (ret != 0) { return ERROR_MOD_EXP; } - // Calculate the adjusted exponent length + // Calculate the adjusted exponent length int msb = 0; int exp_head_bitlen = mbedtls_mpi_bitlen(&exp_head); if (exp_head_bitlen > 0) { @@ -287,12 +281,12 @@ int big_mod_exp_required_gas(const uint8_t *input, const size_t input_size, uint if (ret != 0) { return ERROR_MOD_EXP; } - // Calculate the gas cost of the operation + // Calculate the gas cost of the operation size_t base_gas = mod_size > base_size ? mod_size : base_size; uint128_t gas = modexp_mult_complexity((uint128_t)base_gas); mbedtls_mpi gas_big; mbedtls_mpi_init(&gas_big); - ret = mbedtls_mpi_read_binary_le(&gas_big, (unsigned char *)(&gas), 16); + ret = mbedtls_mpi_read_binary_le(&gas_big, (unsigned char*)(&gas), 16); if (ret != 0) { return ERROR_MOD_EXP; } @@ -310,7 +304,8 @@ int big_mod_exp_required_gas(const uint8_t *input, const size_t input_size, uint if (mbedtls_mpi_bitlen(&gas_big) > 64) { *target_gas = UINT64_MAX; } else { - ret = mbedtls_mpi_write_binary_le(&gas_big, (unsigned char *)(&target_gas), sizeof(target_gas)); + ret = mbedtls_mpi_write_binary_le(&gas_big, (unsigned char*)(&target_gas), + sizeof(target_gas)); if (ret != 0) { return ERROR_MOD_EXP; } @@ -318,11 +313,9 @@ int big_mod_exp_required_gas(const uint8_t *input, const size_t input_size, uint return 0; } - -int big_mod_exp(gw_context_t *ctx, - const uint8_t *input_src, - const size_t input_size, - uint8_t **output, size_t *output_size) { +int big_mod_exp(gw_context_t* ctx, const uint8_t* input_src, + const size_t input_size, uint8_t** output, + size_t* output_size) { int ret; mbedtls_mpi base_len; mbedtls_mpi exp_len; @@ -330,20 +323,20 @@ int big_mod_exp(gw_context_t *ctx, size_t base_size; size_t exp_size; size_t mod_size; - ret = read_lens(input_src, input_size, - &base_len, &exp_len, &mod_len, + ret = read_lens(input_src, input_size, &base_len, &exp_len, &mod_len, &base_size, &exp_size, &mod_size); if (ret != 0) { return ERROR_MOD_EXP; } - const uint8_t *content = input_size > 96 ? input_src + 96 : NULL; + const uint8_t* content = input_size > 96 ? input_src + 96 : NULL; const size_t content_size = content != NULL ? input_size - 96 : 0; if (content_size < (base_size + exp_size + mod_size)) { return ERROR_MOD_EXP; } - if (mbedtls_mpi_cmp_int(&base_len, 0) == 0 && mbedtls_mpi_cmp_int(&mod_len, 0) == 0) { + if (mbedtls_mpi_cmp_int(&base_len, 0) == 0 && + mbedtls_mpi_cmp_int(&mod_len, 0) == 0) { *output = NULL; *output_size = 0; return 0; @@ -388,30 +381,32 @@ int big_mod_exp(gw_context_t *ctx, return 0; } -static uint8_t precomputed[10][16] = {{0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15}, - {14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3}, - {11, 12, 5, 15, 8, 0, 2, 13, 10, 3, 7, 9, 14, 6, 1, 4}, - {7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8}, - {9, 5, 2, 10, 0, 7, 4, 15, 14, 11, 6, 3, 1, 12, 8, 13}, - {2, 6, 0, 8, 12, 10, 11, 3, 4, 7, 15, 1, 13, 5, 14, 9}, - {12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11}, - {13, 7, 12, 3, 11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10}, - {6, 14, 11, 0, 15, 9, 3, 8, 12, 13, 1, 10, 2, 7, 4, 5}, - {10, 8, 7, 1, 2, 4, 6, 5, 15, 9, 3, 13, 11, 14, 12, 0},}; -static uint64_t iv[8] = { 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, - 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, - 0x510e527fade682d1, 0x9b05688c2b3e6c1f, - 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,}; - -int blake2f_required_gas(const uint8_t *input, const size_t input_size, uint64_t *target_gas) { +static uint8_t precomputed[10][16] = { + {0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15}, + {14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3}, + {11, 12, 5, 15, 8, 0, 2, 13, 10, 3, 7, 9, 14, 6, 1, 4}, + {7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8}, + {9, 5, 2, 10, 0, 7, 4, 15, 14, 11, 6, 3, 1, 12, 8, 13}, + {2, 6, 0, 8, 12, 10, 11, 3, 4, 7, 15, 1, 13, 5, 14, 9}, + {12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11}, + {13, 7, 12, 3, 11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10}, + {6, 14, 11, 0, 15, 9, 3, 8, 12, 13, 1, 10, 2, 7, 4, 5}, + {10, 8, 7, 1, 2, 4, 6, 5, 15, 9, 3, 13, 11, 14, 12, 0}, +}; +static uint64_t iv[8] = { + 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, + 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, + 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, +}; + +int blake2f_required_gas(const uint8_t* input, const size_t input_size, + uint64_t* target_gas) { if (input_size != BLAKE2F_INPUT_LENGTH) { *target_gas = 0; return 0; } - uint32_t gas = ((uint32_t)input[0] << 24 - | (uint32_t)input[1] << 16 - | (uint32_t)input[2] << 8 - | (uint32_t)input[3] << 0); + uint32_t gas = ((uint32_t)input[0] << 24 | (uint32_t)input[1] << 16 | + (uint32_t)input[2] << 8 | (uint32_t)input[3] << 0); *target_gas = (uint64_t)gas; return 0; } @@ -419,15 +414,11 @@ int blake2f_required_gas(const uint8_t *input, const size_t input_size, uint64_t uint64_t rotate_left64(uint64_t x, int k) { size_t n = 64; size_t s = (size_t)(k) & (n - 1); - return x<>(n-s); + return x << s | x >> (n - s); } -void f_generic(uint64_t h[8], - uint64_t m[16], - uint64_t c0, - uint64_t c1, - uint64_t flag, - uint64_t rounds) { +void f_generic(uint64_t h[8], uint64_t m[16], uint64_t c0, uint64_t c1, + uint64_t flag, uint64_t rounds) { uint64_t v0 = h[0]; uint64_t v1 = h[1]; uint64_t v2 = h[2]; @@ -444,185 +435,182 @@ void f_generic(uint64_t h[8], uint64_t v13 = iv[5]; uint64_t v14 = iv[6]; uint64_t v15 = iv[7]; - v12 ^= c0; + v12 ^= c0; v13 ^= c1; v14 ^= flag; - for (uint64_t i = 0; i < rounds; i++) { - uint8_t *s = precomputed[i%10]; - - v0 += m[s[0]]; - v0 += v4; - v12 ^= v0; - v12 = rotate_left64(v12, -32); - v8 += v12; - v4 ^= v8; - v4 = rotate_left64(v4, -24); - v1 += m[s[1]]; - v1 += v5; - v13 ^= v1; - v13 = rotate_left64(v13, -32); - v9 += v13; - v5 ^= v9; - v5 = rotate_left64(v5, -24); - v2 += m[s[2]]; - v2 += v6; - v14 ^= v2; - v14 = rotate_left64(v14, -32); - v10 += v14; - v6 ^= v10; - v6 = rotate_left64(v6, -24); - v3 += m[s[3]]; - v3 += v7; - v15 ^= v3; - v15 = rotate_left64(v15, -32); - v11 += v15; - v7 ^= v11; - v7 = rotate_left64(v7, -24); - - v0 += m[s[4]]; - v0 += v4; - v12 ^= v0; - v12 = rotate_left64(v12, -16); - v8 += v12; - v4 ^= v8; - v4 = rotate_left64(v4, -63); - v1 += m[s[5]]; - v1 += v5; - v13 ^= v1; - v13 = rotate_left64(v13, -16); + for (uint64_t i = 0; i < rounds; i++) { + uint8_t* s = precomputed[i % 10]; + + v0 += m[s[0]]; + v0 += v4; + v12 ^= v0; + v12 = rotate_left64(v12, -32); + v8 += v12; + v4 ^= v8; + v4 = rotate_left64(v4, -24); + v1 += m[s[1]]; + v1 += v5; + v13 ^= v1; + v13 = rotate_left64(v13, -32); v9 += v13; v5 ^= v9; + v5 = rotate_left64(v5, -24); + v2 += m[s[2]]; + v2 += v6; + v14 ^= v2; + v14 = rotate_left64(v14, -32); + v10 += v14; + v6 ^= v10; + v6 = rotate_left64(v6, -24); + v3 += m[s[3]]; + v3 += v7; + v15 ^= v3; + v15 = rotate_left64(v15, -32); + v11 += v15; + v7 ^= v11; + v7 = rotate_left64(v7, -24); + + v0 += m[s[4]]; + v0 += v4; + v12 ^= v0; + v12 = rotate_left64(v12, -16); + v8 += v12; + v4 ^= v8; + v4 = rotate_left64(v4, -63); + v1 += m[s[5]]; + v1 += v5; + v13 ^= v1; + v13 = rotate_left64(v13, -16); + v9 += v13; + v5 ^= v9; + v5 = rotate_left64(v5, -63); + v2 += m[s[6]]; + v2 += v6; + v14 ^= v2; + v14 = rotate_left64(v14, -16); + v10 += v14; + v6 ^= v10; + v6 = rotate_left64(v6, -63); + v3 += m[s[7]]; + v3 += v7; + v15 ^= v3; + v15 = rotate_left64(v15, -16); + v11 += v15; + v7 ^= v11; + v7 = rotate_left64(v7, -63); + + v0 += m[s[8]]; + v0 += v5; + v15 ^= v0; + v15 = rotate_left64(v15, -32); + v10 += v15; + v5 ^= v10; + v5 = rotate_left64(v5, -24); + v1 += m[s[9]]; + v1 += v6; + v12 ^= v1; + v12 = rotate_left64(v12, -32); + v11 += v12; + v6 ^= v11; + v6 = rotate_left64(v6, -24); + v2 += m[s[10]]; + v2 += v7; + v13 ^= v2; + v13 = rotate_left64(v13, -32); + v8 += v13; + v7 ^= v8; + v7 = rotate_left64(v7, -24); + v3 += m[s[11]]; + v3 += v4; + v14 ^= v3; + v14 = rotate_left64(v14, -32); + v9 += v14; + v4 ^= v9; + v4 = rotate_left64(v4, -24); + + v0 += m[s[12]]; + v0 += v5; + v15 ^= v0; + v15 = rotate_left64(v15, -16); + v10 += v15; + v5 ^= v10; v5 = rotate_left64(v5, -63); - v2 += m[s[6]]; - v2 += v6; - v14 ^= v2; - v14 = rotate_left64(v14, -16); - v10 += v14; - v6 ^= v10; - v6 = rotate_left64(v6, -63); - v3 += m[s[7]]; - v3 += v7; - v15 ^= v3; - v15 = rotate_left64(v15, -16); - v11 += v15; - v7 ^= v11; - v7 = rotate_left64(v7, -63); - - v0 += m[s[8]]; - v0 += v5; - v15 ^= v0; - v15 = rotate_left64(v15, -32); - v10 += v15; - v5 ^= v10; - v5 = rotate_left64(v5, -24); - v1 += m[s[9]]; - v1 += v6; - v12 ^= v1; - v12 = rotate_left64(v12, -32); - v11 += v12; - v6 ^= v11; - v6 = rotate_left64(v6, -24); - v2 += m[s[10]]; - v2 += v7; - v13 ^= v2; - v13 = rotate_left64(v13, -32); - v8 += v13; - v7 ^= v8; - v7 = rotate_left64(v7, -24); - v3 += m[s[11]]; - v3 += v4; - v14 ^= v3; - v14 = rotate_left64(v14, -32); - v9 += v14; - v4 ^= v9; - v4 = rotate_left64(v4, -24); - - v0 += m[s[12]]; - v0 += v5; - v15 ^= v0; - v15 = rotate_left64(v15, -16); - v10 += v15; - v5 ^= v10; - v5 = rotate_left64(v5, -63); - v1 += m[s[13]]; - v1 += v6; - v12 ^= v1; - v12 = rotate_left64(v12, -16); - v11 += v12; - v6 ^= v11; - v6 = rotate_left64(v6, -63); - v2 += m[s[14]]; - v2 += v7; - v13 ^= v2; - v13 = rotate_left64(v13, -16); - v8 += v13; - v7 ^= v8; - v7 = rotate_left64(v7, -63); - v3 += m[s[15]]; - v3 += v4; - v14 ^= v3; - v14 = rotate_left64(v14, -16); - v9 += v14; - v4 ^= v9; - v4 = rotate_left64(v4, -63); - } - h[0] ^= v0 ^ v8; - h[1] ^= v1 ^ v9; - h[2] ^= v2 ^ v10; - h[3] ^= v3 ^ v11; - h[4] ^= v4 ^ v12; - h[5] ^= v5 ^ v13; - h[6] ^= v6 ^ v14; - h[7] ^= v7 ^ v15; + v1 += m[s[13]]; + v1 += v6; + v12 ^= v1; + v12 = rotate_left64(v12, -16); + v11 += v12; + v6 ^= v11; + v6 = rotate_left64(v6, -63); + v2 += m[s[14]]; + v2 += v7; + v13 ^= v2; + v13 = rotate_left64(v13, -16); + v8 += v13; + v7 ^= v8; + v7 = rotate_left64(v7, -63); + v3 += m[s[15]]; + v3 += v4; + v14 ^= v3; + v14 = rotate_left64(v14, -16); + v9 += v14; + v4 ^= v9; + v4 = rotate_left64(v4, -63); + } + h[0] ^= v0 ^ v8; + h[1] ^= v1 ^ v9; + h[2] ^= v2 ^ v10; + h[3] ^= v3 ^ v11; + h[4] ^= v4 ^ v12; + h[5] ^= v5 ^ v13; + h[6] ^= v6 ^ v14; + h[7] ^= v7 ^ v15; } -int blake2f(gw_context_t *ctx, - const uint8_t *input_src, - const size_t input_size, - uint8_t **output, size_t *output_size) { +int blake2f(gw_context_t* ctx, const uint8_t* input_src, + const size_t input_size, uint8_t** output, size_t* output_size) { if (input_size != BLAKE2F_INPUT_LENGTH) { return ERROR_BLAKE2F; } - if (input_src[212] != BLAKE2F_NON_FINAL_BLOCK_BYTES - && input_src[212] != BLAKE2F_FINAL_BLOCK_BYTES) { + if (input_src[212] != BLAKE2F_NON_FINAL_BLOCK_BYTES && + input_src[212] != BLAKE2F_FINAL_BLOCK_BYTES) { return ERROR_BLAKE2F; } - uint32_t rounds = ((uint32_t)input_src[0] << 24 - | (uint32_t)input_src[1] << 16 - | (uint32_t)input_src[2] << 8 - | (uint32_t)input_src[3] << 0); + uint32_t rounds = + ((uint32_t)input_src[0] << 24 | (uint32_t)input_src[1] << 16 | + (uint32_t)input_src[2] << 8 | (uint32_t)input_src[3] << 0); bool final = input_src[212] == BLAKE2F_FINAL_BLOCK_BYTES; uint64_t h[8]; uint64_t m[16]; uint64_t t[2]; for (size_t i = 0; i < 8; i++) { size_t offset = 4 + i * 8; - h[i] = *(uint64_t *)(input_src + offset); + h[i] = *(uint64_t*)(input_src + offset); } for (size_t i = 0; i < 16; i++) { size_t offset = 68 + i * 8; - m[i] = *(uint64_t *)(input_src + offset); + m[i] = *(uint64_t*)(input_src + offset); } - t[0] = *(uint64_t *)(input_src + 196); - t[1] = *(uint64_t *)(input_src + 204); + t[0] = *(uint64_t*)(input_src + 196); + t[1] = *(uint64_t*)(input_src + 204); uint64_t flag = final ? 0xFFFFFFFFFFFFFFFF : 0; f_generic(h, m, t[0], t[1], flag, (uint64_t)rounds); - *output = (uint8_t *)malloc(64); + *output = (uint8_t*)malloc(64); *output_size = 64; for (size_t i = 0; i < 8; i++) { size_t offset = i * 8; - memcpy(*output + offset, (uint8_t *)(&h[i]), 8); + memcpy(*output + offset, (uint8_t*)(&h[i]), 8); } return 0; } -bool match_precompiled_address(const evmc_address *destination, - precompiled_contract_gas_fn *contract_gas, - precompiled_contract_fn *contract) { +bool match_precompiled_address(const evmc_address* destination, + precompiled_contract_gas_fn* contract_gas, + precompiled_contract_fn* contract) { for (int i = 0; i < 19; i++) { if (destination->bytes[i] != 0) { return false; @@ -630,39 +618,39 @@ bool match_precompiled_address(const evmc_address *destination, } switch (destination->bytes[19]) { - case 1: - *contract_gas = ecrecover_required_gas; - *contract = ecrecover; - break; - case 2: - *contract_gas = sha256hash_required_gas; - *contract = sha256hash; - break; - case 3: - *contract_gas = ripemd160hash_required_gas; - *contract = ripemd160hash; - break; - case 4: - *contract_gas = data_copy_required_gas; - *contract = data_copy; - break; - case 5: - *contract_gas = big_mod_exp_required_gas; - *contract = big_mod_exp; - break; - /* FIXME: - common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, - common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, - common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, - */ - case 9: - *contract_gas = blake2f_required_gas; - *contract = blake2f; - break; - default: - *contract_gas = NULL; - *contract = NULL; - return false; + case 1: + *contract_gas = ecrecover_required_gas; + *contract = ecrecover; + break; + case 2: + *contract_gas = sha256hash_required_gas; + *contract = sha256hash; + break; + case 3: + *contract_gas = ripemd160hash_required_gas; + *contract = ripemd160hash; + break; + case 4: + *contract_gas = data_copy_required_gas; + *contract = data_copy; + break; + case 5: + *contract_gas = big_mod_exp_required_gas; + *contract = big_mod_exp; + break; + /* FIXME: + common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, + common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, + common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, + */ + case 9: + *contract_gas = blake2f_required_gas; + *contract = blake2f; + break; + default: + *contract_gas = NULL; + *contract = NULL; + return false; } return true; } diff --git a/c/dump_secp256k1_data.c b/c/dump_secp256k1_data.c index 83f60654..e36a55aa 100644 --- a/c/dump_secp256k1_data.c +++ b/c/dump_secp256k1_data.c @@ -1,4 +1,5 @@ #include + #include "blake2b.h" /* diff --git a/c/generator.c b/c/generator.c index afe919fb..70fcd61c 100644 --- a/c/generator.c +++ b/c/generator.c @@ -13,6 +13,4 @@ #include "polyjuice.h" -int main() { - return run_polyjuice(); -} +int main() { return run_polyjuice(); } diff --git a/c/generator/secp256k1_helper.h b/c/generator/secp256k1_helper.h index ed243596..ebdb711e 100644 --- a/c/generator/secp256k1_helper.h +++ b/c/generator/secp256k1_helper.h @@ -32,11 +32,12 @@ void secp256k1_default_error_callback_fn(const char* str, void* data) { * data should at least be CKB_SECP256K1_DATA_SIZE big * so as to hold all loaded data. */ -int ckb_secp256k1_custom_verify_only_initialize(gw_context_t *ctx, +int ckb_secp256k1_custom_verify_only_initialize(gw_context_t* ctx, secp256k1_context* context, void* data) { uint32_t len = 0; - int ret = ctx->sys_load_data(ctx, ckb_secp256k1_data_hash, &len, 0, (uint8_t *)data); + int ret = + ctx->sys_load_data(ctx, ckb_secp256k1_data_hash, &len, 0, (uint8_t*)data); if (ret != 0 || len != CKB_SECP256K1_DATA_SIZE) { return CKB_SECP256K1_HELPER_ERROR_LOADING_DATA; } @@ -48,7 +49,7 @@ int ckb_secp256k1_custom_verify_only_initialize(gw_context_t *ctx, secp256k1_ecmult_gen_context_init(&context->ecmult_gen_ctx); /* Recasting data to (uint8_t*) for pointer math */ - uint8_t* p = (uint8_t *)data; + uint8_t* p = (uint8_t*)data; secp256k1_ge_storage(*pre_g)[] = (secp256k1_ge_storage(*)[])p; secp256k1_ge_storage(*pre_g_128)[] = (secp256k1_ge_storage(*)[])(&p[CKB_SECP256K1_DATA_PRE_SIZE]); diff --git a/c/polyjuice.h b/c/polyjuice.h index 7ffca496..07a21ba8 100644 --- a/c/polyjuice.h +++ b/c/polyjuice.h @@ -1,20 +1,20 @@ +#include +#include #include #include -#include #include +#include #include +#include +#include + +#include "ckb_syscalls.h" #include "common.h" #include "godwoken.h" -#include "ckb_syscalls.h" #include "gw_syscalls.h" #include "sudt_utils.h" -#include -#include -#include -#include - #ifdef GW_GENERATOR #include "generator/secp256k1_helper.h" #else @@ -23,11 +23,11 @@ #include "contracts.h" #define is_create(kind) ((kind) == EVMC_CREATE || (kind) == EVMC_CREATE2) -#define is_special_call(kind) ((kind) == EVMC_CALLCODE || (kind) == EVMC_DELEGATECALL) +#define is_special_call(kind) \ + ((kind) == EVMC_CALLCODE || (kind) == EVMC_DELEGATECALL) static char debug_buffer[64 * 1024]; -static void debug_print_data(const char *prefix, - const uint8_t *data, +static void debug_print_data(const char* prefix, const uint8_t* data, uint32_t data_len) { int offset = 0; offset += sprintf(debug_buffer, "%s 0x", prefix); @@ -37,7 +37,7 @@ static void debug_print_data(const char *prefix, debug_buffer[offset] = '\0'; ckb_debug(debug_buffer); } -static void debug_print_int(const char *prefix, int64_t ret) { +static void debug_print_int(const char* prefix, int64_t ret) { sprintf(debug_buffer, "%s => %ld", prefix, ret); ckb_debug(debug_buffer); } @@ -57,11 +57,10 @@ static evmc_address tx_origin; static uint8_t script_code_hash[32]; static uint8_t script_hash_type; -void polyjuice_build_system_key(uint32_t id, - uint8_t polyjuice_field_type, +void polyjuice_build_system_key(uint32_t id, uint8_t polyjuice_field_type, uint8_t key[GW_KEY_BYTES]) { memset(key, 0, 32); - memcpy(key, (uint8_t *)(&id), sizeof(uint32_t)); + memcpy(key, (uint8_t*)(&id), sizeof(uint32_t)); key[4] = POLYJUICE_SYSTEM_PREFIX; key[5] = polyjuice_field_type; } @@ -73,13 +72,11 @@ void polyjuice_build_destructed_key(uint32_t id, uint8_t key[GW_KEY_BYTES]) { polyjuice_build_system_key(id, POLYJUICE_DESTRUCTED, key); } -int handle_message(gw_context_t *ctx, - uint32_t parent_from_id, - const evmc_message *msg, - struct evmc_result *res); -typedef int (*stream_data_loader_fn)(gw_context_t *ctx, long data_id, - uint32_t *len, uint32_t offset, - uint8_t *data); +int handle_message(gw_context_t* ctx, uint32_t parent_from_id, + const evmc_message* msg, struct evmc_result* res); +typedef int (*stream_data_loader_fn)(gw_context_t* ctx, long data_id, + uint32_t* len, uint32_t offset, + uint8_t* data); struct evmc_host_context { gw_context_t* gw_ctx; @@ -91,17 +88,17 @@ struct evmc_host_context { evmc_address account_id_to_address(uint32_t account_id) { evmc_address addr; memset(addr.bytes, 0, 20); - memcpy(addr.bytes, (uint8_t *)(&account_id), 4); + memcpy(addr.bytes, (uint8_t*)(&account_id), 4); return addr; } -int address_to_account_id(const evmc_address* address, uint32_t *account_id) { +int address_to_account_id(const evmc_address* address, uint32_t* account_id) { for (size_t i = 4; i < 20; i++) { if (address->bytes[i] != 0) { /* ERROR: invalid polyjuice address */ return -1; } } - *account_id = *((uint32_t *)(address->bytes)); + *account_id = *((uint32_t*)(address->bytes)); return 0; } @@ -116,16 +113,15 @@ int address_to_account_id(const evmc_address* address, uint32_t *account_id) { input_data : [u8], ] */ -int parse_args(struct evmc_message *msg, - uint128_t *gas_price, - gw_transaction_context_t* tx_ctx) { +int parse_args(struct evmc_message* msg, uint128_t* gas_price, + gw_transaction_context_t* tx_ctx) { debug_print_int("args_len", tx_ctx->args_len); /* == Args decoder */ size_t offset = 0; - uint8_t *args = tx_ctx->args; + uint8_t* args = tx_ctx->args; /* args[0] call kind */ - evmc_call_kind kind = (evmc_call_kind)*(args + offset); + evmc_call_kind kind = (evmc_call_kind) * (args + offset); offset += 1; debug_print_int("[kind]", kind); @@ -135,27 +131,27 @@ int parse_args(struct evmc_message *msg, debug_print_int("[flags]", flags); /* args[2..10] gas limit */ - int64_t gas_limit = (int64_t)*((uint64_t *)(args + offset)); + int64_t gas_limit = (int64_t) * ((uint64_t*)(args + offset)); offset += 8; debug_print_int("[gas_limit]", gas_limit); /* args[10..26] gas price */ - *gas_price = *((uint128_t *)(args + offset)); + *gas_price = *((uint128_t*)(args + offset)); offset += 16; debug_print_int("[gas_price]", (int64_t)gas_price); /* args[26..58] transfer value */ - evmc_uint256be value = *((evmc_uint256be *)(args + offset)); + evmc_uint256be value = *((evmc_uint256be*)(args + offset)); offset += 32; debug_print_data("[value]", value.bytes, 32); /* args[58..62] */ - uint32_t input_size = *((uint32_t *)(args + offset)); + uint32_t input_size = *((uint32_t*)(args + offset)); offset += 4; debug_print_int("[input_size]", input_size); /* args[62..62+input_size] */ - uint8_t *input_data = args + offset; + uint8_t* input_data = args + offset; debug_print_data("[input_data]", input_data, input_size); if (tx_ctx->args_len != (input_size + offset)) { @@ -185,56 +181,50 @@ int parse_args(struct evmc_message *msg, return 0; } -int build_script(uint8_t code_hash[32], - uint8_t hash_type, - uint8_t *args, - uint32_t args_len, - mol_seg_t *script_seg) { - /* 1. Build Script by receipt.return_data */ - mol_seg_t args_seg; - args_seg.size = 4 + args_len; - args_seg.ptr = (uint8_t *)malloc(4 + args_seg.size); - if (args_seg.ptr == NULL) { - return -1; - } - memcpy(args_seg.ptr, (uint8_t *)(&args_len), 4); - memcpy(args_seg.ptr + 4, args, args_len); - debug_print_data("script.args", args_seg.ptr, args_seg.size); - debug_print_data("script.code_hash", code_hash, 32); - debug_print_int("script.hash_type", hash_type); - - mol_builder_t script_builder; - MolBuilder_Script_init(&script_builder); - MolBuilder_Script_set_code_hash(&script_builder, code_hash, 32); - MolBuilder_Script_set_hash_type(&script_builder, hash_type); - MolBuilder_Script_set_args(&script_builder, args_seg.ptr, args_seg.size); - mol_seg_res_t script_res = MolBuilder_Script_build(script_builder); - // Because errno is keyword - uint8_t error_num = *(uint8_t *)(&script_res); - if (error_num != MOL_OK) { - /* ERROR: build script failed */ - return -1; - } - *script_seg = script_res.seg; +int build_script(uint8_t code_hash[32], uint8_t hash_type, uint8_t* args, + uint32_t args_len, mol_seg_t* script_seg) { + /* 1. Build Script by receipt.return_data */ + mol_seg_t args_seg; + args_seg.size = 4 + args_len; + args_seg.ptr = (uint8_t*)malloc(4 + args_seg.size); + if (args_seg.ptr == NULL) { + return -1; + } + memcpy(args_seg.ptr, (uint8_t*)(&args_len), 4); + memcpy(args_seg.ptr + 4, args, args_len); + debug_print_data("script.args", args_seg.ptr, args_seg.size); + debug_print_data("script.code_hash", code_hash, 32); + debug_print_int("script.hash_type", hash_type); + + mol_builder_t script_builder; + MolBuilder_Script_init(&script_builder); + MolBuilder_Script_set_code_hash(&script_builder, code_hash, 32); + MolBuilder_Script_set_hash_type(&script_builder, hash_type); + MolBuilder_Script_set_args(&script_builder, args_seg.ptr, args_seg.size); + mol_seg_res_t script_res = MolBuilder_Script_build(script_builder); + // Because errno is keyword + uint8_t error_num = *(uint8_t*)(&script_res); + if (error_num != MOL_OK) { + /* ERROR: build script failed */ + return -1; + } + *script_seg = script_res.seg; - debug_print_data("script ", script_seg->ptr, script_seg->size); - if (MolReader_Script_verify(script_seg, false) != MOL_OK) { - ckb_debug("built an invalid script"); - return -1; - } - return 0; + debug_print_data("script ", script_seg->ptr, script_seg->size); + if (MolReader_Script_verify(script_seg, false) != MOL_OK) { + ckb_debug("built an invalid script"); + return -1; + } + return 0; } void release_result(const struct evmc_result* result) { - free((void *)result->output_data); + free((void*)result->output_data); return; } -int load_account_code(gw_context_t *gw_ctx, - uint32_t account_id, - uint32_t *code_size, - uint32_t offset, - uint8_t *code) { +int load_account_code(gw_context_t* gw_ctx, uint32_t account_id, + uint32_t* code_size, uint32_t offset, uint8_t* code) { debug_print_int("load_account_code, account_id:", account_id); uint8_t key[32]; uint8_t data_hash[32]; @@ -256,11 +246,9 @@ int load_account_code(gw_context_t *gw_ctx, return 0; } -int load_account_script(gw_context_t *gw_ctx, - uint32_t account_id, - uint8_t *buffer, - uint32_t buffer_size, - mol_seg_t *script_seg) { +int load_account_script(gw_context_t* gw_ctx, uint32_t account_id, + uint8_t* buffer, uint32_t buffer_size, + mol_seg_t* script_seg) { debug_print_int("load_account_script, account_id:", account_id); int ret; uint32_t len = buffer_size; @@ -282,7 +270,7 @@ int load_account_script(gw_context_t *gw_ctx, //// Callbacks //////////////////////////////////////////////////////////////////////////// struct evmc_tx_context get_tx_context(struct evmc_host_context* context) { - struct evmc_tx_context ctx{}; + struct evmc_tx_context ctx {}; /* gas price = 1 */ ctx.tx_gas_price.bytes[31] = 0x01; memcpy(ctx.tx_origin.bytes, tx_origin.bytes, 20); @@ -292,10 +280,11 @@ struct evmc_tx_context get_tx_context(struct evmc_host_context* context) { ctx.block_timestamp = context->gw_ctx->block_info.timestamp; ctx.block_gas_limit = 10000000000; /* 2500000000000000, TODO: read from aggregator */ - ctx.block_difficulty = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x08, 0xe1, 0xbc, 0x9b, 0xf0, 0x40, 0x00,}; + ctx.block_difficulty = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x08, 0xe1, 0xbc, 0x9b, 0xf0, 0x40, 0x00, + }; /* chain id = 1 */ ctx.chain_id.bytes[31] = 0x01; return ctx; @@ -314,14 +303,11 @@ bool account_exists(struct evmc_host_context* context, } evmc_bytes32 get_storage(struct evmc_host_context* context, - const evmc_address* address, - const evmc_bytes32* key) { + const evmc_address* address, const evmc_bytes32* key) { ckb_debug("BEGIN get_storage"); evmc_bytes32 value{}; - int ret = context->gw_ctx->sys_load(context->gw_ctx, - context->to_id, - key->bytes, - (uint8_t *)value.bytes); + int ret = context->gw_ctx->sys_load(context->gw_ctx, context->to_id, + key->bytes, (uint8_t*)value.bytes); if (ret != 0) { context->error_code = ret; } @@ -334,10 +320,8 @@ enum evmc_storage_status set_storage(struct evmc_host_context* context, const evmc_bytes32* key, const evmc_bytes32* value) { ckb_debug("BEGIN set_storage"); - int ret = context->gw_ctx->sys_store(context->gw_ctx, - context->to_id, - key->bytes, - value->bytes); + int ret = context->gw_ctx->sys_store(context->gw_ctx, context->to_id, + key->bytes, value->bytes); if (ret != 0) { context->error_code = ret; } @@ -393,11 +377,8 @@ evmc_bytes32 get_code_hash(struct evmc_host_context* context, return hash; } -size_t copy_code(struct evmc_host_context* context, - const evmc_address* address, - size_t code_offset, - uint8_t* buffer_data, - size_t buffer_size) { +size_t copy_code(struct evmc_host_context* context, const evmc_address* address, + size_t code_offset, uint8_t* buffer_data, size_t buffer_size) { ckb_debug("BEGIN copy_code"); uint32_t account_id = 0; int ret = address_to_account_id(address, &account_id); @@ -405,12 +386,9 @@ size_t copy_code(struct evmc_host_context* context, return (size_t)ret; } - uint32_t code_size = (uint32_t) buffer_size; - ret = load_account_code(context->gw_ctx, - account_id, - &code_size, - (uint32_t)code_offset, - buffer_data); + uint32_t code_size = (uint32_t)buffer_size; + ret = load_account_code(context->gw_ctx, account_id, &code_size, + (uint32_t)code_offset, buffer_data); if (ret != 0) { return ret; } @@ -439,9 +417,9 @@ evmc_uint256be get_balance(struct evmc_host_context* context, context->error_code = -1; return balance; } - uint8_t *value_ptr = (uint8_t *)(&value_u128); + uint8_t* value_ptr = (uint8_t*)(&value_u128); for (int i = 0; i < 16; i++) { - balance.bytes[31-i] = *(value_ptr + i); + balance.bytes[31 - i] = *(value_ptr + i); } return balance; } @@ -458,14 +436,16 @@ void selfdestruct(struct evmc_host_context* context, return; } uint128_t balance; - ret = sudt_get_balance(context->gw_ctx, sudt_id, beneficiary_account_id, &balance); + ret = sudt_get_balance(context->gw_ctx, sudt_id, beneficiary_account_id, + &balance); if (ret != 0) { ckb_debug("get balance failed"); context->error_code = ret; return; } if (balance > 0 && beneficiary_account_id != context->to_id) { - ret = sudt_transfer(context->gw_ctx, sudt_id, context->to_id, beneficiary_account_id, balance); + ret = sudt_transfer(context->gw_ctx, sudt_id, context->to_id, + beneficiary_account_id, balance); if (ret != 0) { ckb_debug("transfer beneficiary failed"); context->error_code = ret; @@ -494,7 +474,7 @@ struct evmc_result call(struct evmc_host_context* context, ckb_debug("BEGIN call"); int ret; struct evmc_result res; - gw_context_t *gw_ctx = context->gw_ctx; + gw_context_t* gw_ctx = context->gw_ctx; /* FIXME: Handle pre-compiled contracts * - check msg->destination @@ -516,11 +496,8 @@ struct evmc_result call(struct evmc_host_context* context, return res; } res.gas_left = msg->gas - (int64_t)gas_cost; - ret = contract(gw_ctx, - msg->input_data, - msg->input_size, - (uint8_t **)&res.output_data, - &res.output_size); + ret = contract(gw_ctx, msg->input_data, msg->input_size, + (uint8_t**)&res.output_data, &res.output_size); if (ret != 0) { ckb_debug("call pre-compiled contract failed"); context->error_code = ret; @@ -555,9 +532,8 @@ struct evmc_result call(struct evmc_host_context* context, evmc_bytes32 get_block_hash(struct evmc_host_context* context, int64_t number) { ckb_debug("BEGIN get_block_hash"); evmc_bytes32 block_hash{}; - int ret = context->gw_ctx->sys_get_block_hash(context->gw_ctx, - number, - (uint8_t *)block_hash.bytes); + int ret = context->gw_ctx->sys_get_block_hash(context->gw_ctx, number, + (uint8_t*)block_hash.bytes); if (ret != 0) { context->error_code = ret; return block_hash; @@ -566,38 +542,33 @@ evmc_bytes32 get_block_hash(struct evmc_host_context* context, int64_t number) { return block_hash; } -void emit_log(struct evmc_host_context* context, - const evmc_address* address, - const uint8_t* data, - size_t data_size, - const evmc_bytes32 topics[], - size_t topics_count) { +void emit_log(struct evmc_host_context* context, const evmc_address* address, + const uint8_t* data, size_t data_size, + const evmc_bytes32 topics[], size_t topics_count) { ckb_debug("BEGIN emit_log"); size_t output_size = 20 + (4 + data_size) + (4 + topics_count * 32); - uint8_t *output = (uint8_t *)malloc(output_size); + uint8_t* output = (uint8_t*)malloc(output_size); if (output == NULL) { context->error_code = -1; return; } uint32_t data_size_u32 = (uint32_t)(data_size); uint32_t topics_count_u32 = (uint32_t)(topics_count); - uint8_t *output_current = output; + uint8_t* output_current = output; memcpy(output_current, address->bytes, 20); output_current += 20; - memcpy(output_current, (uint8_t *)(&data_size_u32), 4); + memcpy(output_current, (uint8_t*)(&data_size_u32), 4); output_current += 4; memcpy(output_current, data, data_size); output_current += data_size; - memcpy(output_current, (uint8_t *)(&topics_count_u32), 4); + memcpy(output_current, (uint8_t*)(&topics_count_u32), 4); output_current += 4; for (size_t i = 0; i < topics_count; i++) { memcpy(output_current, topics[i].bytes, 32); output_current += 32; } - int ret = context->gw_ctx->sys_log(context->gw_ctx, - context->to_id, - (uint32_t) output_size, - output); + int ret = context->gw_ctx->sys_log(context->gw_ctx, context->to_id, + (uint32_t)output_size, output); if (ret != 0) { context->error_code = ret; } @@ -606,16 +577,13 @@ void emit_log(struct evmc_host_context* context, return; } - /** * call/create contract * * Must allocate an account id before create contract */ -int handle_message(gw_context_t *ctx, - uint32_t parent_from_id, - const evmc_message *msg_origin, - struct evmc_result *res) { +int handle_message(gw_context_t* ctx, uint32_t parent_from_id, + const evmc_message* msg_origin, struct evmc_result* res) { ckb_debug("BEGIN handle_message"); evmc_message msg = *msg_origin; @@ -645,9 +613,11 @@ int handle_message(gw_context_t *ctx, uint8_t destructed_raw_value[GW_VALUE_BYTES]; polyjuice_build_destructed_key(to_id, destructed_raw_key); #ifdef GW_VALIDATOR - ret = gw_state_fetch(&ctx->kv_state, destructed_raw_key, destructed_raw_key); + ret = + gw_state_fetch(&ctx->kv_state, destructed_raw_key, destructed_raw_key); #else - ret = syscall(GW_SYS_LOAD, destructed_raw_key, destructed_raw_value, 0, 0, 0, 0); + ret = syscall(GW_SYS_LOAD, destructed_raw_key, destructed_raw_value, 0, 0, + 0, 0); #endif if (ret != 0) { ckb_debug("load destructed key failed"); @@ -674,17 +644,17 @@ int handle_message(gw_context_t *ctx, mol_seg_t raw_args_seg = MolReader_Bytes_raw_bytes(&args_seg); memcpy(script_code_hash, code_hash_seg.ptr, 32); script_hash_type = *hash_type_seg.ptr; - sudt_id = *(uint32_t *)(raw_args_seg.ptr); - free((void *)script_seg.ptr); + sudt_id = *(uint32_t*)(raw_args_seg.ptr); + free((void*)script_seg.ptr); has_touched = true; } /* Load contract code from evmc_message or by sys_load_data */ - uint8_t *code_data = NULL; + uint8_t* code_data = NULL; size_t code_size = 0; if (is_create(msg.kind)) { /* use input as code */ - code_data = (uint8_t *)msg.input_data; + code_data = (uint8_t*)msg.input_data; code_size = msg.input_size; msg.input_data = NULL; msg.input_size = 0; @@ -711,14 +681,15 @@ int handle_message(gw_context_t *ctx, uint32_t script_args_len = 0; if (msg.kind == EVMC_CREATE) { /* create account id - Include: - - [4 bytes] sudt id - - [4 bytes] sender account id - - [4 bytes] sender nonce (NOTE: only use first 4 bytes (u32)) - */ - memcpy(script_args, (uint8_t *)(&sudt_id), 4); - memcpy(script_args + 4, (uint8_t *)(&from_id), 4); - // TODO: the nonce length can be optimized (change nonce data type, u32 is not enough) + Include: + - [4 bytes] sudt id + - [4 bytes] sender account id + - [4 bytes] sender nonce (NOTE: only use first 4 bytes (u32)) +*/ + memcpy(script_args, (uint8_t*)(&sudt_id), 4); + memcpy(script_args + 4, (uint8_t*)(&from_id), 4); + // TODO: the nonce length can be optimized (change nonce data type, u32 is + // not enough) ret = ctx->sys_load_nonce(ctx, from_id, script_args + 8); if (ret != 0) { return ret; @@ -726,33 +697,31 @@ int handle_message(gw_context_t *ctx, script_args_len = 4 + 4 + 4; } else if (msg.kind == EVMC_CREATE2) { /* create account id - Include: - - [ 4 bytes] sudt id - - [ 1 byte ] 0xff (refer to ethereum) - - [ 4 bytes] sender account id - - [32 bytes] create2_salt - - [32 bytes] keccak256(init_code) - */ - memcpy(script_args, (uint8_t *)(&sudt_id), 4); + Include: + - [ 4 bytes] sudt id + - [ 1 byte ] 0xff (refer to ethereum) + - [ 4 bytes] sender account id + - [32 bytes] create2_salt + - [32 bytes] keccak256(init_code) +*/ + memcpy(script_args, (uint8_t*)(&sudt_id), 4); script_args[4] = 0xff; - memcpy(script_args + (4+1), (uint8_t *)(&from_id), 4); - memcpy(script_args + (4+1+4), msg.create2_salt.bytes, 32); + memcpy(script_args + (4 + 1), (uint8_t*)(&from_id), 4); + memcpy(script_args + (4 + 1 + 4), msg.create2_salt.bytes, 32); union ethash_hash256 hash_result = ethash::keccak256(code_data, code_size); - memcpy(script_args + (4+1+4+32), hash_result.bytes, 32); + memcpy(script_args + (4 + 1 + 4 + 32), hash_result.bytes, 32); script_args_len = 4 + 1 + 4 + 32 + 32; } if (script_args_len > 0) { mol_seg_t new_script_seg; uint32_t new_account_id; - ret = build_script(script_code_hash, - script_hash_type, - script_args, - script_args_len, - &new_script_seg); + ret = build_script(script_code_hash, script_hash_type, script_args, + script_args_len, &new_script_seg); if (ret != 0) { return ret; } - ret = ctx->sys_create(ctx, new_script_seg.ptr, new_script_seg.size, &new_account_id); + ret = ctx->sys_create(ctx, new_script_seg.ptr, new_script_seg.size, + &new_account_id); if (ret != 0) { return ret; } @@ -760,24 +729,17 @@ int handle_message(gw_context_t *ctx, } /* Execute the code in EVM */ - struct evmc_host_context context { ctx, from_id, to_id, 0 }; - struct evmc_vm *vm = evmc_create_evmone(); - struct evmc_host_interface interface = { account_exists, - get_storage, set_storage, - get_balance, - get_code_size, get_code_hash, copy_code, - selfdestruct, call, - get_tx_context, - get_block_hash, - emit_log }; - *res = vm->execute(vm, - &interface, - &context, - EVMC_MAX_REVISION, - &msg, - code_data, - code_size); - if (context.error_code != 0) { + struct evmc_host_context context { + ctx, from_id, to_id, 0 + }; + struct evmc_vm* vm = evmc_create_evmone(); + struct evmc_host_interface interface = { + account_exists, get_storage, set_storage, get_balance, + get_code_size, get_code_hash, copy_code, selfdestruct, + call, get_tx_context, get_block_hash, emit_log}; + *res = vm->execute(vm, &interface, &context, EVMC_MAX_REVISION, &msg, + code_data, code_size); + if (context.error_code != 0) { debug_print_int("context.error_code:", context.error_code); return context.error_code; } @@ -797,17 +759,13 @@ int handle_message(gw_context_t *ctx, if (!is_zero_value) { uint8_t value_u128_bytes[16]; for (int i = 0; i < 16; i++) { - value_u128_bytes[i] = msg.value.bytes[31-i]; + value_u128_bytes[i] = msg.value.bytes[31 - i]; } - uint128_t value_u128 = *(uint128_t *)value_u128_bytes; + uint128_t value_u128 = *(uint128_t*)value_u128_bytes; debug_print_int("from_id", from_id); debug_print_int("to_id", to_id); debug_print_int("transfer value", value_u128); - ret = sudt_transfer(ctx, - sudt_id, - from_id, - to_id, - value_u128); + ret = sudt_transfer(ctx, sudt_id, from_id, to_id, value_u128); if (ret != 0) { ckb_debug("transfer failed"); return ret; @@ -818,7 +776,7 @@ int handle_message(gw_context_t *ctx, if (is_create(msg.kind)) { uint8_t key[32]; uint8_t data_hash[32]; - blake2b_hash(data_hash, (uint8_t *)res->output_data, res->output_size); + blake2b_hash(data_hash, (uint8_t*)res->output_data, res->output_size); polyjuice_build_contract_code_key(to_id, key); ckb_debug("BEGIN store data key"); ret = ctx->sys_store(ctx, to_id, key, data_hash); @@ -826,7 +784,8 @@ int handle_message(gw_context_t *ctx, return ret; } ckb_debug("BEGIN store data"); - ret = ctx->sys_store_data(ctx, res->output_size, (uint8_t *)res->output_data); + ret = + ctx->sys_store_data(ctx, res->output_size, (uint8_t*)res->output_data); ckb_debug("END store data"); if (ret != 0) { return ret; @@ -840,7 +799,6 @@ int handle_message(gw_context_t *ctx, return (int)res->status_code; } - int run_polyjuice() { int ret; @@ -867,8 +825,7 @@ int run_polyjuice() { return ret; } - ret = context.sys_set_program_return_data(&context, - (uint8_t *)res.output_data, + ret = context.sys_set_program_return_data(&context, (uint8_t*)res.output_data, res.output_size); if (ret != 0) { ckb_debug("set return data failed"); @@ -885,11 +842,8 @@ int run_polyjuice() { return -1; } uint128_t fee = gas_price * (uint128_t)(msg.gas - res.gas_left); - ret = sudt_transfer(&context, - sudt_id, - context.transaction_context.from_id, - context.block_info.aggregator_id, - fee); + ret = sudt_transfer(&context, sudt_id, context.transaction_context.from_id, + context.block_info.aggregator_id, fee); ret = gw_finalize(&context); if (ret != 0) { diff --git a/c/ripemd160/memzero.c b/c/ripemd160/memzero.c index 6a517ff3..510a255f 100644 --- a/c/ripemd160/memzero.c +++ b/c/ripemd160/memzero.c @@ -46,7 +46,7 @@ // Adapted from // https://github.com/jedisct1/libsodium/blob/1647f0d53ae0e370378a9195477e3df0a792408f/src/libsodium/sodium/utils.c#L102-L130 -void memzero(void *const pnt, const size_t len) { +void memzero(void* const pnt, const size_t len) { #ifdef _WIN32 SecureZeroMemory(pnt, len); #elif defined(HAVE_MEMSET_S) @@ -56,7 +56,7 @@ void memzero(void *const pnt, const size_t len) { #elif defined(HAVE_EXPLICIT_MEMSET) explicit_memset(pnt, 0, len); #else - volatile unsigned char *volatile pnt_ = (volatile unsigned char *volatile)pnt; + volatile unsigned char* volatile pnt_ = (volatile unsigned char* volatile)pnt; size_t i = (size_t)0U; while (i < len) { diff --git a/c/ripemd160/ripemd160.c b/c/ripemd160/ripemd160.c index 60d007ac..4a564b08 100644 --- a/c/ripemd160/ripemd160.c +++ b/c/ripemd160/ripemd160.c @@ -25,319 +25,307 @@ * http://ehash.iaik.tugraz.at/wiki/RIPEMD-160 */ +#include "ripemd160.h" + #include -#include "ripemd160.h" #include "memzero.h" /* * 32-bit integer manipulation macros (little endian) */ #ifndef GET_UINT32_LE -#define GET_UINT32_LE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} +#define GET_UINT32_LE(n, b, i) \ + { \ + (n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | \ + ((uint32_t)(b)[(i) + 2] << 16) | ((uint32_t)(b)[(i) + 3] << 24); \ + } #endif #ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (uint8_t) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (uint8_t) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (uint8_t) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (uint8_t) ( ( (n) >> 24 ) & 0xFF ); \ -} +#define PUT_UINT32_LE(n, b, i) \ + { \ + (b)[(i)] = (uint8_t)(((n)) & 0xFF); \ + (b)[(i) + 1] = (uint8_t)(((n) >> 8) & 0xFF); \ + (b)[(i) + 2] = (uint8_t)(((n) >> 16) & 0xFF); \ + (b)[(i) + 3] = (uint8_t)(((n) >> 24) & 0xFF); \ + } #endif /* * RIPEMD-160 context setup */ -void ripemd160_Init(RIPEMD160_CTX *ctx) -{ - memzero(ctx, sizeof(RIPEMD160_CTX)); - ctx->total[0] = 0; - ctx->total[1] = 0; - ctx->state[0] = 0x67452301; - ctx->state[1] = 0xEFCDAB89; - ctx->state[2] = 0x98BADCFE; - ctx->state[3] = 0x10325476; - ctx->state[4] = 0xC3D2E1F0; +void ripemd160_Init(RIPEMD160_CTX* ctx) { + memzero(ctx, sizeof(RIPEMD160_CTX)); + ctx->total[0] = 0; + ctx->total[1] = 0; + ctx->state[0] = 0x67452301; + ctx->state[1] = 0xEFCDAB89; + ctx->state[2] = 0x98BADCFE; + ctx->state[3] = 0x10325476; + ctx->state[4] = 0xC3D2E1F0; } #if !defined(MBEDTLS_RIPEMD160_PROCESS_ALT) /* * Process one block */ -void ripemd160_process( RIPEMD160_CTX *ctx, const uint8_t data[RIPEMD160_BLOCK_LENGTH] ) -{ - uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; - - GET_UINT32_LE( X[ 0], data, 0 ); - GET_UINT32_LE( X[ 1], data, 4 ); - GET_UINT32_LE( X[ 2], data, 8 ); - GET_UINT32_LE( X[ 3], data, 12 ); - GET_UINT32_LE( X[ 4], data, 16 ); - GET_UINT32_LE( X[ 5], data, 20 ); - GET_UINT32_LE( X[ 6], data, 24 ); - GET_UINT32_LE( X[ 7], data, 28 ); - GET_UINT32_LE( X[ 8], data, 32 ); - GET_UINT32_LE( X[ 9], data, 36 ); - GET_UINT32_LE( X[10], data, 40 ); - GET_UINT32_LE( X[11], data, 44 ); - GET_UINT32_LE( X[12], data, 48 ); - GET_UINT32_LE( X[13], data, 52 ); - GET_UINT32_LE( X[14], data, 56 ); - GET_UINT32_LE( X[15], data, 60 ); - - A = Ap = ctx->state[0]; - B = Bp = ctx->state[1]; - C = Cp = ctx->state[2]; - D = Dp = ctx->state[3]; - E = Ep = ctx->state[4]; - -#define F1( x, y, z ) ( x ^ y ^ z ) -#define F2( x, y, z ) ( ( x & y ) | ( ~x & z ) ) -#define F3( x, y, z ) ( ( x | ~y ) ^ z ) -#define F4( x, y, z ) ( ( x & z ) | ( y & ~z ) ) -#define F5( x, y, z ) ( x ^ ( y | ~z ) ) - -#define S( x, n ) ( ( x << n ) | ( x >> (32 - n) ) ) - -#define P( a, b, c, d, e, r, s, f, k ) \ - a += f( b, c, d ) + X[r] + k; \ - a = S( a, s ) + e; \ - c = S( c, 10 ); - -#define P2( a, b, c, d, e, r, s, rp, sp ) \ - P( a, b, c, d, e, r, s, F, K ); \ - P( a ## p, b ## p, c ## p, d ## p, e ## p, rp, sp, Fp, Kp ); - -#define F F1 -#define K 0x00000000 -#define Fp F5 -#define Kp 0x50A28BE6 - P2( A, B, C, D, E, 0, 11, 5, 8 ); - P2( E, A, B, C, D, 1, 14, 14, 9 ); - P2( D, E, A, B, C, 2, 15, 7, 9 ); - P2( C, D, E, A, B, 3, 12, 0, 11 ); - P2( B, C, D, E, A, 4, 5, 9, 13 ); - P2( A, B, C, D, E, 5, 8, 2, 15 ); - P2( E, A, B, C, D, 6, 7, 11, 15 ); - P2( D, E, A, B, C, 7, 9, 4, 5 ); - P2( C, D, E, A, B, 8, 11, 13, 7 ); - P2( B, C, D, E, A, 9, 13, 6, 7 ); - P2( A, B, C, D, E, 10, 14, 15, 8 ); - P2( E, A, B, C, D, 11, 15, 8, 11 ); - P2( D, E, A, B, C, 12, 6, 1, 14 ); - P2( C, D, E, A, B, 13, 7, 10, 14 ); - P2( B, C, D, E, A, 14, 9, 3, 12 ); - P2( A, B, C, D, E, 15, 8, 12, 6 ); +void ripemd160_process(RIPEMD160_CTX* ctx, + const uint8_t data[RIPEMD160_BLOCK_LENGTH]) { + uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; + + GET_UINT32_LE(X[0], data, 0); + GET_UINT32_LE(X[1], data, 4); + GET_UINT32_LE(X[2], data, 8); + GET_UINT32_LE(X[3], data, 12); + GET_UINT32_LE(X[4], data, 16); + GET_UINT32_LE(X[5], data, 20); + GET_UINT32_LE(X[6], data, 24); + GET_UINT32_LE(X[7], data, 28); + GET_UINT32_LE(X[8], data, 32); + GET_UINT32_LE(X[9], data, 36); + GET_UINT32_LE(X[10], data, 40); + GET_UINT32_LE(X[11], data, 44); + GET_UINT32_LE(X[12], data, 48); + GET_UINT32_LE(X[13], data, 52); + GET_UINT32_LE(X[14], data, 56); + GET_UINT32_LE(X[15], data, 60); + + A = Ap = ctx->state[0]; + B = Bp = ctx->state[1]; + C = Cp = ctx->state[2]; + D = Dp = ctx->state[3]; + E = Ep = ctx->state[4]; + +#define F1(x, y, z) (x ^ y ^ z) +#define F2(x, y, z) ((x & y) | (~x & z)) +#define F3(x, y, z) ((x | ~y) ^ z) +#define F4(x, y, z) ((x & z) | (y & ~z)) +#define F5(x, y, z) (x ^ (y | ~z)) + +#define S(x, n) ((x << n) | (x >> (32 - n))) + +#define P(a, b, c, d, e, r, s, f, k) \ + a += f(b, c, d) + X[r] + k; \ + a = S(a, s) + e; \ + c = S(c, 10); + +#define P2(a, b, c, d, e, r, s, rp, sp) \ + P(a, b, c, d, e, r, s, F, K); \ + P(a##p, b##p, c##p, d##p, e##p, rp, sp, Fp, Kp); + +#define F F1 +#define K 0x00000000 +#define Fp F5 +#define Kp 0x50A28BE6 + P2(A, B, C, D, E, 0, 11, 5, 8); + P2(E, A, B, C, D, 1, 14, 14, 9); + P2(D, E, A, B, C, 2, 15, 7, 9); + P2(C, D, E, A, B, 3, 12, 0, 11); + P2(B, C, D, E, A, 4, 5, 9, 13); + P2(A, B, C, D, E, 5, 8, 2, 15); + P2(E, A, B, C, D, 6, 7, 11, 15); + P2(D, E, A, B, C, 7, 9, 4, 5); + P2(C, D, E, A, B, 8, 11, 13, 7); + P2(B, C, D, E, A, 9, 13, 6, 7); + P2(A, B, C, D, E, 10, 14, 15, 8); + P2(E, A, B, C, D, 11, 15, 8, 11); + P2(D, E, A, B, C, 12, 6, 1, 14); + P2(C, D, E, A, B, 13, 7, 10, 14); + P2(B, C, D, E, A, 14, 9, 3, 12); + P2(A, B, C, D, E, 15, 8, 12, 6); #undef F #undef K #undef Fp #undef Kp -#define F F2 -#define K 0x5A827999 -#define Fp F4 -#define Kp 0x5C4DD124 - P2( E, A, B, C, D, 7, 7, 6, 9 ); - P2( D, E, A, B, C, 4, 6, 11, 13 ); - P2( C, D, E, A, B, 13, 8, 3, 15 ); - P2( B, C, D, E, A, 1, 13, 7, 7 ); - P2( A, B, C, D, E, 10, 11, 0, 12 ); - P2( E, A, B, C, D, 6, 9, 13, 8 ); - P2( D, E, A, B, C, 15, 7, 5, 9 ); - P2( C, D, E, A, B, 3, 15, 10, 11 ); - P2( B, C, D, E, A, 12, 7, 14, 7 ); - P2( A, B, C, D, E, 0, 12, 15, 7 ); - P2( E, A, B, C, D, 9, 15, 8, 12 ); - P2( D, E, A, B, C, 5, 9, 12, 7 ); - P2( C, D, E, A, B, 2, 11, 4, 6 ); - P2( B, C, D, E, A, 14, 7, 9, 15 ); - P2( A, B, C, D, E, 11, 13, 1, 13 ); - P2( E, A, B, C, D, 8, 12, 2, 11 ); +#define F F2 +#define K 0x5A827999 +#define Fp F4 +#define Kp 0x5C4DD124 + P2(E, A, B, C, D, 7, 7, 6, 9); + P2(D, E, A, B, C, 4, 6, 11, 13); + P2(C, D, E, A, B, 13, 8, 3, 15); + P2(B, C, D, E, A, 1, 13, 7, 7); + P2(A, B, C, D, E, 10, 11, 0, 12); + P2(E, A, B, C, D, 6, 9, 13, 8); + P2(D, E, A, B, C, 15, 7, 5, 9); + P2(C, D, E, A, B, 3, 15, 10, 11); + P2(B, C, D, E, A, 12, 7, 14, 7); + P2(A, B, C, D, E, 0, 12, 15, 7); + P2(E, A, B, C, D, 9, 15, 8, 12); + P2(D, E, A, B, C, 5, 9, 12, 7); + P2(C, D, E, A, B, 2, 11, 4, 6); + P2(B, C, D, E, A, 14, 7, 9, 15); + P2(A, B, C, D, E, 11, 13, 1, 13); + P2(E, A, B, C, D, 8, 12, 2, 11); #undef F #undef K #undef Fp #undef Kp -#define F F3 -#define K 0x6ED9EBA1 -#define Fp F3 -#define Kp 0x6D703EF3 - P2( D, E, A, B, C, 3, 11, 15, 9 ); - P2( C, D, E, A, B, 10, 13, 5, 7 ); - P2( B, C, D, E, A, 14, 6, 1, 15 ); - P2( A, B, C, D, E, 4, 7, 3, 11 ); - P2( E, A, B, C, D, 9, 14, 7, 8 ); - P2( D, E, A, B, C, 15, 9, 14, 6 ); - P2( C, D, E, A, B, 8, 13, 6, 6 ); - P2( B, C, D, E, A, 1, 15, 9, 14 ); - P2( A, B, C, D, E, 2, 14, 11, 12 ); - P2( E, A, B, C, D, 7, 8, 8, 13 ); - P2( D, E, A, B, C, 0, 13, 12, 5 ); - P2( C, D, E, A, B, 6, 6, 2, 14 ); - P2( B, C, D, E, A, 13, 5, 10, 13 ); - P2( A, B, C, D, E, 11, 12, 0, 13 ); - P2( E, A, B, C, D, 5, 7, 4, 7 ); - P2( D, E, A, B, C, 12, 5, 13, 5 ); +#define F F3 +#define K 0x6ED9EBA1 +#define Fp F3 +#define Kp 0x6D703EF3 + P2(D, E, A, B, C, 3, 11, 15, 9); + P2(C, D, E, A, B, 10, 13, 5, 7); + P2(B, C, D, E, A, 14, 6, 1, 15); + P2(A, B, C, D, E, 4, 7, 3, 11); + P2(E, A, B, C, D, 9, 14, 7, 8); + P2(D, E, A, B, C, 15, 9, 14, 6); + P2(C, D, E, A, B, 8, 13, 6, 6); + P2(B, C, D, E, A, 1, 15, 9, 14); + P2(A, B, C, D, E, 2, 14, 11, 12); + P2(E, A, B, C, D, 7, 8, 8, 13); + P2(D, E, A, B, C, 0, 13, 12, 5); + P2(C, D, E, A, B, 6, 6, 2, 14); + P2(B, C, D, E, A, 13, 5, 10, 13); + P2(A, B, C, D, E, 11, 12, 0, 13); + P2(E, A, B, C, D, 5, 7, 4, 7); + P2(D, E, A, B, C, 12, 5, 13, 5); #undef F #undef K #undef Fp #undef Kp -#define F F4 -#define K 0x8F1BBCDC -#define Fp F2 -#define Kp 0x7A6D76E9 - P2( C, D, E, A, B, 1, 11, 8, 15 ); - P2( B, C, D, E, A, 9, 12, 6, 5 ); - P2( A, B, C, D, E, 11, 14, 4, 8 ); - P2( E, A, B, C, D, 10, 15, 1, 11 ); - P2( D, E, A, B, C, 0, 14, 3, 14 ); - P2( C, D, E, A, B, 8, 15, 11, 14 ); - P2( B, C, D, E, A, 12, 9, 15, 6 ); - P2( A, B, C, D, E, 4, 8, 0, 14 ); - P2( E, A, B, C, D, 13, 9, 5, 6 ); - P2( D, E, A, B, C, 3, 14, 12, 9 ); - P2( C, D, E, A, B, 7, 5, 2, 12 ); - P2( B, C, D, E, A, 15, 6, 13, 9 ); - P2( A, B, C, D, E, 14, 8, 9, 12 ); - P2( E, A, B, C, D, 5, 6, 7, 5 ); - P2( D, E, A, B, C, 6, 5, 10, 15 ); - P2( C, D, E, A, B, 2, 12, 14, 8 ); +#define F F4 +#define K 0x8F1BBCDC +#define Fp F2 +#define Kp 0x7A6D76E9 + P2(C, D, E, A, B, 1, 11, 8, 15); + P2(B, C, D, E, A, 9, 12, 6, 5); + P2(A, B, C, D, E, 11, 14, 4, 8); + P2(E, A, B, C, D, 10, 15, 1, 11); + P2(D, E, A, B, C, 0, 14, 3, 14); + P2(C, D, E, A, B, 8, 15, 11, 14); + P2(B, C, D, E, A, 12, 9, 15, 6); + P2(A, B, C, D, E, 4, 8, 0, 14); + P2(E, A, B, C, D, 13, 9, 5, 6); + P2(D, E, A, B, C, 3, 14, 12, 9); + P2(C, D, E, A, B, 7, 5, 2, 12); + P2(B, C, D, E, A, 15, 6, 13, 9); + P2(A, B, C, D, E, 14, 8, 9, 12); + P2(E, A, B, C, D, 5, 6, 7, 5); + P2(D, E, A, B, C, 6, 5, 10, 15); + P2(C, D, E, A, B, 2, 12, 14, 8); #undef F #undef K #undef Fp #undef Kp -#define F F5 -#define K 0xA953FD4E -#define Fp F1 -#define Kp 0x00000000 - P2( B, C, D, E, A, 4, 9, 12, 8 ); - P2( A, B, C, D, E, 0, 15, 15, 5 ); - P2( E, A, B, C, D, 5, 5, 10, 12 ); - P2( D, E, A, B, C, 9, 11, 4, 9 ); - P2( C, D, E, A, B, 7, 6, 1, 12 ); - P2( B, C, D, E, A, 12, 8, 5, 5 ); - P2( A, B, C, D, E, 2, 13, 8, 14 ); - P2( E, A, B, C, D, 10, 12, 7, 6 ); - P2( D, E, A, B, C, 14, 5, 6, 8 ); - P2( C, D, E, A, B, 1, 12, 2, 13 ); - P2( B, C, D, E, A, 3, 13, 13, 6 ); - P2( A, B, C, D, E, 8, 14, 14, 5 ); - P2( E, A, B, C, D, 11, 11, 0, 15 ); - P2( D, E, A, B, C, 6, 8, 3, 13 ); - P2( C, D, E, A, B, 15, 5, 9, 11 ); - P2( B, C, D, E, A, 13, 6, 11, 11 ); +#define F F5 +#define K 0xA953FD4E +#define Fp F1 +#define Kp 0x00000000 + P2(B, C, D, E, A, 4, 9, 12, 8); + P2(A, B, C, D, E, 0, 15, 15, 5); + P2(E, A, B, C, D, 5, 5, 10, 12); + P2(D, E, A, B, C, 9, 11, 4, 9); + P2(C, D, E, A, B, 7, 6, 1, 12); + P2(B, C, D, E, A, 12, 8, 5, 5); + P2(A, B, C, D, E, 2, 13, 8, 14); + P2(E, A, B, C, D, 10, 12, 7, 6); + P2(D, E, A, B, C, 14, 5, 6, 8); + P2(C, D, E, A, B, 1, 12, 2, 13); + P2(B, C, D, E, A, 3, 13, 13, 6); + P2(A, B, C, D, E, 8, 14, 14, 5); + P2(E, A, B, C, D, 11, 11, 0, 15); + P2(D, E, A, B, C, 6, 8, 3, 13); + P2(C, D, E, A, B, 15, 5, 9, 11); + P2(B, C, D, E, A, 13, 6, 11, 11); #undef F #undef K #undef Fp #undef Kp - C = ctx->state[1] + C + Dp; - ctx->state[1] = ctx->state[2] + D + Ep; - ctx->state[2] = ctx->state[3] + E + Ap; - ctx->state[3] = ctx->state[4] + A + Bp; - ctx->state[4] = ctx->state[0] + B + Cp; - ctx->state[0] = C; + C = ctx->state[1] + C + Dp; + ctx->state[1] = ctx->state[2] + D + Ep; + ctx->state[2] = ctx->state[3] + E + Ap; + ctx->state[3] = ctx->state[4] + A + Bp; + ctx->state[4] = ctx->state[0] + B + Cp; + ctx->state[0] = C; } #endif /* !MBEDTLS_RIPEMD160_PROCESS_ALT */ /* * RIPEMD-160 process buffer */ -void ripemd160_Update( RIPEMD160_CTX *ctx, const uint8_t *input, uint32_t ilen ) -{ - uint32_t fill; - uint32_t left; - - if( ilen == 0 ) - return; - - left = ctx->total[0] & 0x3F; - fill = RIPEMD160_BLOCK_LENGTH - left; - - ctx->total[0] += (uint32_t) ilen; - ctx->total[0] &= 0xFFFFFFFF; - - if( ctx->total[0] < (uint32_t) ilen ) - ctx->total[1]++; - - if( left && ilen >= fill ) - { - memcpy( (void *) (ctx->buffer + left), input, fill ); - ripemd160_process( ctx, ctx->buffer ); - input += fill; - ilen -= fill; - left = 0; - } - - while( ilen >= RIPEMD160_BLOCK_LENGTH ) - { - ripemd160_process( ctx, input ); - input += RIPEMD160_BLOCK_LENGTH; - ilen -= RIPEMD160_BLOCK_LENGTH; - } - - if( ilen > 0 ) - { - memcpy( (void *) (ctx->buffer + left), input, ilen ); - } +void ripemd160_Update(RIPEMD160_CTX* ctx, const uint8_t* input, uint32_t ilen) { + uint32_t fill; + uint32_t left; + + if (ilen == 0) return; + + left = ctx->total[0] & 0x3F; + fill = RIPEMD160_BLOCK_LENGTH - left; + + ctx->total[0] += (uint32_t)ilen; + ctx->total[0] &= 0xFFFFFFFF; + + if (ctx->total[0] < (uint32_t)ilen) ctx->total[1]++; + + if (left && ilen >= fill) { + memcpy((void*)(ctx->buffer + left), input, fill); + ripemd160_process(ctx, ctx->buffer); + input += fill; + ilen -= fill; + left = 0; + } + + while (ilen >= RIPEMD160_BLOCK_LENGTH) { + ripemd160_process(ctx, input); + input += RIPEMD160_BLOCK_LENGTH; + ilen -= RIPEMD160_BLOCK_LENGTH; + } + + if (ilen > 0) { + memcpy((void*)(ctx->buffer + left), input, ilen); + } } -static const uint8_t ripemd160_padding[RIPEMD160_BLOCK_LENGTH] = -{ - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; +static const uint8_t ripemd160_padding[RIPEMD160_BLOCK_LENGTH] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* * RIPEMD-160 final digest */ -void ripemd160_Final( RIPEMD160_CTX *ctx, uint8_t output[RIPEMD160_DIGEST_LENGTH] ) -{ - uint32_t last, padn; - uint32_t high, low; - uint8_t msglen[8]; +void ripemd160_Final(RIPEMD160_CTX* ctx, + uint8_t output[RIPEMD160_DIGEST_LENGTH]) { + uint32_t last, padn; + uint32_t high, low; + uint8_t msglen[8]; - high = ( ctx->total[0] >> 29 ) - | ( ctx->total[1] << 3 ); - low = ( ctx->total[0] << 3 ); + high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); + low = (ctx->total[0] << 3); - PUT_UINT32_LE( low, msglen, 0 ); - PUT_UINT32_LE( high, msglen, 4 ); + PUT_UINT32_LE(low, msglen, 0); + PUT_UINT32_LE(high, msglen, 4); - last = ctx->total[0] & 0x3F; - padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); + last = ctx->total[0] & 0x3F; + padn = (last < 56) ? (56 - last) : (120 - last); - ripemd160_Update( ctx, ripemd160_padding, padn ); - ripemd160_Update( ctx, msglen, 8 ); + ripemd160_Update(ctx, ripemd160_padding, padn); + ripemd160_Update(ctx, msglen, 8); - PUT_UINT32_LE( ctx->state[0], output, 0 ); - PUT_UINT32_LE( ctx->state[1], output, 4 ); - PUT_UINT32_LE( ctx->state[2], output, 8 ); - PUT_UINT32_LE( ctx->state[3], output, 12 ); - PUT_UINT32_LE( ctx->state[4], output, 16 ); + PUT_UINT32_LE(ctx->state[0], output, 0); + PUT_UINT32_LE(ctx->state[1], output, 4); + PUT_UINT32_LE(ctx->state[2], output, 8); + PUT_UINT32_LE(ctx->state[3], output, 12); + PUT_UINT32_LE(ctx->state[4], output, 16); - memzero(ctx, sizeof(RIPEMD160_CTX)); + memzero(ctx, sizeof(RIPEMD160_CTX)); } /* * output = RIPEMD-160( input buffer ) */ -void ripemd160(const uint8_t *msg, uint32_t msg_len, uint8_t hash[RIPEMD160_DIGEST_LENGTH]) -{ - RIPEMD160_CTX ctx; - ripemd160_Init( &ctx ); - ripemd160_Update( &ctx, msg, msg_len ); - ripemd160_Final( &ctx, hash ); +void ripemd160(const uint8_t* msg, uint32_t msg_len, + uint8_t hash[RIPEMD160_DIGEST_LENGTH]) { + RIPEMD160_CTX ctx; + ripemd160_Init(&ctx); + ripemd160_Update(&ctx, msg, msg_len); + ripemd160_Final(&ctx, hash); } diff --git a/c/ripemd160/ripemd160.h b/c/ripemd160/ripemd160.h index 8256b08a..637abbf8 100644 --- a/c/ripemd160/ripemd160.h +++ b/c/ripemd160/ripemd160.h @@ -12,11 +12,11 @@ typedef struct _RIPEMD160_CTX { uint8_t buffer[RIPEMD160_BLOCK_LENGTH]; /*!< data block being processed */ } RIPEMD160_CTX; -void ripemd160_Init(RIPEMD160_CTX *ctx); -void ripemd160_Update(RIPEMD160_CTX *ctx, const uint8_t *input, uint32_t ilen); -void ripemd160_Final(RIPEMD160_CTX *ctx, +void ripemd160_Init(RIPEMD160_CTX* ctx); +void ripemd160_Update(RIPEMD160_CTX* ctx, const uint8_t* input, uint32_t ilen); +void ripemd160_Final(RIPEMD160_CTX* ctx, uint8_t output[RIPEMD160_DIGEST_LENGTH]); -void ripemd160(const uint8_t *msg, uint32_t msg_len, +void ripemd160(const uint8_t* msg, uint32_t msg_len, uint8_t hash[RIPEMD160_DIGEST_LENGTH]); #endif diff --git a/c/validator.c b/c/validator.c index 6905077b..fe6db9d2 100644 --- a/c/validator.c +++ b/c/validator.c @@ -4,7 +4,8 @@ * 1. The kv state changes are valid * - verify old state * - verify new state - * 2. The entrance account script is valid (lazy, verify when load account script) + * 2. The entrance account script is valid (lazy, verify when load account + * script) * 3. Verify new accounts * 4. Verify return data: hash(return_data) == return_data_hash */ @@ -13,6 +14,4 @@ #include "polyjuice.h" -int main() { - return run_polyjuice(); -} +int main() { return run_polyjuice(); } diff --git a/c/validator/secp256k1_helper.h b/c/validator/secp256k1_helper.h index eb31b5be..d164b838 100644 --- a/c/validator/secp256k1_helper.h +++ b/c/validator/secp256k1_helper.h @@ -75,7 +75,7 @@ int ckb_secp256k1_custom_verify_only_initialize(secp256k1_context* context, secp256k1_ecmult_gen_context_init(&context->ecmult_gen_ctx); /* Recasting data to (uint8_t*) for pointer math */ - uint8_t* p = (uint8_t *)data; + uint8_t* p = (uint8_t*)data; secp256k1_ge_storage(*pre_g)[] = (secp256k1_ge_storage(*)[])p; secp256k1_ge_storage(*pre_g_128)[] = (secp256k1_ge_storage(*)[])(&p[CKB_SECP256K1_DATA_PRE_SIZE]);