From de9af5b9e66a989442ac756109193afb30a5dc95 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Tue, 10 Aug 2021 23:40:57 +0300 Subject: [PATCH] fix: negate negative witness elements --- src/circom/builder.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/circom/builder.rs b/src/circom/builder.rs index 995650a..d339a34 100644 --- a/src/circom/builder.rs +++ b/src/circom/builder.rs @@ -75,11 +75,22 @@ impl CircomBuilder { .wtns .calculate_witness(self.inputs, self.cfg.sanity_check)?; + use ark_ff::{PrimeField, FpParameters}; + let modulus = <::Params as FpParameters>::MODULUS; + // convert it to field elements + use num_traits::Signed; let witness = witness .into_iter() - .map(|w| E::Fr::from(w.to_biguint().unwrap())) - .collect::>(); + .map(|w| { + let w = if w.sign() == num_bigint::Sign::Minus { + // Need to negate the witness element if negative + modulus.into() - w.abs().to_biguint().unwrap() + } else { + w.to_biguint().unwrap() + }; + E::Fr::from(w) + }).collect::>(); circom.witness = Some(witness); // sanity check