I've read the following pdf document http://cs.ucsb.edu/~koc/ns/docs/kaufman/04hash.pdf (starting at page 16 )
I think I've understand everything, but I couldn't figure out what x y and z in the functions are. Could anybody tell me which bits are meant with that?
The function F(x, y, z) in MD4 operates on 32-bit words, not bits, and is used as part of the round to generate a value that is XORed with part of the digest. It could be expressed in C as:
uint32_t F(uint32_t x, uint32_t y, uint32_t z)
{
return (x & y) | (~x ^ z);
}
As used in MD4, x, y, and z represent three chunks of the digest being computed, but that's not inherent to the function. Just keep reading the definition of the algorithm to see how it's used.
Related
We have the following equation, that we want to implement into our code with arrays/matrices
h(x, y, z) = ax + by + cz
pseudeo code:
X = [a, b, c]
A = [x, y, z]
Often I see the equation being implemented like this:
h = transpose(A) * T
Is there any difference of just using the dot product?
h = dotproduct(A, X)
Is there a specific reason why the transpose is used over the dotproduct?
Mathematically, there's no difference. In implementation- dotproduct(...) may actually be faster, since transpose(...) may eagerly move the elements around prior to the matrix multiplication, doing "unnecessary" work.
I'm trying to multiply two numbers in Prolog recursively i.e. 3*4 = 3+3+3+3 = 12.
My code is :
mult(0,Y,Y).
mult(X,Y,Z) :-
NewX is X-1,
Z is Y + mult(NewX,Y,Z).
but I keep either in an infinite loop or being told mult is not a function.
What you here constructed is a predicate. A predicate is not the same as a function in computer science, you can not write A is B + some_pred(C), or at least not as far as I know in ISO Prolog, and definitely not without adding some extra logic.
In order to pass values, one uses variables. We can thus call the mult/3 predicate recursively, and use a variable that will be unified with the result. We can then perform arithmetic with it, like:
mult(0, _, 0).
mult(X, Y, Z) :-
X1 is X - 1,
mult(X1, Y, Z1),
Z is Y + Z1.
Note that you can not reassign a (different) value to a variable. So if, like you did in the question, use Z twice, then given Y is not 0, this will fail.
The above is however still not sufficient, since it will produce a result, but then get stuck in an infinite loop since if it calls (eventually) mult(0, 4, Z) (4 is here just a value), there are two ways to resolve this: with the base case, and with the recursive case.
We thus need a "guard" for the second case, like:
mult(0, _, 0).
mult(X, Y, Z) :-
X > 0,
X1 is X - 1,
mult(X1, Y, Z1),
Z is Y + Z1.
We then obtain for example:
?- mult(14, 25, Z).
Z = 350 ;
false.
One can improve the speed of this mult/3 predicate by implementing a version with an accumulator. I leave this as an exercise.
So I have this equation: x % a = b. Values a and b are already known. I want to find the value of x. So, how do I reverse plain modulus? Everything I've found was in the order (a + x) % m = b.
I need to do this for an encryption algorithm. It has a way of knowing what the number is, I just need to separate out the parts of it and this is the only part I haven't reversed.
Assume x, a, and b are integers, as the other commenters have noted, you cannot find a unique value. In fact, there are an infinite number of x values that will work, namely x = b, x = b + a, x = b + 2 * a, ... or:
x = b + a*k, for any integer k.
Of course, for an actual java int (or long) there are only a finite set of x values.
Is there a way to represent a 3D Vector as a definite number? I mean that two vectors with different values can't ever have the same hash value. I'm sure there already is a question about this but I haven't found it unfortunately. Thanks for your help.
EDIT:
I know this algorithm for 2D vectors which is pretty good (I think): (x + y) * (x + y + 1) / 2 + y
The best approach to get a hash for a vector of floats is to convert it to a string of bytes or characters and calculate a hash on it. An example of this is given using numpy and python in the following answer:
Most efficient property to hash for numpy array.
This will work efficiently for large numbers of vectors, but you cannot guarantee that you will not get collisions due to the simple fact of mapping three floats onto an integer. However there are a number of hashing algorithms available in the python hashlib library to choose from, you might need to experiment. An option in C++ is Boost::Hash.
See the pigeon-hole principle - in the same way you can't fit you can't 100 pigeons into 10 holes, you can't uniquely convert 3 values into 1 value (with all values of the same size). There will have to be duplicates.
Now, if you could have a number with 3x as many bits as the vector values, the problem becomes fairly easy:
// convert x, y and z to the range 0-...
x -= minimum possible value
y -= minimum possible value
z -= minimum possible value
mult = maximum possible value + 1
hash = x * mult * mult + y * mult + z
If you're having trouble understanding the above, just take the example of the range of the values being 0-99. We'd multiple x by 100*100 = 10000 and y by 100, so the hash would be a decimal value with (at most) 6 digits with x, y and z all next to each other, guaranteed to not overlap:
x = 12
y = 34
z = 56
hash = 123456
Now this same idea will hold for any maximum value by just changing the base / radix.
If there isn't any overlap in some base, each unique combination of values of x, y and z will result in a unique hash.
This is by far the simplest approach, although it doesn't produce a particularly good hash, so it depends what you want to use it for - there might be a way to uniquely convert this number to another number which will be a good hash.
Responding to this post a little late, and perhaps this isn't what you're looking for, but I figured I would chime in with another answer.
You could use the function you mentioned, (x + y) * (x + y + 1) / 2 + y , and do it recursively, ex. f( f(x,y) , z).
You can also use other pairing functions as well and use the same method (https://en.wikipedia.org/wiki/Pairing_function).
For my problem, I wanted a function that would order vectors based on their location. The order itself didn't matter, only that a close value means a similar vector. What I ended up doing was:
double get_pairing(double x, double y, double z) {
double normalizer = 0.0;
if(x < 0) {
normalizer += (3.0 * MAX_COORD_VAL);
}
if (y < 0) {
normalizer += (6.0 * MAX_COORD_VAL);
}
if (z < 0) {
normalizer += (9.0 * MAX_COORD_VAL);
}
double g = x + y + z - normalizer + (21 * MAX_COORD_VAL);
return g;
}
This orders vectors based on whether they have negative coordinate values and whether they have large coordinate values.
This works assuming you have a max coordinate value.
I am trying to write to a 2-dimensional cudaArray through a surface<void, 2>.
The array has a channel format {32, 32, 0, 0, cudaChannelFormatKindFloat} or to put it more simply, holds vector2s.
I am trying to write a vector2 to the surface at the position indicated by integer coordinates (x, y). The following works well:
// write the float2 vector d to outSurf
surf2Dwrite(d.x, outSurf, x * sizeof(float2), y);
surf2Dwrite(d.y, outSurf, x * sizeof(float2) + sizeof(float), y);
However, if I do
surf2Dwrite(d, outSurf, x * sizeof(float2), y);
only the x component of the vector is being written. What is the reason for this slightly unintuitive behaviour?
I find it hard to believe that any of those surf2Dwrite calls actually do what you think they do. To write a float2 I would use this:
surf2Dwrite<float2>(d, outSurf, x, y);
The x and y arguments are the coordinates on the surface you are writing to and the template parameter tells the call the size of the type being accessed.