如何用 Golang big.Int 计算 `x^3 + ax + b mod p`

How to calculate `x^3 + ax + b mod p` with Golang big.Int

我正在寻找椭圆点的 Y 坐标。我理解公式是 y^2≡x^3+ax+b mod p.

但是我不确定我将如何在 Go 中实际编程。

xCubed.Exp(X, 3, nil)
AX.Mul(A,X)

N.Add(XPow3, AX) // x^3 + ax
N.Mod(N, P) // mod p
N.Add(N, B) // (x^3 + ax) + b
N.Mod(N, P) // mod p

return N

其中 xCubed、X、N、P、B 是 *big.Int,N 代表 y^2

我是 mod 元算术的菜鸟,所以我的假设 mod P 应用于每个运算;这是计算 x^3 + ax + b mod p 的正确方法吗?

编辑:我应该添加 AX.Mod(p) 吗?

我通过搜索 elliptic lib

中的一些代码弄明白了
func GetY(x *big.Int, curve *elliptic.CurveParams) *big.Int {


    x3 := new(big.Int).Mul(x, x)
    x3.Mul(x3, x)

    threeX := new(big.Int).Lsh(x, 1)
    threeX.Add(threeX, x)

    x3.Sub(x3, threeX)
    x3.Add(x3, curve.B)
    x3.Mod(x3, curve.P)

    return x3.ModSqrt(x3, curve.P)
}