From 3971ecd375b8854da71d764c2d2e166a5efca747 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Mon, 5 Mar 2018 10:52:56 -0700 Subject: [PATCH] Abstract away the boolean conversion of field witnessing. --- src/circuit/boolean.rs | 10 +++++++++ src/circuit/mod.rs | 49 ++++++++++++------------------------------ 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/circuit/boolean.rs b/src/circuit/boolean.rs index 6da5398..239d404 100644 --- a/src/circuit/boolean.rs +++ b/src/circuit/boolean.rs @@ -301,6 +301,16 @@ pub fn u64_into_boolean_vec_le>( Ok(bits) } +pub fn field_into_boolean_vec_le, F: PrimeField>( + cs: CS, + value: Option +) -> Result, SynthesisError> +{ + let v = field_into_allocated_bits_le::(cs, value)?; + + Ok(v.into_iter().map(|e| Boolean::from(e)).collect()) +} + pub fn field_into_allocated_bits_le, F: PrimeField>( mut cs: CS, value: Option diff --git a/src/circuit/mod.rs b/src/circuit/mod.rs index 9f8e1d2..8058d02 100644 --- a/src/circuit/mod.rs +++ b/src/circuit/mod.rs @@ -81,13 +81,10 @@ impl<'a, E: JubjubEngine> Circuit for Spend<'a, E> { )?; // Booleanize the randomness - let hr = boolean::field_into_allocated_bits_le( + let hr = boolean::field_into_boolean_vec_le( cs.namespace(|| "hr"), self.value_randomness - )? - .into_iter() - .map(|e| boolean::Boolean::from(e)) - .collect::>(); + )?; let hr = ecc::fixed_base_multiplication( cs.namespace(|| "computation of randomization for value commitment"), @@ -109,13 +106,10 @@ impl<'a, E: JubjubEngine> Circuit for Spend<'a, E> { let rk; { // Witness rsk as bits - let rsk = boolean::field_into_allocated_bits_le( + let rsk = boolean::field_into_boolean_vec_le( cs.namespace(|| "rsk"), self.rsk - )? - .into_iter() - .map(|e| boolean::Boolean::from(e)) - .collect::>(); + )?; // NB: We don't ensure that the bit representation of rsk // is "in the field" (Fs) because it's not used except to @@ -205,13 +199,10 @@ impl<'a, E: JubjubEngine> Circuit for Spend<'a, E> { { // Booleanize the randomness - let cmr = boolean::field_into_allocated_bits_le( + let cmr = boolean::field_into_boolean_vec_le( cs.namespace(|| "cmr"), self.commitment_randomness - )? - .into_iter() - .map(|e| boolean::Boolean::from(e)) - .collect::>(); + )?; let cmr = ecc::fixed_base_multiplication( cs.namespace(|| "computation of commitment randomness"), @@ -356,13 +347,10 @@ impl<'a, E: JubjubEngine> Circuit for Output<'a, E> { )?; // Booleanize the randomness - let hr = boolean::field_into_allocated_bits_le( + let hr = boolean::field_into_boolean_vec_le( cs.namespace(|| "hr"), self.value_randomness - )? - .into_iter() - .map(|e| boolean::Boolean::from(e)) - .collect::>(); + )?; let hr = ecc::fixed_base_multiplication( cs.namespace(|| "computation of randomization for value commitment"), @@ -419,13 +407,10 @@ impl<'a, E: JubjubEngine> Circuit for Output<'a, E> { ); // Compute epk from esk - let esk = boolean::field_into_allocated_bits_le( + let esk = boolean::field_into_boolean_vec_le( cs.namespace(|| "esk"), self.esk - )? - .into_iter() - .map(|e| boolean::Boolean::from(e)) - .collect::>(); + )?; let epk = g_d.mul( cs.namespace(|| "epk computation"), @@ -442,13 +427,10 @@ impl<'a, E: JubjubEngine> Circuit for Output<'a, E> { { let p_d = self.p_d.map(|e| e.into_xy()); - let y_contents = boolean::field_into_allocated_bits_le( + let y_contents = boolean::field_into_boolean_vec_le( cs.namespace(|| "p_d bits of y"), p_d.map(|e| e.1) - )? - .into_iter() - .map(|e| boolean::Boolean::from(e)) - .collect::>(); + )?; let sign_bit = boolean::Boolean::from(boolean::AllocatedBit::alloc( cs.namespace(|| "p_d bit of x"), @@ -476,13 +458,10 @@ impl<'a, E: JubjubEngine> Circuit for Output<'a, E> { { // Booleanize the randomness - let cmr = boolean::field_into_allocated_bits_le( + let cmr = boolean::field_into_boolean_vec_le( cs.namespace(|| "cmr"), self.commitment_randomness - )? - .into_iter() - .map(|e| boolean::Boolean::from(e)) - .collect::>(); + )?; let cmr = ecc::fixed_base_multiplication( cs.namespace(|| "computation of commitment randomness"),