openvm_circuit_primitives/
utils.rs1use itertools::zip_eq;
2use openvm_stark_backend::{
3 p3_air::{AirBuilder, VirtualPairCol},
4 p3_field::{Field, PrimeCharacteristicRing},
5};
6
7pub const fn next_power_of_two_or_zero(n: usize) -> usize {
10 if n == 0 {
11 0
12 } else {
13 n.next_power_of_two()
14 }
15}
16
17pub fn not<F: PrimeCharacteristicRing>(a: impl Into<F>) -> F {
18 F::ONE - a.into()
19}
20
21pub fn and<F: PrimeCharacteristicRing>(a: impl Into<F>, b: impl Into<F>) -> F {
22 a.into() * b.into()
23}
24
25pub fn or<F: PrimeCharacteristicRing>(a: impl Into<F>, b: impl Into<F>) -> F {
27 let a = a.into();
28 let b = b.into();
29 a.clone() + b.clone() - and(a, b)
30}
31
32pub fn implies<F: PrimeCharacteristicRing>(a: impl Into<F>, b: impl Into<F>) -> F {
34 or(F::ONE - a.into(), b.into())
35}
36
37pub fn select<F: PrimeCharacteristicRing>(
39 cond: impl Into<F>,
40 a: impl Into<F>,
41 b: impl Into<F>,
42) -> F {
43 let cond = cond.into();
44 cond.clone() * a.into() + (F::ONE - cond) * b.into()
45}
46
47pub fn to_vcols<F: Field>(cols: &[usize]) -> Vec<VirtualPairCol<F>> {
48 cols.iter()
49 .copied()
50 .map(VirtualPairCol::single_main)
51 .collect()
52}
53
54pub fn fill_slc_to_f<F: Field>(dest: &mut [F], src: &[u32]) {
55 dest.iter_mut()
56 .zip(src.iter())
57 .for_each(|(d, s)| *d = F::from_u32(*s));
58}
59
60pub fn to_field_vec<F: Field>(src: &[u32]) -> Vec<F> {
61 src.iter().map(|s| F::from_u32(*s)).collect()
62}
63
64pub fn assert_array_eq<AB: AirBuilder, I1: Into<AB::Expr>, I2: Into<AB::Expr>, const N: usize>(
65 builder: &mut AB,
66 x: [I1; N],
67 y: [I2; N],
68) {
69 for (x, y) in zip_eq(x, y) {
70 builder.assert_eq(x, y);
71 }
72}
73
74#[inline]
76pub fn compose<F: PrimeCharacteristicRing>(a: &[impl Into<F> + Clone], limb_size: usize) -> F {
77 a.iter().enumerate().fold(F::ZERO, |acc, (i, x)| {
78 acc + x.clone().into() * F::from_usize(1 << (i * limb_size))
79 })
80}