Skip to content

Commit

Permalink
refactor(crypto): CRP-2441 Remove EccFieldElement abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Mar 12, 2024
1 parent 26a91d1 commit 725a52e
Show file tree
Hide file tree
Showing 20 changed files with 364 additions and 1,441 deletions.
10 changes: 0 additions & 10 deletions rs/crypto/internal/crypto_lib/threshold_sig/tecdsa/BUILD.bazel
Expand Up @@ -92,16 +92,6 @@ rust_bench(
],
)

rust_bench(
name = "field_ops_bench",
testonly = True,
srcs = ["benches/field_ops.rs"],
deps = [
":tecdsa",
"@crate_index//:criterion",
],
)

rust_bench(
name = "group_ops_bench",
testonly = True,
Expand Down
4 changes: 0 additions & 4 deletions rs/crypto/internal/crypto_lib/threshold_sig/tecdsa/Cargo.toml
Expand Up @@ -39,10 +39,6 @@ bip32 = { version = "0.5", features = ["secp256k1"] }
num-traits = { version = "0.2.15" }
strum = "0.25"

[[bench]]
name = "field_ops"
harness = false

[[bench]]
name = "poly"
harness = false
Expand Down

This file was deleted.

Expand Up @@ -244,6 +244,9 @@ fn define_fe_struct(config: &FieldElementConfig) -> proc_macro2::TokenStream {
const MONTY_R3: Self = Self::from_limbs([#(#monty_r3_limbs,)*]);
const P_DASH: u64 = #p_dash;

/// Size of this field element in bytes
pub const BYTES: usize = 8 * #limbs;

/// Initialize from limbs (private constructor)
const fn from_limbs(limbs: [u64; #limbs]) -> Self {
Self { limbs }
Expand Down Expand Up @@ -285,11 +288,24 @@ fn define_fe_struct(config: &FieldElementConfig) -> proc_macro2::TokenStream {
}
}

/// Conditional assignment matching hash2curve notation
///
/// CMOV(a, b, c): If c is False, CMOV returns a, otherwise it returns b.
pub fn cmov(a: &Self, b: &Self, assign: subtle::Choice) -> Self {
use subtle::ConditionallySelectable;

let mut r = [0u64; #limbs];
for i in 0..#limbs {
r[i] = u64::conditional_select(&a.limbs[i], &b.limbs[i], assign);
}
Self::from_limbs(r)
}

/// Parse the given byte array as a field element.
///
/// Return None if the byte array does not represeent an integer in [0,p)
pub fn from_bytes(bytes: &[u8]) -> std::option::Option<Self> {
if bytes.len() != 8 * #limbs {
if bytes.len() != Self::BYTES {
return None;
}

Expand All @@ -316,35 +332,46 @@ fn define_fe_struct(config: &FieldElementConfig) -> proc_macro2::TokenStream {
}

pub fn from_bytes_wide(bytes: &[u8]) -> Option<Self> {
if bytes.len() > 2 * 8 * #limbs {
if bytes.len() > 2 * Self::BYTES {
return None;
}

let mut wide_bytes = [0u8; 2 * 8 * #limbs];
wide_bytes[2 * 8 * #limbs - bytes.len()..].copy_from_slice(bytes);
let mut wide_bytes = [0u8; 2 * Self::BYTES];
wide_bytes[2 * Self::BYTES - bytes.len()..].copy_from_slice(bytes);

Some(Self::from_bytes_wide_exact(&wide_bytes))
}

pub fn from_bytes_wide_exact(wide_bytes: &[u8; 2*Self::BYTES]) -> Self {
let mut limbs = [0u64; 2 * #limbs];

for i in 0..2 * #limbs {
limbs[2 * #limbs - 1 - i] = u64::from_be_bytes(wide_bytes[i * 8..(i + 1) * 8].try_into().unwrap());
}

// First reduce then mul by R3 to convert to Montgomery form
Some(Self::redc(&limbs).mul(&Self::MONTY_R3))
Self::redc(&limbs).mul(&Self::MONTY_R3)
}

/// Return the encoding of this field element
pub fn as_bytes(&self) -> [u8; 8*#limbs] {
pub fn as_bytes(&self) -> [u8; Self::BYTES] {
// Convert from Montgomery form
let value = Self::redc(&self.limbs);
let mut ret = [0u8; 8 * #limbs];
let mut ret = [0u8; Self::BYTES];
for i in 0..#limbs {
ret[8 * i..(8 * i + 8)]
.copy_from_slice(&value.limbs[#limbs - 1 - i].to_be_bytes());
}
ret
}

/// Return the "sign" of self (effectively self mod 2)
///
/// See Section 4.1 of RFC 9380 for details
pub fn sign(&self) -> u8 {
(self.as_bytes()[Self::BYTES - 1] & 1) as u8
}

/// Return self + rhs mod p
pub fn add(&self, rhs: &Self) -> Self {
let mut sum = [0u64; #limbs + 1];
Expand All @@ -359,10 +386,15 @@ fn define_fe_struct(config: &FieldElementConfig) -> proc_macro2::TokenStream {
}

/// Return self - rhs mod p
pub fn subtract(&self, rhs: &Self) -> Self {
pub fn sub(&self, rhs: &Self) -> Self {
Self::mod_sub(&self.limbs, rhs.limbs)
}

/// Return -self mod p
pub fn negate(&self) -> Self {
Self::zero().sub(self)
}

fn mod_sub(l: &[u64], r: [u64; #limbs]) -> Self {
let mut borrow = 0;
let mut w = [0u64; #limbs];
Expand Down
Expand Up @@ -12,27 +12,13 @@ DEPENDENCIES = [

MACRO_DEPENDENCIES = []

rust_fuzz_test_binary(
name = "fe",
srcs = ["fuzz_targets/fe.rs"],
proc_macro_deps = MACRO_DEPENDENCIES,
deps = DEPENDENCIES,
)

rust_fuzz_test_binary(
name = "scalar",
srcs = ["fuzz_targets/scalar.rs"],
proc_macro_deps = MACRO_DEPENDENCIES,
deps = DEPENDENCIES,
)

rust_fuzz_test_binary(
name = "sqrt_ratio",
srcs = ["fuzz_targets/sqrt_ratio.rs"],
proc_macro_deps = MACRO_DEPENDENCIES,
deps = DEPENDENCIES,
)

rust_fuzz_test_binary(
name = "cbor_deserialize_dealing",
srcs = ["fuzz_targets/cbor_deserialize_dealing.rs"],
Expand Down
12 changes: 0 additions & 12 deletions rs/crypto/internal/crypto_lib/threshold_sig/tecdsa/fuzz/Cargo.toml
Expand Up @@ -23,20 +23,8 @@ path = ".."
[workspace]
members = ["."]

[[bin]]
name = "fe"
path = "fuzz_targets/fe.rs"
test = false
doc = false

[[bin]]
name = "scalar"
path = "fuzz_targets/scalar.rs"
test = false
doc = false

[[bin]]
name = "sqrt_ratio"
path = "fuzz_targets/sqrt_ratio.rs"
test = false
doc = false

This file was deleted.

0 comments on commit 725a52e

Please sign in to comment.