Files
app-ethereum/src/stark_crypto.c

91 lines
3.5 KiB
C
Raw Normal View History

2020-06-27 13:24:04 +02:00
#ifdef HAVE_STARKWARE
#include "shared_context.h"
#include "stark_utils.h"
#include "ui_callbacks.h"
#include "utils.h"
#include "ethUtils.h"
2020-06-27 13:24:04 +02:00
2020-12-01 16:20:13 +01:00
static unsigned char const C_cx_Stark256_n[] = {
// n: 0x0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xb7, 0x81, 0x12, 0x6d, 0xca, 0xe7, 0xb2, 0x32, 0x1e, 0x66, 0xa2, 0x41, 0xad, 0xc6, 0x4d, 0x2f};
// C_cx_secp256k1_n - (C_cx_secp256k1_n % C_cx_Stark256_n)
2020-06-27 13:24:04 +02:00
static unsigned char const STARK_DERIVE_BIAS[] = {
2020-12-01 16:20:13 +01:00
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf7,
0x38, 0xa1, 0x3b, 0x4b, 0x92, 0x0e, 0x94, 0x11, 0xae, 0x6d, 0xa5, 0xf4, 0x0b, 0x03, 0x58, 0xb1};
2020-06-27 13:24:04 +02:00
2020-12-01 16:20:13 +01:00
void starkDerivePrivateKey(uint32_t *bip32Path, uint32_t bip32PathLength, uint8_t *privateKeyData) {
#if 0
// Sanity check
2020-06-27 13:24:04 +02:00
if (bip32Path[0] != STARK_BIP32_PATH_0) {
PRINTF("Invalid Stark derivation path %d\n", bip32Path[0]);
THROW(0x6a80);
}
os_perso_derive_node_bip32(CX_CURVE_256K1, bip32Path, bip32PathLength, privateKeyData, NULL);
2020-06-27 13:24:04 +02:00
PRINTF("Private key before processing %.*H\n", 32, privateKeyData);
// TODO - support additional schemes
cx_math_modm(privateKeyData, 32, C_cx_Stark256_n, 32);
PRINTF("Private key after processing %.*H\n", 32, privateKeyData);
#else
2020-12-01 16:20:13 +01:00
uint8_t tmp[33];
uint8_t index = 0;
// Sanity check
if ((bip32PathLength < 2) || (bip32Path[0] != STARK_BIP32_PATH_0) ||
(bip32Path[1] != STARK_BIP32_PATH_1)) {
PRINTF("Invalid Stark derivation path %d %d\n", bip32Path[0], bip32Path[1]);
THROW(0x6a80);
2020-06-27 13:24:04 +02:00
}
2020-12-01 16:20:13 +01:00
os_perso_derive_node_bip32(CX_CURVE_256K1, bip32Path, bip32PathLength, tmp, NULL);
PRINTF("Private key before processing %.*H\n", 32, tmp);
for (;;) {
tmp[32] = index;
cx_hash_sha256(tmp, 33, privateKeyData, 32);
PRINTF("Key hash %.*H\n", 32, privateKeyData);
if (cx_math_cmp(privateKeyData, STARK_DERIVE_BIAS, 32) < 0) {
cx_math_modm(privateKeyData, 32, C_cx_Stark256_n, 32);
break;
}
index++;
}
PRINTF("Key result %.*H\n", 32, privateKeyData);
2020-06-27 13:24:04 +02:00
2020-12-01 16:20:13 +01:00
#endif
2020-06-27 13:24:04 +02:00
}
2020-12-01 16:20:13 +01:00
void stark_get_amount_string(uint8_t *contractAddress,
uint8_t *quantum256,
uint8_t *amount64,
char *tmp100,
char *target100) {
uint256_t amountPre, quantum, amount;
uint8_t decimals;
char *ticker = chainConfig->coinName;
2020-06-27 13:24:04 +02:00
2020-12-01 16:20:13 +01:00
PRINTF("stark_get_amount_string %.*H\n", 20, contractAddress);
2020-06-27 13:24:04 +02:00
2020-12-01 16:20:13 +01:00
if (allzeroes(contractAddress, 20)) {
decimals = WEI_TO_ETHER;
PRINTF("stark_get_amount_string - ETH\n");
} else {
Add support for ERC-721 and ERC-1155 (v3) (#218) * First draft for erc721 token allowance * Split ui and provide parameters into their own files * Print txtype when not supported * fix compilation for erc721 * Use pluginType * Add debug statement in compound plugin * add debug error msg in plugin error * Add parameter parsing for all methods * Remove debug logs * Add SET_APPROVAL_FOR_ALL; Add correct parsing method on contract init * Add dst_size parameter to copy functions * Add query contract id code * format * Add UIs * update ethapp.asc * Change setExternalPlugin to setPlugin; Add support for ERC721 * clang-format * Fix typo Unconsistent -> Inconsistent * Add support for 721; use extraInfo * Add extraInfo to ethpluginQueryConractUI * Rename extraInfo to item * Add txFromEtherscan to tests * Add nft key and temp padding * Remove comments around HAVE_BYPASS_SIGNATURES * Rename TESTING_KEY to NFT_TESTING_KEY * Add comments regarding value of queryContractUI->item * Fix comment regarding method selector * Rename provideToken to provideInfo; Update plugin doc * fix caps of eth_plugin_prepare_provide_info * fix caps of handle_provide_info * Use verificationFn insead of hardcoded cx_ecdsa_verify * Add comments about nftInfo_t and tokenDefinition_t * Add erc721 test * Remove comment from plugin interface version * Fix network_ticker duplicate * Add setPlugin and provideNFTInfo to doc.asc * Add back setExternalPlugin; implement new setPlugin * Update plugin sdk * Call setPlugin instead of setExternalPlugin * setPlugin work without checking sig * Remove printf of displayed fees * Add working 721 test * Finalize ERC721 and add simple test * Display NFT address on set approval and operator * Support set approval for all for erc721 * Finish UI for set approval for all erc721 * Move copy_parameter and copy_address to eth_plugin_internal; Add tests for erc721 * update plugin sdk * Add erc1155 plugin and 1155 tests placeholder * Add restriction for AWS key and setPlugin * Add NOT_OLD_INTERNAL variant; Add erc_1155_plugin_call * Fixed compilation warnings (function pointer casting) Co-authored-by: pscott <scott.piriou@ledger.fr>
2021-11-22 14:39:36 +01:00
tokenDefinition_t *token = &getKnownToken(contractAddress)->token;
2020-12-01 16:20:13 +01:00
if (token == NULL) { // caught earlier
THROW(0x6A80);
}
decimals = token->decimals;
ticker = (char *) token->ticker;
PRINTF("stark_get_amount_string - decimals %d ticker %s\n", decimals, ticker);
2020-06-27 13:24:04 +02:00
}
2020-12-01 16:20:13 +01:00
convertUint256BE(amount64, 8, &amountPre);
readu256BE(quantum256, &quantum);
mul256(&amountPre, &quantum, &amount);
tostring256(&amount, 10, tmp100, 100);
PRINTF("stark_get_amount_string - mul256 %s\n", tmp100);
strlcpy(target100, ticker, 100);
2020-12-01 16:20:13 +01:00
adjustDecimals(tmp100, strlen(tmp100), target100 + strlen(ticker), 100, decimals);
PRINTF("get_amount_string %s\n", target100);
2020-06-27 13:24:04 +02:00
}
2020-12-01 16:20:13 +01:00
#endif // HAVE_STARK