github.com/consensys/gnark@v0.11.0/test/assert_options.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/consensys/gnark-crypto/ecc"
     7  	"github.com/consensys/gnark/backend"
     8  	"github.com/consensys/gnark/backend/solidity"
     9  	"github.com/consensys/gnark/constraint/solver"
    10  	"github.com/consensys/gnark/frontend"
    11  )
    12  
    13  // TestingOption defines option for altering the behavior of Assert methods.
    14  // See the descriptions of functions returning instances of this type for
    15  // particular options.
    16  type TestingOption func(*testingConfig) error
    17  
    18  type testingConfig struct {
    19  	profile
    20  	solverOpts   []solver.Option
    21  	proverOpts   []backend.ProverOption
    22  	verifierOpts []backend.VerifierOption
    23  	compileOpts  []frontend.CompileOption
    24  	solidityOpts []solidity.ExportOption
    25  
    26  	validAssignments   []frontend.Circuit
    27  	invalidAssignments []frontend.Circuit
    28  }
    29  
    30  // default options
    31  func (assert *Assert) options(opts ...TestingOption) testingConfig {
    32  	var opt testingConfig
    33  
    34  	// default options;
    35  	// go test -short 					--> testEngineOnly
    36  	// go test 							--> constraintOnlyProfile
    37  	// go test -tags=prover_checks 		--> proverOnlyProfile
    38  	// go test -tags=release_checks 	--> releaseProfile
    39  
    40  	if releaseTestFlag {
    41  		opt.profile = releaseChecks
    42  	} else if proverTestFlag {
    43  		opt.profile = proverChecks
    44  	} else if testing.Short() {
    45  		opt.profile = testEngineChecks
    46  	} else {
    47  		opt.profile = constraintSolverChecks
    48  	}
    49  
    50  	// apply user provided options.
    51  	for _, option := range opts {
    52  		err := option(&opt)
    53  		assert.NoError(err, "parsing TestingOption")
    54  	}
    55  
    56  	return opt
    57  }
    58  
    59  // WithValidAssignment is a testing option which adds a valid assignment
    60  func WithValidAssignment(validAssignment frontend.Circuit) TestingOption {
    61  	return func(opt *testingConfig) error {
    62  		opt.validAssignments = append(opt.validAssignments, validAssignment)
    63  		return nil
    64  	}
    65  }
    66  
    67  // WithInvalidAssignment is a testing option which adds an invalid assignment
    68  func WithInvalidAssignment(invalidAssignment frontend.Circuit) TestingOption {
    69  	return func(opt *testingConfig) error {
    70  		opt.invalidAssignments = append(opt.invalidAssignments, invalidAssignment)
    71  		return nil
    72  	}
    73  }
    74  
    75  // WithBackends is testing option which restricts the backends the assertions are
    76  // run. When not given, runs on all implemented backends.
    77  func WithBackends(b backend.ID, backends ...backend.ID) TestingOption {
    78  	return func(opt *testingConfig) error {
    79  		opt.backends = []backend.ID{b}
    80  		opt.backends = append(opt.backends, backends...)
    81  		return nil
    82  	}
    83  }
    84  
    85  // WithCurves is a testing option which restricts the curves the assertions are
    86  // run. When not given, runs on all implemented curves.
    87  func WithCurves(c ecc.ID, curves ...ecc.ID) TestingOption {
    88  	return func(opt *testingConfig) error {
    89  		opt.curves = []ecc.ID{c}
    90  		opt.curves = append(opt.curves, curves...)
    91  		return nil
    92  	}
    93  }
    94  
    95  // NoSerializationChecks is a testing option which disables serialization checks,
    96  // even when the build tag "release_checks" is set.
    97  func NoSerializationChecks() TestingOption {
    98  	return func(opt *testingConfig) error {
    99  		opt.checkSerialization = false
   100  		return nil
   101  	}
   102  }
   103  
   104  // NoFuzzing is a testing option which disables fuzzing tests,
   105  // even when the build tag "release_checks" is set.
   106  func NoFuzzing() TestingOption {
   107  	return func(opt *testingConfig) error {
   108  		opt.fuzzing = false
   109  		return nil
   110  	}
   111  }
   112  
   113  // NoProverChecks is a testing option which disables prover checks,
   114  // even when the build tag "prover_checks" or "release_checks" are set.
   115  func NoProverChecks() TestingOption {
   116  	return func(opt *testingConfig) error {
   117  		opt.checkProver = false
   118  		return nil
   119  	}
   120  }
   121  
   122  // NoTestEngine is a testing option which disables test engine checks
   123  func NoTestEngine() TestingOption {
   124  	return func(opt *testingConfig) error {
   125  		opt.skipTestEngine = true
   126  		return nil
   127  	}
   128  }
   129  
   130  // NoSolidityChecks is a testing option which disables solidity checks,
   131  // even when the build tags "solccheck" and "release_checks" are set.
   132  //
   133  // When the tags are set; this requires gnark-solidity-checker to be installed, which in turns
   134  // requires solc and abigen to be reachable in the PATH.
   135  //
   136  // See https://github.com/ConsenSys/gnark-solidity-checker for more details.
   137  func NoSolidityChecks() TestingOption {
   138  	return func(opt *testingConfig) error {
   139  		opt.checkSolidity = false
   140  		return nil
   141  	}
   142  }
   143  
   144  // WithProverOpts is a testing option which uses the given proverOpts when
   145  // calling backend.Prover, backend.ReadAndProve and backend.IsSolved methods in
   146  // assertions.
   147  func WithProverOpts(proverOpts ...backend.ProverOption) TestingOption {
   148  	return func(opt *testingConfig) error {
   149  		opt.proverOpts = proverOpts
   150  		return nil
   151  	}
   152  }
   153  
   154  // WithSolverOpts is a testing option which uses the given solverOpts when
   155  // calling constraint system solver.
   156  func WithSolverOpts(solverOpts ...solver.Option) TestingOption {
   157  	return func(opt *testingConfig) error {
   158  		opt.proverOpts = append(opt.proverOpts, backend.WithSolverOptions(solverOpts...))
   159  		opt.solverOpts = solverOpts
   160  		return nil
   161  	}
   162  }
   163  
   164  // WithCompileOpts is a testing option which uses the given compileOpts when
   165  // calling frontend.Compile in assertions.
   166  func WithCompileOpts(compileOpts ...frontend.CompileOption) TestingOption {
   167  	return func(opt *testingConfig) error {
   168  		opt.compileOpts = compileOpts
   169  		return nil
   170  	}
   171  }
   172  
   173  // WithVerifierOpts is a testing option which uses the given verifierOpts when
   174  // calling backend.Verify method.
   175  func WithVerifierOpts(verifierOpts ...backend.VerifierOption) TestingOption {
   176  	return func(tc *testingConfig) error {
   177  		tc.verifierOpts = append(tc.verifierOpts, verifierOpts...)
   178  		return nil
   179  	}
   180  }
   181  
   182  // WithSolidityExportOptions is a testing option which uses the given solidityOpts when
   183  // calling ExportSolidity method on the verification key.
   184  func WithSolidityExportOptions(solidityOpts ...solidity.ExportOption) TestingOption {
   185  	return func(tc *testingConfig) error {
   186  		tc.solidityOpts = solidityOpts
   187  		return nil
   188  	}
   189  }