Skip to content

Commit

Permalink
make exp_mod work with parametric fields (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBelling authored Jun 28, 2024
1 parent db299ce commit 9c41095
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 2 additions & 3 deletions std/evmprecompiles/05-expmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/math/emulated"
"github.com/consensys/gnark/std/math/emulated/emparams"
)

// Expmod implements [MODEXP] precompile contract at address 0x05.
Expand All @@ -15,10 +14,10 @@ import (
// the actual length of the inputs.
//
// [MODEXP]: https://ethereum.github.io/execution-specs/autoapi/ethereum/paris/vm/precompiled_contracts/expmod/index.html
func Expmod(api frontend.API, base, exp, modulus *emulated.Element[emparams.Mod1e4096]) *emulated.Element[emparams.Mod1e4096] {
func Expmod[P emulated.FieldParams](api frontend.API, base, exp, modulus *emulated.Element[P]) *emulated.Element[P] {
// x^0 = 1
// x mod 0 = 0
f, err := emulated.NewField[emparams.Mod1e4096](api)
f, err := emulated.NewField[P](api)
if err != nil {
panic(fmt.Sprintf("new field: %v", err))
}
Expand Down
19 changes: 19 additions & 0 deletions std/math/emulated/emparams/emparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,22 @@ func (Mod1e512) Modulus() *big.Int {
val, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
return val
}

// Mod1e256 provides type parametrization for emulated aritmetic:
// - limbs: 4
// - limb width: 64 bits
//
// The modulus for type parametrisation is 2^256-1.
//
// This is non-prime modulus. It is mainly targeted for using variable-modulus
// operations (ModAdd, ModMul, ModExp, ModAssertIsEqual) for variable modulus
// arithmetic.
type Mod1e256 struct{}

func (Mod1e256) NbLimbs() uint { return 4 }
func (Mod1e256) BitsPerLimb() uint { return 64 }
func (Mod1e256) IsPrime() bool { return false }
func (Mod1e256) Modulus() *big.Int {
val, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
return val
}

0 comments on commit 9c41095

Please sign in to comment.