Skip to main content

zinc_utils/
lib.rs

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