Bitcoin Forum

Other => Off-topic => Topic started by: oinkoink on November 17, 2016, 01:53:07 PM



Title: math/hash function question
Post by: oinkoink on November 17, 2016, 01:53:07 PM
Hi all!
lets say i take
H(H(A) XOR H(B)) % 2
where A is a user input, and B .. i dunno .. lets say the valid POW hash of the next block.
I'm wondering if i do get, in the long run, a 50/50 distribution of 0's and 1's and if so, is there a "simpler" or "better" approach where i can get a user input to influence the outcome in a non predictable way?

merci  :-*


Title: Re: math/hash function question
Post by: BurtW on November 17, 2016, 07:54:06 PM
Just an aside this might be slightly better as it gets rid of any inter-dependencies that might be introduced by the XOR operation.

  H(H(A) concatenated to H(B)) % 2



Title: Re: math/hash function question
Post by: oinkoink on November 18, 2016, 11:47:45 AM
so you mean just putting them together like: 
$H1 = 2; $H2 = 5; $toH = $H1.$H2;
echo $toH //25 
i can't see how this would be better,  can you point me somewhere or share your wisdom with me?  my thought was that xor returns a 50/50 distribution itself if the two hashes would consist of (pseudo) random 0s and 1s.
ABXORANDOR
00000
10101
01101
11011
  but im pretty sure this is nonsense anyway bc the resulting hash gets hashed again and in doing so the bits are getting shuffled nicely again. In other words: I never know what output i get from the hash function as long as i don't enter the same input.  And the only inter-dependencies i can see is that  H(A) XOR H(B) == H(B) XOR H(A) and i can not think of any resulting problem. On the other hand $toH = $H1.$H2; would result i a pretty long number and i'm not sure if this is preferable ???
BUT you are dealing here with a total noob and there is a good chance i get a lot of this wrong ^^ 
To summarize:
  • maybe it doesn't matter how i combine the two hashes, probably the fastest method is the best method here?
  • XOR takes away the "order" of my input H(A) XOR H(B) == H(B) XOR H(A) (which is totally ok here imo)


Title: Re: math/hash function question
Post by: Swtrse on November 18, 2016, 12:06:10 PM
From a statistical point of view the distribution must be 50/50.
The reason for that is the % 2 which is just an other way to determine of the least significant bit is a 0 or a 1.
Since we can assume that a hash algorithm generates hashes with 0 and 1s at the last significant bit with the same frequency it is irrelevant how many hashes you chain with an XOR. The distribution will always be 50/50.
If you have hash algorithm where the distribution for the hash itself is not 50/50 then the final distribution must be the average of all chained hash distributions.

I hope this is somehow understandable since English is not my native language and this is really technical.


Title: Re: math/hash function question
Post by: BurtW on November 18, 2016, 12:12:33 PM
It was just an aside.  Either concatenation or xor will work for what you are trying to do.  It is just that when used for security related purposes the hash function is designed to work with the concatenation of values.  You are not using the hash function for a security related purpose here so either way.


Title: Re: math/hash function question
Post by: oinkoink on November 18, 2016, 05:23:08 PM
Thanks BurtW, Swtrse!
You are awesome!


@Swtrse
least significant bit? you mean, it could be also the most significant bit but choose one and watch for the distribution, right? otherwise i don't get why it hast to be the LSB?


Title: Re: math/hash function question
Post by: Swtrse on November 21, 2016, 04:43:50 PM
Thanks BurtW, Swtrse!
You are awesome!


@Swtrse
least significant bit? you mean, it could be also the most significant bit but choose one and watch for the distribution, right? otherwise i don't get why it hast to be the LSB?

It's called least significant bit because it has the least effect on the number For examlpe
Code:
      LSB
       |
       v
00000000 = 0
00000001 = 1
00001000 = 8
00001001 = 9
As you can see in binary system odd numbers end with an '1' and even numbers end with an '0'.
% 2 is a way to determine if an number is odd or even
taking all of that into account you only have to look at the LSB to see if an number is odd or even or from the binary view if the LSB is a 1 or a 0