Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/backend/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ impl Integer {
}
/// Parses the integer using the given radix
pub fn from_str_radix(s: &str, radix: u16) -> Option<Self> {
// num-bigint already enforces these checks, but we double-enforce them
// to make sure they are in sync with what we already enforce for rug.
// See RugInteger::from_str_radix
if !s
.chars()
.enumerate()
.skip_while(|&(i, c)| i == 0 && (c == '+' || c == '-'))
.all(|(_, c)| c.is_ascii_alphanumeric())
{
return None;
}
num_traits::Num::from_str_radix(s, radix.into())
.ok()
.map(Integer)
Expand Down Expand Up @@ -451,7 +462,7 @@ impl quickcheck::Arbitrary for Integer {
if prev.cmp0().is_eq() {
None
} else {
prev >>= 1;
prev /= 2;
Some(prev.clone())
}
}))
Expand Down
14 changes: 13 additions & 1 deletion src/backend/rug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ impl Integer {
}
/// Parses the integer using the given radix
pub fn from_str_radix(s: &str, radix: u16) -> Option<Self> {
// rug accepts strings that have whitespaces in them, integer is parsed while
// ignoring whitespaces, even in the middle of the number. This behavior is
// different from num-bigint backend, and it is strange. We manually restrict
// strings to be ascii alphanum
if !s
.chars()
.enumerate()
.skip_while(|&(i, c)| i == 0 && (c == '+' || c == '-'))
.all(|(_, c)| c.is_ascii_alphanumeric())
{
return None;
}
Comment on lines +81 to +92

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we actually care about this change. It's not like we want our backends to be exactly identical everywhere, just give the same numerical operations and serialize to the same format. If the deserialization of one is more permissive than another - that doesn't concern our usage

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does concern our usage if we have signers with different backends running in the same cluster. In this case, malicious party may send a broadcast message which will cause one subset of parties to abort (cuz num-bigint doesn't accept whitespaces) and the other subset of signers will continue the protocol.

rug::Integer::from_str_radix(s, radix.into())
.ok()
.map(Integer)
Expand Down Expand Up @@ -344,7 +356,7 @@ impl quickcheck::Arbitrary for Integer {
if prev.cmp0().is_eq() {
None
} else {
prev >>= 1;
prev /= 2;
Some(prev.clone())
}
}))
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ quickcheck::quickcheck! {
// It matters to check if one returns `Yes|Probably`, and the other returns `No`
matches!((r1, r2), (IsPrime::Yes | IsPrime::Probably, IsPrime::Yes | IsPrime::Probably) | (IsPrime::No, IsPrime::No))
}

fn from_radix(s: AsciiString) -> bool {
let nbi = NbiInteger::from_str_radix(&s.0, 16);
let rug = RugInteger::from_str_radix(&s.0, 16);
eprintln!("{nbi:?}, {rug:?}");
match (nbi, rug) {
(Some(a), Some(b)) => a.equals(b),
(None, None) => true,
_ => false
}
}
}

///// Helper traits for macro /////
Expand Down Expand Up @@ -455,3 +466,16 @@ impl<'a> Arg<'a> for Prng {
&mut self.rng
}
}

#[derive(Debug, Clone)]
struct AsciiString(String);
impl quickcheck::Arbitrary for AsciiString {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let size = u8::arbitrary(g) % 32;
let s = std::iter::repeat_with(|| u8::arbitrary(g) & 0x7F)
.take(usize::from(size))
.map(char::from)
.collect();
Self(s)
}
}
5 changes: 5 additions & 0 deletions tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,8 @@ fn random_key_for_tests(rng: &mut impl rand_core::RngCore) -> DecryptionKey {
let q = Integer::generate_safe_prime(rng, 512);
DecryptionKey::from_primes(p, q).unwrap()
}

#[test]
fn bigint_from_radix_prohibits_whitespaces() {
assert_eq!(Integer::from_str_radix("123 4", 10), None)
}