Skip to content

Commit

Permalink
SFT-1909: Measure size
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Pierre De Jesus DIAZ <me@jeandudey.tech>
  • Loading branch information
jeandudey committed Apr 20, 2023
1 parent b3d7d15 commit a6f9138
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 9 deletions.
86 changes: 85 additions & 1 deletion extmod/foundation-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions extmod/foundation-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ license = "GPL-3.0-or-later"
name = "sizes"
required-features = ["std"]

[dependencies.bitcoin]
version = "0.30"
default-features = false
features = ["no-std", "rand"]

[dependencies.minicbor]
version = "0.19"
default-features = false
Expand Down
2 changes: 1 addition & 1 deletion extmod/foundation-rust/Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ generate: build

# Build the crate.
build:
cargo build --target thumbv7em-none-eabihf --release
cargo +nightly build --target thumbv7em-none-eabihf --release

# Lint the crate.
lint:
Expand Down
7 changes: 7 additions & 0 deletions extmod/foundation-rust/include/foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ typedef struct UR_Decoder UR_Decoder;
*/
typedef struct UR_Encoder UR_Encoder;

typedef struct secp256k1_keypair_t secp256k1_keypair_t;

typedef struct secp256k1_t secp256k1_t;

typedef struct {
UR_ErrorKind kind;
const char *message;
Expand Down Expand Up @@ -494,6 +498,9 @@ void ur_registry_new_passport_response(UR_Value *value,
const char *passport_firmware_version,
size_t passport_firmware_version_len);

void foundation_secp256k1_sign_schnorr(const secp256k1_t *secp,
const secp256k1_keypair_t *keypair);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
11 changes: 11 additions & 0 deletions extmod/foundation-rust/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use bitcoin::key::{KeyPair, Secp256k1};
use bitcoin::secp256k1::{All, Message};

pub struct secp256k1_t(Secp256k1<All>);

pub struct secp256k1_keypair_t(KeyPair);

#[no_mangle]
pub extern "C" fn foundation_secp256k1_sign_schnorr(secp: &secp256k1_t, keypair: &secp256k1_keypair_t) {
secp.0.sign_schnorr_with_aux_rand(&Message::from_slice(&[0]).unwrap(), &keypair.0, &[0; 32]);
}
41 changes: 41 additions & 0 deletions extmod/foundation-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,55 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(alloc_error_handler)]
#![allow(non_camel_case_types)]

extern crate alloc;

pub mod ur;
pub mod bitcoin;

/// cbindgen:ignore
#[cfg(not(feature = "std"))]
mod stdout;

#[cfg(not(feature = "std"))]
pub struct SystemAllocator;

#[cfg(not(feature = "std"))]
unsafe impl core::alloc::GlobalAlloc for SystemAllocator {
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
extern "C" {
fn malloc(size: usize) -> *mut core::ffi::c_void;
}

malloc(layout.size()) as *mut u8
}

unsafe fn dealloc(&self, ptr: *mut u8, _: core::alloc::Layout) {
extern "C" {
fn free(data: *mut core::ffi::c_void);
}

free(ptr as *mut core::ffi::c_void)
}
}

#[cfg(not(feature = "std"))]
#[global_allocator]
static mut SYSTEM_ALLOCATOR: SystemAllocator = SystemAllocator;

#[cfg(not(feature = "std"))]
#[alloc_error_handler]
fn alloc_error_handler(layout: core::alloc::Layout) -> ! {
use core::fmt::Write;

let mut stdout = stdout::Stdout;
writeln!(stdout, "could not allocate: {:?}", layout).ok();

loop {}
}

#[panic_handler]
#[cfg(all(not(feature = "std"), not(test)))]
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
Expand Down
33 changes: 26 additions & 7 deletions extmod/foundation/modfoundation.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "uECC.h"
#include "utils.h"

#include "foundation.h"

#include "modfoundation-bip39.h"
#include "modfoundation-qr.h"
#include "modfoundation-ur.h"
Expand Down Expand Up @@ -260,17 +262,34 @@ STATIC const mp_obj_type_t mp_type_fixedbytesio = {
.locals_dict = (mp_obj_dict_t *)&fixedbytesio_locals_dict,
};

/* Module Global configuration */
/* Define all properties of the module.
* Table entries are key/value pairs of the attribute name (a string)
* and the MicroPython object reference.
* All identifiers and strings are written as MP_QSTR_xxx and will be
* optimized to word-sized integers by the build system (interned strings).
*/
typedef struct _mp_obj_secp256k1_t {
mp_obj_base_t base;
const secp256k1_t *secp;
} mp_obj_secp256k1_t;


STATIC mp_obj_t secp256k1_sign_schnorr(mp_obj_t self_in) {
mp_obj_secp256k1_t *self = MP_OBJ_TO_PTR(self_in);
foundation_secp256k1_sign_schnorr(self->secp, NULL);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(secp256k1_sign_schnorr_obj, secp256k1_sign_schnorr);

STATIC const mp_rom_map_elem_t secp256k1_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_sign_schnorr), MP_ROM_PTR(&secp256k1_sign_schnorr_obj) },
};
STATIC MP_DEFINE_CONST_DICT(secp256k1_locals_dict, secp256k1_locals_dict_table);

STATIC const mp_obj_type_t mp_type_secp256k1 = {
{ &mp_type_type },
.name = MP_QSTR_Secp256k1,
.locals_dict = (mp_obj_dict_t *)&secp256k1_locals_dict,
};

STATIC const mp_rom_map_elem_t foundation_module_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_foundation)},
{MP_ROM_QSTR(MP_QSTR_FixedBytesIO), MP_ROM_PTR(&mp_type_fixedbytesio)},
{MP_ROM_QSTR(MP_QSTR_Secp56k1), MP_ROM_PTR(&mp_type_secp256k1)},
{MP_ROM_QSTR(MP_QSTR_bip39), MP_ROM_PTR(&mod_foundation_bip39_module)},
{MP_ROM_QSTR(MP_QSTR_qr), MP_ROM_PTR(&mod_foundation_qr_module)},
{MP_ROM_QSTR(MP_QSTR_ur), MP_ROM_PTR(&mod_foundation_ur_module)},
Expand Down

0 comments on commit a6f9138

Please sign in to comment.