This project is a CNF Conversion Tool that reads gate definitions from an input file and outputs a Conjunctive Normal Form (CNF) representation of the circuit. The tool handles AND, OR, and NOT gates, and generates the equivalent CNF formulas to be used in SAT solvers. The tool also includes a shell script to verify the output using CaDiCaL, a SAT solver.
- main.c: The main C source file that includes the implementation of reading gate inputs, generating CNF formulas, and writing them to an output file.
- checker/check.sh: A shell script that compiles the source, runs the tool on several test cases, and uses CaDiCaL to check the correctness of the output.
- Gate Handling: The tool supports AND, OR, and NOT gates.
- CNF Conversion: Converts logic gate definitions into CNF format for use in SAT solvers.
- Verification: The checker script verifies the CNF using CaDiCaL.
- Error Handling: Allocates and deallocates memory properly to prevent memory leaks.
- GCC or any compatible C compiler.
- Make for build automation.
- CaDiCaL SAT Solver (included in the
checkerdirectory).
To build the project, navigate to the src directory and run:
make buildThis command will compile the source code and generate an executable.
To run the CNF conversion tool:
./main <input_file> <output_file><input_file>: File containing gate definitions.<output_file>: File to write the CNF output.
To run the tests provided in the checker directory, execute the following command in the root of the project:
./checker/check.shThis script runs the executable on a set of predefined test cases and compares the results against reference outputs using CaDiCaL.
The input file should start with two numbers:
- Number of Inputs: The number of input variables.
- Last Gate Number: The highest gate number defined.
Each subsequent line should define a gate operation:
- N X Y: Represents a NOT gate with input
Xand outputY. - O X1 X2 ... Xn Y: Represents an OR gate with inputs
X1, X2, ..., Xnand outputY. - A X1 X2 ... Xn Y: Represents an AND gate with inputs
X1, X2, ..., Xnand outputY.
The output is written in DIMACS CNF format:
-
The first line follows the convention:
p cnf <number_of_variables> <number_of_clauses> -
Each subsequent line contains a clause followed by
0. -
The last gate is written as an assertion.
3
4
N 1 4
A 1 2 3 5
O 4 5 6
p cnf 6 4
-1 -4 0
1 4 0
-1 -2 -3 5 0
-4 5 0
The checker/check.sh script is used to run automated tests to validate the output. It consists of the following steps:
- Compilation: Compiles the source code using
make build. - Execution: Runs the executable against the input files located in
checker/in/. - Verification: Uses CaDiCaL to verify if the output is satisfiable or unsatisfiable and compares it with reference outputs (
.reffiles).
The script outputs the result of each test (PASS, FAIL, TLE for timeout) and calculates the total score.
The program ensures proper memory allocation and deallocation:
- The
gate_listis allocated dynamically to hold gate definitions. - Functions like
destroy_gate_list()andfree()are used to release the allocated memory to prevent memory leaks.
- Gate Limitations: The number of gates is currently limited to 100 and inputs to 60. This could be made dynamic based on input requirements.
- Concurrency: The current implementation processes gates sequentially. Future versions could introduce parallelism for more efficient handling of larger circuits.
This project is open source and can be freely modified and distributed.