mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-03 03:50:58 +00:00
remove t.c.base58.*_check functions from module (this is app logic), add t.c.sha256 module
This commit is contained in:
parent
2694e3ad63
commit
ed39c2001e
3
emu.sh
3
emu.sh
@ -1,5 +1,4 @@
|
||||
#!/bin/bash
|
||||
cd `dirname $0`/src
|
||||
|
||||
rm -f ../pipe.*
|
||||
../vendor/micropython/unix/micropython -O0 -X heapsize=100000 main.py
|
||||
../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py
|
||||
|
@ -31,18 +31,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Base58_encode(mp_obj_t self, mp_obj_t data) {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_encode_obj, mod_TrezorCrypto_Base58_encode);
|
||||
|
||||
// def Base58.encode_check(self, data: bytes) -> str
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Base58_encode_check(mp_obj_t self, mp_obj_t data) {
|
||||
mp_buffer_info_t databuf;
|
||||
mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ);
|
||||
vstr_t vstr;
|
||||
vstr_init(&vstr, (databuf.len + 4) * 8000 / 5857 + 1); // 256 = 2^8 ; 58 > 2^5.857
|
||||
vstr.len = base58_encode_check(databuf.buf, databuf.len, vstr.buf, vstr.alloc);
|
||||
vstr.len--; // base58_encode_check returns length including the trailing zero
|
||||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_encode_check_obj, mod_TrezorCrypto_Base58_encode_check);
|
||||
|
||||
// def Base58.decode(self, string: str) -> bytes
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Base58_decode(mp_obj_t self, mp_obj_t string) {
|
||||
mp_buffer_info_t stringbuf;
|
||||
@ -54,24 +42,11 @@ STATIC mp_obj_t mod_TrezorCrypto_Base58_decode(mp_obj_t self, mp_obj_t string) {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_decode_obj, mod_TrezorCrypto_Base58_decode);
|
||||
|
||||
// def Base58.decode_check(self, string: str) -> bytes
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Base58_decode_check(mp_obj_t self, mp_obj_t string) {
|
||||
mp_buffer_info_t stringbuf;
|
||||
mp_get_buffer_raise(string, &stringbuf, MP_BUFFER_READ);
|
||||
vstr_t vstr;
|
||||
vstr_init(&vstr, stringbuf.len * 5858 / 8000 + 1); // 256 = 2^8 ; 58 < 2^5.858
|
||||
vstr.len = base58_decode_check(stringbuf.buf, (uint8_t *)vstr.buf, vstr.alloc);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_decode_check_obj, mod_TrezorCrypto_Base58_decode_check);
|
||||
|
||||
// Base58 stuff
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Base58_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&mod_TrezorCrypto_Base58_encode_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_encode_check), MP_ROM_PTR(&mod_TrezorCrypto_Base58_encode_check_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&mod_TrezorCrypto_Base58_decode_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_decode_check), MP_ROM_PTR(&mod_TrezorCrypto_Base58_decode_check_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Base58_locals_dict, mod_TrezorCrypto_Base58_locals_dict_table);
|
||||
|
||||
|
45
extmod/modTrezorCrypto/modTrezorCrypto-sha256.h
Normal file
45
extmod/modTrezorCrypto/modTrezorCrypto-sha256.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
||||
*
|
||||
* Licensed under Microsoft Reference Source License (Ms-RSL)
|
||||
* see LICENSE.md file for details
|
||||
*/
|
||||
|
||||
#include "trezor-crypto/sha2.h"
|
||||
|
||||
// class Sha256(object):
|
||||
typedef struct _mp_obj_Sha256_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Sha256_t;
|
||||
|
||||
// def Sha256.__init__(self):
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
mp_obj_Sha256_t *o = m_new_obj(mp_obj_Sha256_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
// def Sha256.hash(self, data: bytes) -> bytes
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Sha256_hash(mp_obj_t self, mp_obj_t data) {
|
||||
mp_buffer_info_t databuf;
|
||||
mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32); // 256 bit = 32 bytes
|
||||
sha256_Raw(databuf.buf, databuf.len, (uint8_t *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha256_hash_obj, mod_TrezorCrypto_Sha256_hash);
|
||||
|
||||
// Sha256 stuff
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha256_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_hash), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_hash_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha256_locals_dict, mod_TrezorCrypto_Sha256_locals_dict_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_TrezorCrypto_Sha256_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Sha256,
|
||||
.make_new = mod_TrezorCrypto_Sha256_make_new,
|
||||
.locals_dict = (void*)&mod_TrezorCrypto_Sha256_locals_dict,
|
||||
};
|
@ -16,12 +16,14 @@
|
||||
#if MICROPY_PY_TREZORCRYPTO
|
||||
|
||||
#include "modTrezorCrypto-base58.h"
|
||||
#include "modTrezorCrypto-sha256.h"
|
||||
|
||||
// module stuff
|
||||
|
||||
STATIC const mp_rom_map_elem_t mp_module_TrezorCrypto_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorCrypto) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Base58), MP_ROM_PTR(&mod_TrezorCrypto_Base58_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Sha256), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_type) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorCrypto_globals, mp_module_TrezorCrypto_globals_table);
|
||||
|
@ -1,3 +1,4 @@
|
||||
from TrezorCrypto import Base58
|
||||
from TrezorCrypto import Base58, Sha256
|
||||
|
||||
base58 = Base58()
|
||||
sha256 = Sha256()
|
||||
|
Loading…
Reference in New Issue
Block a user