Skip to main content

zinc_utils/
lib.rs

1pub mod field;
2pub mod from_ref;
3pub mod inner_product;
4pub mod mul_by_scalar;
5pub mod named;
6pub mod ops_macros;
7pub mod parallel;
8pub mod projectable_to_field;
9
10use crypto_primitives::SemiringConfig;
11
12// Can't use enums in const generics in stable Rust yet, so we use constants
13// instead.
14pub const CHECKED: bool = true;
15pub const UNCHECKED: bool = false;
16
17/// Returns ceil(log2(x)).
18/// Copied from ark-std.
19#[inline(always)]
20#[allow(clippy::arithmetic_side_effects)]
21pub const fn log2(x: usize) -> u32 {
22    if x == 0 {
23        0
24    } else if x.is_power_of_two() {
25        1usize.leading_zeros() - x.leading_zeros()
26    } else {
27        0usize.leading_zeros() - x.leading_zeros()
28    }
29}
30
31/// Powers `[1, x, x^2, ..., x^(num_pows-1)]` computed via the config.
32pub fn powers<S: SemiringConfig>(cfg: &S, x: &S::Element, num_pows: usize) -> Vec<S::Element> {
33    if num_pows == 0 {
34        return Vec::new();
35    }
36
37    let mut pows = Vec::with_capacity(num_pows);
38    pows.push(cfg.one());
39
40    let mut curr_pow = x.clone();
41    for _ in 1..num_pows {
42        pows.push(curr_pow.clone());
43        cfg.mul_assign(&mut curr_pow, x);
44    }
45
46    pows
47}