From c358090f1681c06ac8a015f19b299fad7a162ea2 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Tue, 7 Mar 2023 04:22:57 -0500 Subject: [PATCH] Use black_box to help obscure the dalek-ff-group bool -> Choice conversion I have no idea if this will actually help, yet it can't hurt. Feature gated due to MSRV requirements. Fixes #242. --- crypto/dalek-ff-group/Cargo.toml | 3 +++ crypto/dalek-ff-group/src/lib.rs | 9 ++++++++- processor/Cargo.toml | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crypto/dalek-ff-group/Cargo.toml b/crypto/dalek-ff-group/Cargo.toml index bea86d89..3403282b 100644 --- a/crypto/dalek-ff-group/Cargo.toml +++ b/crypto/dalek-ff-group/Cargo.toml @@ -29,3 +29,6 @@ curve25519-dalek = "^3.2" [dev-dependencies] ff-group-tests = { path = "../ff-group-tests" } + +[features] +black_box = [] diff --git a/crypto/dalek-ff-group/src/lib.rs b/crypto/dalek-ff-group/src/lib.rs index 56aaf275..4b5d5fc3 100644 --- a/crypto/dalek-ff-group/src/lib.rs +++ b/crypto/dalek-ff-group/src/lib.rs @@ -43,7 +43,14 @@ pub mod field; // Convert a boolean to a Choice in a *presumably* constant time manner fn choice(value: bool) -> Choice { - Choice::from(u8::from(value)) + #[cfg(not(feature = "black_box"))] + let res = Choice::from(u8::from(value)); + #[cfg(feature = "black_box")] + let res = { + use core::hint::black_box; + Choice::from(black_box(u8::from(black_box(value)))) + }; + res } macro_rules! deref_borrow { diff --git a/processor/Cargo.toml b/processor/Cargo.toml index f86fb7a6..127d5bd6 100644 --- a/processor/Cargo.toml +++ b/processor/Cargo.toml @@ -24,7 +24,7 @@ group = "0.12" curve25519-dalek = { version = "3", features = ["std"] } transcript = { package = "flexible-transcript", path = "../crypto/transcript", features = ["recommended"] } -dalek-ff-group = { path = "../crypto/dalek-ff-group" } +dalek-ff-group = { path = "../crypto/dalek-ff-group", features = ["black_box"] } frost = { package = "modular-frost", path = "../crypto/frost", features = ["ed25519"] } monero-serai = { path = "../coins/monero", features = ["multisig"] }