Add Swap feature

This commit is contained in:
TamtamHero
2020-06-29 15:43:02 +02:00
parent 2027fecffa
commit 9d4dfbe1bf
31 changed files with 650 additions and 312 deletions

View File

@@ -32,8 +32,6 @@
#include "ethUtils.h"
#include "chainConfig.h"
extern chain_config_t *chainConfig;
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid) {
if (*buffer <= 0x7f) {
} else if (*buffer <= 0xb7) {
@@ -156,15 +154,16 @@ char convertDigit(uint8_t *address, uint8_t index, uint8_t *hash) {
}
void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
cx_sha3_t *sha3Context) {
cx_sha3_t *sha3Context, chain_config_t* chain_config) {
uint8_t hashAddress[32];
cx_keccak_init(sha3Context, 256);
cx_hash((cx_hash_t*)sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress, 32);
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context);
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chain_config);
}
void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
cx_sha3_t *sha3Context) {
cx_sha3_t *sha3Context, chain_config_t* chain_config) {
UNUSED(chain_config);
uint8_t hashChecksum[32];
uint8_t i;
cx_keccak_init(sha3Context, 256);
@@ -180,37 +179,43 @@ void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
static const uint8_t const HEXDIGITS[] = "0123456789abcdef";
void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
cx_sha3_t *sha3Context) {
cx_sha3_t *sha3Context, chain_config_t* chain_config) {
uint8_t hashAddress[32];
cx_keccak_init(sha3Context, 256);
cx_hash((cx_hash_t*)sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress, 32);
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context);
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chain_config);
}
void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
cx_sha3_t *sha3Context) {
uint8_t hashChecksum[32];
uint8_t tmp[100];
cx_sha3_t *sha3Context, chain_config_t* chain_config) {
// save some precious stack space
union locals_union
{
uint8_t hashChecksum[32];
uint8_t tmp[51];
} locals_union;
uint8_t i;
bool eip1191 = false;
uint32_t offset = 0;
switch(chainConfig->chainId) {
switch(chain_config->chainId) {
case 30:
case 31:
eip1191 = true;
break;
}
if (eip1191) {
snprintf((char*)tmp, sizeof(tmp), "%d0x", chainConfig->chainId);
offset = strlen((char*)tmp);
snprintf((char*)locals_union.tmp, sizeof(locals_union.tmp), "%d0x", chain_config->chainId);
offset = strlen((char*)locals_union.tmp);
}
for (i = 0; i < 20; i++) {
uint8_t digit = address[i];
tmp[offset + 2 * i] = HEXDIGITS[(digit >> 4) & 0x0f];
tmp[offset + 2 * i + 1] = HEXDIGITS[digit & 0x0f];
locals_union.tmp[offset + 2 * i] = HEXDIGITS[(digit >> 4) & 0x0f];
locals_union.tmp[offset + 2 * i + 1] = HEXDIGITS[digit & 0x0f];
}
cx_keccak_init(sha3Context, 256);
cx_hash((cx_hash_t*)sha3Context, CX_LAST, tmp, offset + 40, hashChecksum, 32);
cx_hash((cx_hash_t*)sha3Context, CX_LAST, locals_union.tmp, offset + 40, locals_union.hashChecksum, 32);
for (i = 0; i < 40; i++) {
uint8_t digit = address[i / 2];
if ((i % 2) == 0) {
@@ -222,7 +227,7 @@ void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
out[i] = HEXDIGITS[digit];
}
else {
int v = (hashChecksum[i / 2] >> (4 * (1 - i % 2))) & 0x0f;
int v = (locals_union.hashChecksum[i / 2] >> (4 * (1 - i % 2))) & 0x0f;
if (v >= 8) {
out[i] = HEXDIGITS[digit] - 'a' + 'A';
}

View File

@@ -21,7 +21,7 @@
#include <stdint.h>
#include "cx.h"
#include "chainConfig.h"
/**
* @brief Decode an RLP encoded field - see
* https://github.com/ethereum/wiki/wiki/RLP
@@ -43,14 +43,17 @@ void getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
cx_sha3_t *sha3Context);
void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
cx_sha3_t *sha3Context);
cx_sha3_t *sha3Context,
chain_config_t* chain_config);
void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
cx_sha3_t *sha3Context);
cx_sha3_t *sha3Context,
chain_config_t* chain_config);
bool adjustDecimals(char *src, uint32_t srcLength, char *target,
uint32_t targetLength, uint8_t decimals);
__attribute__((no_instrument_function))
inline int allzeroes(uint8_t *buf, int n) {
for (int i = 0; i < n; ++i) {
if (buf[i]) {
@@ -59,7 +62,7 @@ inline int allzeroes(uint8_t *buf, int n) {
}
return 1;
}
__attribute__((no_instrument_function))
inline int ismaxint(uint8_t *buf, int n) {
for (int i = 0; i < n; ++i) {
if (buf[i] != 0xff) {

View File

@@ -325,98 +325,40 @@ void mul128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
add128(&tmp, &tmp2, target);
}
void write_u64_be(uint8_t *buffer, uint64_t value) {
buffer[0] = ((value >> 56) & 0xff);
buffer[1] = ((value >> 48) & 0xff);
buffer[2] = ((value >> 40) & 0xff);
buffer[3] = ((value >> 32) & 0xff);
buffer[4] = ((value >> 24) & 0xff);
buffer[5] = ((value >> 16) & 0xff);
buffer[6] = ((value >> 8) & 0xff);
buffer[7] = (value & 0xff);
}
void read_u64_be(uint8_t *in, uint64_t *out) {
uint8_t *out_ptr = (uint8_t*)out;
*out_ptr++ = in[7];
*out_ptr++ = in[6];
*out_ptr++ = in[5];
*out_ptr++ = in[4];
*out_ptr++ = in[3];
*out_ptr++ = in[2];
*out_ptr++ = in[1];
*out_ptr = in[0];
}
void mul256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
uint128_t top[4];
uint128_t bottom[4];
uint128_t products[4][4];
uint128_t tmp, tmp2, fourth64, third64, second64, first64;
uint256_t target1, target2;
UPPER(top[0]) = 0;
LOWER(top[0]) = UPPER(UPPER_P(number1));
UPPER(top[1]) = 0;
LOWER(top[1]) = LOWER(UPPER_P(number1));
UPPER(top[2]) = 0;
LOWER(top[2]) = UPPER(LOWER_P(number1));
UPPER(top[3]) = 0;
LOWER(top[3]) = LOWER(LOWER_P(number1));
UPPER(bottom[0]) = 0;
LOWER(bottom[0]) = UPPER(UPPER_P(number2));
UPPER(bottom[1]) = 0;
LOWER(bottom[1]) = LOWER(UPPER_P(number2));
UPPER(bottom[2]) = 0;
LOWER(bottom[2]) = UPPER(LOWER_P(number2));
UPPER(bottom[3]) = 0;
LOWER(bottom[3]) = LOWER(LOWER_P(number2));
for (int y = 3; y > -1; y--) {
for (int x = 3; x > -1; x--) {
mul128(&top[x], &bottom[y], &products[3 - x][y]);
}
uint8_t num1[32], num2[32], result[64];
os_memset(&result, 0, sizeof(result));
for(uint8_t i = 0; i<4; i++){
write_u64_be(num1+i*sizeof(uint64_t), number1->elements[i/2].elements[i%2]);
write_u64_be(num2+i*sizeof(uint64_t), number2->elements[i/2].elements[i%2]);
}
cx_math_mult(result, num1, num2, sizeof(num1));
for(uint8_t i = 0; i<4; i++){
read_u64_be(result+32+i*sizeof(uint64_t), &target->elements[i/2].elements[i%2]);
}
UPPER(fourth64) = 0;
LOWER(fourth64) = LOWER(products[0][3]);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[0][2]);
UPPER(tmp2) = 0;
LOWER(tmp2) = UPPER(products[0][3]);
add128(&tmp, &tmp2, &third64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[0][1]);
UPPER(tmp2) = 0;
LOWER(tmp2) = UPPER(products[0][2]);
add128(&tmp, &tmp2, &second64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[0][0]);
UPPER(tmp2) = 0;
LOWER(tmp2) = UPPER(products[0][1]);
add128(&tmp, &tmp2, &first64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[1][3]);
add128(&tmp, &third64, &tmp2);
copy128(&third64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[1][2]);
add128(&tmp, &second64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = UPPER(products[1][3]);
add128(&tmp, &tmp2, &second64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[1][1]);
add128(&tmp, &first64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = UPPER(products[1][2]);
add128(&tmp, &tmp2, &first64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[2][3]);
add128(&tmp, &second64, &tmp2);
copy128(&second64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[2][2]);
add128(&tmp, &first64, &tmp2);
UPPER(tmp) = 0;
LOWER(tmp) = UPPER(products[2][3]);
add128(&tmp, &tmp2, &first64);
UPPER(tmp) = 0;
LOWER(tmp) = LOWER(products[3][3]);
add128(&tmp, &first64, &tmp2);
copy128(&first64, &tmp2);
clear256(&target1);
shiftl128(&first64, 64, &UPPER(target1));
clear256(&target2);
UPPER(UPPER(target2)) = UPPER(third64);
shiftl128(&third64, 64, &LOWER(target2));
add256(&target1, &target2, target);
clear256(&target1);
copy128(&UPPER(target1), &second64);
add256(&target1, target, &target2);
clear256(&target1);
copy128(&LOWER(target1), &fourth64);
add256(&target1, &target2, target);
}
void divmod128(uint128_t *l, uint128_t *r, uint128_t *retDiv,

View File

@@ -22,6 +22,8 @@
#include <stdint.h>
#include <stdbool.h>
#include "os.h"
#include "cx.h"
typedef struct uint128_t { uint64_t elements[2]; } uint128_t;