-
Notifications
You must be signed in to change notification settings - Fork 26
/
poly.go
40 lines (37 loc) · 1.01 KB
/
poly.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package kzg
import "github.com/protolambda/go-kzg/bls"
// invert the divisor, then multiply
func polyFactorDiv(dst *bls.Fr, a *bls.Fr, b *bls.Fr) {
// TODO: use divmod instead.
var tmp bls.Fr
bls.InvModFr(&tmp, b)
bls.MulModFr(dst, &tmp, a)
}
// Long polynomial division for two polynomials in coefficient form
func polyLongDiv(dividend []bls.Fr, divisor []bls.Fr) []bls.Fr {
a := make([]bls.Fr, len(dividend), len(dividend))
for i := 0; i < len(a); i++ {
bls.CopyFr(&a[i], ÷nd[i])
}
aPos := len(a) - 1
bPos := len(divisor) - 1
diff := aPos - bPos
out := make([]bls.Fr, diff+1, diff+1)
for diff >= 0 {
quot := &out[diff]
polyFactorDiv(quot, &a[aPos], &divisor[bPos])
var tmp, tmp2 bls.Fr
for i := bPos; i >= 0; i-- {
// In steps: a[diff + i] -= b[i] * quot
// tmp = b[i] * quot
bls.MulModFr(&tmp, quot, &divisor[i])
// tmp2 = a[diff + i] - tmp
bls.SubModFr(&tmp2, &a[diff+i], &tmp)
// a[diff + i] = tmp2
bls.CopyFr(&a[diff+i], &tmp2)
}
aPos -= 1
diff -= 1
}
return out
}