Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Last active November 6, 2017 04:46
Show Gist options
  • Save Spuffynism/9a2a8d8dbef31ca210c560aedb96318d to your computer and use it in GitHub Desktop.
Save Spuffynism/9a2a8d8dbef31ca210c560aedb96318d to your computer and use it in GitHub Desktop.
A bitcoin attacker's catching-up success probability
// converted from C code from p.6 of the btc whitepaper
// tested with p.7's provided sample inputs
// q being the probability the attacker finds the next block
// z being the number of blocks the attacker is behind
var AttackerSuccessProbability = function(q, z) {
var p = 1.0 - q;
var lambda = z * (q / p);
var sum = 1.0;
var poisson;
for (var k = 0; k <= z; k++) {
var poisson = Math.exp(-lambda);
for (var i = 1; i <= k; i++)
poisson *= lambda / i;
sum -= poisson * (1 - Math.pow(q / p, z - k));
}
return sum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment