You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
According to that page it is called "Compressed SEC Format". I know that there is another library specialized to secp256k1, but I am already using cryptonite and don't want to add more dependencies. I am using Crypto.PubKey.ECC.ECDSA.PublicKey with hardcoded Curve, so I only need to encode and decode Crypto.PubKey.ECC.Types.Point. It is quite easy to implement encoding function (here I am using ByteString for simplicity, it can be generalized to ByteArray):
publicKeyToBytes :: HasCallStack => ECDSA.PublicKey -> ByteString
publicKeyToBytes (ECDSA.PublicKey curve publicPoint) =
case publicPoint of
-- We are using `i2ospOf_` because `curveSizeBits` ensures that
-- the number won't have more than that many bytes.
Point x y -> prefix y <> i2ospOf_ (curveSizeBits curve `div` 8) x
PointO -> error "PublicKey somehow contains infinity point"
where
prefix :: Integer -> ByteString
prefix y
| odd y = "\003"
| otherwise = "\002"
Decoding function is a bit trickier, I haven't tried implementing it yet, but the algorithm is described in the linked page, so I think I can do it as well.
I know that there is EllipticCurve class with encodePoint and decodePoint functions, but I couldn't find any implementation that uses compressed format.
So I have a couple of questions:
Is such functionality already present anywhere in the library (maybe I just missed that)? Maybe not encoding/decoding, but at least a function which takes x coordinate and Curve and returns y coordinate or Point? It would be very helpful for decoding.
If there is no such functionality, does it make sense to add it? If yes, where should I put it? I can do it in Crypto.PubKey.ECC.ECDSA or I can add a newtype wrapper which customizes EllipticCurve instance so that encodePoint and decodePoint use compressed format.
P. S. I am a crypto newbie, so please don't hate me if I wrote nonsense ^_^
The text was updated successfully, but these errors were encountered:
In my application I need to encode and decode
secp256k1
public keys in the same format as Bitcoin uses: https://www.oreilly.com/library/view/programming-bitcoin/9781492031482/ch04.htmlAccording to that page it is called "Compressed SEC Format". I know that there is another library specialized to
secp256k1
, but I am already usingcryptonite
and don't want to add more dependencies. I am usingCrypto.PubKey.ECC.ECDSA.PublicKey
with hardcodedCurve
, so I only need to encode and decodeCrypto.PubKey.ECC.Types.Point
. It is quite easy to implement encoding function (here I am usingByteString
for simplicity, it can be generalized toByteArray
):Decoding function is a bit trickier, I haven't tried implementing it yet, but the algorithm is described in the linked page, so I think I can do it as well.
I know that there is
EllipticCurve
class withencodePoint
anddecodePoint
functions, but I couldn't find any implementation that uses compressed format.So I have a couple of questions:
x
coordinate andCurve
and returnsy
coordinate orPoint
? It would be very helpful for decoding.Crypto.PubKey.ECC.ECDSA
or I can add a newtype wrapper which customizesEllipticCurve
instance so thatencodePoint
anddecodePoint
use compressed format.P. S. I am a crypto newbie, so please don't hate me if I wrote nonsense ^_^
The text was updated successfully, but these errors were encountered: