Skip to content

Commit 4f4c1f5

Browse files
authored
srp: store group G constant in ConstMontyForm (#250)
Uses `crypto-bigint`'s stack-allocated Montgomery form types to define group constants, including adding an impl of `ConstMontyParams` to them. This precomputes all of their Montgomery form constants at compile-time, meaning initializing a `BoxedMontyForm` (for now, since it's the only thing that currently has modpow) can use the precomputed constants. This is also the first step towards supporting a fully stack allocated implementation (#248).
1 parent 8b2e0eb commit 4f4c1f5

File tree

10 files changed

+75
-46
lines changed

10 files changed

+75
-46
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srp/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ edition = "2024"
1717
rust-version = "1.85"
1818

1919
[dependencies]
20-
bigint = { package = "crypto-bigint", version = "0.7.0-rc.17", features = ["alloc"] }
20+
bigint = { package = "crypto-bigint", version = "0.7.0-rc.18", features = ["alloc"] }
2121
common = { package = "crypto-common", version = "0.2.0-rc.9" }
2222
digest = "0.11.0-rc.5"
2323
subtle = { version = "2.4", default-features = false }

srp/src/groups.rs

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//! [RFC5054]: https://tools.ietf.org/html/rfc5054
99
1010
use bigint::{
11-
BoxedUint, Odd, Resize,
12-
modular::{BoxedMontyForm, BoxedMontyParams},
11+
Odd, U1024, U1536, U2048, U3072, U4096,
12+
modular::{BoxedMontyForm, ConstMontyForm, ConstMontyParams, MontyParams},
1313
};
1414
use core::{
1515
any,
@@ -18,32 +18,30 @@ use core::{
1818

1919
/// Group used for SRP computations.
2020
pub trait Group {
21-
/// Group generator modulo `N`.
22-
const G: u64;
21+
/// Group elements.
22+
type Element: Into<BoxedMontyForm>;
2323

24-
/// Big endian bytes representing a large safe prime (`N = 2q + 1`, where `q` is prime) which
25-
/// acts as the modulus.
26-
const N: &'static [u8];
24+
/// Group generator modulo `N` represented as `ConstMontyForm`, where `N` is a large safe prime
25+
/// (`N = 2q + 1`, where `q` is prime)
26+
const G: Self::Element;
2727

2828
/// Initialize group generator as a [`BoxedMontyForm`].
2929
fn generator() -> BoxedMontyForm {
30-
let n = BoxedUint::from_be_slice_vartime(Self::N);
31-
let n = BoxedMontyParams::new(Odd::new(n).expect("n should be odd"));
32-
BoxedMontyForm::new(BoxedUint::from(Self::G).resize(n.bits_precision()), &n)
30+
Self::G.into()
3331
}
3432
}
3533

3634
macro_rules! define_group {
37-
($name:ident, $g:expr, $n:expr, $doc:expr) => {
35+
($name:ident, $uint:ident, $g:expr, $doc:expr, $n:expr) => {
3836
#[doc = $doc]
39-
#[derive(Clone, Copy)]
37+
#[derive(Clone, Copy, Default, Eq, PartialEq)]
4038
pub struct $name;
41-
group_trait_impls!($name, $g, $n);
39+
group_trait_impls!($name, $uint, $g, $n);
4240
};
4341
}
4442

4543
macro_rules! define_deprecated_group {
46-
($name:ident, $g:expr, $n:expr, $doc:expr) => {
44+
($name:ident, $uint:ident, $g:expr, $doc:expr, $n:expr) => {
4745
/// DEPRECATED:
4846
#[doc = $doc]
4947
///
@@ -52,52 +50,89 @@ macro_rules! define_deprecated_group {
5250
///
5351
/// It is recommended to use a group which is 2048-bits or larger.
5452
/// </div>
55-
#[derive(Clone, Copy)]
53+
#[derive(Clone, Copy, Default, Eq, PartialEq)]
5654
#[deprecated(
5755
since = "0.7.0",
5856
note = "this group is too small to be secure. Prefer to use G2048+"
5957
)]
6058
pub struct $name;
61-
group_trait_impls!($name, $g, $n);
59+
group_trait_impls!($name, $uint, $g, $n);
6260
};
6361
}
6462

6563
macro_rules! group_trait_impls {
66-
($name:ident, $g:expr, $n:expr) => {
64+
($name:ident, $uint:ident, $g:expr, $n:expr) => {
65+
#[allow(deprecated)]
66+
impl ConstMontyParams<{ <$uint>::LIMBS }> for $name {
67+
const LIMBS: usize = <$uint>::LIMBS;
68+
const PARAMS: MontyParams<{ <$uint>::LIMBS }> =
69+
MontyParams::new_vartime(Odd::<$uint>::from_be_hex($n));
70+
}
71+
6772
#[allow(deprecated)]
6873
impl Group for $name {
69-
const G: u64 = $g;
70-
const N: &'static [u8] = include_bytes!($n);
74+
type Element = ConstMontyForm<Self, { <$uint>::LIMBS }>;
75+
const G: Self::Element = ConstMontyForm::new(&<$uint>::from_u128($g));
7176
}
7277

7378
#[allow(deprecated)]
7479
impl Debug for $name {
7580
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7681
let name = any::type_name::<$name>();
7782
let name = name.split("::").last().unwrap_or(name);
78-
79-
write!(f, "{} {{ G: {}, N: 0x", name, Self::G)?;
80-
for byte in Self::N {
81-
write!(f, "{byte:02X}")?;
82-
}
83-
write!(f, " }}")
84-
}
85-
}
86-
87-
#[allow(deprecated)]
88-
impl<Rhs: Group> PartialEq<Rhs> for $name {
89-
fn eq(&self, _other: &Rhs) -> bool {
90-
Self::G == Rhs::G && Self::N == Rhs::N
83+
f.debug_struct(name)
84+
.field("G", &Self::G.retrieve())
85+
.field("N", &**Self::PARAMS.modulus())
86+
.finish()
9187
}
9288
}
9389
};
9490
}
9591

96-
define_deprecated_group!(G1024, 2, "groups/1024.bin", "1024-bit group.");
97-
define_deprecated_group!(G1536, 2, "groups/1536.bin", "1536-bit group.");
98-
define_group!(G2048, 2, "groups/2048.bin", "2048-bit group.");
99-
define_group!(G3072, 5, "groups/3072.bin", "3072-bit group.");
100-
define_group!(G4096, 5, "groups/4096.bin", "4096-bit group.");
92+
// G1024
93+
define_deprecated_group!(
94+
G1024,
95+
U1024,
96+
2,
97+
"1024-bit group",
98+
"eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3"
99+
);
100+
101+
// G1536
102+
define_deprecated_group!(
103+
G1536,
104+
U1536,
105+
2,
106+
"1536-bit group",
107+
"9def3cafb939277ab1f12a8617a47bbbdba51df499ac4c80beeea9614b19cc4d5f4f5f556e27cbde51c6a94be4607a291558903ba0d0f84380b655bb9a22e8dcdf028a7cec67f0d08134b1c8b97989149b609e0be3bab63d47548381dbc5b1fc764e3f4b53dd9da1158bfd3e2b9c8cf56edf019539349627db2fd53d24b7c48665772e437d6c7f8ce442734af7ccb7ae837c264ae3a9beb87f8a2fe9b8b5292e5a021fff5e91479e8ce7a28c2442c6f315180f93499a234dcf76e3fed135f9bb"
108+
);
109+
110+
// G2048
111+
define_group!(
112+
G2048,
113+
U2048,
114+
2,
115+
"2048-bit group",
116+
"ac6bdb41324a9a9bf166de5e1389582faf72b6651987ee07fc3192943db56050a37329cbb4a099ed8193e0757767a13dd52312ab4b03310dcd7f48a9da04fd50e8083969edb767b0cf6095179a163ab3661a05fbd5faaae82918a9962f0b93b855f97993ec975eeaa80d740adbf4ff747359d041d5c33ea71d281e446b14773bca97b43a23fb801676bd207a436c6481f1d2b9078717461a5b9d32e688f87748544523b524b0d57d5ea77a2775d2ecfa032cfbdbf52fb3786160279004e57ae6af874e7303ce53299ccc041c7bc308d82a5698f3a8d0c38271ae35f8e9dbfbb694b5c803d89f7ae435de236d525f54759b65e372fcd68ef20fa7111f9e4aff73"
117+
);
118+
119+
// G3072
120+
define_group!(
121+
G3072,
122+
U3072,
123+
5,
124+
"3072-bit group",
125+
"ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a93ad2caffffffffffffffff"
126+
);
127+
128+
// G0496
129+
define_group!(
130+
G4096,
131+
U4096,
132+
5,
133+
"4096-bit group",
134+
"ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c934063199ffffffffffffffff"
135+
);
101136

102137
#[cfg(test)]
103138
#[allow(deprecated)]

srp/src/groups/1024.bin

Lines changed: 0 additions & 3 deletions
This file was deleted.

srp/src/groups/1536.bin

Lines changed: 0 additions & 1 deletion
This file was deleted.

srp/src/groups/2048.bin

Lines changed: 0 additions & 2 deletions
This file was deleted.

srp/src/groups/3072.bin

-384 Bytes
Binary file not shown.

srp/src/groups/4096.bin

-512 Bytes
Binary file not shown.

srp/src/groups/6144.bin

-768 Bytes
Binary file not shown.

srp/src/groups/8192.bin

-1 KB
Binary file not shown.

0 commit comments

Comments
 (0)