init: v1.0.0

This commit is contained in:
yaole
2026-05-27 23:03:00 +08:00
commit 8d97f750eb
466 changed files with 80067 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
import math
def loggam(x):
# double x0, x2, xp, gl, gl0;
# long k, n;
a = [8.333333333333333e-02, -2.777777777777778e-03,
7.936507936507937e-04, -5.952380952380952e-04,
8.417508417508418e-04, -1.917526917526918e-03,
6.410256410256410e-03, -2.955065359477124e-02,
1.796443723688307e-01, -1.39243221690590e+00]
x *= 1.0
x0 = x
n = 0
if x == 1.0 or x == 2.0:
return 0.0
elif x <= 7.0:
n = int(7 - x)
x0 = x + n
x2 = 1.0 / (x0*x0)
xp = 2 * math.pi
gl0 = a[9]
for k in range(8, -1, -1):
gl0 *= x2
gl0 += a[k]
gl = gl0/x0 + 0.5 * math.log(xp) + (x0-0.5) * math.log(x0) - x0
if x <= 7.0:
for k in range(1, n + 1):
gl -= math.log(x0-1.0)
x0 -= 1.0
return gl
print(loggam(2))
print(loggam(3))
print(loggam(4))
print(loggam(5))
+115
View File
@@ -0,0 +1,115 @@
package ope
import (
"math"
)
// uniformRand returns a random number in [0, 1]
// len(coins) >= 32
func uniformRand(coins []byte) float64 {
b := int64(0)
for i := 0; i < 32; i++ {
b <<= 1
b |= int64(coins[i])
}
return float64(b) / float64(uint64(1)<<32-1)
}
// hypergeometric10 returns a sample in hypergeometric distribution.
// Pr[X=x] = choose(y, x) * choose(N-y, M-x) / choose(N, M)
func hypergeometricSmall(M int64, N int64, y int64, coins []byte) int64 {
d1 := N - y
d2 := min(M, N-M)
Y := d2
K := y
for Y > 0.0 && K != 0 {
U := uniformRand(coins)
Y -= int64(U + float64(Y)/float64(d1+K))
K--
}
Z := d2 - Y
if N-M < M {
Z = y - Z
}
return Z
}
func loggam[T int | int64 | float32 | float64](x T) float64 {
return math.Log(math.Gamma(float64(x)))
}
func hypergeometric(M int64, N int64, y int64, coins []byte) int64 {
if y > 10 {
return hypergeometricSmall(M, N, y, coins)
}
return hypergeometricBig(M, N, y, coins)
}
func hypergeometricBig(M int64, N int64, y int64, coins []byte) int64 {
D1 := 1.7155277699214135
D2 := 0.8989161620588988
// # long mingoodbad, maxgoodbad, popsize, m, d9;
// # double d4, d5, d6, d7, d8, d10, d11;
// # long Z;
// # double T, W, X, Y;
good := M
bad := N - M
sample := y
mingoodbad := min(good, bad)
popsize := N
maxgoodbad := max(good, bad)
m := min(sample, popsize-sample)
d4 := float64(mingoodbad) / float64(popsize)
d5 := 1.0 - d4
d6 := float64(m)*d4 + 0.5
d7 := math.Sqrt(float64(popsize-m)*float64(sample)*d4*d5/float64(popsize-1) + 0.5)
d8 := D1*d7 + D2
d9 := (m + 1) * (mingoodbad + 1) / (popsize + 2)
d10 := loggam(d9+1) + loggam(mingoodbad-d9+1) + loggam(m-d9+1) + loggam(maxgoodbad-m+d9+1)
d11 := min(float64(min(m, mingoodbad)+1), math.Floor(d6+16*d7))
// # 16 for 16-decimal-digit precision in D1 and D2
var Z int64
for {
X := uniformRand(coins)
Y := uniformRand(coins)
W := d6 + d8*(Y-0.5)/X
// # fast rejection:
if W < 0.0 || W >= d11 {
continue
}
Z = int64(math.Floor(W))
T := d10 - (loggam(Z+1) + loggam(mingoodbad-Z+1) + loggam(m-Z+1) + loggam(maxgoodbad-m+Z+1))
// # fast acceptance:
if (X*(4.0-X) - 3.0) <= T {
break
}
// # fast rejection:
if X*(X-T) >= 1 {
continue
}
// # acceptance:
if 2.0*math.Log(X) <= T {
break
}
}
// # this is a correction to HRUA* by Ivan Frohne in rv.py
if good > bad {
Z = m - Z
}
// # another fix from rv.py to allow sample to exceed popsize/2
if m < sample {
Z = good - Z
}
return Z
}
+135
View File
@@ -0,0 +1,135 @@
package ope
import (
"encoding/binary"
"math"
"math/big"
"xdx.jelly/xgcl/grand"
)
// uniformRand returns a random number in [0, 1]
func uniformRand() float64 {
b := make([]byte, 4)
_, _ = grand.GenerateRandom(b)
return float64(binary.LittleEndian.Uint32(b)) / float64(uint64(1)<<32-1)
}
func minBig(a, b *big.Int) *big.Int {
if a.Cmp(b) < 0 {
return a
}
return b
}
func maxBig(a, b *big.Int) *big.Int {
if a.Cmp(b) < 0 {
return b
}
return a
}
func floatFromBig(a *big.Int) float64 {
r, _ := a.Float64()
return r
}
// hypergeometric10 returns a sample in hypergeometric distribution.
// Pr[X=x] = choose(y, x) * choose(N-y, M-x) / choose(N, M)
func hypergeometricSmall(M *big.Int, N *big.Int, y *big.Int) *big.Int {
d1 := new(big.Int).Sub(N, y)
nm := new(big.Int).Sub(N, M)
d2 := minBig(M, nm)
Y := new(big.Int).Set(d2)
K := y.Int64()
for Y.Sign() > 0 && K != 0 {
U := uniformRand()
Y.Sub(Y, big.NewInt(int64(U+floatFromBig(Y)/(floatFromBig(d1)+float64(K)))))
K--
}
Z := new(big.Int).Sub(d2, Y)
if nm.Cmp(M) < 0 {
Z.Sub(y, Z)
}
return Z
}
func loggam(x int64) float64 {
return math.Log(math.Gamma(float64(x)))
}
func hypergeometric(M *big.Int, N *big.Int, y *big.Int) *big.Int {
if y.BitLen() < 4 {
return hypergeometricSmall(M, N, y)
}
return hypergeometricBig(M, N, y)
}
func hypergeometricBig(M *big.Int, N *big.Int, y *big.Int) *big.Int {
D1 := 1.7155277699214135
D2 := 0.8989161620588988
// # long mingoodbad, maxgoodbad, popsize, m, d9;
// # double d4, d5, d6, d7, d8, d10, d11;
// # long Z;
// # double T, W, X, Y;
good := M
bad := new(big.Int).Sub(N, M)
sample := y
mingoodbad := minBig(good, bad)
popsize := N
maxgoodbad := maxBig(good, bad)
m := minBig(sample, new(big.Int).Sub(popsize, sample))
d4 := floatFromBig(mingoodbad) / floatFromBig(popsize)
d5 := 1.0 - d4
d6 := floatFromBig(m)*d4 + 0.5
d7 := math.Sqrt(floatFromBig(new(big.Int).Sub(popsize, m))*floatFromBig(sample)*d4*d5/floatFromBig(new(big.Int).Sub(popsize, one)) + 0.5)
d8 := D1*d7 + D2
d9 := floatFromBig(new(big.Int).Add(m, one).Mul(new(big.Int).Add(mingoodbad, one))) / (popsize + 2)
d10 := loggam(d9+1) + loggam(mingoodbad-d9+1) + loggam(m-d9+1) + loggam(maxgoodbad-m+d9+1)
d11 := min(float64(min(m, mingoodbad)+1), math.Floor(d6+16*d7))
// # 16 for 16-decimal-digit precision in D1 and D2
var Z int64
for {
X := uniformRand()
Y := uniformRand()
W := d6 + d8*(Y-0.5)/X
// # fast rejection:
if W < 0.0 || W >= d11 {
continue
}
Z = int64(math.Floor(W))
T := d10 - (loggam(Z+1) + loggam(mingoodbad-Z+1) + loggam(m-Z+1) + loggam(maxgoodbad-m+Z+1))
// # fast acceptance:
if (X*(4.0-X) - 3.0) <= T {
break
}
// # fast rejection:
if X*(X-T) >= 1 {
continue
}
// # acceptance:
if 2.0*math.Log(X) <= T {
break
}
}
// # this is a correction to HRUA* by Ivan Frohne in rv.py
if good > bad {
Z = m - Z
}
// # another fix from rv.py to allow sample to exceed popsize/2
if m < sample {
Z = good - Z
}
return Z
}
+103
View File
@@ -0,0 +1,103 @@
// package ope is the Order-Preserving Encryption
// 保序加密, 即保持密文的顺序. 可用于数据库.
package ope
func bytesToCoins(b []byte) []byte {
res := make([]byte, 0, 8*len(b))
for _, x := range b {
for i := 0; i < 8; i++ {
res = append(res, x&1)
x >>= 1
}
}
return res
}
// ValueRange reprents the range [start, end]
type ValueRange struct {
start int64
end int64
}
func (v *ValueRange) Set(start int64, end int64) *ValueRange {
if start > end {
panic("not a valid value range, start must no greater then end")
}
v.start = start
v.end = end
return v
}
func (v *ValueRange) Size() int64 {
return v.end - v.start + 1
}
func (v *ValueRange) Contains(n int64) bool {
return n >= v.start && n <= v.end
}
// func (v *ValueRange) BitSize() int64 {
// n := v.Size()
// return int64(n.BitLen())
// }
func (v *ValueRange) Clone() *ValueRange {
return new(ValueRange).Set(v.start, v.end)
}
// UniformRand returns a number in range uniformly
func UniformRand(M *ValueRange, coins []byte) int64 {
start, end := M.start, M.end
for i := 0; end > start; i++ {
mid := (start + end) / 2
if coins[i] == 0 {
end = mid
} else {
start = mid + 1
}
}
return start
}
// HyperGeometricRand returns a number x in inRange, satisfying the distribution
// Prob[X = x] = HyperGeometric(M, N, y)
func HyperGeometricRand(M, N *ValueRange, y int64, coins []byte) int64 {
m := M.Size()
n := N.Size()
nsampleIndex := y - N.start + 1
if M == N {
return M.start + nsampleIndex - 1
}
inSampleNum := hypergeometric(m, n, nsampleIndex, coins)
if inSampleNum == 0 {
return M.start
} else {
return M.start + inSampleNum - 1
}
}
type OPE struct {
}
func (o *OPE) Init(key []byte) {
}
func (o *OPE) Encrypt() {
}
func (o *OPE) Decrypts() {
}
func tapeGen(key []byte, data []byte) []byte {
return nil
}
func sampleUniform() {
}
+87
View File
@@ -0,0 +1,87 @@
package ope
import (
"fmt"
"math"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"xdx.jelly/xgcl/grand"
)
func choose(y int64, x int64) float64 {
a, _ := new(big.Int).MulRange(y-x+1, y).Float64()
b, _ := new(big.Int).MulRange(2, x).Float64()
return a / b
}
func hyperProb(x int64, M int64, N int64, y int64) float64 {
return choose(M, x) * choose(N-M, y-x) / choose(N, y)
}
func TestUniform(t *testing.T) {
stats := make([]int, 10)
for i := 0; i < 1000000; i++ {
f := uniformRand(bytesToCoins(grand.GetRandom(4)))
stats[int(f*10)]++
}
fmt.Println(stats)
}
func TestHyperSmall(t *testing.T) {
M := int64(4)
N := int64(10)
y := int64(5)
stats := make([]float64, min(y, M)+1)
wants := make([]float64, min(y, M)+1)
count := 1000000
for i := 0; i < count; i++ {
x := hypergeometricSmall(M, N, y, bytesToCoins(grand.GetRandom(4)))
stats[x] += 1
}
for i := 0; i < len(stats); i++ {
stats[i] = stats[i] / float64(count)
wants[i] = hyperProb(int64(i), M, N, y)
}
if true {
for i := 0; i < len(stats); i++ {
fmt.Printf("%.2f ", stats[i])
}
fmt.Println()
for i := 0; i < len(stats); i++ {
fmt.Printf("%.2f ", wants[i])
}
fmt.Println()
}
epsilon := 0.001
for i := 0; i < len(stats); i++ {
assert.Less(t, math.Abs(stats[i]-wants[i]), epsilon)
}
}
func TestHyperBig(t *testing.T) {
M := int64(30)
N := int64(100)
y := int64(51)
stats := make([]float64, min(y, M)+1)
wants := make([]float64, min(y, M)+1)
count := 1000000
for i := 0; i < count; i++ {
x := hypergeometricBig(M, N, y, bytesToCoins(grand.GetRandom(4)))
stats[x] += 1
}
for i := 0; i < len(stats); i++ {
stats[i] = stats[i] / float64(count)
wants[i] = hyperProb(int64(i), M, N, y)
}
epsilon := 0.001
for i := 0; i < len(stats); i++ {
assert.Less(t, math.Abs(stats[i]-wants[i]), epsilon)
}
}