binius_circuits/lasso/
batch.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
// Copyright 2024-2025 Irreducible Inc.

use anyhow::Ok;
use binius_core::oracle::OracleId;
use binius_field::{
	as_packed_field::{PackScalar, PackedType},
	ExtensionField, PackedFieldIndexable, TowerField,
};

use super::lasso::lasso;
use crate::builder::ConstraintSystemBuilder;
pub struct LookupBatch {
	lookup_us: Vec<OracleId>,
	u_to_t_mappings: Vec<Vec<usize>>,
	lookup_col_lens: Vec<usize>,
	lookup_t: OracleId,
	executed: bool,
}

impl Drop for LookupBatch {
	fn drop(&mut self) {
		if !self.executed {
			tracing::warn!("LookupBatch dropped before calling execute!");
		}
	}
}

impl LookupBatch {
	pub fn new(t_table_id: OracleId) -> Self {
		Self {
			lookup_t: t_table_id,
			lookup_us: vec![],
			u_to_t_mappings: vec![],
			lookup_col_lens: vec![],
			executed: false,
		}
	}

	pub fn add(&mut self, lookup_u: OracleId, u_to_t_mapping: Vec<usize>, lookup_u_col_len: usize) {
		self.lookup_us.push(lookup_u);
		self.u_to_t_mappings.push(u_to_t_mapping);
		self.lookup_col_lens.push(lookup_u_col_len);
	}

	pub fn execute<U, F, FS, FC>(
		mut self,
		builder: &mut ConstraintSystemBuilder<U, F>,
	) -> Result<(), anyhow::Error>
	where
		U: PackScalar<FC> + PackScalar<F>,
		PackedType<U, FC>: PackedFieldIndexable,
		FC: TowerField,
		FS: TowerField,
		F: ExtensionField<FC> + TowerField,
	{
		let channel = builder.add_channel();

		lasso::<_, _, FS, FC>(
			builder,
			"batched lasso",
			&self.lookup_col_lens,
			&self.u_to_t_mappings,
			&self.lookup_us,
			self.lookup_t,
			channel,
		)?;

		self.executed = true;

		Ok(())
	}
}