mirror of
https://github.com/SwingbyProtocol/tss-lib.git
synced 2026-01-10 14:28:20 -05:00
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
// Copyright © 2019 Binance
|
|
//
|
|
// This file is part of Binance. The full Binance copyright notice, including
|
|
// terms governing use, modification, and redistribution, is contained in the
|
|
// file LICENSE at the root of the source code distribution tree.
|
|
|
|
package common
|
|
|
|
import (
|
|
"math/big"
|
|
)
|
|
|
|
// modInt is a *big.Int that performs all of its arithmetic with modular reduction.
|
|
type modInt big.Int
|
|
|
|
var (
|
|
zero = big.NewInt(0)
|
|
one = big.NewInt(1)
|
|
two = big.NewInt(2)
|
|
)
|
|
|
|
func ModInt(mod *big.Int) *modInt {
|
|
return (*modInt)(mod)
|
|
}
|
|
|
|
func (mi *modInt) Add(x, y *big.Int) *big.Int {
|
|
i := new(big.Int)
|
|
i.Add(x, y)
|
|
return i.Mod(i, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) Sub(x, y *big.Int) *big.Int {
|
|
i := new(big.Int)
|
|
i.Sub(x, y)
|
|
return i.Mod(i, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) Div(x, y *big.Int) *big.Int {
|
|
i := new(big.Int)
|
|
i.Div(x, y)
|
|
return i.Mod(i, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) Mul(x, y *big.Int) *big.Int {
|
|
i := new(big.Int)
|
|
i.Mul(x, y)
|
|
return i.Mod(i, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) Exp(x, y *big.Int) *big.Int {
|
|
return new(big.Int).Exp(x, y, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) Neg(x *big.Int) *big.Int {
|
|
i := new(big.Int)
|
|
i.Neg(x)
|
|
return i.Mod(i, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) Inverse(g *big.Int) *big.Int {
|
|
return new(big.Int).ModInverse(g, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) Sqrt(x *big.Int) *big.Int {
|
|
return new(big.Int).ModSqrt(x, mi.i())
|
|
}
|
|
|
|
func (mi *modInt) i() *big.Int {
|
|
return (*big.Int)(mi)
|
|
}
|