Skip to content

Commit

Permalink
Remove assertion and add test (#759)
Browse files Browse the repository at this point in the history
* Remove assertion and add test

* Tweak
  • Loading branch information
Pratyush authored Jan 16, 2024
1 parent 7357e5e commit bf96a5b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 3 additions & 11 deletions ec/src/scalar_mul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ pub trait ScalarMul:
pub struct BatchMulPreprocessing<T: ScalarMul> {
pub window: usize,
pub max_scalar_size: usize,
pub max_num_scalars: usize,
pub table: Vec<Vec<T::MulBase>>,
}

Expand All @@ -178,10 +177,10 @@ impl<T: ScalarMul> BatchMulPreprocessing<T> {

pub fn with_num_scalars_and_scalar_size(
base: T,
max_num_scalars: usize,
num_scalars: usize,
max_scalar_size: usize,
) -> Self {
let window = Self::compute_window_size(max_num_scalars);
let window = Self::compute_window_size(num_scalars);
let in_window = 1 << window;
let outerc = (max_scalar_size + window - 1) / window;
let last_in_window = 1 << (max_scalar_size - (outerc - 1) * window);
Expand Down Expand Up @@ -219,7 +218,6 @@ impl<T: ScalarMul> BatchMulPreprocessing<T> {
Self {
window,
max_scalar_size,
max_num_scalars,
table,
}
}
Expand All @@ -233,13 +231,7 @@ impl<T: ScalarMul> BatchMulPreprocessing<T> {
}

pub fn batch_mul(&self, v: &[T::ScalarField]) -> Vec<T::MulBase> {
assert!(
v.len() <= self.max_num_scalars,
"number of scalars exceeds the maximum number of scalars supported by this table"
);
let result = cfg_iter!(v)
.map(|e| self.windowed_mul(e))
.collect::<Vec<_>>();
let result: Vec<_> = cfg_iter!(v).map(|e| self.windowed_mul(e)).collect();
T::batch_convert_to_mul_base(&result)
}

Expand Down
12 changes: 12 additions & 0 deletions test-templates/src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ macro_rules! __test_group {
assert_eq!(context.mul_with_table(&bad_table, &b), None);
}
}

// num_scalars != scalars.len()
let mut scalars = vec![ScalarField::rand(&mut rng); 100];
scalars[0] = ScalarField::zero();
let table = BatchMulPreprocessing::new(a, scalars.len() - 1);
let result = table.batch_mul(&scalars);
let naive_result = scalars
.iter()
.enumerate()
.map(|(i, s)| a * s)
.collect::<Vec<_>>();
assert_eq!(result, naive_result);
}
}

Expand Down

0 comments on commit bf96a5b

Please sign in to comment.