Mastering Bitcoin chapter 4 - 03 compressed Public key

{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
        base58 {:git/url "https://github.com/fjsousa/base58"
                :sha "807fba7f9f38175531be81c4f4c4a682fc380433"}
        buddy/buddy-core {:mvn/version "1.10.413"}
        org.bouncycastle/bcprov-jdk15on {:mvn/version "1.70"}
        ;; complient is used for autocompletion
        ;; add your libs here (and restart the runtime to pick up changes)
        compliment/compliment {:mvn/version "0.3.9"}}}
Extensible Data Notation
(ns public-compressed-key
  (:require
   [buddy.core.hash :as hash]
   [buddy.core.codecs :as codecs]
   [base58.core :as base58])
  (:import java.math.BigInteger
   [org.bouncycastle.jce ECNamedCurveTable]))
0.0s

Getting public keys from previous notebook.

(def public-key
  "04F028892BAD7ED57D2FB57BF33081D5CFCF6F9ED3D3D7F159C2E2FFF579DC341A07CF33DA18BD734C600B96A72BBC4749D5141C90EC8AC328AE52DDFE2E505BDB")
0.0s
(def version (subs public-key 0 2 ))
0.0s
(def x (subs public-key 2 (+ 64 2) ))
0.0s
(def y (subs public-key (+ 64 2)))
0.0s

If y is even, the prefix of the compressed key is 02, otherwise, 03

(even? (BigInteger. y 16))
0.0s
(def compressed-publick-key-hex
 (str (if (even? (BigInteger. y 16)) "02" "03")
  x))
0.0s
Runtimes (1)