Skip to content

Commit

Permalink
SFT-2271: Use foundation-rs crates
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Pierre De Jesus DIAZ <[email protected]>
  • Loading branch information
jeandudey committed Jul 14, 2023
1 parent a4ca850 commit 3f1aca7
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 186 deletions.
76 changes: 45 additions & 31 deletions extmod/foundation-rust/Cargo.lock

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

12 changes: 6 additions & 6 deletions extmod/foundation-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ default-features = false
version = "1"
default-features = false

[dependencies.ur]
git = "https://github.com/Foundation-Devices/ur-rs"
branch = "dev"
[dependencies.foundation-ur]
git = "https://github.com/Foundation-Devices/foundation-rs"
branch = "main"
default-features = false

[dependencies.ur-foundation]
git = "https://github.com/Foundation-Devices/rust-ur-foundation"
branch = "dev-v0.1.0"
[dependencies.foundation-urtypes]
git = "https://github.com/Foundation-Devices/foundation-rs"
branch = "main"
default-features = false

[target.'cfg(target_arch = "arm")'.dependencies.cortex-m]
Expand Down
5 changes: 0 additions & 5 deletions extmod/foundation-rust/include/foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@
*/
#define UR_NETWORK_TESTNET 1

/**
* Maximum number of components that a `crypto-keypath` can have.
*/
#define UR_MAX_PATH_COMPONENTS 4

typedef enum {
BTC,
} UR_CoinType;
Expand Down
26 changes: 11 additions & 15 deletions extmod/foundation-rust/src/ur/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

use core::{fmt, slice, str};

use ur::UR;
use ur_foundation::{ur, ur::decoder::Error};

