binius_circuits/builder/
witness.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2024-2025 Irreducible Inc.

use std::{cell::RefCell, marker::PhantomData, rc::Rc};

use anyhow::{anyhow, Error};
use binius_core::{
	oracle::{MultilinearOracleSet, OracleId},
	witness::{MultilinearExtensionIndex, MultilinearWitness},
};
use binius_field::{
	as_packed_field::{PackScalar, PackedType},
	underlier::WithUnderlier,
	ExtensionField, Field, PackedField, TowerField,
};
use binius_math::MultilinearExtension;
use binius_utils::bail;
use bytemuck::{must_cast_slice, must_cast_slice_mut, Pod};

pub struct Builder<'arena, U: PackScalar<FW>, FW: TowerField> {
	bump: &'arena bumpalo::Bump,

	oracles: Rc<RefCell<MultilinearOracleSet<FW>>>,

	#[allow(clippy::type_complexity)]
	entries: Rc<RefCell<Vec<Option<WitnessBuilderEntry<'arena, U, FW>>>>>,
}

struct WitnessBuilderEntry<'arena, U: PackScalar<FW>, FW: Field> {
	witness: Result<MultilinearWitness<'arena, PackedType<U, FW>>, binius_math::Error>,
	tower_level: usize,
	data: &'arena [U],
}

impl<'arena, U, FW> Builder<'arena, U, FW>
where
	U: PackScalar<FW>,
	FW: TowerField,
{
	pub fn new(
		allocator: &'arena bumpalo::Bump,
		oracles: Rc<RefCell<MultilinearOracleSet<FW>>>,
	) -> Self {
		Self {
			bump: allocator,
			oracles,
			entries: Rc::new(RefCell::new(Vec::new())),
		}
	}

	pub fn new_column<FS: TowerField>(&self, id: OracleId) -> EntryBuilder<'arena, U, FW, FS>
	where
		U: PackScalar<FS>,
		FW: ExtensionField<FS>,
	{
		let oracles = self.oracles.borrow();
		let log_rows = oracles.n_vars(id);
		let len = 1 << log_rows.saturating_sub(<PackedType<U, FS>>::LOG_WIDTH);
		let data = bumpalo::vec![in self.bump; U::default(); len].into_bump_slice_mut();
		EntryBuilder {
			_marker: PhantomData,
			log_rows,
			id,
			data: Some(data),
			entries: self.entries.clone(),
		}
	}

	pub fn new_column_with_default<FS: TowerField>(
		&self,
		id: OracleId,
		default: FS,
	) -> EntryBuilder<'arena, U, FW, FS>
	where
		U: PackScalar<FS>,
		FW: ExtensionField<FS>,
	{
		let oracles = self.oracles.borrow();
		let log_rows = oracles.n_vars(id);
		let len = 1 << log_rows.saturating_sub(<PackedType<U, FS>>::LOG_WIDTH);
		let default = WithUnderlier::to_underlier(PackedType::<U, FS>::broadcast(default));
		let data = bumpalo::vec![in self.bump; default; len].into_bump_slice_mut();
		EntryBuilder {
			_marker: PhantomData,
			log_rows,
			id,
			data: Some(data),
			entries: self.entries.clone(),
		}
	}

	pub fn get<FS: TowerField>(&self, id: OracleId) -> Result<WitnessEntry<'arena, U, FS>, Error>
	where
		U: PackScalar<FS>,
		FW: ExtensionField<FS>,
	{
		let entries = self.entries.borrow();
		let oracles = self.oracles.borrow();
		if !oracles.is_valid_oracle_id(id) {
			bail!(anyhow!("OracleId {id} does not exist in MultilinearOracleSet"));
		}
		let entry = entries
			.get(id)
			.and_then(|entry| entry.as_ref())
			.ok_or_else(|| anyhow!("Witness for {} is missing", oracles.label(id)))?;

		if entry.tower_level != FS::TOWER_LEVEL {
			bail!(anyhow!(
				"Provided tower level ({}) for {} does not match stored tower level {}.",
				FS::TOWER_LEVEL,
				oracles.label(id),
				entry.tower_level
			));
		}

		Ok(WitnessEntry {
			data: entry.data,
			log_rows: oracles.n_vars(id),
			_marker: PhantomData,
		})
	}

	pub fn set<FS: TowerField>(
		&self,
		id: OracleId,
		entry: WitnessEntry<'arena, U, FS>,
	) -> Result<(), Error>
	where
		U: PackScalar<FS>,
		FW: ExtensionField<FS>,
	{
		let oracles = self.oracles.borrow();
		if !oracles.is_valid_oracle_id(id) {
			bail!(anyhow!("OracleId {id} does not exist in MultilinearOracleSet"));
		}
		let mut entries = self.entries.borrow_mut();
		if id >= entries.len() {
			entries.resize_with(id + 1, || None);
		}
		entries[id] = Some(WitnessBuilderEntry {
			data: entry.data,
			tower_level: FS::TOWER_LEVEL,
			witness: MultilinearExtension::new(entry.log_rows, entry.packed())
				.map(|x| x.specialize_arc_dyn()),
		});
		Ok(())
	}

	pub fn build(self) -> Result<MultilinearExtensionIndex<'arena, U, FW>, Error> {
		let mut result = MultilinearExtensionIndex::new();
		let entries = Rc::into_inner(self.entries)
			.ok_or(anyhow!("Failed to build. There are still entries refs. Make sure there are no pending column insertions."))?
			.into_inner()
			.into_iter()
			.enumerate()
			.filter_map(|(id, entry)| entry.map(|entry| Ok((id, entry.witness?))))
			.collect::<Result<Vec<_>, Error>>()?;
		result.update_multilin_poly(entries)?;
		Ok(result)
	}
}

