I’m getting a confusing result and think that I am missing something basic about how ByteStrings work. I have a signed event:
data Event = Event {
eid :: ByteString -- < hash of the Ev
, sig :: ByteString -- < schnorr signature
, ev :: Ev
}
And I have example data that I can use to verify:
goldid :: ByteString
goldid = "43 ... "
goldsig :: ByteString
goldsig = "908a ... "
The eventId calculation works as expected
eventId :: Ev -> ByteString
eventId =~ Hex.encode . SHA256.hash . BS.toStrict . J.encode
Next, the Validation function passes, but I do not understand why and think the next step, generating the signature, is not working because of what I don’t understand here. From the secp256 bindings
import Crypto.Schnorr (
xOnlyPubKey, schnorrSig, msg, verifyMsgSchnorr)
isValid :: Event -> Bool
isValid (Event i s e) =
let p = xOnlyPubKey . Hex.decodeLenient . pubkey $ e
s' = schnorrSig . Hex.decodeLenient $ s
m = msg . Hex.decodeLenient $ i
in maybe False id $ verifyMsgSchnorr <$> p <*> s' <*> m
What is Hex.decodeLenient actually doing and (I doubt) should I rely on it? When using the ByteStrings directly they are double the expected length and result in Nothing. Just by guessing I tried decodeLenient and it cut the length in half and then the signature verified so it must not be losing data, but I don’t understand what is happening.
For reference this is the source of decodeLenient:
-- base16-bytestring
decodeLenient :: ByteString -> ByteString
decodeLenient = LBS.fromChunks
. fmap B16.decodeLenient
. reChunk
. fmap (BS.filter (flip BS.elem extendedHex))
. LBS.toChunks
where
extendedHex = BS.pack (fmap c2w "0123456789abcdefABCDEF")
-- base16
decodeLenient :: ByteString -> ByteString
decodeLenient bs = withBS bs go
where
go !sptr !slen
| slen == 0 = return empty
| otherwise = do
dfp <- mallocPlainForeignPtrBytes (q * 2)
withForeignPtr dfp $ \dptr ->
lenientLoop dfp dptr sptr (plusPtr sptr slen)
where
!q = slen `quot` 2
link to stack project github /Spec.hs
I was hoping to find a Haskell crypto library that could do the schnorr signatures. Does that exist? Can’t find it.