Skip to content

Commit 8387f8b

Browse files
authored
Make range constraints copy. (#3450)
Range constraints consist of three items that are all copy, so it makes sense to make it copy as well.
1 parent 186302a commit 8387f8b

File tree

9 files changed

+57
-66
lines changed

9 files changed

+57
-66
lines changed

autoprecompiles/src/low_degree_bus_interaction_optimizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<
184184
})
185185
.filter(|function| {
186186
self.has_few_possible_values(
187-
function.inputs.iter().map(|f| f.range_constraint.clone()),
187+
function.inputs.iter().map(|f| f.range_constraint),
188188
MAX_DOMAIN_SIZE,
189189
)
190190
})

constraint-solver/src/algebraic_constraint/solve.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ fn effect_to_range_constraint<T: FieldElement, V: Ord + Clone + Eq>(
317317
effect: &Effect<T, V>,
318318
) -> Option<(V, RangeConstraint<T>)> {
319319
match effect {
320-
Effect::RangeConstraint(var, rc) => Some((var.clone(), rc.clone())),
320+
Effect::RangeConstraint(var, rc) => Some((var.clone(), *rc)),
321321
Effect::Assignment(var, value) => Some((var.clone(), value.range_constraint())),
322322
_ => None,
323323
}
@@ -465,8 +465,7 @@ mod tests {
465465
let b = Qse::from_unknown_variable("b");
466466
let c = Qse::from_unknown_variable("c");
467467
let z = Qse::from_unknown_variable("Z");
468-
let range_constraints =
469-
HashMap::from([("a", rc.clone()), ("b", rc.clone()), ("c", rc.clone())]);
468+
let range_constraints = HashMap::from([("a", rc), ("b", rc), ("c", rc)]);
470469
// a * 0x100 + b * 0x10000 + c * 0x1000000 + 10 - Z = 0
471470
let ten = constant(10);
472471
let constr =
@@ -503,7 +502,7 @@ mod tests {
503502
let Effect::RangeConstraint(var, rc) = effect else {
504503
panic!();
505504
};
506-
(var, rc.clone())
505+
(var, *rc)
507506
}
508507

509508
#[test]
@@ -583,7 +582,7 @@ mod tests {
583582
fn bool_plus_one_cant_be_zero() {
584583
let expr = var("a") + constant(1);
585584
let rc = RangeConstraint::from_mask(0x1u64);
586-
let range_constraints = HashMap::from([("a", rc.clone())]);
585+
let range_constraints = HashMap::from([("a", rc)]);
587586
assert!(AlgebraicConstraint::assert_zero(&expr)
588587
.solve(&range_constraints)
589588
.is_err());

constraint-solver/src/range_constraint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use powdr_number::{log2_exact, FieldElement, LargeInt};
3535
/// of the full system.
3636
///
3737
/// Finally, please be aware that same constraint can have multiple representations.
38-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
38+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
3939
pub struct RangeConstraint<T: FieldElement> {
4040
/// Bit-mask. A value `x` is allowed only if `x & mask == x` (when seen as unsigned integer).
4141
mask: T::Integer,

constraint-solver/src/solver/constraint_splitter.rs

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ mod test {
380380
fn split_simple() {
381381
let four_bit_rc = RangeConstraint::from_mask(0xfu32);
382382
let rcs = [
383-
("x", four_bit_rc.clone()),
384-
("y", four_bit_rc.clone()),
385-
("a", four_bit_rc.clone()),
386-
("b", four_bit_rc.clone()),
383+
("x", four_bit_rc),
384+
("y", four_bit_rc),
385+
("a", four_bit_rc),
386+
("b", four_bit_rc),
387387
]
388388
.into_iter()
389389
.collect::<HashMap<_, _>>();
@@ -397,13 +397,13 @@ mod test {
397397
fn split_multiple() {
398398
let four_bit_rc = RangeConstraint::from_mask(0xfu32);
399399
let rcs = [
400-
("x", four_bit_rc.clone()),
401-
("y", four_bit_rc.clone()),
402-
("a", four_bit_rc.clone()),
403-
("b", four_bit_rc.clone()),
404-
("r", four_bit_rc.clone()),
405-
("s", four_bit_rc.clone()),
406-
("w", four_bit_rc.clone()),
400+
("x", four_bit_rc),
401+
("y", four_bit_rc),
402+
("a", four_bit_rc),
403+
("b", four_bit_rc),
404+
("r", four_bit_rc),
405+
("s", four_bit_rc),
406+
("w", four_bit_rc),
407407
]
408408
.into_iter()
409409
.collect::<HashMap<_, _>>();
@@ -432,13 +432,9 @@ w = 0"
432432

433433
let byte_rc = RangeConstraint::from_mask(0xffu32);
434434
let bit_rc = RangeConstraint::from_mask(0x1u32);
435-
let rcs = [
436-
("b__3_0", byte_rc.clone()),
437-
("b_msb_f_0", byte_rc.clone()),
438-
("x", bit_rc.clone()),
439-
]
440-
.into_iter()
441-
.collect::<HashMap<_, _>>();
435+
let rcs = [("b__3_0", byte_rc), ("b_msb_f_0", byte_rc), ("x", bit_rc)]
436+
.into_iter()
437+
.collect::<HashMap<_, _>>();
442438
let expr1 = var("b__3_0") - var("b_msb_f_0") + constant(256) * var("x");
443439
let items = try_split(expr1, &rcs).unwrap().iter().join("\n");
444440
assert_eq!(
@@ -459,13 +455,13 @@ x - 1 = 0"
459455
fn split_multiple_with_const() {
460456
let four_bit_rc = RangeConstraint::from_mask(0xfu32);
461457
let rcs = [
462-
("x", four_bit_rc.clone()),
463-
("y", four_bit_rc.clone()),
464-
("a", four_bit_rc.clone()),
465-
("b", four_bit_rc.clone()),
466-
("r", four_bit_rc.clone()),
467-
("s", four_bit_rc.clone()),
468-
("w", four_bit_rc.clone()),
458+
("x", four_bit_rc),
459+
("y", four_bit_rc),
460+
("a", four_bit_rc),
461+
("b", four_bit_rc),
462+
("r", four_bit_rc),
463+
("s", four_bit_rc),
464+
("w", four_bit_rc),
469465
]
470466
.into_iter()
471467
.collect::<HashMap<_, _>>();
@@ -491,10 +487,10 @@ w - 5 = 0"
491487
fn split_limb_decomposition() {
492488
let four_bit_rc = RangeConstraint::from_mask(0xfu32);
493489
let rcs = [
494-
("l0", four_bit_rc.clone()),
495-
("l1", four_bit_rc.clone()),
496-
("l2", four_bit_rc.clone()),
497-
("l3", four_bit_rc.clone()),
490+
("l0", four_bit_rc),
491+
("l1", four_bit_rc),
492+
("l2", four_bit_rc),
493+
("l3", four_bit_rc),
498494
]
499495
.into_iter()
500496
.collect::<HashMap<_, _>>();
@@ -520,7 +516,7 @@ l3 - 1 = 0"
520516
// a__0_12 + 256 * bool_113 - 216
521517
let byte_rc = RangeConstraint::from_mask(0xffu32);
522518
let bit_rc = RangeConstraint::from_mask(0x1u32);
523-
let rcs = [("a__0_12", byte_rc.clone()), ("bool_113", bit_rc.clone())]
519+
let rcs = [("a__0_12", byte_rc), ("bool_113", bit_rc)]
524520
.into_iter()
525521
.collect::<HashMap<_, _>>();
526522
let expr1: GroupedExpression<BabyBearField, _> =
@@ -545,9 +541,9 @@ l3 - 1 = 0"
545541
// -(c__1_3) + 256 * (30720 * c__0_3 - c__2_3) = 1226833928
546542
let byte_rc = RangeConstraint::from_mask(0xffu32);
547543
let rcs = [
548-
("c__0_3", byte_rc.clone()),
549-
("c__1_3", byte_rc.clone()),
550-
("c__2_3", byte_rc.clone()),
544+
("c__0_3", byte_rc),
545+
("c__1_3", byte_rc),
546+
("c__2_3", byte_rc),
551547
]
552548
.into_iter()
553549
.collect::<HashMap<_, _>>();
@@ -572,7 +568,7 @@ l3 - 1 = 0"
572568
fn wrapping_2() {
573569
// bool_17 + 1069547521 * (a__0_0) = 943718400
574570
let bit_rc = RangeConstraint::from_mask(0x1u32);
575-
let rcs = [("bool_17", bit_rc.clone()), ("a__0_0", bit_rc.clone())]
571+
let rcs = [("bool_17", bit_rc), ("a__0_0", bit_rc)]
576572
.into_iter()
577573
.collect::<HashMap<_, _>>();
578574
let expr: GroupedExpression<BabyBearField, _> =
@@ -589,9 +585,9 @@ l3 - 1 = 0"
589585
let bit_rc = RangeConstraint::from_mask(0x1u32);
590586
let limb_rc = RangeConstraint::from_mask(0x7fffu32);
591587
let rcs = [
592-
("bool_103", bit_rc.clone()),
593-
("to_pc_least_sig_bit_4", bit_rc.clone()),
594-
("to_pc_limbs__0_4", limb_rc.clone()),
588+
("bool_103", bit_rc),
589+
("to_pc_least_sig_bit_4", bit_rc),
590+
("to_pc_limbs__0_4", limb_rc),
595591
]
596592
.into_iter()
597593
.collect::<HashMap<_, _>>();
@@ -629,7 +625,7 @@ l3 - 1 = 0"
629625
#[test]
630626
fn split_fail_overlapping() {
631627
let four_bit_rc = RangeConstraint::from_mask(0xfu32);
632-
let rcs = [("x", four_bit_rc.clone()), ("y", four_bit_rc.clone())]
628+
let rcs = [("x", four_bit_rc), ("y", four_bit_rc)]
633629
.into_iter()
634630
.collect::<HashMap<_, _>>();
635631
// The RC of x is not tight enough
@@ -640,13 +636,9 @@ l3 - 1 = 0"
640636
#[test]
641637
fn split_fail_not_unique() {
642638
let four_bit_rc = RangeConstraint::from_mask(0xfu32);
643-
let rcs = [
644-
("x", four_bit_rc.clone()),
645-
("y", four_bit_rc.clone()),
646-
("z", four_bit_rc.clone()),
647-
]
648-
.into_iter()
649-
.collect::<HashMap<_, _>>();
639+
let rcs = [("x", four_bit_rc), ("y", four_bit_rc), ("z", four_bit_rc)]
640+
.into_iter()
641+
.collect::<HashMap<_, _>>();
650642
// There are multiple ways to solve the modulo equation.
651643
let expr = (var("x") - var("y")) + constant(16) * var("z") - constant(1);
652644
assert!(try_split(expr, &rcs).is_none());

constraint-solver/src/symbolic_expression.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,21 @@ impl<T: FieldElement, S1: Ord + Clone, S2: Ord + Clone> VarTransformable<S1, S2>
173173
Some(match self {
174174
SymbolicExpression::Concrete(n) => SymbolicExpression::Concrete(*n),
175175
SymbolicExpression::Symbol(v, rc) => {
176-
SymbolicExpression::from_symbol(var_transform(v)?, rc.clone())
176+
SymbolicExpression::from_symbol(var_transform(v)?, *rc)
177177
}
178178
SymbolicExpression::BinaryOperation(lhs, op, rhs, rc) => {
179179
SymbolicExpression::BinaryOperation(
180180
Arc::new(lhs.try_transform_var_type(var_transform)?),
181181
*op,
182182
Arc::new(rhs.try_transform_var_type(var_transform)?),
183-
rc.clone(),
183+
*rc,
184184
)
185185
}
186186
SymbolicExpression::UnaryOperation(op, inner, rc) => {
187187
SymbolicExpression::UnaryOperation(
188188
*op,
189189
Arc::new(inner.try_transform_var_type(var_transform)?),
190-
rc.clone(),
190+
*rc,
191191
)
192192
}
193193
})
@@ -449,7 +449,7 @@ impl<T: FieldElement, V: Clone + Eq> RuntimeConstant for SymbolicExpression<T, V
449449
SymbolicExpression::Concrete(v) => RangeConstraint::from_value(*v),
450450
SymbolicExpression::Symbol(.., rc)
451451
| SymbolicExpression::BinaryOperation(.., rc)
452-
| SymbolicExpression::UnaryOperation(.., rc) => rc.clone(),
452+
| SymbolicExpression::UnaryOperation(.., rc) => *rc,
453453
}
454454
}
455455

constraint-solver/tests/solver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ impl BusInteractionHandler<GoldilocksField> for TestBusInteractionHandler {
155155
^ b.to_integer().try_into_u64().unwrap(),
156156
);
157157
vec![
158-
bus_interaction.payload[0].clone(),
159-
bus_interaction.payload[1].clone(),
158+
bus_interaction.payload[0],
159+
bus_interaction.payload[1],
160160
RangeConstraint::from_value(result),
161161
]
162162
} else {

openvm/src/bus_interaction_handler/bitwise_lookup.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ pub fn bitwise_lookup_pure_range_constraints<T: FieldElement, V: Ord + Clone + E
8080
if op.try_to_number() == Some(T::from(0u64)) {
8181
Some(
8282
[
83-
(x.clone(), byte_rc.clone()),
84-
(y.clone(), byte_rc.clone()),
85-
(z.clone(), zero_rc.clone()),
83+
(x.clone(), byte_rc),
84+
(y.clone(), byte_rc),
85+
(z.clone(), zero_rc),
8686
]
8787
.into(),
8888
)
@@ -93,8 +93,8 @@ pub fn bitwise_lookup_pure_range_constraints<T: FieldElement, V: Ord + Clone + E
9393
// Note that this block also gets executed if `op` is unknown (but we know that `op` can only be 0 or 1).
9494
Some(
9595
[
96-
(x.clone(), byte_rc.clone()),
97-
(z.clone(), zero_rc.clone()),
96+
(x.clone(), byte_rc),
97+
(z.clone(), zero_rc),
9898
(op.clone(), RangeConstraint::from_mask(1)),
9999
]
100100
.into(),

openvm/src/bus_interaction_handler/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ pub fn handle_memory<T: FieldElement>(
3939
data.iter().map(|_| byte_constraint()).collect::<Vec<_>>()
4040
};
4141

42-
[address_space.clone(), pointer.clone()]
42+
[*address_space, *pointer]
4343
.into_iter()
4444
.chain(data)
45-
.chain(std::iter::once(timestamp.clone()))
45+
.chain(std::iter::once(*timestamp))
4646
.collect()
4747
}
4848
// Otherwise, we can't improve the constraints

openvm/src/bus_interaction_handler/variable_range_checker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn handle_variable_range_checker<T: FieldElement>(
2222
Some(bits_value) if bits_value.to_degree() <= MAX_BITS => {
2323
let bits_value = bits_value.to_integer().try_into_u64().unwrap();
2424
let mask = (1u64 << bits_value) - 1;
25-
vec![RangeConstraint::from_mask(mask), bits.clone()]
25+
vec![RangeConstraint::from_mask(mask), *bits]
2626
}
2727
_ => {
2828
vec![

0 commit comments

Comments
 (0)