github.com/consensys/gnark-crypto@v0.14.0/field/generator/generator_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  package generator
    16  
    17  import (
    18  	"crypto/rand"
    19  	"fmt"
    20  	"math/big"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	field "github.com/consensys/gnark-crypto/field/generator/config"
    27  )
    28  
    29  // integration test will create modulus for various field sizes and run tests
    30  
    31  const rootDir = "integration_test"
    32  
    33  func TestIntegration(t *testing.T) {
    34  	os.RemoveAll(rootDir)
    35  	err := os.MkdirAll(rootDir, 0700)
    36  	defer os.RemoveAll(rootDir)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	var bits []int
    42  	for i := 64; i <= 448; i += 64 {
    43  		bits = append(bits, i-3, i-2, i-1, i, i+1)
    44  	}
    45  
    46  	moduli := make(map[string]string)
    47  	for _, i := range bits {
    48  		var q *big.Int
    49  		var nbWords int
    50  		if i%64 == 0 {
    51  			q, _ = rand.Prime(rand.Reader, i)
    52  			moduli[fmt.Sprintf("e_cios_%04d", i)] = q.String()
    53  		} else {
    54  			for {
    55  				q, _ = rand.Prime(rand.Reader, i)
    56  				nbWords = len(q.Bits())
    57  				const B = (^uint64(0) >> 1) - 1
    58  				if uint64(q.Bits()[nbWords-1]) <= B {
    59  					break
    60  				}
    61  			}
    62  			moduli[fmt.Sprintf("e_nocarry_%04d", i)] = q.String()
    63  		}
    64  	}
    65  
    66  	moduli["forty_seven"] = "47"
    67  	moduli["small"] = "9459143039767"
    68  	moduli["small_without_no_carry"] = "18446744073709551557" // 64bits
    69  
    70  	moduli["e_secp256k1"] = "115792089237316195423570985008687907853269984665640564039457584007908834671663"
    71  
    72  	// JUST fails to be nocarry -- only the following two can occur for < 3000 bits
    73  	moduli["e_nocarry_edge_0127"] = "170141183460469231731687303715884105727"
    74  	moduli["e_nocarry_edge_1279"] = "10407932194664399081925240327364085538615262247266704805319112350403608059673360298012239441732324184842421613954281007791383566248323464908139906605677320762924129509389220345773183349661583550472959420547689811211693677147548478866962501384438260291732348885311160828538416585028255604666224831890918801847068222203140521026698435488732958028878050869736186900714720710555703168729087"
    75  
    76  	for elementName, modulus := range moduli {
    77  		var fIntegration *field.FieldConfig
    78  		// generate field
    79  		childDir := filepath.Join(rootDir, elementName)
    80  		fIntegration, err = field.NewFieldConfig("integration", elementName, modulus, false)
    81  		if err != nil {
    82  			t.Fatal(elementName, err)
    83  		}
    84  		if err = GenerateFF(fIntegration, childDir); err != nil {
    85  			t.Fatal(elementName, err)
    86  		}
    87  	}
    88  
    89  	// run go test
    90  	wd, err := os.Getwd()
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	packageDir := filepath.Join(wd, rootDir) + string(filepath.Separator) + "..."
    95  	cmd := exec.Command("go", "test", packageDir)
    96  	if err := cmd.Run(); err != nil {
    97  		if exitErr, ok := err.(*exec.ExitError); ok {
    98  			t.Fatal(string(exitErr.Stderr))
    99  		} else {
   100  			t.Fatal(err)
   101  		}
   102  	}
   103  
   104  }