Skip to content

Instantly share code, notes, and snippets.

@Zenithar
Last active October 12, 2023 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zenithar/7bc1ed1e70707507f4559d430dd357b4 to your computer and use it in GitHub Desktop.
Save Zenithar/7bc1ed1e70707507f4559d430dd357b4 to your computer and use it in GitHub Desktop.
Provide CP-256, CP-384, CP-521 Y point resolution from an OKP serialized X point - https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-dnhpke-02#section-4.1.1
package main
import (
"crypto/elliptic"
"encoding/hex"
"fmt"
"math/big"
)
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-dnhpke-02#section-4.1.1
func main() {
X, _ := hex.DecodeString("8f0cace46987ad7efec5959a925e05bf2c3e0f3d1637a861f79226715be99c41")
Y, _ := hex.DecodeString("2c704aa459ef59c319bc1f872f5a7c532143f2f94b56d5565666488bb64c1748")
fmt.Println("X: " + hex.EncodeToString(X))
fmt.Println("Y: " + hex.EncodeToString(Y))
fmt.Println("D: " + hex.EncodeToString(D))
c := elliptic.P256()
// https://crypto.stackexchange.com/a/20640
// y = ((x^3 + a*x + b)^((p + 1)/4)) mod p
x := big.NewInt(0).SetBytes(X)
b := c.Params().B
p := c.Params().P
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, b)
p1 := new(big.Int).Add(p, big.NewInt(1))
p1.Div(p1, big.NewInt(4))
y := big.NewInt(0).Exp(x3, p1, p)
fmt.Println("Y' : " + hex.EncodeToString(y.Bytes()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment