-
-
Notifications
You must be signed in to change notification settings - Fork 668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): add Zcash Rust primitives #2510
base: main
Are you sure you want to change the base?
Conversation
30ab596
to
8715425
Compare
It would be great if you introduced |
How to deal with this ? @prusnak ZCASH_SHIELDED
ERROR: Altcoin strings found in Bitcoin-only firmware. I will need this flag to be accessible from python to disable some functionalities in module |
109f81a
to
385157e
Compare
We need to replace
Maybe this does the trick? diff --git a/core/SConscript.firmware b/core/SConscript.firmware
index 96ffec17d..3d0d30cb4 100644
--- a/core/SConscript.firmware
+++ b/core/SConscript.firmware
@@ -678,7 +678,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)
diff --git a/core/site_scons/site_tools/micropython/__init__.py b/core/site_scons/site_tools/micropython/__init__.py
index 38c40880f..8f897a3ca 100644
--- a/core/site_scons/site_tools/micropython/__init__.py
+++ b/core/site_scons/site_tools/micropython/__init__.py
@@ -26,9 +26,11 @@ def generate(env):
# replace "utils.BITCOIN_ONLY" with literal constant (True/False)
# so the compiler can optimize out the things we don't want
btc_only = env['bitcoin_only'] == '1'
+ zcash_shielded = env['zcash_shielded'] == '1'
interim = f"{target[:-4]}.i" # replace .mpy with .i
sed_scripts = " ".join([
rf"-e 's/utils\.BITCOIN_ONLY/{btc_only}/g'",
+ rf"-e 's/utils\.ZCASH_SHIELDED/{zcash_shielded}/g'",
r"-e 's/if TYPE_CHECKING/if False/'",
r"-e 's/import typing/# \0/'",
r"-e '/from typing import (/,/^\s*)/ {s/^/# /}'", If we do the change above I guess we can drop this change completely? Not sure if that's what we want or we want to add exception for this to the check. Maybe @matejcik can help? |
I made changes suggested by @prusnak , but I'm still getting I isolated all new code by |
178d06b should fix it.
I dropped the QSTR, which means that |
thank you andrew |
Darn, I hadn't realized this would break the unit tests:
I think there is no way around that, is there? Adding an exception to |
istm we could keep |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review is here.
Before merging, we will also need to do at least a brief review of the pulled in dependencies.
impl<const N: usize> TryFrom<Obj> for [u8; N] { | ||
type Error = Error; | ||
|
||
fn try_from(obj: Obj) -> Result<[u8; N], Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: we have removed the Buffer
type so this will need to be redone in terms of get_buffer()
.
istm it's gonna be trivially Ok(unsafe { get_buffer(obj) }.try_into()?)
with the appropriate SAFETY comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok . I suggest resolving this after squashing and rebasing
23beeec
to
e73a02a
Compare
e73a02a
to
6311675
Compare
Tentative approve, pending review of the Rust dependencies. |
@Jarys just to clarify, the only thing you need patched into |
Yes, I confirm. The only thing I need to be patched in |
This PR adds
pasta_curves
crate)reddsa
crate)Implementation details
Three pasta curves struct (
Fp
,Scalar
andPoint
) are exposed to micropython using generic containerWrapped<T>
defined inmicropython/wrap.rs
.Newly imported crates depend on
blake2b_simd
crate. Since there is already a C implementation of blake2b, I just added a Rust interface to it (inrust/blake2b_hal
) and then I overwroteblake2b_simd
byblake2b_hal
via[crates-io.patch]
.Poseidon function requires some precomputed round constants. Since the generating algorithm is pretty lightweight (it is based on LSFR), I decided to replace the constants table (6144 bytes) by my own highly optimized implementation of the generator itself (circa 512 kb). Generator is tested on all expected constants. Poseidon implementation is tested on official zcash test vectors.
Cratereddsa
is patched to include this minor fixFix alloc feature ZcashFoundation/reddsa#28Crate
pasta_curves
is patched, because these issue and pr are pending:hash_to_curve
API zcash/pasta_curves#46uninline-portable
feature zcash/pasta_curves#47Related links:
reddsa crate