Skip to content

Commit

Permalink
test: start fixing integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
gbotrel committed Sep 23, 2024
1 parent 5261c89 commit fccce32
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 26 deletions.
5 changes: 5 additions & 0 deletions field/asm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# generated by integration tests
element_2w_amd64.h
element_3w_amd64.h
element_7w_amd64.h
element_8w_amd64.h
7 changes: 4 additions & 3 deletions field/generator/asm/amd64/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package amd64
import (
"fmt"
"io"
"path/filepath"
"strings"

"github.com/consensys/bavard"
Expand Down Expand Up @@ -161,7 +162,7 @@ func (f *FFAmd64) mu() string {
return "mu<>(SB)"
}

func GenerateFieldWrapper(w io.Writer, F *config.FieldConfig) error {
func GenerateFieldWrapper(w io.Writer, F *config.FieldConfig, asmDir string) error {
// for each field we generate the defines for the modulus and the montgomery constant
f := NewFFAmd64(w, F.NbWords)

Expand All @@ -172,8 +173,8 @@ func GenerateFieldWrapper(w io.Writer, F *config.FieldConfig) error {
f.WriteLn(fmt.Sprintf("#define q%d $%#016x", i, F.Q[i]))
}

toInclude := fmt.Sprintf("../../../field/asm/element_%dw_amd64.h", F.NbWords)
f.WriteLn(fmt.Sprintf("\n#include \"%s\"\n", toInclude))
toInclude := fmt.Sprintf("element_%dw_amd64.h", F.NbWords)
f.WriteLn(fmt.Sprintf("\n#include \"%s\"\n", filepath.Join(asmDir, toInclude)))

f.GenerateFieldDefines(F)

Expand Down
4 changes: 2 additions & 2 deletions field/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
//
// fp, _ = config.NewField("fp", "Element", fpModulus")
// generator.GenerateFF(fp, filepath.Join(baseDir, "fp"))
func GenerateFF(F *config.FieldConfig, outputDir string) error {
func GenerateFF(F *config.FieldConfig, outputDir, asmDir string) error {
// source file templates
sourceFiles := []string{
element.Base,
Expand Down Expand Up @@ -137,7 +137,7 @@ func GenerateFF(F *config.FieldConfig, outputDir string) error {

_, _ = io.WriteString(f, "// +build !purego\n")

if err := amd64.GenerateFieldWrapper(f, F); err != nil {
if err := amd64.GenerateFieldWrapper(f, F, asmDir); err != nil {
_ = f.Close()
return err
}
Expand Down
62 changes: 46 additions & 16 deletions field/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

field "github.com/consensys/gnark-crypto/field/generator/config"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)

// integration test will create modulus for various field sizes and run tests

const rootDir = "integration_test"
const asmDir = "../asm"

func TestIntegration(t *testing.T) {
assert := require.New(t)

os.RemoveAll(rootDir)
err := os.MkdirAll(rootDir, 0700)
defer os.RemoveAll(rootDir)
if err != nil {
t.Fatal(err)
}
assert.NoError(err)

var bits []int
for i := 64; i <= 448; i += 64 {
Expand Down Expand Up @@ -78,27 +82,53 @@ func TestIntegration(t *testing.T) {
// generate field
childDir := filepath.Join(rootDir, elementName)
fIntegration, err = field.NewFieldConfig("integration", elementName, modulus, false)
if err != nil {
t.Fatal(elementName, err)
}
if err = GenerateFF(fIntegration, childDir); err != nil {
t.Fatal(elementName, err)
}
assert.NoError(err)
assert.NoError(GenerateFF(fIntegration, childDir, "../../../asm"))
}

assert.NoError(GenerateCommonASM(2, asmDir))
assert.NoError(GenerateCommonASM(3, asmDir))
assert.NoError(GenerateCommonASM(7, asmDir))
assert.NoError(GenerateCommonASM(8, asmDir))

// run go test
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
packageDir := filepath.Join(wd, rootDir) + string(filepath.Separator) + "..."
cmd := exec.Command("go", "test", packageDir)
if err := cmd.Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
t.Fatal(string(exitErr.Stderr))
} else {
t.Fatal(err)
packageDir := filepath.Join(wd, rootDir) // + string(filepath.Separator) + "..."

// list all subdirectories in package dir
var subDirs []string
err = filepath.Walk(packageDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && path != packageDir {
subDirs = append(subDirs, path)
}
return nil
})
if err != nil {
t.Fatal(err)
}

errGroup := errgroup.Group{}

for _, subDir := range subDirs {
// run go test in parallel
errGroup.Go(func() error {
cmd := exec.Command("go", "test")
cmd.Dir = subDir
var stdouterr strings.Builder
cmd.Stdout = &stdouterr
cmd.Stderr = &stdouterr
if err := cmd.Run(); err != nil {
return fmt.Errorf("go test failed, output:\n%s\n%s", stdouterr.String(), err)
}
return nil
})
}

if err := errGroup.Wait(); err != nil {
t.Fatal(err)
}

}
3 changes: 2 additions & 1 deletion field/goff/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func cmdGenerate(cmd *cobra.Command, args []string) {
fmt.Printf("\n%s\n", err.Error())
os.Exit(-1)
}
if err := generator.GenerateFF(F, fOutputDir); err != nil {
// TODO @gbotrel this is broken with new asm dir.
if err := generator.GenerateFF(F, fOutputDir, "FIXME"); err != nil {
fmt.Printf("\n%s\n", err.Error())
os.Exit(-1)
}
Expand Down
2 changes: 1 addition & 1 deletion field/goldilocks/internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
if err != nil {
panic(err)
}
if err := generator.GenerateFF(goldilocks, "../"); err != nil {
if err := generator.GenerateFF(goldilocks, "../", ""); err != nil {
panic(err)
}
fmt.Println("successfully generated goldilocks field")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.26.0
golang.org/x/sync v0.1.0
golang.org/x/sys v0.24.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
6 changes: 3 additions & 3 deletions internal/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func main() {

config.Curves[i] = conf
}
asmDir := filepath.Join(baseDir, "field", "asm")
for nbWords := range m {
asmDir := filepath.Join(baseDir, "field", "asm")
assertNoError(generator.GenerateCommonASM(nbWords, asmDir))
}

Expand All @@ -76,8 +76,8 @@ func main() {

conf.FpUnusedBits = 64 - (conf.Fp.NbBits % 64)

assertNoError(generator.GenerateFF(conf.Fr, filepath.Join(curveDir, "fr")))
assertNoError(generator.GenerateFF(conf.Fp, filepath.Join(curveDir, "fp")))
assertNoError(generator.GenerateFF(conf.Fr, filepath.Join(curveDir, "fr"), filepath.Join("..", asmDir)))
assertNoError(generator.GenerateFF(conf.Fp, filepath.Join(curveDir, "fp"), filepath.Join("..", asmDir)))

// generate ecdsa
assertNoError(ecdsa.Generate(conf, curveDir, bgen))
Expand Down

0 comments on commit fccce32

Please sign in to comment.