Skip to content

Commit

Permalink
feat(core): add Zcash Rust primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
krnak committed Nov 16, 2022
1 parent 031bac4 commit 6311675
Show file tree
Hide file tree
Showing 43 changed files with 3,516 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/.changelog.d/2510.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Zcash Rust primitives
9 changes: 8 additions & 1 deletion core/SConscript.firmware
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ FEATURE_FLAGS = {
"RDI": True,
"SECP256K1_ZKP": True, # required for trezor.crypto.curve.bip340 (BIP340/Taproot)
"SYSTEM_VIEW": False,
"ZCASH_SHIELDED": False,
}

CCFLAGS_MOD = ''
Expand Down Expand Up @@ -202,6 +203,10 @@ if UI2:
SOURCE_MOD += [
'embed/extmod/rustmods/modtrezorui2.c',
]
if FEATURE_FLAGS["ZCASH_SHIELDED"]:
SOURCE_MOD += [
'embed/extmod/rustmods/modtrezorzcashprimitives.c'
]

# modutime
SOURCE_MOD += [
Expand Down Expand Up @@ -678,7 +683,7 @@ if FROZEN:
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/bitcoin/sign_tx/zcash_v4.py'))
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/enums/Zcash*.py'))

source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY)
source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY, zcash_shielded=FEATURE_FLAGS['ZCASH_SHIELDED'])

source_mpyc = env.FrozenCFile(
target='frozen_mpy.c', source=source_mpy, qstr_header=qstr_preprocessed)
Expand Down Expand Up @@ -720,6 +725,8 @@ def cargo_build():
features.append('ui')
if PYOPT == '0':
features.append('ui_debug')
if FEATURE_FLAGS["ZCASH_SHIELDED"]:
features.append("zcash_shielded")

cargo_opts = [
f'--target={RUST_TARGET}',
Expand Down
9 changes: 8 additions & 1 deletion core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ UI2 = ARGUMENTS.get('UI2', '0') == '1' or TREZOR_MODEL in ('1', 'R')

FEATURE_FLAGS = {
"SECP256K1_ZKP": True, # required for trezor.crypto.curve.bip340 (BIP340/Taproot)
"ZCASH_SHIELDED": EVERYTHING,
}

CCFLAGS_MOD = ''
Expand Down Expand Up @@ -202,6 +203,10 @@ if UI2:
SOURCE_MOD += [
'embed/extmod/rustmods/modtrezorui2.c',
]
if FEATURE_FLAGS["ZCASH_SHIELDED"]:
SOURCE_MOD += [
'embed/extmod/rustmods/modtrezorzcashprimitives.c'
]

# modutime
SOURCE_MOD += [
Expand Down Expand Up @@ -634,7 +639,7 @@ if FROZEN:
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/bitcoin/sign_tx/zcash_v4.py'))
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/enums/Zcash*.py'))

source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY)
source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY, zcash_shielded=FEATURE_FLAGS['ZCASH_SHIELDED'])

source_mpyc = env.FrozenCFile(
target='frozen_mpy.c', source=source_mpy, qstr_header=qstr_preprocessed)
Expand Down Expand Up @@ -674,6 +679,8 @@ def cargo_build():
features.append('ui')
if PYOPT == '0':
features.append('debug')
if FEATURE_FLAGS["ZCASH_SHIELDED"]:
features.append('zcash_shielded')

return f'cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}"'

Expand Down
6 changes: 6 additions & 0 deletions core/embed/extmod/modtrezorutils/modtrezorutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ STATIC mp_obj_str_t mod_trezorutils_revision_obj = {
/// MODEL: str
/// EMULATOR: bool
/// BITCOIN_ONLY: bool
/// ZCASH_SHIELDED: bool

STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorutils)},
Expand Down Expand Up @@ -283,6 +284,11 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
#else
{MP_ROM_QSTR(MP_QSTR_BITCOIN_ONLY), mp_const_false},
#endif
#ifdef ZCASH_SHIELDED
{MP_ROM_QSTR(MP_QSTR_ZCASH_SHIELDED), mp_const_true},
#elif TREZOR_EMULATOR
{MP_ROM_QSTR(MP_QSTR_ZCASH_SHIELDED), mp_const_false},
#endif
};

