1

I'm trying to solve a system of equations. Each equation is in the form:

V1 xor V2 xor ... xor Vx = Sx

Vx and Sx are single bits variables. The Sx are known and I need to find the value of all the Vx


ex:

V1 xor V2 xor V3 = 1
V1 xor V2 = 0
V2 xor V3 = 1

(solution V1 = 0, V2 = 0, V3 = 1)


In reality, I have thousands variables (each is a single bit) and thousands of equations (only xor operations). I know that there is at least one solution and I only need one solution.

I know how to solve this by hand for a small system but I don't know how to build an algorithm to solve this.

Can you help me with this? I'm a developer and I understand how to work with bits, xor operators and data structures, but I'm less experienced in mathematics and I don't know which equations system solving method to use. I'm also not very intuitive with matrix operations so if I need it please try to explain it very slowly! :p

Thanks!

Gary Poc
  • 63
  • 3
  • Can you elaborate further on what exactly you are trying to solve? Does your example mean you are trying to identify which bit is set (V3 in the example) Typically, you solve systems of equations in the other direction, you know 3 equations of 3 Vx variables and you are looking for the Sx solution. Given the Sx solution - are you wanting to "discover" the system of equations for a matrix with thousands of rows and columns? – geneSummons Apr 23 '19 at 23:42
  • Thx, I'm trying to reverse an algorithm, more precisely a non secure random generator. Using the output of the PRG, I want to find the internal state (the input). This is how I get a system of xor equation for which I know the result and I want to know the inputs. This is also why I know that there is at least one valid solution. So now I have the system of equation but I don't know how to solve it, because I'm very lame at Matrix calculations :p – Gary Poc Apr 23 '19 at 23:56
  • You could throw it at a Constraint Solver: https://developers.google.com/optimization/cp/cp_solver – Ian Mercer Apr 24 '19 at 01:17

4 Answers4

4

You can use Gaussian elimination for this: https://en.wikipedia.org/wiki/Gaussian_elimination

XOR is addition (and subtraction -- it's the same) for integers taken modulo 2, so it's quite easy:

Find an equation that contains v1, for example, and add it to all the other equations that contain v1 to remove v1 from them:

v1 XOR v2        = 1
      +
v1        XOR v3 = 0
--------------------
       v2 XOR v3 = 1

Use a different equation to remove v2 from all other equations, a different one to remove v3, etc., until all the equations have only one variable.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • Thanks. Can you describe a little bit more how to use Gaussian elimination to solve my case? or point to code that would work in my case? I'm not being lazy but I just don't get it about matrix calculations! – Gary Poc Apr 23 '19 at 23:47
  • Sure. Hope that helps – Matt Timmermans Apr 24 '19 at 01:42
0

I almost put this in another comment, but it seems like an answer, so here it is.

I'm afraid you may be SOL. For example, given an Sx of 111, one matrix leading there is

L1 = 100   | Sx(L1) = 1
L2 = 010   | Sx(l2) = 1
L3 = 001   | Sx(L3) = 1

and there are 2 more equivalent matrices that fit the solution (L3 could be 010 or 100 just as easily).

Also, given an Sx of 001 - you will have no idea which Vx in L3 is the "active" bit, even though you know L1 and L2 have coefficients of 0 on every variable.

geneSummons
  • 907
  • 5
  • 15
0

OK, to do this you will need to know a few algebraic rules about xor and not and 0 = false and 1 = true.

First of all, xor satisfies both the associative and commutative laws. If we xor a long chain together, we can rearrange to our heart's content.

Next, x xor x = 0. When we add to this the fact that 0 xor y = y we can just drop matched pairs of variables.

Next, substitution. An equation of the form x1 xor x2 xor ... xor xn = 0 implies that x1 = x2 xor x3 xor ... xor xn. Likewise x1 xor x2 xor ... xor xn = 1 implies that x1 = 1 xor x2 xor x3 xor ... xor xn. Those facts can be substituted in your other equations. This may result in repeated variables, that we can then drop.

This means that every equation can be used to write one variable in terms of the others, and then that can be substituted into the other equations. That is now a dependent variable. At the end we'll have one of three states.

  1. 1 = 0 means that there are no solutions.
  2. No equations and no variables left. There is one solution. Just substitute backwards and you have it.
  3. Several variables were never eliminated but you are out of equations. Those variables are free. You can set them to anything and get an answer. You might as well set them to 1.

Let me illustrate with your equations.

(1) 1 = V1 xor V2 xor V3
(2) 0 = V1 xor V2
(3) 1 = V2 xor V3

From (1) we know that:

(4) V1 = 1 xor V2 xor V3

(Note, V1 is eliminated.) Substitute (4) into (2) and (3) to get:

(5) 0 = V1 xor V2
      = (1 xor V2 xor V3) xor V2
      = 1 xor V3

(6) 1 = V2 xor V3

(Note that (6) is a trivial substitution.)

From (5) we get:

(7) 1 = V3

(Note, V3 is eliminated.) Substitute (7) into (6) and we get:

(8) 1 = V2 xor V3
      = V2 xor 1

From (8) we get:

(9) V2 = 1 xor 1 = 0

(Note, V2 is eliminated.)

Rules (9), (7) and (4) eliminated V2, V3 and V1 so there is one solution. And it is:

(9) V2 = 0
(7) V3 = 1
(4) V1 = 1 xor V2 xor V3 = 1 xor 0 xor 1 = 0

Note that this is an entirely mechanical procedure. At each step I took the first equation I had left, used it to write one variable in terms of the others, then substituted it into the others. One less equation, one less variable. It always works.

You will have to work out a good representation and code for this. But knowing what you are trying to do will hopefully help.

btilly
  • 43,296
  • 3
  • 59
  • 88
0

You can use the MiniSAT algorithm. In Java it is for example available in the LogicNG project.

Your example can be posted like this in the SatisfiabilityInstances() function of the Symja project. Under the hood the LogicNG MiniSat.miniSat() is called.

SatisfiabilityInstances(Xor(v1,v2,v3)&&Not(Xor(v1,v2))&&Xor(v2,v3),{v1,v2,v3})

result:

{{False,False,True}}
axelclk
  • 950
  • 9
  • 28