#[derive(Debug, Clone, Copy)]
pub struct WitnessEntry<'arena, U: PackScalar<FS>, FS: TowerField> {
	data: &'arena [U],
	log_rows: usize,
	_marker: PhantomData<FS>,
}

impl<'arena, U: PackScalar<FS>, FS: TowerField> WitnessEntry<'arena, U, FS> {
	#[inline]
	pub fn packed(&self) -> &'arena [PackedType<U, FS>] {
		WithUnderlier::from_underliers_ref(self.data)
	}

	pub fn repacked<FW>(&self) -> WitnessEntry<'arena, U, FW>
	where
		FW: TowerField + ExtensionField<FS>,
		U: PackScalar<FW>,
	{
		WitnessEntry {
			data: self.data,
			log_rows: self.log_rows - <FW as ExtensionField<FS>>::LOG_DEGREE,
			_marker: PhantomData,
		}
	}

	pub fn low_rows(&self) -> usize {
		self.log_rows
	}
}

impl<'arena, U: PackScalar<FS> + Pod, FS: TowerField> WitnessEntry<'arena, U, FS> {
	#[inline]
	pub fn as_slice<T: Pod>(&self) -> &'arena [T] {
		must_cast_slice(self.data)
	}
}

pub struct EntryBuilder<'arena, U, FW, FS>
where
	U: PackScalar<FW> + PackScalar<FS>,
	FS: TowerField,
	FW: TowerField + ExtensionField<FS>,
{
	_marker: PhantomData<FS>,
	#[allow(clippy::type_complexity)]
	entries: Rc<RefCell<Vec<Option<WitnessBuilderEntry<'arena, U, FW>>>>>,
	id: OracleId,
	log_rows: usize,
	data: Option<&'arena mut [U]>,
}

impl<U, FW, FS> EntryBuilder<'_, U, FW, FS>
where
	U: PackScalar<FW> + PackScalar<FS>,
	FS: TowerField,
	FW: TowerField + ExtensionField<FS>,
{
	#[inline]
	pub fn packed(&mut self) -> &mut [PackedType<U, FS>] {
		PackedType::<U, FS>::from_underliers_ref_mut(self.underliers())
	}

	#[inline]
	fn underliers(&mut self) -> &mut [U] {
		self.data
			.as_mut()
			.expect("Should only be None after Drop::drop has run")
	}
}

impl<U, FW, FS> EntryBuilder<'_, U, FW, FS>
where
	U: PackScalar<FW> + PackScalar<FS> + Pod,
	FS: TowerField,
	FW: TowerField + ExtensionField<FS>,
{
	#[inline]
	pub fn as_mut_slice<T: Pod>(&mut self) -> &mut [T] {
		must_cast_slice_mut(self.underliers())
	}
}

impl<U, FW, FS> Drop for EntryBuilder<'_, U, FW, FS>
where
	U: PackScalar<FW> + PackScalar<FS>,
	FS: TowerField,
	FW: TowerField + ExtensionField<FS>,
{
	fn drop(&mut self) {
		let data = Option::take(&mut self.data).expect("data is always Some until this point");
		let mut entries = self.entries.borrow_mut();
		let id = self.id;
		if id >= entries.len() {
			entries.resize_with(id + 1, || None);
		}
		entries[id] = Some(WitnessBuilderEntry {
			data,
			tower_level: FS::TOWER_LEVEL,
			witness: MultilinearExtension::new(
				self.log_rows,
				PackedType::<U, FS>::from_underliers_ref(data),
			)
			.map(|x| x.specialize_arc_dyn()),
		})
	}
}