From d550e156d9333e3bf9b3a7582fbca25ccd067edf Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Sat, 3 Jul 2021 17:06:55 +0800 Subject: [PATCH] mul_fixed_*::tests: Constrain zero outputs in mul_fixed tests. Co-authored-by: Jack Grigg --- src/circuit/gadget/ecc.rs | 3 +++ .../ecc/chip/mul_fixed/base_field_elem.rs | 19 ++++++++++++++++--- .../gadget/ecc/chip/mul_fixed/full_width.rs | 18 +++++++++++++++--- .../gadget/ecc/chip/mul_fixed/short.rs | 6 ++++-- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/circuit/gadget/ecc.rs b/src/circuit/gadget/ecc.rs index 2414412c..5bc4f17b 100644 --- a/src/circuit/gadget/ecc.rs +++ b/src/circuit/gadget/ecc.rs @@ -544,6 +544,7 @@ mod tests { super::chip::mul_fixed::full_width::tests::test_mul_fixed( chip.clone(), layouter.namespace(|| "full-width fixed-base scalar mul"), + &zero, )?; } @@ -552,6 +553,7 @@ mod tests { super::chip::mul_fixed::short::tests::test_mul_fixed_short( chip.clone(), layouter.namespace(|| "signed short fixed-base scalar mul"), + &zero, )?; } @@ -560,6 +562,7 @@ mod tests { super::chip::mul_fixed::base_field_elem::tests::test_mul_fixed_base_field( chip, layouter.namespace(|| "fixed-base scalar mul with base field element"), + &zero, )?; } diff --git a/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs b/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs index 8260425d..de643a05 100644 --- a/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs +++ b/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs @@ -248,7 +248,7 @@ pub mod tests { use crate::circuit::gadget::{ ecc::{ chip::{EccChip, OrchardFixedBasesFull}, - FixedPoint, + FixedPoint, Point, }, utilities::{CellValue, UtilitiesInstructions}, }; @@ -257,6 +257,7 @@ pub mod tests { pub fn test_mul_fixed_base_field( chip: EccChip, mut layouter: impl Layouter, + zero: &Point, ) -> Result<(), Error> { impl UtilitiesInstructions for EccChip { type Var = CellValue; @@ -269,6 +270,7 @@ pub mod tests { chip.clone(), layouter.namespace(|| "commit_ivk_r"), commit_ivk_r, + &zero, )?; // note_commit_r @@ -278,6 +280,7 @@ pub mod tests { chip.clone(), layouter.namespace(|| "note_commit_r"), note_commit_r, + &zero, )?; // nullifier_k @@ -287,6 +290,7 @@ pub mod tests { chip.clone(), layouter.namespace(|| "nullifier_k"), nullifier_k, + &zero, )?; // value_commit_r @@ -296,12 +300,18 @@ pub mod tests { chip.clone(), layouter.namespace(|| "value_commit_r"), value_commit_r, + &zero, )?; // spend_auth_g let spend_auth_g = OrchardFixedBasesFull::SpendAuthG; let spend_auth_g = FixedPoint::from_inner(chip.clone(), spend_auth_g); - test_single_base(chip, layouter.namespace(|| "spend_auth_g"), spend_auth_g)?; + test_single_base( + chip, + layouter.namespace(|| "spend_auth_g"), + spend_auth_g, + &zero, + )?; Ok(()) } @@ -311,6 +321,7 @@ pub mod tests { chip: EccChip, mut layouter: impl Layouter, base: FixedPoint, + zero: &Point, ) -> Result<(), Error> { let column = chip.config().advices[0]; @@ -356,7 +367,9 @@ pub mod tests { column, Some(scalar_fixed), )?; - base.mul_base_field_elem(layouter.namespace(|| "mul by zero"), scalar_fixed)?; + let result = + base.mul_base_field_elem(layouter.namespace(|| "mul by zero"), scalar_fixed)?; + result.constrain_equal(layouter.namespace(|| "[0]B = 𝒪"), &zero)?; } // [-1]B is the largest base field element diff --git a/src/circuit/gadget/ecc/chip/mul_fixed/full_width.rs b/src/circuit/gadget/ecc/chip/mul_fixed/full_width.rs index 1b9b4110..87bdb493 100644 --- a/src/circuit/gadget/ecc/chip/mul_fixed/full_width.rs +++ b/src/circuit/gadget/ecc/chip/mul_fixed/full_width.rs @@ -62,13 +62,14 @@ pub mod tests { use crate::circuit::gadget::ecc::{ chip::{EccChip, OrchardFixedBasesFull}, - FixedPoint, ScalarFixed, + FixedPoint, Point, ScalarFixed, }; use crate::constants; pub fn test_mul_fixed( chip: EccChip, mut layouter: impl Layouter, + zero: &Point, ) -> Result<(), Error> { // commit_ivk_r let commit_ivk_r = OrchardFixedBasesFull::CommitIvkR; @@ -77,6 +78,7 @@ pub mod tests { chip.clone(), layouter.namespace(|| "commit_ivk_r"), commit_ivk_r, + &zero, )?; // note_commit_r @@ -86,6 +88,7 @@ pub mod tests { chip.clone(), layouter.namespace(|| "note_commit_r"), note_commit_r, + &zero, )?; // nullifier_k @@ -95,6 +98,7 @@ pub mod tests { chip.clone(), layouter.namespace(|| "nullifier_k"), nullifier_k, + &zero, )?; // value_commit_r @@ -104,12 +108,18 @@ pub mod tests { chip.clone(), layouter.namespace(|| "value_commit_r"), value_commit_r, + &zero, )?; // spend_auth_g let spend_auth_g = OrchardFixedBasesFull::SpendAuthG; let spend_auth_g = FixedPoint::from_inner(chip.clone(), spend_auth_g); - test_single_base(chip, layouter.namespace(|| "spend_auth_g"), spend_auth_g)?; + test_single_base( + chip, + layouter.namespace(|| "spend_auth_g"), + spend_auth_g, + &zero, + )?; Ok(()) } @@ -119,6 +129,7 @@ pub mod tests { chip: EccChip, mut layouter: impl Layouter, base: FixedPoint, + zero: &Point, ) -> Result<(), Error> where pallas::Scalar: PrimeFieldBits, @@ -166,7 +177,8 @@ pub mod tests { layouter.namespace(|| "ScalarFixed"), Some(scalar_fixed), )?; - base.mul(layouter.namespace(|| "mul by zero"), &scalar_fixed)?; + let result = base.mul(layouter.namespace(|| "mul by zero"), &scalar_fixed)?; + result.constrain_equal(layouter.namespace(|| "[0]B = 𝒪"), &zero)?; } // [-1]B is the largest scalar field element. diff --git a/src/circuit/gadget/ecc/chip/mul_fixed/short.rs b/src/circuit/gadget/ecc/chip/mul_fixed/short.rs index cc20df5a..bb244851 100644 --- a/src/circuit/gadget/ecc/chip/mul_fixed/short.rs +++ b/src/circuit/gadget/ecc/chip/mul_fixed/short.rs @@ -157,13 +157,14 @@ pub mod tests { use halo2::{circuit::Layouter, plonk::Error}; use pasta_curves::{arithmetic::FieldExt, pallas}; - use crate::circuit::gadget::ecc::{chip::EccChip, FixedPointShort, ScalarFixedShort}; + use crate::circuit::gadget::ecc::{chip::EccChip, FixedPointShort, Point, ScalarFixedShort}; use crate::constants::load::ValueCommitV; #[allow(clippy::op_ref)] pub fn test_mul_fixed_short( chip: EccChip, mut layouter: impl Layouter, + zero: &Point, ) -> Result<(), Error> where pallas::Scalar: PrimeFieldBits, @@ -181,7 +182,8 @@ pub mod tests { layouter.namespace(|| "ScalarFixedShort"), Some(scalar_fixed), )?; - value_commit_v.mul(layouter.namespace(|| "mul"), &scalar_fixed)?; + let result = value_commit_v.mul(layouter.namespace(|| "mul by zero"), &scalar_fixed)?; + result.constrain_equal(layouter.namespace(|| "[0]B = 𝒪"), &zero)?; } // Random [a]B