Skip to content

Commit

Permalink
fix: update MontConfig implementation for byte arrays
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Oct 13, 2024
1 parent 196cb2c commit c5e695c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 2 additions & 3 deletions crates/proof-of-sql/src/base/scalar/mont_scalar_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<T: MontConfig<4>> From<&[u8]> for MontScalar<T> {
///
/// - If the byte slice is empty, the result is the zero scalar.
/// - If the byte slice has length 31 or less, the bytes are directly converted to a scalar.
/// - If the byte slice has length 32, the bytes are hashed using `blake3` and the result is
/// - If the byte slice has length 32 or larger, the bytes are hashed using `blake3` and the result is
/// converted to a scalar.
fn from(x: &[u8]) -> Self {
match x.len() {
Expand All @@ -29,14 +29,13 @@ impl<T: MontConfig<4>> From<&[u8]> for MontScalar<T> {
bytes[..x.len()].copy_from_slice(x);
Self::from_le_bytes_mod_order(&bytes)
}
32 => {
_ => {
// Hash and convert if exactly 32 bytes
let hash = blake3::hash(x);
let mut bytes: [u8; 32] = hash.into();
bytes[31] &= 0b0000_1111_u8;
Self::from_le_bytes_mod_order(&bytes)
}
_ => panic!("Unsupported byte length for conversion to MontScalar"),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/proof-of-sql/src/base/scalar/mont_scalar_from_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ fn strings_of_arbitrary_size_map_to_different_scalars() {
}
}

#[test]
fn byte_arrays_of_arbitrary_size_map_to_different_scalars() {
let mut prev_scalars = IndexSet::default();
let mut rng = StdRng::from_seed([0u8; 32]);
let dist = Uniform::new(1, 100);

for _ in 0..100 {
let v = (0..dist.sample(&mut rng))
.map(|_v| (dist.sample(&mut rng) % 255) as u8)
.collect::<Vec<u8>>();
assert!(prev_scalars.insert(Curve25519Scalar::from(&v[..])));
}
}

#[test]
fn the_string_hash_implementation_uses_the_full_range_of_bits() {
let max_iters = 20;
Expand Down

0 comments on commit c5e695c

Please sign in to comment.