## SOLVED: Given the output C of the function H(A, B) = (A ^ B) ^ ((A & B) << 1), how to write a function that outputs A given C and B?

lyrically wicked:

Assuming that `a` and `b` are unsigned integers, consider the following function:

``````function H(a, b) {
var c = (a ^ b) ^ ((a & b) << 1);
return c
};
```
```

The description of this function can be found in NORX specification (the links are available at this page).

I need to implement the reverse function of `H` (in Javascript). That is, given `c` and `b`, the function is required to output `a`.

The specification provides an algorithm for the reverse function (assuming that `v[i]` denotes an `i`-th bit of an integer `v`):

``````a = c XOR b;
a = (c XOR b) XOR (a AND b);
...
a[i] = (c[i] XOR b[i]) XOR (a[i-1] AND b[i-1]);
```
```

But I don't know how to implement it.

I tried the following function (note that the arguments are the elements of UintNArrays, so we assume that `N` is either 8 or 16 or 32):

``````function revH(c, b) {
var x = (c ^ b) >>> 0; var a = x;

for (var i = 1; i < N; ++i) {
x0 = (c ^ b) ^ ( (a >>> i) & (b >>> i ));
a = (a & ~(0x1 << i)) | ((x >>> (N-i)) << i);
};
return a
};
```
```

But this function is not correct (it does not output correct results). How to implement the reverse function of `H`?

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots