Skip to content

Commit 507a952

Browse files
chore: remove custom vector splitting (#138)
This PR removes a custom vector splitting function with [new functionality](https://doc.rust-lang.org/std/primitive.slice.html#method.split_at_checked) from the standard library. It also removes an old unused comment line that made the formatter angry.
1 parent 52bcd74 commit 507a952

File tree

3 files changed

+14
-46
lines changed

3 files changed

+14
-46
lines changed

src/generators/pedersen_gens.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::{errors::ProofError, traits::Compressable};
2121
///
2222
/// * `h_base`: the curve basepoint;
2323
/// * `g_base_vec`: the result of domain separated SHA3-512 (hash of unique indexed strings)
24-
/// hash-to-group on input `B_bytes`.
2524
#[derive(Clone, Debug, PartialEq)]
2625
pub struct PedersenGens<P: Compressable> {
2726
/// Base for the committed value

src/range_proof.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::{
3737
traits::{Compressable, Decompressable, FixedBytesRepr, Precomputable},
3838
transcripts::RangeProofTranscript,
3939
utils::{
40-
generic::{compute_generator_padding, nonce, split_at_checked},
40+
generic::{compute_generator_padding, nonce},
4141
nullrng::NullRng,
4242
},
4343
};
@@ -410,10 +410,18 @@ where
410410
n /= 2;
411411

412412
// Split the vectors for folding
413-
let (a_lo, a_hi) = split_at_checked(&a_li, n)?;
414-
let (b_lo, b_hi) = split_at_checked(&a_ri, n)?;
415-
let (gi_base_lo, gi_base_hi) = split_at_checked(&gi_base, n)?;
416-
let (hi_base_lo, hi_base_hi) = split_at_checked(&hi_base, n)?;
413+
let (a_lo, a_hi) = a_li
414+
.split_at_checked(n)
415+
.ok_or(ProofError::InvalidLength("Invalid vector split index".to_string()))?;
416+
let (b_lo, b_hi) = a_ri
417+
.split_at_checked(n)
418+
.ok_or(ProofError::InvalidLength("Invalid vector split index".to_string()))?;
419+
let (gi_base_lo, gi_base_hi) = gi_base
420+
.split_at_checked(n)
421+
.ok_or(ProofError::InvalidLength("Invalid vector split index".to_string()))?;
422+
let (hi_base_lo, hi_base_hi) = hi_base
423+
.split_at_checked(n)
424+
.ok_or(ProofError::InvalidLength("Invalid vector split index".to_string()))?;
417425

418426
let y_n_inverse = if y_powers[n] == Scalar::ZERO {
419427
return Err(ProofError::InvalidArgument(

src/utils/generic.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ pub fn nonce(
5959
Ok(Scalar::from_hasher_blake2b(hasher))
6060
}
6161

62-
/// Split a vector, checking the bound to avoid a panic
63-
pub fn split_at_checked<T>(vec: &[T], n: usize) -> Result<(&[T], &[T]), ProofError> {
64-
if n <= vec.len() {
65-
Ok(vec.split_at(n))
66-
} else {
67-
Err(ProofError::InvalidLength("Invalid vector split index".to_string()))
68-
}
69-
}
70-
7162
/// Compute the padding needed for generator vectors
7263
pub fn compute_generator_padding(
7364
bit_length: usize,
@@ -92,7 +83,7 @@ pub fn compute_generator_padding(
9283

9384
#[cfg(test)]
9485
mod tests {
95-
use alloc::{vec, vec::Vec};
86+
use alloc::vec;
9687

9788
use curve25519_dalek::scalar::Scalar;
9889
use rand_chacha::ChaCha12Rng;
@@ -113,36 +104,6 @@ mod tests {
113104
assert!(compute_generator_padding(64, usize::MAX - 1, usize::MAX).is_err());
114105
}
115106

116-
#[test]
117-
fn test_split() {
118-
// Check valid splits
119-
let v = vec![0u8, 1u8, 2u8];
120-
let (l, r) = split_at_checked(&v, 0).unwrap();
121-
assert_eq!(l, Vec::<u8>::new());
122-
assert_eq!(r, vec![0u8, 1u8, 2u8]);
123-
124-
let (l, r) = split_at_checked(&v, 1).unwrap();
125-
assert_eq!(l, vec![0u8]);
126-
assert_eq!(r, vec![1u8, 2u8]);
127-
128-
let (l, r) = split_at_checked(&v, 2).unwrap();
129-
assert_eq!(l, vec![0u8, 1u8]);
130-
assert_eq!(r, vec![2u8]);
131-
132-
let (l, r) = split_at_checked(&v, 3).unwrap();
133-
assert_eq!(l, vec![0u8, 1u8, 2u8]);
134-
assert_eq!(r, Vec::<u8>::new());
135-
136-
// Check invalid split
137-
assert!(split_at_checked(&v, 4).is_err());
138-
139-
// Split an empty vector
140-
let v = Vec::<u8>::new();
141-
let (l, r) = split_at_checked(&v, 0).unwrap();
142-
assert_eq!(l, Vec::<u8>::new());
143-
assert_eq!(r, Vec::<u8>::new());
144-
}
145-
146107
#[test]
147108
fn test_nonce() {
148109
let mut rng = ChaCha12Rng::seed_from_u64(8675309); // for testing only!

0 commit comments

Comments
 (0)