@@ -1478,88 +1478,70 @@ impl<'block> BrilligBlock<'block> {
1478
1478
is_signed : bool ,
1479
1479
) {
1480
1480
let bit_size = left. bit_size ;
1481
- let max_lhs_bits = dfg. get_value_max_num_bits ( binary. lhs ) ;
1482
- let max_rhs_bits = dfg. get_value_max_num_bits ( binary. rhs ) ;
1483
1481
1484
- if bit_size == FieldElement :: max_num_bits ( ) {
1482
+ if bit_size == FieldElement :: max_num_bits ( ) || is_signed {
1485
1483
return ;
1486
1484
}
1487
1485
1488
- match ( binary_operation, is_signed) {
1489
- ( BrilligBinaryOp :: Add , false ) => {
1490
- if std:: cmp:: max ( max_lhs_bits, max_rhs_bits) < bit_size {
1491
- // `left` and `right` have both been casted up from smaller types and so cannot overflow.
1492
- return ;
1493
- }
1494
-
1495
- let condition =
1496
- SingleAddrVariable :: new ( self . brillig_context . allocate_register ( ) , 1 ) ;
1497
- // Check that lhs <= result
1498
- self . brillig_context . binary_instruction (
1499
- left,
1500
- result,
1501
- condition,
1502
- BrilligBinaryOp :: LessThanEquals ,
1503
- ) ;
1504
- self . brillig_context
1505
- . codegen_constrain ( condition, Some ( "attempt to add with overflow" . to_string ( ) ) ) ;
1506
- self . brillig_context . deallocate_single_addr ( condition) ;
1507
- }
1508
- ( BrilligBinaryOp :: Sub , false ) => {
1509
- if dfg. is_constant ( binary. lhs ) && max_lhs_bits > max_rhs_bits {
1510
- // `left` is a fixed constant and `right` is restricted such that `left - right > 0`
1511
- // Note strict inequality as `right > left` while `max_lhs_bits == max_rhs_bits` is possible.
1512
- return ;
1513
- }
1514
-
1515
- let condition =
1516
- SingleAddrVariable :: new ( self . brillig_context . allocate_register ( ) , 1 ) ;
1517
- // Check that rhs <= lhs
1518
- self . brillig_context . binary_instruction (
1519
- right,
1520
- left,
1521
- condition,
1522
- BrilligBinaryOp :: LessThanEquals ,
1523
- ) ;
1524
- self . brillig_context . codegen_constrain (
1525
- condition,
1526
- Some ( "attempt to subtract with overflow" . to_string ( ) ) ,
1527
- ) ;
1528
- self . brillig_context . deallocate_single_addr ( condition) ;
1529
- }
1530
- ( BrilligBinaryOp :: Mul , false ) => {
1531
- if bit_size == 1 || max_lhs_bits + max_rhs_bits <= bit_size {
1532
- // Either performing boolean multiplication (which cannot overflow),
1533
- // or `left` and `right` have both been casted up from smaller types and so cannot overflow.
1534
- return ;
1486
+ if let Some ( msg) = binary. check_unsigned_overflow_msg ( dfg, bit_size) {
1487
+ match binary_operation {
1488
+ BrilligBinaryOp :: Add => {
1489
+ let condition =
1490
+ SingleAddrVariable :: new ( self . brillig_context . allocate_register ( ) , 1 ) ;
1491
+ // Check that lhs <= result
1492
+ self . brillig_context . binary_instruction (
1493
+ left,
1494
+ result,
1495
+ condition,
1496
+ BrilligBinaryOp :: LessThanEquals ,
1497
+ ) ;
1498
+ self . brillig_context . codegen_constrain ( condition, Some ( msg. to_string ( ) ) ) ;
1499
+ self . brillig_context . deallocate_single_addr ( condition) ;
1535
1500
}
1536
-
1537
- let is_right_zero =
1538
- SingleAddrVariable :: new ( self . brillig_context . allocate_register ( ) , 1 ) ;
1539
- let zero = self . brillig_context . make_constant_instruction ( 0_usize . into ( ) , bit_size) ;
1540
- self . brillig_context . binary_instruction (
1541
- zero,
1542
- right,
1543
- is_right_zero,
1544
- BrilligBinaryOp :: Equals ,
1545
- ) ;
1546
- self . brillig_context . codegen_if_not ( is_right_zero. address , |ctx| {
1547
- let condition = SingleAddrVariable :: new ( ctx. allocate_register ( ) , 1 ) ;
1548
- let division = SingleAddrVariable :: new ( ctx. allocate_register ( ) , bit_size) ;
1549
- // Check that result / rhs == lhs
1550
- ctx. binary_instruction ( result, right, division, BrilligBinaryOp :: UnsignedDiv ) ;
1551
- ctx. binary_instruction ( division, left, condition, BrilligBinaryOp :: Equals ) ;
1552
- ctx. codegen_constrain (
1501
+ BrilligBinaryOp :: Sub => {
1502
+ let condition =
1503
+ SingleAddrVariable :: new ( self . brillig_context . allocate_register ( ) , 1 ) ;
1504
+ // Check that rhs <= lhs
1505
+ self . brillig_context . binary_instruction (
1506
+ right,
1507
+ left,
1553
1508
condition,
1554
- Some ( "attempt to multiply with overflow" . to_string ( ) ) ,
1509
+ BrilligBinaryOp :: LessThanEquals ,
1555
1510
) ;
1556
- ctx. deallocate_single_addr ( condition) ;
1557
- ctx. deallocate_single_addr ( division) ;
1558
- } ) ;
1559
- self . brillig_context . deallocate_single_addr ( is_right_zero) ;
1560
- self . brillig_context . deallocate_single_addr ( zero) ;
1511
+ self . brillig_context . codegen_constrain ( condition, Some ( msg. to_string ( ) ) ) ;
1512
+ self . brillig_context . deallocate_single_addr ( condition) ;
1513
+ }
1514
+ BrilligBinaryOp :: Mul => {
1515
+ let is_right_zero =
1516
+ SingleAddrVariable :: new ( self . brillig_context . allocate_register ( ) , 1 ) ;
1517
+ let zero =
1518
+ self . brillig_context . make_constant_instruction ( 0_usize . into ( ) , bit_size) ;
1519
+ self . brillig_context . binary_instruction (
1520
+ zero,
1521
+ right,
1522
+ is_right_zero,
1523
+ BrilligBinaryOp :: Equals ,
1524
+ ) ;
1525
+ self . brillig_context . codegen_if_not ( is_right_zero. address , |ctx| {
1526
+ let condition = SingleAddrVariable :: new ( ctx. allocate_register ( ) , 1 ) ;
1527
+ let division = SingleAddrVariable :: new ( ctx. allocate_register ( ) , bit_size) ;
1528
+ // Check that result / rhs == lhs
1529
+ ctx. binary_instruction (
1530
+ result,
1531
+ right,
1532
+ division,
1533
+ BrilligBinaryOp :: UnsignedDiv ,
1534
+ ) ;
1535
+ ctx. binary_instruction ( division, left, condition, BrilligBinaryOp :: Equals ) ;
1536
+ ctx. codegen_constrain ( condition, Some ( msg. to_string ( ) ) ) ;
1537
+ ctx. deallocate_single_addr ( condition) ;
1538
+ ctx. deallocate_single_addr ( division) ;
1539
+ } ) ;
1540
+ self . brillig_context . deallocate_single_addr ( is_right_zero) ;
1541
+ self . brillig_context . deallocate_single_addr ( zero) ;
1542
+ }
1543
+ _ => { }
1561
1544
}
1562
- _ => { }
1563
1545
}
1564
1546
}
1565
1547
0 commit comments