|
| 1 | +// Package polynomial provides representations of polynomials over the scalars |
| 2 | +// of a group. |
| 3 | +package polynomial |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/cloudflare/circl/group" |
| 9 | +) |
| 10 | + |
| 11 | +// Polynomial stores a polynomial over the set of scalars of a group. |
| 12 | +type Polynomial struct { |
| 13 | + // Internal representation is in polynomial basis: |
| 14 | + // Thus, |
| 15 | + // p(x) = \sum_i^k c[i] x^i, |
| 16 | + // where k = len(c)-1 is the degree of the polynomial. |
| 17 | + c []group.Scalar |
| 18 | +} |
| 19 | + |
| 20 | +// New creates a new polynomial given its coefficients in ascending order. |
| 21 | +// Thus, |
| 22 | +// p(x) = \sum_i^k c[i] x^i, |
| 23 | +// where k = len(c)-1 is the degree of the polynomial. |
| 24 | +func New(coeffs []group.Scalar) Polynomial { |
| 25 | + if len(coeffs) == 0 { |
| 26 | + panic("polynomial: invalid length") |
| 27 | + } |
| 28 | + return Polynomial{coeffs} |
| 29 | +} |
| 30 | + |
| 31 | +func (p Polynomial) Degree() uint { return uint(len(p.c) - 1) } |
| 32 | + |
| 33 | +func (p Polynomial) Evaluate(x group.Scalar) group.Scalar { |
| 34 | + l := len(p.c) |
| 35 | + px := p.c[l-1].Copy() |
| 36 | + for i := l - 2; i >= 0; i-- { |
| 37 | + px.Mul(px, x) |
| 38 | + px.Add(px, p.c[i]) |
| 39 | + } |
| 40 | + return px |
| 41 | +} |
| 42 | + |
| 43 | +// LagrangePolynomial stores a Lagrange polynomial over the set of scalars of a group. |
| 44 | +type LagrangePolynomial struct { |
| 45 | + // Internal representation is in Lagrange basis: |
| 46 | + // Thus, |
| 47 | + // p(x) = \sum_i^k y[i] L_j(x), where k is the degree of the polynomial, |
| 48 | + // L_j(x) = \prod_i^k (x-x[i])/(x[j]-x[i]), |
| 49 | + // y[i] = p(x[i]), and |
| 50 | + // all x[i] are different. |
| 51 | + g group.Group |
| 52 | + x, y []group.Scalar |
| 53 | +} |
| 54 | + |
| 55 | +// NewLagrangePolynomial creates a polynomial in Lagrange basis given a list |
| 56 | +// of nodes (x) and values (y), such that: |
| 57 | +// p(x) = \sum_i^k y[i] L_j(x), where k is the degree of the polynomial, |
| 58 | +// L_j(x) = \prod_i^k (x-x[i])/(x[j]-x[i]), |
| 59 | +// y[i] = p(x[i]), and |
| 60 | +// all x[i] are different. |
| 61 | +// It panics if one of these conditions does not hold. |
| 62 | +func NewLagrangePolynomial(g group.Group, x, y []group.Scalar) LagrangePolynomial { |
| 63 | + if len(x) != len(y) { |
| 64 | + panic("lagrange: invalid length") |
| 65 | + } |
| 66 | + if !isAllDifferent(x) { |
| 67 | + panic("lagrange: nodes must be different") |
| 68 | + } |
| 69 | + |
| 70 | + return LagrangePolynomial{g, x, y} |
| 71 | +} |
| 72 | + |
| 73 | +func (l LagrangePolynomial) Degree() uint { return uint(len(l.x) - 1) } |
| 74 | + |
| 75 | +func (l LagrangePolynomial) Evaluate(x group.Scalar) group.Scalar { |
| 76 | + px := l.g.NewScalar() |
| 77 | + tmp := l.g.NewScalar() |
| 78 | + for i := range l.x { |
| 79 | + LjX := baseRatio(uint(i), l.x, x) |
| 80 | + tmp.Mul(l.y[i], LjX) |
| 81 | + px.Add(px, tmp) |
| 82 | + } |
| 83 | + |
| 84 | + return px |
| 85 | +} |
| 86 | + |
| 87 | +// LagrangeBase returns the j-th Lagrange polynomial base evaluated at x. |
| 88 | +// Thus, L_j(x) = \prod (x - x[i]) / (x[j] - x[i]) for 0 <= i < k, and i != j. |
| 89 | +func LagrangeBase(jth uint, xi []group.Scalar, x group.Scalar) group.Scalar { |
| 90 | + if jth >= uint(len(xi)) { |
| 91 | + panic("lagrange: invalid index") |
| 92 | + } |
| 93 | + return baseRatio(jth, xi, x) |
| 94 | +} |
| 95 | + |
| 96 | +func baseRatio(jth uint, xi []group.Scalar, x group.Scalar) group.Scalar { |
| 97 | + num := x.Copy() |
| 98 | + num.SetUint64(1) |
| 99 | + den := x.Copy() |
| 100 | + den.SetUint64(1) |
| 101 | + |
| 102 | + tmp := x.Copy() |
| 103 | + for i := range xi { |
| 104 | + if uint(i) != jth { |
| 105 | + num.Mul(num, tmp.Sub(x, xi[i])) |
| 106 | + den.Mul(den, tmp.Sub(xi[jth], xi[i])) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return num.Mul(num, den.Inv(den)) |
| 111 | +} |
| 112 | + |
| 113 | +func isAllDifferent(x []group.Scalar) bool { |
| 114 | + m := make(map[string]struct{}) |
| 115 | + for i := range x { |
| 116 | + k := x[i].(fmt.Stringer).String() |
| 117 | + if _, exists := m[k]; exists { |
| 118 | + return false |
| 119 | + } |
| 120 | + m[k] = struct{}{} |
| 121 | + } |
| 122 | + return true |
| 123 | +} |
0 commit comments