use crate::ur::{
max_fragment_len, max_message_len, registry::UR_Value, UR_Error,
use foundation_ur::{
bytewords, bytewords::Style, decoder::Error, max_fragment_len,
HeaplessDecoder, UR,
};

use crate::ur::{max_message_len, registry::UR_Value, UR_Error, UR_MAX_TYPE};

/// Maximum size of an encoded Uniform Resource.
///
/// This value assumes a QR code of version of 16 with ECC L using
Expand All @@ -25,7 +25,7 @@ pub const UR_DECODER_MAX_SEQUENCE_COUNT: usize = 128;

/// Maximum fragment length.
pub const UR_DECODER_MAX_FRAGMENT_LEN: usize =
max_fragment_len(UR_DECODER_MAX_STRING);
max_fragment_len(UR_MAX_TYPE, usize::MAX, UR_DECODER_MAX_STRING);

/// Maximum message length that can be decoded.
pub const UR_DECODER_MAX_MESSAGE_LEN: usize = 24 * 1024;
Expand All @@ -49,7 +49,7 @@ pub const UR_DECODER_MAX_UR_TYPE: usize = "crypto-coin-info".len();
#[used]
#[cfg_attr(dtcm, link_section = ".dtcm")]
pub static mut UR_DECODER: UR_Decoder = UR_Decoder {
inner: ur::HeaplessDecoder::new_heapless(),
inner: HeaplessDecoder::new(),
};

/// cbindgen:ignore
Expand All @@ -62,7 +62,7 @@ static mut UR_DECODER_SINGLE_PART_MESSAGE: heapless::Vec<

/// Uniform Resource decoder.
pub struct UR_Decoder {
inner: ur::HeaplessDecoder<
inner: HeaplessDecoder<
UR_DECODER_MAX_MESSAGE_LEN,
UR_DECODER_MAX_MIXED_PARTS,
UR_DECODER_MAX_FRAGMENT_LEN,
Expand Down Expand Up @@ -213,11 +213,7 @@ pub unsafe extern "C" fn ur_validate(ur: *const u8, ur_len: usize) -> bool {
.as_bytewords()
.expect("Parsed URs shouldn't be in a deserialized variant");

return ur::bytewords::validate(
bytewords,
ur::bytewords::Style::Minimal,
)
.is_ok();
return bytewords::validate(bytewords, Style::Minimal).is_ok();
}

false
Expand Down Expand Up @@ -256,11 +252,11 @@ pub unsafe extern "C" fn ur_decode_single_part(
.resize(UR_DECODER_MAX_SINGLE_PART_MESSAGE_LEN, 0)
.expect("Message buffer should have the same size as in the constant");

let result = ur::bytewords::decode_to_slice(
let result = bytewords::decode_to_slice(
ur.as_bytewords()
.expect("Uniform Resource should contain bytewords"),
message,
ur::bytewords::Style::Minimal,
Style::Minimal,
)
.map_err(|e| unsafe { UR_Error::other(&e) });

Expand Down
33 changes: 21 additions & 12 deletions extmod/foundation-rust/src/ur/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

use core::{ffi::c_char, fmt::Write};

use foundation_ur::{max_fragment_len, HeaplessEncoder};
use minicbor::{Encode, Encoder};

use crate::ur::{
decoder::UR_DECODER_MAX_MESSAGE_LEN, max_fragment_len, registry::UR_Value,
decoder::UR_DECODER_MAX_MESSAGE_LEN, registry::UR_Value, UR_MAX_TYPE,
};
use ur_foundation::ur;

/// Maximum size of an encoded Uniform Resource.
///
Expand All @@ -29,11 +31,11 @@ pub const UR_ENCODER_MIN_STRING: usize = 224;
/// parts that the message is divided into.
/// cbindgen:ignore
pub const UR_ENCODER_MAX_FRAGMENT_LEN: usize =
max_fragment_len(UR_ENCODER_MAX_STRING);
max_fragment_len(UR_MAX_TYPE, usize::MAX, UR_ENCODER_MAX_STRING);

/// Minimum fragment length.
pub const UR_ENCODER_MIN_FRAGMENT_LEN: usize =
max_fragment_len(UR_ENCODER_MIN_STRING);
max_fragment_len(UR_MAX_TYPE, usize::MAX, UR_ENCODER_MIN_STRING);

/// Maximum sequence count for the decoder.
///
Expand All @@ -52,7 +54,7 @@ pub const UR_ENCODER_MAX_MESSAGE_LEN: usize = UR_DECODER_MAX_MESSAGE_LEN;
#[used]
#[cfg_attr(dtcm, link_section = ".dtcm")]
pub static mut UR_ENCODER: UR_Encoder = UR_Encoder {
inner: ur::HeaplessEncoder::new_heapless(),
inner: HeaplessEncoder::new(),
};

/// cbindgen:ignore
Expand All @@ -69,7 +71,7 @@ static mut UR_ENCODER_MESSAGE: heapless::Vec<u8, UR_ENCODER_MAX_MESSAGE_LEN> =

/// Uniform Resource encoder.
pub struct UR_Encoder {
inner: ur::HeaplessEncoder<
inner: HeaplessEncoder<
'static,
'static,
UR_ENCODER_MAX_FRAGMENT_LEN,
Expand All @@ -82,7 +84,7 @@ pub struct UR_Encoder {
/// # Parameters
///
/// - `value` is the uniform resource to encode.
/// - `max_fragment_len` is the maximum fragment length in bytes.
/// - `max_chars` is the maximum fragment length in bytes.
///
/// # Safety
///
Expand All @@ -107,11 +109,18 @@ pub unsafe extern "C" fn ur_encoder_start(
let message = unsafe { &mut UR_ENCODER_MESSAGE };

message.clear();
value.encode(Writer(message)).expect("Couldn't encode UR");

encoder
.inner
.start(value.ur_type(), message, max_fragment_len(max_chars));
let mut e = Encoder::new(Writer(message));
value.encode(&mut e, &mut ()).expect("Couldn't encode UR");

encoder.inner.start(
value.ur_type(),
message,
max_fragment_len(
value.ur_type(),
UR_ENCODER_MAX_SEQUENCE_COUNT,
max_chars,
),
);
}

/// Returns the UR corresponding to the next fountain encoded part.
Expand Down
25 changes: 5 additions & 20 deletions extmod/foundation-rust/src/ur/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,23 @@
//! Uniform Resources.

use core::{ffi::c_char, fmt, fmt::Write};
use ur::fountain::part::Part;
use ur_foundation::ur;

/// cbindgen:ignore
#[used]
#[cfg_attr(sram4, link_section = ".sram4")]
static mut UR_ERROR: heapless::String<256> = heapless::String::new();

pub const fn max_fragment_len(max_characters: usize) -> usize {
const MAX_UR_PREFIX: usize = "ur:crypto-coin-info/".len()
+ digit_count(u32::MAX as usize)
+ "-".len()
+ digit_count(u32::MAX as usize)
+ "/".len();

let max_chars_for_part = max_characters.saturating_sub(MAX_UR_PREFIX);
let max_bytes_for_part = (max_chars_for_part / 2).saturating_sub(4);
max_bytes_for_part.saturating_sub(Part::max_encoded_len())
}
/// The maximum length of a UR type that will be accepted or produced.
/// cbindgen:ignore
const UR_MAX_TYPE: &str = "crypto-coin-info";

pub const fn max_message_len(max_characters: usize) -> usize {
const MAX_UR_PREFIX: usize = "ur:crypto-coin-info/".len();
const fn max_message_len(max_characters: usize) -> usize {
const MAX_UR_PREFIX: usize = "ur:".len() + UR_MAX_TYPE.len();

let max_chars_for_message = max_characters.saturating_sub(MAX_UR_PREFIX);
(max_chars_for_message / 2).saturating_sub(4)
}

/// Count the number of digits in a number.
pub const fn digit_count(v: usize) -> usize {
(v.ilog10() + 1) as usize
}

#[repr(C)]
pub enum UR_ErrorKind {
UR_ERROR_KIND_OTHER,
Expand Down
Loading

0 comments on commit 3f1aca7

Please sign in to comment.