From 335b629724a072aeadb87cac02e971e37881e32e Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 3 Sep 2020 14:26:00 -0600 Subject: [PATCH] Avoid redundant wire queries by searching for an existing query. --- src/plonk/circuit.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 3c300fd..e6b71b1 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -210,19 +210,30 @@ impl MetaCircuit { index } - /// Query a fixed wire at a relative position - pub fn query_fixed(&mut self, wire: FixedWire, at: i32) -> Polynomial { + fn query_fixed_index(&mut self, wire: FixedWire, at: i32) -> usize { let at = Rotation(at); { let len = self.rotations.len(); self.rotations.entry(at).or_insert(PointIndex(len)); } - // TODO: check for existing query so we don't make redundant queries + // Return existing query, if it exists + for (index, fixed_query) in self.fixed_queries.iter().enumerate() { + if fixed_query == &(wire, at) { + return index; + } + } + + // Make a new query let index = self.fixed_queries.len(); self.fixed_queries.push((wire, at)); - Polynomial::Fixed(index) + index + } + + /// Query a fixed wire at a relative position + pub fn query_fixed(&mut self, wire: FixedWire, at: i32) -> Polynomial { + Polynomial::Fixed(self.query_fixed_index(wire, at)) } fn query_advice_index(&mut self, wire: AdviceWire, at: i32) -> usize { @@ -232,7 +243,14 @@ impl MetaCircuit { self.rotations.entry(at).or_insert(PointIndex(len)); } - // TODO: check for existing query so we don't make redundant queries + // Return existing query, if it exists + for (index, advice_query) in self.advice_queries.iter().enumerate() { + if advice_query == &(wire, at) { + return index; + } + } + + // Make a new query let index = self.advice_queries.len(); self.advice_queries.push((wire, at));