binius_core/polynomial/
test_utils.rs

1// Copyright 2024-2025 Irreducible Inc.
2
3use binius_field::{packed::set_packed_slice, BinaryField1b, Field, PackedField};
4
5use crate::polynomial::MultivariatePoly;
6
7pub fn hypercube_evals_from_oracle<F: Field>(oracle: &dyn MultivariatePoly<F>) -> Vec<F> {
8	(0..(1 << oracle.n_vars()))
9		.map(|i| {
10			oracle
11				.evaluate(&decompose_index_to_hypercube_point(oracle.n_vars(), i))
12				.unwrap()
13		})
14		.collect()
15}
16
17pub fn decompose_index_to_hypercube_point<F: Field>(n_vars: usize, index: usize) -> Vec<F> {
18	(0..n_vars)
19		.map(|k| match (index >> k) & 1 {
20			0 => F::ZERO,
21			_ => F::ONE,
22		})
23		.collect()
24}
25
26pub fn packed_slice<P>(assignments: &[(std::ops::Range<usize>, u8)]) -> Vec<P>
27where
28	P: PackedField<Scalar = BinaryField1b>,
29{
30	assert_eq!(assignments[0].0.start, 0, "First assignment must start at index 0");
31	assert_eq!(
32		assignments[assignments.len() - 1].0.end % P::WIDTH,
33		0,
34		"Last assignment must end at an index divisible by packing width"
35	);
36	for i in 1..assignments.len() {
37		assert_eq!(
38			assignments[i].0.start,
39			assignments[i - 1].0.end,
40			"2 assignments following each other can't be overlapping or have holes in between"
41		);
42	}
43	assignments
44		.iter()
45		.for_each(|(r, _)| assert!(r.end > r.start, "Range must have positive size"));
46	let packed_len = assignments
47		.iter()
48		.map(|(range, _)| range.end)
49		.max()
50		.unwrap_or(0)
51		.div_ceil(P::WIDTH);
52	let mut result: Vec<P> = vec![P::default(); packed_len];
53	for (range, value) in assignments {
54		for i in range.clone() {
55			set_packed_slice(&mut result, i, P::Scalar::from(*value));
56		}
57	}
58	result
59}