File: assets/code/IntFact2Sat.java

You can also download this file here.

/*
 This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org
 */

package com.crasmaru.sat;

import java.math.BigInteger;
import java.util.Random;
import java.util.Vector;

/**
 * Utility to generate SAT instances equivalent to Integer Factorization. Prints
 * a SAT instance in DIMACS format to the standard output.
 * 
 * Usage: java com.crasmaru.sat.IntFact2Sat factors_length_in_bits
 */
public class IntFact2Sat {
	int current_var_;
	int no_clauses_;
	int dim_;
	StringBuffer sb_;

	private int nextVar() {
		return ++current_var_;
	}

	private Vector nextVector() {
		Vector ret = new Vector();
		for (int i = 0; i < dim_; i++) {
			ret.add(nextVar());
		}
		return ret;
	}

	// Returns the complement of var.
	private int not(int var) {
		return -var;
	}

	// Returns a new var x so that x = a and b.
	private int and(int a, int b) {
		int x = nextVar();

		out(x + " " + not(a) + " " + not(b) + " 0");
		out(not(x) + " " + a + " 0");
		out(not(x) + " " + b + " 0");

		no_clauses_ += 3;
		return x;
	}

	// Returns a new var x so that x = a xor b xor c.
	private int xor(int a, int b, int c) {
		int x = nextVar();

		out(x + " " + not(a) + " " + b + " " + c + " 0");
		out(x + " " + a + " " + not(b) + " " + c + " 0");
		out(x + " " + a + " " + b + " " + not(c) + " 0");
		out(x + " " + not(a) + " " + not(b) + " " + not(c) + " 0");
		out(not(x) + " " + not(a) + " " + not(b) + " " + c + " 0");
		out(not(x) + " " + not(a) + " " + b + " " + not(c) + " 0");
		out(not(x) + " " + a + " " + not(b) + " " + not(c) + " 0");
		out(not(x) + " " + a + " " + b + " " + c + " 0");

		no_clauses_ += 8;
		return x;
	}

	// Returns a new var x so that x = a xor b.
	private int xor(int a, int b) {
		int x = nextVar();

		out(x + " " + not(a) + " " + b + " 0");
		out(x + " " + a + " " + not(b) + " 0");
		out(not(x) + " " + a + " " + b + " 0");
		out(not(x) + " " + not(a) + " " + not(b) + " 0");

		no_clauses_ += 4;
		return x;
	}

	// Returns a new var x so that x = carry(a + b + c).
	private int carry(int a, int b, int c) {
		int x = nextVar();

		out(x + " " + not(a) + " " + not(b) + " 0");
		out(x + " " + not(a) + " " + not(c) + " 0");
		out(x + " " + not(b) + " " + not(c) + " 0");
		out(not(x) + " " + a + " " + b + " 0");
		out(not(x) + " " + a + " " + c + " 0");
		out(not(x) + " " + b + " " + c + " 0");

		no_clauses_ += 6;
		return x;
	}

	// Returns a new var x so that x = carry(a + b).
	private int carry(int a, int b) {
		int x = nextVar();

		out(x + " " + not(a) + " " + not(b) + " 0");
		out(not(x) + " " + a + " 0");
		out(not(x) + " " + b + " 0");

		no_clauses_ += 3;
		return x;
	}

	// Returns an unary class that sets the var to value.
	private void set(int var, boolean value) {
		out((value ? var : not(var)) + " 0");
		no_clauses_++;
	}

	private void out(Object o) {
		sb_.append(o).append('\n');
	}

	private void generate() {
		Vector in_x = nextVector();
		Vector in_y = nextVector();
		Vector output = generateFactCircuit(in_x, in_y);

		Random rand = new Random();
		BigInteger big1 = dim_ > 2 ? BigInteger.probablePrime(dim_, rand) : BigInteger.valueOf(3);
		BigInteger big2 = dim_ > 2 ? BigInteger.probablePrime(dim_, rand) : BigInteger.valueOf(3);
		assert (big1.bitLength() == dim_);
		assert (big2.bitLength() == dim_);

		setOutputBits(in_x, output, big1, big2);
	}

	private void setOutputBits(Vector in_x, Vector output, BigInteger big1, BigInteger big2) {
		BigInteger ret = big1.multiply(big2);
		for (int i = 0; i < ret.bitLength(); i++) {
			set(output.get(i), ret.testBit(i));
		}
		for (int i = ret.bitLength(); i < output.size(); i++) {
			set(output.get(i), false);
		}

		StringBuffer sb1 = new StringBuffer();
		for (int i = 0; i < dim_; i++) {
			sb1.append((big2.testBit(i) ? in_x.get(i) : not(in_x.get(i))) + " ");
		}
		sb_.insert(0, "c " + big2 + ": " + sb1 + "\n");

		sb1 = new StringBuffer();
		for (int i = 0; i < dim_; i++) {
			sb1.append((big1.testBit(i) ? in_x.get(i) : not(in_x.get(i))) + " ");
		}
		sb_.insert(0, "c " + big1 + ": " + sb1 + "\n");

		sb_.insert(0, "p cnf " + current_var_ + " " + no_clauses_ + "\n");
	}

	@SuppressWarnings("unchecked")
	private Vector generateFactCircuit(Vector in_x, Vector in_y) {
		in_x.add(nextVar()); // pad in_x with a 0
		Vector last = (Vector) in_x.clone();

		Vector output = new Vector();
		Vector carry = new Vector();
		Vector result = new Vector();

		// Start from 1 because in_x[0] == 1.
		for (int in_y_indx = 1; in_y_indx < dim_; in_y_indx++) {
			carry.clear();
			result.clear();

			// gather output bits
			output.add(last.get(0));

			int a_0 = and(in_x.get(0), in_y.get(in_y_indx));
			result.add(xor(last.get(1), a_0));
			carry.add(carry(last.get(1), a_0));

			for (int j = 1; j < dim_; j++) {
				int a_j = and(in_x.get(j), in_y.get(in_y_indx));
				result.add(xor(last.get(j + 1), a_j, carry.get(j - 1)));
				carry.add(carry(last.get(j + 1), a_j, carry.get(j - 1)));
			}

			last = (Vector) result.clone();
			last.add(carry.lastElement());
		}
		for (int i = 0; i < last.size(); i++) {
			output.add(last.elementAt(i));
		}

		set(in_y.firstElement(), true);
		set(in_x.lastElement(), false);
		return output;
	}

	IntFact2Sat(int dim) {
		this.dim_ = dim;
		sb_ = new StringBuffer();
	}

	public static void main(String[] args) {
		IntFact2Sat minFact = new IntFact2Sat(args.length > 0 ? Integer.parseInt(args[0]) : 2);
		minFact.generate();
		System.out.println(minFact.sb_);
	}
}