Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

chore(acvm)!: Remove truncate and oddrange directives #142

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 5 additions & 62 deletions acir/src/circuit/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@ pub enum Directive {
predicate: Option<Expression>,
},

//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,
Expand All @@ -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",
Expand All @@ -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,
}
}

Expand All @@ -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)?;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -275,24 +229,13 @@ 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)],
radix: 4,
};

let directives =
vec![invert, quotient_none, quotient_predicate, truncate, odd_range, 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);
Expand Down
26 changes: 0 additions & 26 deletions acir/src/circuit/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 ")?;
Expand Down
48 changes: 1 addition & 47 deletions acvm/src/pwg/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use acir::{
FieldElement,
};
use num_bigint::BigUint;
use num_traits::{One, Zero};
use num_traits::Zero;

use crate::OpcodeResolutionError;

Expand Down Expand Up @@ -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());
Expand All @@ -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();
Expand Down