binius_core/composition/
index.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2024-2025 Irreducible Inc.

use std::fmt::Debug;

use binius_field::{Field, PackedField};
use binius_math::{ArithExpr, CompositionPolyOS};
use binius_utils::bail;

use crate::polynomial::Error;

/// An adapter which allows evaluating a composition over a larger query by indexing into it.
/// See [`index_composition`] for a factory method.
#[derive(Clone, Debug)]
pub struct IndexComposition<C, const N: usize> {
	/// Number of variables in a larger query
	n_vars: usize,
	/// Mapping from the inner composition query variables to outer query variables
	indices: [usize; N],
	/// Inner composition
	composition: C,
}

impl<C, const N: usize> IndexComposition<C, N> {
	pub fn new(n_vars: usize, indices: [usize; N], composition: C) -> Result<Self, Error> {
		if indices.iter().any(|&index| index >= n_vars) {
			bail!(Error::IndexCompositionIndicesOutOfBounds);
		}

		Ok(Self {
			n_vars,
			indices,
			composition,
		})
	}
}

impl<P: PackedField, C: CompositionPolyOS<P>, const N: usize> CompositionPolyOS<P>
	for IndexComposition<C, N>
{
	fn n_vars(&self) -> usize {
		self.n_vars
	}

	fn degree(&self) -> usize {
		self.composition.degree()
	}

	fn expression(&self) -> ArithExpr<<P as PackedField>::Scalar> {
		fn map_variables<F: Field, const M: usize>(
			index_map: &[usize; M],
			expr: &ArithExpr<F>,
		) -> ArithExpr<F> {
			match expr {
				ArithExpr::Var(i) => ArithExpr::Var(index_map[*i]),
				ArithExpr::Const(c) => ArithExpr::Const(*c),
				ArithExpr::Add(a, b) => ArithExpr::Add(
					Box::new(map_variables(index_map, a)),
					Box::new(map_variables(index_map, b)),
				),
				ArithExpr::Mul(a, b) => ArithExpr::Mul(
					Box::new(map_variables(index_map, a)),
					Box::new(map_variables(index_map, b)),
				),
				ArithExpr::Pow(a, n) => ArithExpr::Pow(Box::new(map_variables(index_map, a)), *n),
			}
		}

		map_variables(&self.indices, &self.composition.expression())
	}

	fn evaluate(&self, query: &[P]) -> Result<P, binius_math::Error> {
		if query.len() != self.n_vars {
			bail!(binius_math::Error::IncorrectQuerySize {
				expected: self.n_vars,
			});
		}

		let subquery = self.indices.map(|index| query[index]);
		self.composition.evaluate(&subquery)
	}

	fn binary_tower_level(&self) -> usize {
		self.composition.binary_tower_level()
	}

	fn batch_evaluate(
		&self,
		batch_query: &[&[P]],
		evals: &mut [P],
	) -> Result<(), binius_math::Error> {
		let batch_subquery = self.indices.map(|index| batch_query[index]);
		self.composition
			.batch_evaluate(batch_subquery.as_slice(), evals)
	}
}

/// A factory helper method to create an [`IndexComposition`] by looking at
///  * `superset` - a set of identifiers of a greater (outer) query
///  * `subset` - a set of identifiers of a smaller query, the one which corresponds to the inner composition directly
///
/// Identifiers may be anything `Eq` - `OracleId`, `MultilinearPolyOracle<F>`, etc.
pub fn index_composition<E, C, const N: usize>(
	superset: &[E],
	subset: [E; N],
	composition: C,
) -> Result<IndexComposition<C, N>, Error>
where
	E: PartialEq,
{
	let n_vars = superset.len();

	// array_try_map is unstable as of 03/24, check the condition beforehand
	let proper_subset = subset.iter().all(|subset_item| {
		superset
			.iter()
			.any(|superset_item| superset_item == subset_item)
	});

	if !proper_subset {
		bail!(Error::MixedMultilinearNotFound);
	}

	let indices = subset.map(|subset_item| {
		superset
			.iter()
			.position(|superset_item| superset_item == &subset_item)
			.expect("Superset condition checked above.")
	});

	Ok(IndexComposition {
		n_vars,
		indices,
		composition,
	})
}

#[cfg(test)]
mod tests {
	use binius_field::BinaryField1b;

	use super::*;
	use crate::polynomial::ArithCircuitPoly;

	#[test]
	fn tests_expr() {
		let expr = ArithExpr::Add(
			Box::new(ArithExpr::Var(0)),
			Box::new(ArithExpr::Mul(
				Box::new(ArithExpr::Var(1)),
				Box::new(ArithExpr::Const(BinaryField1b::ONE)),
			)),
		);
		let circuit = ArithCircuitPoly::new(expr);

		let composition = IndexComposition {
			n_vars: 3,
			indices: [1, 2],
			composition: circuit,
		};

		assert_eq!(
			(&composition as &dyn CompositionPolyOS<BinaryField1b>).expression(),
			ArithExpr::Add(
				Box::new(ArithExpr::Var(1)),
				Box::new(ArithExpr::Mul(
					Box::new(ArithExpr::Var(2)),
					Box::new(ArithExpr::Const(BinaryField1b::ONE)),
				)),
			)
		);
	}
}