ceremonyclient/utils/math.go
Cassandra Heart dbd95bd9e9
v2.1.0 (#439)
* 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
2025-09-30 02:48:15 -05:00

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
}