Skip to content

Commit

Permalink
Rutefig/194 add check that the signal is in the circuit (privacy-scal…
Browse files Browse the repository at this point in the history
…ing-explorations#198)

Adds a check if the signal that is trying to be exposed is already in
the circuit or not, this way the user will always know if is trying to
expose a non existing signal.
  • Loading branch information
rutefig authored Feb 5, 2024
1 parent 4c9a23b commit 5943fbd
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/sbpir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ impl<F, TraceArgs> SBPIR<F, TraceArgs> {
pub fn expose(&mut self, signal: Queriable<F>, offset: ExposeOffset) {
match signal {
Queriable::Forward(..) | Queriable::Shared(..) => {
let existing_forward_signal = self
.forward_signals
.clone()
.iter()
.any(|s| s.uuid() == signal.uuid());
let existing_shared_signal = self
.shared_signals
.clone()
.iter()
.any(|s| s.uuid() == signal.uuid());
if !existing_forward_signal && !existing_shared_signal {
panic!("Signal not found in forward signals.");
}
self.exposed.push((signal, offset));
}
_ => panic!("Can only expose forward and shared signals."),
Expand Down Expand Up @@ -530,4 +543,37 @@ mod tests {
let circuit: SBPIR<i32, i32> = SBPIR::default();
assert!(circuit.q_enable);
}

#[test]
#[should_panic]
fn test_expose_non_existing_signal() {
let mut circuit: SBPIR<i32, i32> = SBPIR::default();
let signal = Queriable::Forward(
ForwardSignal::new_with_phase(0, "signal".to_string()),
false,
);
let offset = ExposeOffset::First;

circuit.expose(signal, offset);
}

#[test]
fn test_expose_forward_signal() {
let mut circuit: SBPIR<i32, i32> = SBPIR::default();
let signal = circuit.add_forward("signal", 0);
let offset = ExposeOffset::Last;
assert_eq!(circuit.exposed.len(), 0);
circuit.expose(Queriable::Forward(signal, false), offset);
assert_eq!(circuit.exposed.len(), 1);
}

#[test]
fn test_expose_shared_signal() {
let mut circuit: SBPIR<i32, i32> = SBPIR::default();
let signal = circuit.add_shared("signal", 0);
let offset = ExposeOffset::Last;
assert_eq!(circuit.exposed.len(), 0);
circuit.expose(Queriable::Shared(signal, 10), offset);
assert_eq!(circuit.exposed.len(), 1);
}
}

0 comments on commit 5943fbd

Please sign in to comment.