github.com/consensys/gnark-crypto@v0.14.0/ecc/bls12-381/fr/polynomial/polynomial_test.go (about) 1 // Copyright 2020 Consensys Software Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Code generated by consensys/gnark-crypto DO NOT EDIT 16 17 package polynomial 18 19 import ( 20 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" 21 "github.com/stretchr/testify/assert" 22 "math/big" 23 "testing" 24 ) 25 26 func TestPolynomialEval(t *testing.T) { 27 28 // build polynomial 29 f := make(Polynomial, 20) 30 for i := 0; i < 20; i++ { 31 f[i].SetOne() 32 } 33 34 // random value 35 var point fr.Element 36 point.SetRandom() 37 38 // compute manually f(val) 39 var expectedEval, one, den fr.Element 40 var expo big.Int 41 one.SetOne() 42 expo.SetUint64(20) 43 expectedEval.Exp(point, &expo). 44 Sub(&expectedEval, &one) 45 den.Sub(&point, &one) 46 expectedEval.Div(&expectedEval, &den) 47 48 // compute purported evaluation 49 purportedEval := f.Eval(&point) 50 51 // check 52 if !purportedEval.Equal(&expectedEval) { 53 t.Fatal("polynomial evaluation failed") 54 } 55 } 56 57 func TestPolynomialAddConstantInPlace(t *testing.T) { 58 59 // build polynomial 60 f := make(Polynomial, 20) 61 for i := 0; i < 20; i++ { 62 f[i].SetOne() 63 } 64 65 // constant to add 66 var c fr.Element 67 c.SetRandom() 68 69 // add constant 70 f.AddConstantInPlace(&c) 71 72 // check 73 var expectedCoeffs, one fr.Element 74 one.SetOne() 75 expectedCoeffs.Add(&one, &c) 76 for i := 0; i < 20; i++ { 77 if !f[i].Equal(&expectedCoeffs) { 78 t.Fatal("AddConstantInPlace failed") 79 } 80 } 81 } 82 83 func TestPolynomialSubConstantInPlace(t *testing.T) { 84 85 // build polynomial 86 f := make(Polynomial, 20) 87 for i := 0; i < 20; i++ { 88 f[i].SetOne() 89 } 90 91 // constant to sub 92 var c fr.Element 93 c.SetRandom() 94 95 // sub constant 96 f.SubConstantInPlace(&c) 97 98 // check 99 var expectedCoeffs, one fr.Element 100 one.SetOne() 101 expectedCoeffs.Sub(&one, &c) 102 for i := 0; i < 20; i++ { 103 if !f[i].Equal(&expectedCoeffs) { 104 t.Fatal("SubConstantInPlace failed") 105 } 106 } 107 } 108 109 func TestPolynomialScaleInPlace(t *testing.T) { 110 111 // build polynomial 112 f := make(Polynomial, 20) 113 for i := 0; i < 20; i++ { 114 f[i].SetOne() 115 } 116 117 // constant to scale by 118 var c fr.Element 119 c.SetRandom() 120 121 // scale by constant 122 f.ScaleInPlace(&c) 123 124 // check 125 for i := 0; i < 20; i++ { 126 if !f[i].Equal(&c) { 127 t.Fatal("ScaleInPlace failed") 128 } 129 } 130 131 } 132 133 func TestPolynomialAdd(t *testing.T) { 134 135 // build unbalanced polynomials 136 f1 := make(Polynomial, 20) 137 f1Backup := make(Polynomial, 20) 138 for i := 0; i < 20; i++ { 139 f1[i].SetOne() 140 f1Backup[i].SetOne() 141 } 142 f2 := make(Polynomial, 10) 143 f2Backup := make(Polynomial, 10) 144 for i := 0; i < 10; i++ { 145 f2[i].SetOne() 146 f2Backup[i].SetOne() 147 } 148 149 // expected result 150 var one, two fr.Element 151 one.SetOne() 152 two.Double(&one) 153 expectedSum := make(Polynomial, 20) 154 for i := 0; i < 10; i++ { 155 expectedSum[i].Set(&two) 156 } 157 for i := 10; i < 20; i++ { 158 expectedSum[i].Set(&one) 159 } 160 161 // caller is empty 162 var g Polynomial 163 g.Add(f1, f2) 164 if !g.Equal(expectedSum) { 165 t.Fatal("add polynomials fails") 166 } 167 if !f1.Equal(f1Backup) { 168 t.Fatal("side effect, f1 should not have been modified") 169 } 170 if !f2.Equal(f2Backup) { 171 t.Fatal("side effect, f2 should not have been modified") 172 } 173 174 // all operands are distinct 175 _f1 := f1.Clone() 176 _f1.Add(f1, f2) 177 if !_f1.Equal(expectedSum) { 178 t.Fatal("add polynomials fails") 179 } 180 if !f1.Equal(f1Backup) { 181 t.Fatal("side effect, f1 should not have been modified") 182 } 183 if !f2.Equal(f2Backup) { 184 t.Fatal("side effect, f2 should not have been modified") 185 } 186 187 // first operand = caller 188 _f1 = f1.Clone() 189 _f2 := f2.Clone() 190 _f1.Add(_f1, _f2) 191 if !_f1.Equal(expectedSum) { 192 t.Fatal("add polynomials fails") 193 } 194 if !_f2.Equal(f2Backup) { 195 t.Fatal("side effect, _f2 should not have been modified") 196 } 197 198 // second operand = caller 199 _f1 = f1.Clone() 200 _f2 = f2.Clone() 201 _f1.Add(_f2, _f1) 202 if !_f1.Equal(expectedSum) { 203 t.Fatal("add polynomials fails") 204 } 205 if !_f2.Equal(f2Backup) { 206 t.Fatal("side effect, _f2 should not have been modified") 207 } 208 } 209 210 func TestPolynomialText(t *testing.T) { 211 var one, negTwo fr.Element 212 one.SetOne() 213 negTwo.SetInt64(-2) 214 215 p := Polynomial{one, negTwo, one} 216 217 assert.Equal(t, "X² - 2X + 1", p.Text(10)) 218 }