From 92decdf2599dd670668045a5eb666c746e5bc279 Mon Sep 17 00:00:00 2001 From: guipublic Date: Wed, 15 Mar 2023 10:26:38 +0000 Subject: [PATCH 1/2] Remove truncate and oddrange directives --- acir/src/circuit/directives.rs | 66 +++------------------------------- acir/src/circuit/opcodes.rs | 26 -------------- acvm/src/pwg/directives.rs | 48 +------------------------ 3 files changed, 6 insertions(+), 134 deletions(-) diff --git a/acir/src/circuit/directives.rs b/acir/src/circuit/directives.rs index f56835dfb..081ee9397 100644 --- a/acir/src/circuit/directives.rs +++ b/acir/src/circuit/directives.rs @@ -26,22 +26,6 @@ pub enum Directive { predicate: Option, }, - //Reduces the value of a modulo 2^bit_size and stores the result in b: a= c*2^bit_size + b - Truncate { - a: Expression, - b: Witness, - c: Witness, - bit_size: u32, - }, - - //Computes the highest bit b of a: a = b*2^(bit_size-1) + r, where a<2^bit_size, b is 0 or 1 and r<2^(bit_size-1) - OddRange { - a: Witness, - b: Witness, - r: Witness, - bit_size: u32, - }, - //decomposition of a: a=\sum b[i]*radix^i where b is an array of witnesses < radix in little endian form ToLeRadix { a: Expression, @@ -65,8 +49,6 @@ impl Directive { match self { Directive::Invert { .. } => "invert", Directive::Quotient { .. } => "quotient", - Directive::Truncate { .. } => "truncate", - Directive::OddRange { .. } => "odd_range", Directive::ToLeRadix { .. } => "to_le_radix", Directive::PermutationSort { .. } => "permutation_sort", Directive::Log { .. } => "log", @@ -76,11 +58,9 @@ impl Directive { match self { Directive::Invert { .. } => 0, Directive::Quotient { .. } => 1, - Directive::Truncate { .. } => 2, - Directive::OddRange { .. } => 3, - Directive::ToLeRadix { .. } => 4, - Directive::Log { .. } => 5, - Directive::PermutationSort { .. } => 6, + Directive::ToLeRadix { .. } => 2, + Directive::Log { .. } => 3, + Directive::PermutationSort { .. } => 4, } } @@ -104,18 +84,6 @@ impl Directive { pred.write(&mut writer)?; } } - Directive::Truncate { a, b, c, bit_size } => { - a.write(&mut writer)?; - write_u32(&mut writer, b.witness_index())?; - write_u32(&mut writer, c.witness_index())?; - write_u32(&mut writer, *bit_size)?; - } - Directive::OddRange { a, b, r, bit_size } => { - write_u32(&mut writer, a.witness_index())?; - write_u32(&mut writer, b.witness_index())?; - write_u32(&mut writer, r.witness_index())?; - write_u32(&mut writer, *bit_size)?; - } Directive::ToLeRadix { a, b, radix } => { a.write(&mut writer)?; write_u32(&mut writer, b.len() as u32)?; @@ -182,20 +150,6 @@ impl Directive { Ok(Directive::Quotient { a, b, q, r, predicate }) } 2 => { - let a = Expression::read(&mut reader)?; - let b = Witness(read_u32(&mut reader)?); - let c = Witness(read_u32(&mut reader)?); - let bit_size = read_u32(&mut reader)?; - Ok(Directive::Truncate { a, b, c, bit_size }) - } - 3 => { - let a = Witness(read_u32(&mut reader)?); - let b = Witness(read_u32(&mut reader)?); - let r = Witness(read_u32(&mut reader)?); - let bit_size = read_u32(&mut reader)?; - Ok(Directive::OddRange { a, b, r, bit_size }) - } - 4 => { let a = Expression::read(&mut reader)?; let b_len = read_u32(&mut reader)?; let mut b = Vec::with_capacity(b_len as usize); @@ -208,7 +162,7 @@ impl Directive { Ok(Directive::ToLeRadix { a, b, radix }) } - 6 => { + 3 => { let tuple = read_u32(&mut reader)?; let a_len = read_u32(&mut reader)?; let mut a = Vec::with_capacity(a_len as usize); @@ -275,16 +229,6 @@ fn serialization_roundtrip() { predicate: Some(Expression::default()), }; - let truncate = Directive::Truncate { - a: Expression::default(), - b: Witness(2u32), - c: Witness(3u32), - bit_size: 123, - }; - - let odd_range = - Directive::OddRange { a: Witness(1u32), b: Witness(2u32), r: Witness(3u32), bit_size: 32 }; - let to_le_radix = Directive::ToLeRadix { a: Expression::default(), b: vec![Witness(1u32), Witness(2u32), Witness(3u32), Witness(4u32)], @@ -292,7 +236,7 @@ fn serialization_roundtrip() { }; let directives = - vec![invert, quotient_none, quotient_predicate, truncate, odd_range, to_le_radix]; + vec![invert, quotient_none, quotient_predicate, to_le_radix]; for directive in directives { let (dir, got_dir) = read_write(directive); diff --git a/acir/src/circuit/opcodes.rs b/acir/src/circuit/opcodes.rs index 7767c5e30..78f7166a8 100644 --- a/acir/src/circuit/opcodes.rs +++ b/acir/src/circuit/opcodes.rs @@ -136,20 +136,6 @@ impl std::fmt::Display for Opcode { write!(f, "DIR::INVERT ")?; write!(f, "(_{}, out: _{}) ", x.witness_index(), r.witness_index()) } - Opcode::Directive(Directive::Truncate { a, b, c, bit_size }) => { - write!(f, "DIR::TRUNCATE ")?; - write!( - f, - "(out: _{}, _{}, _{}, bit_size: {})", - // TODO: Modify Noir to switch a and b - b.witness_index(), - a, - // TODO: check why c was unused before, and check when directive is being processed - // TODO: and if it is used - c.witness_index(), - bit_size - ) - } Opcode::Directive(Directive::Quotient { a, b, q, r, predicate }) => { write!(f, "DIR::QUOTIENT ")?; if let Some(pred) = predicate { @@ -165,18 +151,6 @@ impl std::fmt::Display for Opcode { r.witness_index() ) } - Opcode::Directive(Directive::OddRange { a, b, r, bit_size }) => { - write!(f, "DIR::ODDRANGE ")?; - - write!( - f, - "(out: _{}, (_{}, bit_size: {}), _{})", - a.witness_index(), - b.witness_index(), - bit_size, - r.witness_index() - ) - } Opcode::BlackBoxFuncCall(g) => write!(f, "{g}"), Opcode::Directive(Directive::ToLeRadix { a, b, radix: _ }) => { write!(f, "DIR::TORADIX ")?; diff --git a/acvm/src/pwg/directives.rs b/acvm/src/pwg/directives.rs index 0d5192ced..ee1a038c6 100644 --- a/acvm/src/pwg/directives.rs +++ b/acvm/src/pwg/directives.rs @@ -6,7 +6,7 @@ use acir::{ FieldElement, }; use num_bigint::BigUint; -use num_traits::{One, Zero}; +use num_traits::{Zero}; use crate::OpcodeResolutionError; @@ -55,27 +55,6 @@ pub fn solve_directives( Ok(()) } - Directive::Truncate { a, b, c, bit_size } => { - let val_a = get_value(a, initial_witness)?; - let pow: BigUint = BigUint::one() << bit_size; - - let int_a = BigUint::from_bytes_be(&val_a.to_be_bytes()); - let int_b: BigUint = &int_a % &pow; - let int_c: BigUint = (&int_a - &int_b) / &pow; - - insert_witness( - *b, - FieldElement::from_be_bytes_reduce(&int_b.to_bytes_be()), - initial_witness, - )?; - insert_witness( - *c, - FieldElement::from_be_bytes_reduce(&int_c.to_bytes_be()), - initial_witness, - )?; - - Ok(()) - } Directive::ToLeRadix { a, b, radix } => { let value_a = get_value(a, initial_witness)?; let big_integer = BigUint::from_bytes_be(&value_a.to_be_bytes()); @@ -102,31 +81,6 @@ pub fn solve_directives( Ok(()) } - Directive::OddRange { a, b, r, bit_size } => { - let val_a = witness_to_value(initial_witness, *a)?; - let int_a = BigUint::from_bytes_be(&val_a.to_be_bytes()); - let pow: BigUint = BigUint::one() << (bit_size - 1); - if int_a >= (&pow << 1) { - return Err(OpcodeResolutionError::UnsatisfiedConstrain); - } - - let bb = &int_a & &pow; - let int_r = &int_a - &bb; - let int_b = &bb >> (bit_size - 1); - - insert_witness( - *b, - FieldElement::from_be_bytes_reduce(&int_b.to_bytes_be()), - initial_witness, - )?; - insert_witness( - *r, - FieldElement::from_be_bytes_reduce(&int_r.to_bytes_be()), - initial_witness, - )?; - - Ok(()) - } Directive::PermutationSort { inputs: a, tuple, bits, sort_by } => { let mut val_a = Vec::new(); let mut base = Vec::new(); From c983c820be74527bb5cb6c4fbf433e19701f798b Mon Sep 17 00:00:00 2001 From: guipublic Date: Wed, 15 Mar 2023 10:29:11 +0000 Subject: [PATCH 2/2] format --- acir/src/circuit/directives.rs | 3 +-- acvm/src/pwg/directives.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/acir/src/circuit/directives.rs b/acir/src/circuit/directives.rs index 081ee9397..1ff637a8d 100644 --- a/acir/src/circuit/directives.rs +++ b/acir/src/circuit/directives.rs @@ -235,8 +235,7 @@ fn serialization_roundtrip() { radix: 4, }; - let directives = - vec![invert, quotient_none, quotient_predicate, to_le_radix]; + let directives = vec![invert, quotient_none, quotient_predicate, to_le_radix]; for directive in directives { let (dir, got_dir) = read_write(directive); diff --git a/acvm/src/pwg/directives.rs b/acvm/src/pwg/directives.rs index ee1a038c6..2feb2b231 100644 --- a/acvm/src/pwg/directives.rs +++ b/acvm/src/pwg/directives.rs @@ -6,7 +6,7 @@ use acir::{ FieldElement, }; use num_bigint::BigUint; -use num_traits::{Zero}; +use num_traits::Zero; use crate::OpcodeResolutionError;