mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-02-21 18:37:26 +08:00
* v2.1.0 [omit consensus and adjacent] - this commit will be amended with the full release after the file copy is complete * 2.1.0 main node rollup
27 lines
531 B
Go
27 lines
531 B
Go
package utils
|
|
|
|
import "math/big"
|
|
|
|
// AbsoluteModularMinimumDistance calculates modular distance:
|
|
// min(|a-b|, modulus-|a-b|)
|
|
func AbsoluteModularMinimumDistance(
|
|
targetInt *big.Int,
|
|
keyInt *big.Int,
|
|
modulus *big.Int,
|
|
) *big.Int {
|
|
diff := new(big.Int).Sub(targetInt, keyInt)
|
|
diff.Abs(diff)
|
|
|
|
// Modular complement distance
|
|
modComplement := new(big.Int).Sub(modulus, diff)
|
|
|
|
// Take minimum of two distances
|
|
var dist *big.Int
|
|
if diff.Cmp(modComplement) > 0 {
|
|
dist = modComplement
|
|
} else {
|
|
dist = diff
|
|
}
|
|
return dist
|
|
}
|