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