Created
January 13, 2026 11:53
-
-
Save piersy/dda1b79aefd9e1079f3425a526820c73 to your computer and use it in GitHub Desktop.
Peer Das Security Calculation in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "math/big" | |
| ) | |
| // factorial using big.Int | |
| func factorial(n int64) *big.Int { | |
| result := big.NewInt(1) | |
| if n < 2 { | |
| return result | |
| } | |
| for i := int64(2); i <= n; i++ { | |
| result.Mul(result, big.NewInt(i)) | |
| } | |
| return result | |
| } | |
| // binomial coefficient n choose k | |
| func binomial(n, k int64) *big.Int { | |
| if k < 0 || k > n { | |
| return big.NewInt(0) | |
| } | |
| if k == 0 || k == n { | |
| return big.NewInt(1) | |
| } | |
| num := factorial(n) | |
| den := factorial(k) | |
| den.Mul(den, factorial(n-k)) | |
| return num.Div(num, den) | |
| } | |
| // power for big.Float: x^y where y is integer | |
| func powBigFloat(x *big.Float, y int64) *big.Float { | |
| result := new(big.Float).SetFloat64(1) | |
| tmp := new(big.Float).Set(x) | |
| for i := int64(0); i < y; i++ { | |
| result.Mul(result, tmp) | |
| } | |
| return result | |
| } | |
| func main() { | |
| // Parameters | |
| n := int64(13000) // total nodes | |
| k := int64(8) // samples per node | |
| m := int64(128) // chunks per block | |
| fmt.Printf("e\t n_e\t P_trick\n") | |
| fmt.Println("-------------------------------------------------") | |
| for e := 0.0; e <= 0.05+1e-9; e += 0.001 { | |
| // n_e = e * n | |
| n_e := int64(float64(n) * e) | |
| if n_e == 0 { | |
| n_e = 1 // avoid binomial(0) | |
| } | |
| // binomial(n, n_e) | |
| binNodes := new(big.Int).Set(binomial(n, n_e)) | |
| // binomial(m, m/2-1) | |
| binChunks := new(big.Int).Set(binomial(m, m/2-1)) | |
| // (1/2)^(k * n_e) | |
| probSample := powBigFloat(big.NewFloat(0.5), k*n_e) | |
| // multiply all terms: binNodes * binChunks * probSample | |
| bn := new(big.Float).SetInt(binNodes) | |
| bc := new(big.Float).SetInt(binChunks) | |
| term := new(big.Float).Mul(bn, bc) | |
| term.Mul(term, probSample) | |
| fmt.Printf("%.3f\t %6d\t %.3e\n", e, n_e, term) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment