-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from elsoroka/dev
Architecture improvements and support for fixed-size BitVectors
- Loading branch information
Showing
39 changed files
with
1,818 additions
and
806 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Workflow for Codecov example-julia | ||
name: CI | ||
on: | ||
push: | ||
branches: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Build and Deploy Documentation | ||
name: docs | ||
# https://github.com/julia-actions/julia-docdeploy | ||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
# [BooleanSatisfiability](https://elsoroka.github.io/BooleanSatisfiability.jl) | ||
|
||
![example workflow](https://github.com/github/docs/actions/workflows/ci.yml/badge.svg) [![codecov](https://codecov.io/gh/elsoroka/BooleanSatisfiability.jl/branch/main/graph/badge.svg?token=84BIREQL46)](https://codecov.io/gh/elsoroka/BooleanSatisfiability.jl) | ||
[![build status](https://github.com/elsoroka/BooleanSatisfiability.jl/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/elsoroka/BooleanSatisfiability.jl/actions/workflows/CI.yml?query=branch%3Amain) [![docs](https://github.com/elsoroka/BooleanSatisfiability.jl/actions/workflows/docs.yml/badge.svg)](https://elsoroka.github.io/BooleanSatisfiability.jl/) [![codecov](https://codecov.io/gh/elsoroka/BooleanSatisfiability.jl/branch/main/graph/badge.svg?token=84BIREQL46)](https://codecov.io/gh/elsoroka/BooleanSatisfiability.jl) | ||
|
||
[![Build Status](https://github.com/elsoroka/BooleanSatisfiability.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/elsoroka/BooleanSatisfiability.jl/actions/workflows/CI.yml?query=branch%3Amain) | ||
BooleanSatisfiability.jl is a package for representing satisfiability modulo theories (SMT) problems in Julia. This package provides a simple front-end interface to common SMT solvers, including full support for vector-valued and matrix-valued expressions. Currently, the theories of propositional logic, Integers, Reals and fixed-size BitVectors are supported. We will eventually add support for all [SMT-LIB standard theories](http://smtlib.cs.uiowa.edu/theories.shtml). | ||
|
||
BooleanSatisfiability.jl is a package for representing Boolean satisfiability (SAT) and selected other satisfiability modulo theories (SMT) problems in Julia. This package provides a simple front-end interface to common SMT solvers, including full support for vector-valued and matrix-valued expressions. | ||
What you can do with this package: | ||
* Easily specify single-valued or vector-valued SMT expressions using Julia's built-in broadcasting capabilities. | ||
* Generate files in the [SMT-LIB](http://www.smtlib.org/) specification language. | ||
* Interact with any solver that follows the SMT-LIB standard. | ||
|
||
* Easily specify single-valued or vector-valued Boolean SAT, integer or real-valued SMT problems using Julia's built-in broadcasting capabilities. | ||
* Generate files in [SMT2](http://www.smtlib.org/) format. | ||
* BooleanSatisfiability.jl calls [Z3](https://www.microsoft.com/en-us/research/publication/z3-an-efficient-smt-solver/) as the back-end solver and interprets the result. | ||
## Examples | ||
|
||
## Example | ||
|
||
Solving the single-valued problem **(x ∧ y) ∨ (¬x ∧ y)** | ||
``` | ||
x = Bool("x") | ||
y = Bool("y") | ||
expr = (x ∧ y) ∨ (¬x ∧ y) | ||
status = sat!(expr) | ||
println("x = $(value(x)), y = $(value(y))”) | ||
``` | ||
|
||
Solving the vector-valued problem **(x1 ∧ y1) ∨ (¬x1 ∧ y1) ∧ ... ∧ (xn ∧ yn) ∨ (¬xn ∧ yn)** | ||
### Solving the vector-valued problem | ||
(x1 ∧ y1) ∨ (¬x1 ∧ y1) ∧ ... ∧ (xn ∧ yn) ∨ (¬xn ∧ yn) | ||
``` | ||
x = Bool(n, "x") | ||
y = Bool(n, "y") | ||
expr = (x .∧ y) .∨ (¬x .∧ y) | ||
status = sat!(expr) | ||
status = sat!(expr, solver=Z3()) | ||
println("x = $(value(x)), y = $(value(y))”) | ||
``` | ||
|
||
### Proving a bitwise version of de Morgan's law. | ||
In this example we want to show there is NO possible value of x and y such that de Morgan's bitwise law doesn't hold. | ||
``` | ||
@satvariable(x, BitVector, 64) | ||
@satvariable(y, BitVector, 64) | ||
expr = not((~x & ~y) == ~(x | y)) | ||
status = sat!(expr, solver=Z3()) | ||
println(status) # if status is UNSAT we proved it. | ||
``` | ||
|
||
## Development status | ||
Working on a first release! Follow for updates. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Predicting the output of a tiny LCG | ||
This example is "3.10.1 Cracking LCG with Z3" from *[SAT/SMT by Example](https://sat-smt.codes/SAT_SMT_by_example.pdf)* by Dennis Yurichev. | ||
|
||
A linear congruential generator (LCG) is an algorithm for generating pseudo-random numbers in which a series of transformations is used to move from one number to the next. LCGs are easy to implement using low-level bit operations, making them popular for resource-constrained embedded applications. However, they are unsuitable for many applications because their output is predictable. | ||
|
||
*SAT/SMT by Example*, example 3.10.1 shows how to predict the future output of an LCG by encoding its transformations. The original example starts with a small C program that prints the output of `rand() % 100` 10 times, producing 10 2 digit random numbers. It turns out C's rand() has this LCG implementation: | ||
|
||
1. `state = state * 214013 + 2531011` | ||
2. `state = (state >> 16) & 0x7FFF` | ||
3. `return state` | ||
|
||
Suppose we observe 10 states `n1,...,n10 = [37, 29, 74, 95, 98, 40, 23, 58, 61, 17]` from the LCG. We want to predict `n0`, the number before `n1`, and `n11`, the number after `n10`. (These are the numbers from *SAT/SMT by Example*.) | ||
|
||
```@example | ||
using BooleanSatisfiability | ||
@satvariable(states[1:10], BitVector, 32) | ||
@satvariable(output_prev, BitVector, 32) | ||
@satvariable(output_next, BitVector, 32) | ||
``` | ||
```@example | ||
transitions = BoolExpr[states[i+1] == states[i] * 214013+2531011 for i=1:9] | ||
remainders = BoolExpr[ | ||
output_prev == urem(( states[1] >> 16 ) & 0x7FFF, 100), | ||
urem(( states[2] >> 16) & 0x7FFF, 100) == 29, | ||
urem(( states[3] >> 16) & 0x7FFF, 100) == 74, | ||
urem(( states[4] >> 16) & 0x7FFF, 100) == 95, | ||
urem(( states[5] >> 16) & 0x7FFF, 100) == 98, | ||
urem(( states[6] >> 16) & 0x7FFF, 100) == 40, | ||
urem(( states[7] >> 16) & 0x7FFF, 100) == 23, | ||
urem(( states[8] >> 16) & 0x7FFF, 100) == 58, | ||
urem(( states[9] >> 16) & 0x7FFF, 100) == 61, | ||
output_next == urem(( states[10] >> 16) & 0x7FFF, 100), | ||
] | ||
``` | ||
```@example | ||
expr = and(all(transitions), all(remainders)) | ||
status = sat!(expr, solver=CVC5()) | ||
println("status = $status") | ||
for (i,state) in enumerate(states) | ||
println("state $i = $(value(state))") | ||
end | ||
# According to SAT/SMT By Example the previous output is 37 and the next output is 17. | ||
println("prev = $(value(output_prev))") | ||
println("next = $(value(output_next))") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.