STATIC MP_DEFINE_CONST_DICT(mp_module_trezorutils_globals,
Expand Down
32 changes: 32 additions & 0 deletions core/embed/extmod/rustmods/modtrezorzcashprimitives.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "py/runtime.h"

#include "librust.h"

#if MICROPY_PY_TREZORPALLAS
MP_REGISTER_MODULE(MP_QSTR_trezorpallas, mp_module_trezorpallas,
MICROPY_PY_TREZORPALLAS);
#endif // MICROPY_PY_TREZORPALLAS

#if MICROPY_PY_TREZORPOSEIDON
MP_REGISTER_MODULE(MP_QSTR_trezorposeidon, mp_module_trezorposeidon,
MICROPY_PY_TREZORPOSEIDON);
#endif // MICROPY_PY_TREZORPOSEIDON
2 changes: 2 additions & 0 deletions core/embed/firmware/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
#define MICROPY_PY_TREZORUTILS (1)
#define MICROPY_PY_TREZORPROTO (1)
#define MICROPY_PY_TREZORUI2 (1)
#define MICROPY_PY_TREZORPALLAS (1)
#define MICROPY_PY_TREZORPOSEIDON (1)

#ifdef SYSTEM_VIEW
#define MP_PLAT_PRINT_STRN(str, len) segger_print(str, len)
Expand Down
70 changes: 70 additions & 0 deletions core/embed/rust/Cargo.lock

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

22 changes: 21 additions & 1 deletion core/embed/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ build = "build.rs"
[features]
default = ["model_tt"]
bitcoin_only = []
zcash_shielded = [
"micropython", "pasta_curves", "blake2b_simd",
]
model_tt = ["touch"]
model_t1 = ["buttons"]
model_tr = ["buttons"]
Expand All @@ -19,7 +22,7 @@ buttons = []
touch = []
clippy = []
debug = ["ui_debug"]
test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug"]
test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug", "zcash_shielded"]

[lib]
crate-type = ["staticlib"]
Expand Down Expand Up @@ -53,6 +56,16 @@ default_features = false
version = "0.2.4"
default_features = false

[dependencies.blake2b_simd]
optional = true
version = "1"
default_features = false

[dependencies.pasta_curves]
optional = true
version = "0.4.0"
default-features = false

# Build dependencies

[build-dependencies.bindgen]
Expand All @@ -69,3 +82,10 @@ version = "1.0.69"
[build-dependencies.glob]
optional = true
version = "0.3.0"

[patch.crates-io.blake2b_simd]
path = "./blake2b_hal"

[patch.crates-io.pasta_curves]
git = "https://github.com/jarys/pasta_curves"
rev = "a4f755013aad344982383c9f5af362697d928325"
16 changes: 16 additions & 0 deletions core/embed/rust/blake2b_hal/Cargo.lock

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

9 changes: 9 additions & 0 deletions core/embed/rust/blake2b_hal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "blake2b_simd"
version = "1.0.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cty = "0.2.2"
43 changes: 43 additions & 0 deletions core/embed/rust/blake2b_hal/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct __blake2b_state {
pub h: [u64; 8usize],
pub t: [u64; 2usize],
pub f: [u64; 2usize],
pub buf: [u8; 128usize],
pub buflen: usize,
pub outlen: usize,
pub last_node: u8,
}
#[allow(non_camel_case_types)]
pub type blake2b_state = __blake2b_state;
extern "C" {
pub fn blake2b_Init(S: *mut blake2b_state, outlen: usize) -> cty::c_int;
}
extern "C" {
pub fn blake2b_InitKey(
S: *mut blake2b_state,
outlen: usize,
key: *const cty::c_void,
keylen: usize,
) -> cty::c_int;
}
extern "C" {
pub fn blake2b_InitPersonal(
S: *mut blake2b_state,
outlen: usize,
personal: *const cty::c_void,
personal_len: usize,
) -> cty::c_int;
}
extern "C" {
pub fn blake2b_Update(
S: *mut blake2b_state,
pin: *const cty::c_void,
inlen: usize,
) -> cty::c_int;
}
extern "C" {
pub fn blake2b_Final(S: *mut blake2b_state, out: *mut cty::c_void, outlen: usize)
-> cty::c_int;
}
Loading

0 comments on commit 6311675

Please sign in to comment.