binius_circuits/lasso/big_integer_ops/
byte_sliced_test_utils.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2024-2025 Irreducible Inc.

use std::{array, fmt::Debug};

use alloy_primitives::U512;
use binius_core::{constraint_system::validate::validate_witness, oracle::OracleId};
use binius_field::{
	arch::OptimalUnderlier, tower_levels::TowerLevel, BinaryField128b, BinaryField1b,
	BinaryField32b, BinaryField8b, Field, TowerField,
};
use rand::{rngs::ThreadRng, thread_rng, Rng};

use super::{
	byte_sliced_add, byte_sliced_add_carryfree, byte_sliced_double_conditional_increment,
	byte_sliced_modular_mul, byte_sliced_mul,
};
use crate::{
	builder::ConstraintSystemBuilder,
	lasso::{
		batch::LookupBatch,
		lookups::u8_arithmetic::{add_carryfree_lookup, add_lookup, dci_lookup, mul_lookup},
	},
	transparent,
	unconstrained::unconstrained,
};

type B8 = BinaryField8b;
type B32 = BinaryField32b;

pub fn random_u512(rng: &mut ThreadRng) -> U512 {
	let limbs = array::from_fn(|_| rng.gen());
	U512::from_limbs(limbs)
}

pub fn test_bytesliced_add<const WIDTH: usize, TL>()
where
	TL: TowerLevel<OracleId, Data = [OracleId; WIDTH]>,
{
	type U = OptimalUnderlier;
	type F = BinaryField128b;
	let allocator = bumpalo::Bump::new();
	let mut builder = ConstraintSystemBuilder::<U, F>::new_with_witness(&allocator);
	let log_size = 14;

	let x_in = array::from_fn(|_| {
		unconstrained::<_, _, BinaryField8b>(&mut builder, "x", log_size).unwrap()
	});
	let y_in = array::from_fn(|_| {
		unconstrained::<_, _, BinaryField8b>(&mut builder, "y", log_size).unwrap()
	});
	let c_in = unconstrained::<_, _, BinaryField1b>(&mut builder, "cin first", log_size).unwrap();

	let lookup_t_add = add_lookup(&mut builder, "add table").unwrap();

	let mut lookup_batch_add = LookupBatch::new(lookup_t_add);
	let _sum_and_cout = byte_sliced_add::<_, _, TL>(
		&mut builder,
		"lasso_bytesliced_add",
		&x_in,
		&y_in,
		c_in,
		log_size,
		&mut lookup_batch_add,
	)
	.unwrap();

	lookup_batch_add
		.execute::<_, _, B32, B32>(&mut builder)
		.unwrap();

	let witness = builder.take_witness().unwrap();
	let constraint_system = builder.build().unwrap();
	let boundaries = vec![];
	validate_witness(&constraint_system, &boundaries, &witness).unwrap();
}

