-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from ivokub/hint-registry
Add hint registry
- Loading branch information
Showing
12 changed files
with
204 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package hint | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
"sync" | ||
|
||
"github.com/consensys/gnark-crypto/ecc" | ||
) | ||
|
||
var initBuiltinOnce sync.Once | ||
|
||
func init() { | ||
initBuiltinOnce.Do(func() { | ||
IsZero = builtinIsZero | ||
Register(IsZero) | ||
IthBit = builtinIthBit | ||
Register(IthBit) | ||
}) | ||
} | ||
|
||
// The package provides the following built-in hint functions. All built-in hint | ||
// functions are registered in the registry. | ||
var ( | ||
// IsZero computes the value 1 - a^(modulus-1) for the single input a. This | ||
// corresponds to checking if a == 0 (for which the function returns 1) or a | ||
// != 0 (for which the function returns 0). | ||
IsZero Function | ||
|
||
// IthBit returns the i-tb bit the input. The function expects exactly two | ||
// integer inputs i and n, takes the little-endian bit representation of n and | ||
// returns its i-th bit. | ||
IthBit Function | ||
) | ||
|
||
func builtinIsZero(curveID ecc.ID, inputs []*big.Int, result *big.Int) error { | ||
if len(inputs) != 1 { | ||
return errors.New("IsZero expects one input") | ||
} | ||
|
||
// get fr modulus | ||
q := curveID.Info().Fr.Modulus() | ||
|
||
// save input | ||
result.Set(inputs[0]) | ||
|
||
// reuse input to compute q - 1 | ||
qMinusOne := inputs[0].SetUint64(1) | ||
qMinusOne.Sub(q, qMinusOne) | ||
|
||
// result = 1 - input**(q-1) | ||
result.Exp(result, qMinusOne, q) | ||
inputs[0].SetUint64(1) | ||
result.Sub(inputs[0], result).Mod(result, q) | ||
|
||
return nil | ||
} | ||
|
||
func builtinIthBit(_ ecc.ID, inputs []*big.Int, result *big.Int) error { | ||
if len(inputs) != 2 { | ||
return errors.New("ithBit expects 2 inputs; inputs[0] == value, inputs[1] == bit position") | ||
} | ||
if !inputs[1].IsUint64() { | ||
result.SetUint64(0) | ||
return nil | ||
} | ||
|
||
result.SetUint64(uint64(inputs[0].Bit(int(inputs[1].Uint64())))) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package hint | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
var registry = make(map[ID]Function) | ||
var registryM sync.RWMutex | ||
|
||
// Register registers a hint function in the global registry. All registered | ||
// hint functions can be retrieved with a call to GetAll(). It is an error to | ||
// register a single function twice and results in a panic. | ||
func Register(hintFn Function) { | ||
registryM.Lock() | ||
defer registryM.Unlock() | ||
key := UUID(hintFn) | ||
if _, ok := registry[key]; ok { | ||
panic(fmt.Sprintf("function %d registered twice", key)) | ||
} | ||
registry[key] = hintFn | ||
} | ||
|
||
// GetAll returns all registered hint functions. | ||
func GetAll() []Function { | ||
registryM.RLock() | ||
defer registryM.RUnlock() | ||
ret := make([]Function, 0, len(registry)) | ||
for _, v := range registry { | ||
ret = append(ret, v) | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters