@@ -3,8 +3,7 @@ use acir::{
33 opcodes:: { BlackBoxFuncCall , FunctionInput } ,
44 Circuit , Opcode ,
55 } ,
6- native_types:: { Expression , Witness } ,
7- FieldElement ,
6+ native_types:: Witness ,
87} ;
98use std:: collections:: { BTreeMap , HashSet } ;
109
@@ -105,9 +104,11 @@ impl RangeOptimizer {
105104 let mut new_order_list = Vec :: with_capacity ( order_list. len ( ) ) ;
106105 let mut optimized_opcodes = Vec :: with_capacity ( self . circuit . opcodes . len ( ) ) ;
107106 for ( idx, opcode) in self . circuit . opcodes . into_iter ( ) . enumerate ( ) {
108- let ( witness, num_bits) = match extract_range_opcode ( & opcode) {
109- Some ( range_opcode) => range_opcode,
110- None => {
107+ let ( witness, num_bits) = match & opcode {
108+ Opcode :: BlackBoxFuncCall ( BlackBoxFuncCall :: RANGE { input } ) => {
109+ ( input. witness , input. num_bits )
110+ }
111+ _ => {
111112 // If its not the range opcode, add it to the opcode
112113 // list and continue;
113114 optimized_opcodes. push ( opcode) ;
@@ -131,44 +132,19 @@ impl RangeOptimizer {
131132 if is_lowest_bit_size {
132133 already_seen_witness. insert ( witness) ;
133134 new_order_list. push ( order_list[ idx] ) ;
134- optimized_opcodes. push ( optimized_range_opcode ( witness , num_bits ) ) ;
135+ optimized_opcodes. push ( opcode ) ;
135136 }
136137 }
137138
138139 ( Circuit { opcodes : optimized_opcodes, ..self . circuit } , new_order_list)
139140 }
140141}
141142
142- /// Extract the range opcode from the `Opcode` enum
143- /// Returns None, if `Opcode` is not the range opcode.
144- fn extract_range_opcode ( opcode : & Opcode ) -> Option < ( Witness , u32 ) > {
145- match opcode {
146- Opcode :: BlackBoxFuncCall ( BlackBoxFuncCall :: RANGE { input } ) => {
147- Some ( ( input. witness , input. num_bits ) )
148- }
149- _ => None ,
150- }
151- }
152-
153- fn optimized_range_opcode ( witness : Witness , num_bits : u32 ) -> Opcode {
154- if num_bits == 1 {
155- Opcode :: AssertZero ( Expression {
156- mul_terms : vec ! [ ( FieldElement :: one( ) , witness, witness) ] ,
157- linear_combinations : vec ! [ ( -FieldElement :: one( ) , witness) ] ,
158- q_c : FieldElement :: zero ( ) ,
159- } )
160- } else {
161- Opcode :: BlackBoxFuncCall ( BlackBoxFuncCall :: RANGE {
162- input : FunctionInput { witness, num_bits } ,
163- } )
164- }
165- }
166-
167143#[ cfg( test) ]
168144mod tests {
169145 use std:: collections:: BTreeSet ;
170146
171- use crate :: compiler:: optimizers:: redundant_range:: { extract_range_opcode , RangeOptimizer } ;
147+ use crate :: compiler:: optimizers:: redundant_range:: RangeOptimizer ;
172148 use acir:: {
173149 circuit:: {
174150 opcodes:: { BlackBoxFuncCall , FunctionInput } ,
@@ -218,11 +194,12 @@ mod tests {
218194 let ( optimized_circuit, _) = optimizer. replace_redundant_ranges ( acir_opcode_positions) ;
219195 assert_eq ! ( optimized_circuit. opcodes. len( ) , 1 ) ;
220196
221- let ( witness, num_bits) =
222- extract_range_opcode ( & optimized_circuit. opcodes [ 0 ] ) . expect ( "expected one range opcode" ) ;
223-
224- assert_eq ! ( witness, Witness ( 1 ) ) ;
225- assert_eq ! ( num_bits, 16 ) ;
197+ assert_eq ! (
198+ optimized_circuit. opcodes[ 0 ] ,
199+ Opcode :: BlackBoxFuncCall ( BlackBoxFuncCall :: RANGE {
200+ input: FunctionInput { witness: Witness ( 1 ) , num_bits: 16 }
201+ } )
202+ ) ;
226203 }
227204
228205 #[ test]
@@ -240,15 +217,18 @@ mod tests {
240217 let ( optimized_circuit, _) = optimizer. replace_redundant_ranges ( acir_opcode_positions) ;
241218 assert_eq ! ( optimized_circuit. opcodes. len( ) , 2 ) ;
242219
243- let ( witness_a, num_bits_a) =
244- extract_range_opcode ( & optimized_circuit. opcodes [ 0 ] ) . expect ( "expected two range opcode" ) ;
245- let ( witness_b, num_bits_b) =
246- extract_range_opcode ( & optimized_circuit. opcodes [ 1 ] ) . expect ( "expected two range opcode" ) ;
247-
248- assert_eq ! ( witness_a, Witness ( 1 ) ) ;
249- assert_eq ! ( witness_b, Witness ( 2 ) ) ;
250- assert_eq ! ( num_bits_a, 16 ) ;
251- assert_eq ! ( num_bits_b, 23 ) ;
220+ assert_eq ! (
221+ optimized_circuit. opcodes[ 0 ] ,
222+ Opcode :: BlackBoxFuncCall ( BlackBoxFuncCall :: RANGE {
223+ input: FunctionInput { witness: Witness ( 1 ) , num_bits: 16 }
224+ } )
225+ ) ;
226+ assert_eq ! (
227+ optimized_circuit. opcodes[ 1 ] ,
228+ Opcode :: BlackBoxFuncCall ( BlackBoxFuncCall :: RANGE {
229+ input: FunctionInput { witness: Witness ( 2 ) , num_bits: 23 }
230+ } )
231+ ) ;
252232 }
253233
254234 #[ test]
0 commit comments