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
// Copyright 2024 Ulvetanna Inc.

use super::OracleId;
use crate::{composition::index_composition, polynomial::CompositionPoly};
use binius_field::{Field, PackedField};
use std::sync::Arc;

/// Composition trait object that can be used to create lists of compositions of differing
/// concrete types.
pub type TypeErasedComposition<P> = Arc<dyn CompositionPoly<P>>;

/// Constraint is a type erased composition along with a predicate on its values on the boolean hypercube
#[derive(Clone)]
pub struct Constraint<P: PackedField> {
	pub composition: TypeErasedComposition<P>,
	pub predicate: ConstraintPredicate<P::Scalar>,
}

/// Predicate can either be a sum of values of a composition on the hypercube (sumcheck) or equality to zero
/// on the hypercube (zerocheck)
#[derive(Clone, Debug)]
pub enum ConstraintPredicate<F: Field> {
	Sum(F),
	Zero,
}

impl<F: Field> ConstraintPredicate<F> {
	/// Representation in an isomorphic field
	pub fn isomorphic<FI: Field + From<F>>(self) -> ConstraintPredicate<FI> {
		match self {
			ConstraintPredicate::Sum(sum) => ConstraintPredicate::Sum(sum.into()),
			ConstraintPredicate::Zero => ConstraintPredicate::Zero,
		}
	}
}

/// Constraint set is a group of constraints that operate over the same set of oracle-identified multilinears
#[derive(Clone)]
pub struct ConstraintSet<P: PackedField> {
	pub oracle_ids: Vec<OracleId>,
	pub constraints: Vec<Constraint<P>>,
}

// A deferred constraint constructor that instantiates index composition after the superset of oracles is known
#[allow(clippy::type_complexity)]
struct ConstraintThunk<P: PackedField> {
	composition_thunk: Box<dyn FnOnce(&[OracleId]) -> TypeErasedComposition<P>>,
	predicate: ConstraintPredicate<P::Scalar>,
}

/// A builder struct that turns individual compositions over oraclized multilinears into a set of
/// type erased `IndexComposition` instances operating over a superset of oracles of all constraints.
pub struct ConstraintSetBuilder<P: PackedField> {
	oracle_ids: Vec<OracleId>,
	constraint_thunks: Vec<ConstraintThunk<P>>,
}

impl<P: PackedField> ConstraintSetBuilder<P> {
	#[allow(clippy::new_without_default)]
	pub fn new() -> Self {
		Self {
			oracle_ids: Vec::new(),
			constraint_thunks: Vec::new(),
		}
	}

	pub fn add_sumcheck<Composition, const N: usize>(
		&mut self,
		oracle_ids: [OracleId; N],
		composition: Composition,
		sum: P::Scalar,
	) where
		Composition: CompositionPoly<P> + 'static,
	{
		self.oracle_ids.extend(&oracle_ids);
		self.constraint_thunks.push(ConstraintThunk {
			composition_thunk: thunk(oracle_ids, composition),
			predicate: ConstraintPredicate::Sum(sum),
		});
	}

	pub fn add_zerocheck<Composition, const N: usize>(
		&mut self,
		oracle_ids: [OracleId; N],
		composition: Composition,
	) where
		Composition: CompositionPoly<P> + 'static,
	{
		self.oracle_ids.extend(&oracle_ids);
		self.constraint_thunks.push(ConstraintThunk {
			composition_thunk: thunk(oracle_ids, composition),
			predicate: ConstraintPredicate::Zero,
		});
	}

	pub fn build(self) -> ConstraintSet<P> {
		let mut oracle_ids = self.oracle_ids;

		oracle_ids.sort();
		oracle_ids.dedup();

		// at this point the superset of oracles is known and index compositions
		// may be finally instantiated
		let constraints = self
			.constraint_thunks
			.into_iter()
			.map(|constraint_thunk| {
				let composition = (constraint_thunk.composition_thunk)(&oracle_ids);
				Constraint {
					composition,
					predicate: constraint_thunk.predicate,
				}
			})
			.collect();

		ConstraintSet {
			oracle_ids,
			constraints,
		}
	}
}

// Oracle superset is unknown until the very creation of the constraint set. We
// type erase the index composition constructor to be able to store them in the builder.
//
// Wikipedia: thunk is a subroutine used to inject a calculation into another subroutine.
// Thunks are primarily used to delay a calculation until its result is needed, or to insert
// operations at the beginning or end of the other subroutine.
#[allow(clippy::type_complexity)]
fn thunk<P, Composition, const N: usize>(
	oracle_ids: [OracleId; N],
	composition: Composition,
) -> Box<dyn FnOnce(&[OracleId]) -> TypeErasedComposition<P>>
where
	P: PackedField,
	Composition: CompositionPoly<P> + 'static,
{
	Box::new(move |all_oracle_ids| {
		let indexed = index_composition(all_oracle_ids, oracle_ids, composition)
			.expect("Infallible by ConstraintSetBuilder invariants.");

		Arc::new(indexed)
	})
}