From 4092d473944bf129b0d0bd5991276c4fba4053ec Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Thu, 1 Apr 2021 20:58:21 +0100 Subject: [PATCH] ACIR - Add helper methods, for converting between ACIR and R1CS --- crates/acir/src/circuit/gate.rs | 14 ++++++++++++++ crates/acir/src/circuit/mod.rs | 10 ++++++++++ crates/acir/src/native_types/witness.rs | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/crates/acir/src/circuit/gate.rs b/crates/acir/src/circuit/gate.rs index 0ffce7b89fa..fdab36c5650 100644 --- a/crates/acir/src/circuit/gate.rs +++ b/crates/acir/src/circuit/gate.rs @@ -28,6 +28,20 @@ pub enum Gate { Directive(Directive), } +impl Gate { + pub fn is_arithmetic(&self) -> bool { + if let Gate::Arithmetic(_) = self { + true + } else { false} + } + pub fn arithmetic(self) -> Arithmetic { + match self { + Gate::Arithmetic(gate) => gate, + _=> panic!("tried to convert a non arithmetic gate to an Arithmetic struct") + } + } +} + #[derive(Clone, Debug)] /// Directives do not apply any constraints. pub enum Directive { diff --git a/crates/acir/src/circuit/mod.rs b/crates/acir/src/circuit/mod.rs index f741677670e..fdf764b0502 100644 --- a/crates/acir/src/circuit/mod.rs +++ b/crates/acir/src/circuit/mod.rs @@ -12,6 +12,12 @@ pub struct Circuit { pub public_inputs: PublicInputs, } +impl Circuit { + pub fn num_vars(&self) -> u32 { + self.current_witness_index + 1 + } +} + #[derive(Clone, Debug)] pub struct PublicInputs(pub Vec); @@ -23,6 +29,10 @@ impl PublicInputs { .map(|witness| witness.witness_index() as u32) .collect() } + + pub fn contains(&self, index : usize) -> bool { + self.0.contains(&Witness(index as u32)) + } } #[derive(Clone, Debug)] pub struct Selector(pub String, pub FieldElement); diff --git a/crates/acir/src/native_types/witness.rs b/crates/acir/src/native_types/witness.rs index 36d2fa8d4dd..a706d7ac862 100644 --- a/crates/acir/src/native_types/witness.rs +++ b/crates/acir/src/native_types/witness.rs @@ -15,6 +15,11 @@ impl Witness { pub fn witness_index(&self) -> u32 { self.0 } + pub fn as_usize(&self) -> usize { + // This is safe as long as the architecture is 32bits minimum + self.0 as usize + } + pub const fn can_defer_constraint(&self) -> bool { true