pub fn test_bytesliced_add_carryfree<const WIDTH: usize, TL>()
where
	TL: TowerLevel<OracleId, Data = [OracleId; WIDTH]>,
{
	type U = OptimalUnderlier;
	type F = BinaryField128b;
	let allocator = bumpalo::Bump::new();
	let mut builder = ConstraintSystemBuilder::<U, F>::new_with_witness(&allocator);
	let log_size = 14;
	let x_in = array::from_fn(|_| builder.add_committed("x", log_size, BinaryField8b::TOWER_LEVEL));
	let y_in = array::from_fn(|_| builder.add_committed("y", log_size, BinaryField8b::TOWER_LEVEL));
	let c_in = builder.add_committed("c", log_size, BinaryField1b::TOWER_LEVEL);

	if let Some(witness) = builder.witness() {
		let mut x_in: [_; WIDTH] =
			array::from_fn(|byte_idx| witness.new_column::<BinaryField8b>(x_in[byte_idx]));
		let mut y_in: [_; WIDTH] =
			array::from_fn(|byte_idx| witness.new_column::<BinaryField8b>(y_in[byte_idx]));
		let mut c_in = witness.new_column::<BinaryField1b>(c_in);

		let x_in_bytes_u8: [_; WIDTH] = x_in.each_mut().map(|col| col.as_mut_slice::<u8>());
		let y_in_bytes_u8: [_; WIDTH] = y_in.each_mut().map(|col| col.as_mut_slice::<u8>());
		let c_in_u8 = c_in.as_mut_slice::<u8>();

		for row_idx in 0..1 << log_size {
			let mut rng = thread_rng();
			let input_bitmask = (U512::from(1u8) << (8 * WIDTH)) - U512::from(1u8);
			let mut x = random_u512(&mut rng);
			x &= input_bitmask;
			let mut y = random_u512(&mut rng);
			y &= input_bitmask;

			let mut c: bool = rng.gen();

			while (x + y + U512::from(c)) > input_bitmask {
				x = random_u512(&mut rng);
				x &= input_bitmask;
				y = random_u512(&mut rng);
				y &= input_bitmask;
				c = rng.gen();
			}

			for byte_idx in 0..WIDTH {
				x_in_bytes_u8[byte_idx][row_idx] = x.byte(byte_idx);

				y_in_bytes_u8[byte_idx][row_idx] = y.byte(byte_idx);
			}

			c_in_u8[row_idx / 8] |= (c as u8) << (row_idx % 8);
		}
	}

	let lookup_t_add = add_lookup(&mut builder, "add table").unwrap();
	let lookup_t_add_carryfree = add_carryfree_lookup(&mut builder, "add table").unwrap();

	let mut lookup_batch_add = LookupBatch::new(lookup_t_add);
	let mut lookup_batch_add_carryfree = LookupBatch::new(lookup_t_add_carryfree);

	let _sum_and_cout = byte_sliced_add_carryfree::<_, _, TL>(
		&mut builder,
		"lasso_bytesliced_add_carryfree",
		&x_in,
		&y_in,
		c_in,
		log_size,
		&mut lookup_batch_add,
		&mut lookup_batch_add_carryfree,
	)
	.unwrap();

	lookup_batch_add
		.execute::<_, _, B32, B32>(&mut builder)
		.unwrap();
	lookup_batch_add_carryfree
		.execute::<_, _, B32, B32>(&mut builder)
		.unwrap();

	let witness = builder.take_witness().unwrap();
	let constraint_system = builder.build().unwrap();
	let boundaries = vec![];
	validate_witness(&constraint_system, &boundaries, &witness).unwrap();
}

pub fn test_bytesliced_double_conditional_increment<const WIDTH: usize, TL>()
where
	TL: TowerLevel<OracleId, Data = [OracleId; WIDTH]>,
{
	type U = OptimalUnderlier;
	type F = BinaryField128b;

	let allocator = bumpalo::Bump::new();
	let mut builder = ConstraintSystemBuilder::<U, F>::new_with_witness(&allocator);
	let log_size = 14;

	let x_in = array::from_fn(|_| {
		unconstrained::<_, _, BinaryField8b>(&mut builder, "x", log_size).unwrap()
	});

	let first_c_in =
		unconstrained::<_, _, BinaryField1b>(&mut builder, "cin first", log_size).unwrap();

	let second_c_in =
		unconstrained::<_, _, BinaryField1b>(&mut builder, "cin second", log_size).unwrap();

	let zero_oracle_carry =
		transparent::constant(&mut builder, "zero carry", log_size, BinaryField1b::ZERO).unwrap();
	let lookup_t_dci = dci_lookup(&mut builder, "add table").unwrap();

	let mut lookup_batch_dci = LookupBatch::new(lookup_t_dci);

	let _sum_and_cout = byte_sliced_double_conditional_increment::<_, _, TL>(
		&mut builder,
		"lasso_bytesliced_DCI",
		&x_in,
		first_c_in,
		second_c_in,
		log_size,
		zero_oracle_carry,
		&mut lookup_batch_dci,
	)
	.unwrap();

	lookup_batch_dci
		.execute::<_, _, B32, B32>(&mut builder)
		.unwrap();

	let witness = builder.take_witness().unwrap();
	let constraint_system = builder.build().unwrap();
	let boundaries = vec![];
	validate_witness(&constraint_system, &boundaries, &witness).unwrap();
}

