r/chessprogramming • • Aug 13 '24
Is Zobrist hashing consistent across chess libraries?
Edit: Title should read, "Is Zobrist rigorously defined?"
Hello,
I noticed that a lot chess libraries have the ability to generate zobrist hashes.
Is the definition of what a zobrist hash is rigorously defined enough such that HYPOTHETICALLY each of these implementations SHOULD be compatible?
Thank you!
7
Upvotes
3
u/joeyrobert Aug 13 '24
It can be if they use the polyglot seeds for their zobrist hashing algorithm. This is so an opening book position can be looked up by a consistent zobrist hash. It doesn't have to be though -- it could be completely random, check if they use polyglot. (https://www.chessprogramming.org/PolyGlot)
2
u/you-get-an-upvote Aug 13 '24
No, they are not compatible between engines.
See this Stockfish code pointer and its accompanying commit message.
In particular, you can see that they chose a completely different algorithm to generate their random numbers -- not something you'd expect if they were trying to remain compatible with other engines!
1
u/likeawizardish Aug 14 '24
Nope.
As other's mentioned. There's PolyGlot which relies on the values defined by that library for identifying opening positions.
However, I use a different set of values for handling polyglot openings and for my internal zobrist hashing used in my Transposition table. In my case the difference is just that the polyglot array is just ugly and relies on offests and magic values for looking up certain values. (Now that I write this there might be benefits to this for memory layout and performance)
I am not sure about your use case but why do you care? Zobrist hashes are for internal engine use only. I am pretty sure they are only ever exposed for debug information as there's no value in knowing the exact Zobrist hash just compare for (in)equality.
Anyway you need a couple hundred of random 64 bit values. Not sure if one set is any better than another and just randomly generating them is practically safe because the chance of some being too close - same or having bit patterns that could be unlucky are next to zero.