1 Star 0 Fork 0

lqinggang / psiphon-tunnel-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
polynomial.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
package sss
import "io"
// the degree of the polynomial
func degree(p []byte) int {
return len(p) - 1
}
// evaluate the polynomial at the given point
func eval(p []byte, x byte) (result byte) {
// Horner's scheme
for i := 1; i <= len(p); i++ {
result = mul(result, x) ^ p[len(p)-i]
}
return
}
// generates a random n-degree polynomial w/ a given x-intercept
func generate(degree byte, x byte, rand io.Reader) ([]byte, error) {
result := make([]byte, degree+1)
result[0] = x
buf := make([]byte, degree-1)
if _, err := io.ReadFull(rand, buf); err != nil {
return nil, err
}
for i := byte(1); i < degree; i++ {
result[i] = buf[i-1]
}
// the Nth term can't be zero, or else it's a (N-1) degree polynomial
for {
buf = make([]byte, 1)
if _, err := io.ReadFull(rand, buf); err != nil {
return nil, err
}
if buf[0] != 0 {
result[degree] = buf[0]
return result, nil
}
}
}
// an input/output pair
type pair struct {
x, y byte
}
// Lagrange interpolation
func interpolate(points []pair, x byte) (value byte) {
for i, a := range points {
weight := byte(1)
for j, b := range points {
if i != j {
top := x ^ b.x
bottom := a.x ^ b.x
factor := div(top, bottom)
weight = mul(weight, factor)
}
}
value = value ^ mul(weight, a.y)
}
return
}
Go
1
https://gitee.com/lqinggang/psiphon-tunnel-core.git
git@gitee.com:lqinggang/psiphon-tunnel-core.git
lqinggang
psiphon-tunnel-core
psiphon-tunnel-core
v2.0.2

搜索帮助

53164aa7 5694891 3bd8fe86 5694891