pub fn test_bytesliced_mul<const WIDTH: usize, TL>()
where
	TL: TowerLevel<OracleId>,
	TL::Base: TowerLevel<OracleId, Data = [OracleId; WIDTH]>,
{
	type U = OptimalUnderlier;
	type F = BinaryField128b;

	let allocator = bumpalo::Bump::new();
	let mut builder = ConstraintSystemBuilder::<U, F>::new_with_witness(&allocator);
	let log_size = 14;

	let mult_a = array::from_fn(|_| {
		unconstrained::<_, _, BinaryField8b>(&mut builder, "a", log_size).unwrap()
	});
	let mult_b = array::from_fn(|_| {
		unconstrained::<_, _, BinaryField8b>(&mut builder, "b", log_size).unwrap()
	});

	let zero_oracle_carry =
		transparent::constant(&mut builder, "zero carry", log_size, BinaryField1b::ZERO).unwrap();

	let lookup_t_mul = mul_lookup(&mut builder, "mul lookup").unwrap();
	let lookup_t_add = add_lookup(&mut builder, "add lookup").unwrap();
	let lookup_t_dci = dci_lookup(&mut builder, "dci lookup").unwrap();

	let mut lookup_batch_mul = LookupBatch::new(lookup_t_mul);
	let mut lookup_batch_add = LookupBatch::new(lookup_t_add);
	let mut lookup_batch_dci = LookupBatch::new(lookup_t_dci);

	let _sum_and_cout = byte_sliced_mul::<_, _, TL::Base, TL>(
		&mut builder,
		"lasso_bytesliced_mul",
		&mult_a,
		&mult_b,
		log_size,
		zero_oracle_carry,
		&mut lookup_batch_mul,
		&mut lookup_batch_add,
		&mut lookup_batch_dci,
	)
	.unwrap();

	let witness = builder.take_witness().unwrap();
	let constraint_system = builder.build().unwrap();
	let boundaries = vec![];
	validate_witness(&constraint_system, &boundaries, &witness).unwrap();
}

pub fn test_bytesliced_modular_mul<const WIDTH: usize, TL>()
where
	TL: TowerLevel<OracleId>,
	TL::Base: TowerLevel<OracleId, Data = [OracleId; WIDTH]>,
	<TL as TowerLevel<usize>>::Data: Debug,
{
	type U = OptimalUnderlier;
	type F = BinaryField128b;

	let allocator = bumpalo::Bump::new();
	let mut builder = ConstraintSystemBuilder::<U, F>::new_with_witness(&allocator);
	let log_size = 14;

	let mut rng = thread_rng();

	let mult_a = builder.add_committed_multiple::<WIDTH>("a", log_size, B8::TOWER_LEVEL);
	let mult_b = builder.add_committed_multiple::<WIDTH>("b", log_size, B8::TOWER_LEVEL);

	let input_bitmask = (U512::from(1u8) << (8 * WIDTH)) - U512::from(1u8);

	let modulus = (random_u512(&mut rng) % input_bitmask) + U512::from(1u8);

	if let Some(witness) = builder.witness() {
		let mut mult_a: [_; WIDTH] =
			array::from_fn(|byte_idx| witness.new_column::<BinaryField8b>(mult_a[byte_idx]));

		let mult_a_u8 = mult_a.each_mut().map(|col| col.as_mut_slice::<u8>());

		let mut mult_b: [_; WIDTH] =
			array::from_fn(|byte_idx| witness.new_column::<BinaryField8b>(mult_b[byte_idx]));

		let mult_b_u8 = mult_b.each_mut().map(|col| col.as_mut_slice::<u8>());

		for row_idx in 0..1 << log_size {
			let mut a = random_u512(&mut rng);
			let mut b = random_u512(&mut rng);

			a %= modulus;
			b %= modulus;

			for byte_idx in 0..WIDTH {
				mult_a_u8[byte_idx][row_idx] = a.byte(byte_idx);
				mult_b_u8[byte_idx][row_idx] = b.byte(byte_idx);
			}
		}
	}

	let modulus_input: [_; WIDTH] = array::from_fn(|byte_idx| modulus.byte(byte_idx));

	let zero_oracle_byte =
		transparent::constant(&mut builder, "zero carry", log_size, BinaryField8b::ZERO).unwrap();

	let zero_oracle_carry =
		transparent::constant(&mut builder, "zero carry", log_size, BinaryField1b::ZERO).unwrap();

	let _modded_product = byte_sliced_modular_mul::<_, _, TL::Base, TL>(
		&mut builder,
		"lasso_bytesliced_mul",
		&mult_a,
		&mult_b,
		&modulus_input,
		log_size,
		zero_oracle_byte,
		zero_oracle_carry,
	)
	.unwrap();

	let witness = builder.take_witness().unwrap();
	let constraint_system = builder.build().unwrap();
	let boundaries = vec![];
	validate_witness(&constraint_system, &boundaries, &witness).unwrap();
}