TLDR: sharediff error was a bust, back to square 1.
I analyzed the code and did some testing to verify sharediff and the variables
used to calculate it.
The 256 bit arithmetic is done by converting the hash and target to double
using a simple polynomial:
const double base = (const double)(1<<32) * (const double)(1<<32); // 2**64
double dhash = hash[0] + hash[1]*base + hash[2]*base**2 + hash[3]*base**3;
This will introduce some loss of precision but not a 4x error. The resulting precision
matches the precision of the
networkstratum diff which is also double.
NetworkStratum diff is obtained from the pool and matches, therefore correct.
Hash is as accepted by the pool, therefore correct.
Target is calculated from the
networkstratum diff. If the target was too low if would produce
occasional low diff rejected shares, which isn't happening.
If the target was too high there would be no errors but the miner would neglect to
submit shares that would be accepted, resulting in a lower hashrate at the pool.
That was not observed.
In addition, I did some testing by raising the difficulty by 4x and low difficulty shares
were produced. 2x higher also produced low diff shares proving the original target
was neither too low nor too high.
The formula used:
sharediff =
networkstratumdiff * target / hash
I can find nothing wrong with the sharediff calculation. It points back to the constant divisor
in the wiki arcticle being in error.
I'm willing ro accept it's simply a documentation error. My confidence in my code has increased
by doing a detailed analysis, however, it's still a hack without a proper explanation.
Edit: corrected references to stratum diff instead of network diff