Przeglądaj źródła

zk::gadget::native_range_check: Always strict

The range check gadget should always be strict; non-strict
decomposition should be done in a separate decomposition
gadget.
therealyingtong 2 lat temu
rodzic
commit
10eb8eb6a3
3 zmienionych plików z 11 dodań i 26 usunięć
  1. 5 9
      src/zk/gadget/less_than.rs
  2. 6 15
      src/zk/gadget/native_range_check.rs
  3. 0 2
      src/zk/vm.rs

+ 5 - 9
src/zk/gadget/less_than.rs

@@ -146,7 +146,7 @@ impl<const WINDOW_SIZE: usize, const NUM_OF_BITS: usize, const NUM_OF_WINDOWS: u
             },
         )?;
 
-        self.less_than_range_check(layouter, a, a_offset, strict)?;
+        self.less_than_range_check(layouter, a, a_offset)?;
 
         Ok(())
     }
@@ -194,7 +194,7 @@ impl<const WINDOW_SIZE: usize, const NUM_OF_BITS: usize, const NUM_OF_WINDOWS: u
             },
         )?;
 
-        self.less_than_range_check(layouter, a, a_offset, strict)?;
+        self.less_than_range_check(layouter, a, a_offset)?;
 
         Ok(())
     }
@@ -204,7 +204,6 @@ impl<const WINDOW_SIZE: usize, const NUM_OF_BITS: usize, const NUM_OF_WINDOWS: u
         mut layouter: impl Layouter<pallas::Base>,
         a: AssignedCell<pallas::Base, pallas::Base>,
         a_offset: AssignedCell<pallas::Base, pallas::Base>,
-        strict: bool,
     ) -> Result<(), Error> {
         let range_a_chip =
             NativeRangeCheckChip::<WINDOW_SIZE, NUM_OF_BITS, NUM_OF_WINDOWS>::construct(
@@ -215,13 +214,10 @@ impl<const WINDOW_SIZE: usize, const NUM_OF_BITS: usize, const NUM_OF_WINDOWS: u
                 self.config.range_a_offset_config.clone(),
             );
 
-        range_a_chip.copy_range_check(layouter.namespace(|| "a copy_range_check"), a, strict)?;
+        range_a_chip.copy_range_check(layouter.namespace(|| "a copy_range_check"), a)?;
 
-        range_a_offset_chip.copy_range_check(
-            layouter.namespace(|| "a_offset copy_range_check"),
-            a_offset,
-            strict,
-        )?;
+        range_a_offset_chip
+            .copy_range_check(layouter.namespace(|| "a_offset copy_range_check"), a_offset)?;
 
         Ok(())
     }

+ 6 - 15
src/zk/gadget/native_range_check.rs

@@ -139,7 +139,6 @@ impl<const WINDOW_SIZE: usize, const NUM_BITS: usize, const NUM_WINDOWS: usize>
         region: &mut Region<'_, pallas::Base>,
         z_0: AssignedCell<pallas::Base, pallas::Base>,
         offset: usize,
-        strict: bool,
     ) -> Result<(), plonk::Error> {
         assert!(WINDOW_SIZE * NUM_WINDOWS < NUM_BITS + WINDOW_SIZE);
 
@@ -176,10 +175,8 @@ impl<const WINDOW_SIZE: usize, const NUM_BITS: usize, const NUM_WINDOWS: usize>
 
         assert!(z_values.len() == NUM_WINDOWS + 1);
 
-        if strict {
-            // Constrain the remaining bits to be zero
-            region.constrain_constant(z_values.last().unwrap().cell(), pallas::Base::zero())?;
-        }
+        // Constrain the remaining bits to be zero
+        region.constrain_constant(z_values.last().unwrap().cell(), pallas::Base::zero())?;
 
         Ok(())
     }
@@ -188,13 +185,12 @@ impl<const WINDOW_SIZE: usize, const NUM_BITS: usize, const NUM_WINDOWS: usize>
         &self,
         mut layouter: impl Layouter<pallas::Base>,
         value: Value<pallas::Base>,
-        strict: bool,
     ) -> Result<(), plonk::Error> {
         layouter.assign_region(
             || format!("witness {}-bit native range check", NUM_BITS),
             |mut region: Region<'_, pallas::Base>| {
                 let z_0 = region.assign_advice(|| "z_0", self.config.z, 0, || value)?;
-                self.decompose(&mut region, z_0, 0, strict)?;
+                self.decompose(&mut region, z_0, 0)?;
                 Ok(())
             },
         )
@@ -204,13 +200,12 @@ impl<const WINDOW_SIZE: usize, const NUM_BITS: usize, const NUM_WINDOWS: usize>
         &self,
         mut layouter: impl Layouter<pallas::Base>,
         value: AssignedCell<pallas::Base, pallas::Base>,
-        strict: bool,
     ) -> Result<(), plonk::Error> {
         layouter.assign_region(
             || format!("copy {}-bit native range check", NUM_BITS),
             |mut region: Region<'_, pallas::Base>| {
                 let z_0 = value.copy_advice(|| "z_0", &mut region, self.config.z, 0)?;
-                self.decompose(&mut region, z_0, 0, strict)?;
+                self.decompose(&mut region, z_0, 0)?;
                 Ok(())
             },
         )
@@ -278,16 +273,12 @@ mod tests {
                     )?;
 
                     let a = assign_free_advice(layouter.namespace(|| "load a"), config.1, self.a)?;
-                    rangecheck_chip.copy_range_check(
-                        layouter.namespace(|| "copy a and range check"),
-                        a,
-                        true,
-                    )?;
+                    rangecheck_chip
+                        .copy_range_check(layouter.namespace(|| "copy a and range check"), a)?;
 
                     rangecheck_chip.witness_range_check(
                         layouter.namespace(|| "witness a and range check"),
                         self.a,
-                        true,
                     )?;
 
                     Ok(())

+ 0 - 2
src/zk/vm.rs

@@ -1045,14 +1045,12 @@ impl Circuit<pallas::Base> for ZkCircuit {
                             rangecheck64_chip.as_ref().unwrap().copy_range_check(
                                 layouter.namespace(|| "copy range check 64"),
                                 arg.try_into()?,
-                                true,
                             )?;
                         }
                         253 => {
                             rangecheck253_chip.as_ref().unwrap().copy_range_check(
                                 layouter.namespace(|| "copy range check 253"),
                                 arg.try_into()?,
-                                true,
                             )?;
                         }
                         x => {