Standard library / math/big
big.Int.ModSqrt()
Find a modular square root for an odd prime modulus. No root returns nil and leaves receiver unchanged; even moduli panic, odd composite moduli are unsupported. This method writes to the receiver (use a separate destination to preserve inputs when needed). Expected stdout: '9\n'.
Formal Syntax & Structure
go syntaxgo
func (z *Int) ModSqrt(x, p *Int) *IntParameters & Attributes
| Name | Type | Description |
|---|---|---|
| x | *Int | Value whose square root is sought. |
| p | *Int | Odd prime modulus. |
Return Value
*Int
Returns the result described above; see the example for the observed behavior.
Working Example
Example Snippetgo
package main
import ( "fmt" "math/big")
func main() { p := big.NewInt(11) z := new(big.Int).ModSqrt(big.NewInt(9), p) if z == nil { panic("no root") } fmt.Println(new(big.Int).Mod(new(big.Int).Mul(z, z), p))}