Skip to content

Commit c437049

Browse files
committed
add simple adder test
1 parent fe51204 commit c437049

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

patronus-sca/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,31 @@ mod tests {
532532
}
533533
}
534534

535+
#[test]
536+
fn test_simple_adder() {
537+
let mut ctx = Context::default();
538+
let a = ctx.bv_symbol("a", 2);
539+
let b = ctx.bv_symbol("b", 2);
540+
let word_level = ctx.add(a, b);
541+
let a_0 = ctx.slice(a, 0, 0);
542+
let a_1 = ctx.slice(a, 1, 1);
543+
let b_0 = ctx.slice(b, 0, 0);
544+
let b_1 = ctx.slice(b, 1, 1);
545+
let gate_level_0 = ctx.xor(a_0, b_0);
546+
let carry_1 = ctx.and(a_0, b_0);
547+
let gate_level_1 = ctx.majority(a_1, b_1, carry_1);
548+
let gate_level = ctx.concat(gate_level_1, gate_level_0);
549+
let equality = ctx.equal(word_level, gate_level);
550+
551+
let problem = ScaEqualityProblem {
552+
equality,
553+
gate_level,
554+
word_level,
555+
};
556+
557+
simplify_word_level_equality(&mut ctx, problem).unwrap();
558+
}
559+
535560
#[test]
536561
fn test_simplify_coward_three_add() {
537562
simplify_coward_smt("../inputs/coward/add_three.4_bit.smt2").unwrap();

patronus/src/expr/context.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,20 @@ impl Context {
308308
debug_assert_eq!(a.get_bv_type(self).unwrap(), b.get_bv_type(self).unwrap());
309309
self.add_expr(Expr::BVXor(a, b, b.get_bv_type(self).unwrap()))
310310
}
311+
312+
pub fn xor3(&mut self, a: ExprRef, b: ExprRef, c: ExprRef) -> ExprRef {
313+
let x = self.xor(a, b);
314+
self.xor(x, c)
315+
}
316+
317+
pub fn majority(&mut self, a: ExprRef, b: ExprRef, c: ExprRef) -> ExprRef {
318+
let a_and_b = self.and(a, b);
319+
let a_and_c = self.and(a, c);
320+
let b_and_c = self.and(b, c);
321+
let x = self.or(a_and_b, a_and_c);
322+
self.or(x, b_and_c)
323+
}
324+
311325
pub fn shift_left(&mut self, a: ExprRef, b: ExprRef) -> ExprRef {
312326
debug_assert_eq!(a.get_bv_type(self).unwrap(), b.get_bv_type(self).unwrap());
313327
self.add_expr(Expr::BVShiftLeft(a, b, b.get_bv_type(self).unwrap()))
@@ -512,6 +526,12 @@ impl<'a> Builder<'a> {
512526
pub fn xor(&self, a: ExprRef, b: ExprRef) -> ExprRef {
513527
self.ctx.borrow_mut().xor(a, b)
514528
}
529+
pub fn xor3(&mut self, a: ExprRef, b: ExprRef, c: ExprRef) -> ExprRef {
530+
self.ctx.borrow_mut().xor3(a, b, c)
531+
}
532+
pub fn majority(&mut self, a: ExprRef, b: ExprRef, c: ExprRef) -> ExprRef {
533+
self.ctx.borrow_mut().majority(a, b, c)
534+
}
515535
pub fn shift_left(&self, a: ExprRef, b: ExprRef) -> ExprRef {
516536
self.ctx.borrow_mut().shift_left(a, b)
517537
}

0 commit comments

Comments
 (0)