From 349407faef7586e6a73a9b69ad73af4c4dbd3029 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Mon, 8 Jun 2020 17:18:38 -0500 Subject: [PATCH] Speed up exp_by_const --- r1cs-std/src/fields/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/r1cs-std/src/fields/mod.rs b/r1cs-std/src/fields/mod.rs index 60e22de..51e254f 100644 --- a/r1cs-std/src/fields/mod.rs +++ b/r1cs-std/src/fields/mod.rs @@ -242,17 +242,19 @@ pub trait FieldGadget: mut cs: CS, exp: S, ) -> Result { - let mut res = Self::one(cs.ns(|| "Alloc result"))?; + let mut res = self.clone(); let mut found_one = false; for (i, bit) in BitIterator::new(exp).enumerate() { if found_one { - res.square_in_place(cs.ns(|| format!("square for bit {:?}", i)))?; + res = res.square(cs.ns(|| format!("square for bit {:?}", i)))?; } if bit { + if found_one { + res = res.mul(cs.ns(|| format!("mul for bit {:?}", i)), self)?; + } found_one = true; - res.mul_in_place(cs.ns(|| format!("mul for bit {:?}", i)), self)?; } }