Skip to content

Commit 73b2036

Browse files
verify
1 parent 9e8676b commit 73b2036

2 files changed

Lines changed: 55 additions & 27 deletions

File tree

src/Simplex/Messaging/Crypto/BBS.hs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Simplex.Messaging.Crypto.BBS
1212
BBSPresHeader (..),
1313
bbsKeyGen,
1414
bbsSign,
15+
bbsVerify,
1516
bbsProofGen,
1617
bbsProofVerify,
1718
) where
@@ -81,6 +82,14 @@ foreign import ccall "bbs_sign"
8182
CSize -> Ptr (Ptr Word8) -> Ptr CSize ->
8283
IO CInt
8384

85+
foreign import ccall "bbs_verify"
86+
c_bbs_verify ::
87+
Ptr BBS_Ciphersuite ->
88+
Ptr Word8 -> Ptr Word8 ->
89+
Ptr Word8 -> CSize ->
90+
CSize -> Ptr (Ptr Word8) -> Ptr CSize ->
91+
IO CInt
92+
8493
foreign import ccall "bbs_proof_gen"
8594
c_bbs_proof_gen ::
8695
Ptr BBS_Ciphersuite ->
@@ -111,8 +120,12 @@ getCiphersuite = peek c_bbs_sha256_ciphersuite
111120
-- Helpers
112121

113122
withBS :: ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
114-
withBS bs f = let (fptr, off, len) = BI.toForeignPtr bs
115-
in withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off) (fromIntegral len)
123+
withBS bs f =
124+
let (fptr, off, len) = BI.toForeignPtr bs
125+
in withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off) (fromIntegral len)
126+
127+
packPtr :: Ptr Word8 -> Int -> IO ByteString
128+
packPtr ptr len = B.packCStringLen (castPtr ptr, len)
116129

117130
withMessages :: [ByteString] -> (Ptr (Ptr Word8) -> Ptr CSize -> CSize -> IO a) -> IO a
118131
withMessages msgs f = do
@@ -142,14 +155,15 @@ withIndexes idxs f = do
142155
bbsKeyGen :: IO (BBSSecretKey, BBSPublicKey)
143156
bbsKeyGen = do
144157
cs <- getCiphersuite
145-
sk <- BI.create bbsSkLen $ \_ -> pure ()
146-
pk <- BI.create bbsPkLen $ \_ -> pure ()
147-
withBS sk $ \skPtr _ ->
148-
withBS pk $ \pkPtr _ -> do
158+
allocaBytes bbsSkLen $ \skPtr ->
159+
allocaBytes bbsPkLen $ \pkPtr -> do
149160
rc <- c_bbs_keygen_full cs skPtr pkPtr
150-
if rc == 0
151-
then pure (BBSSecretKey sk, BBSPublicKey pk)
152-
else error "bbsKeyGen failed"
161+
if rc /= 0
162+
then error "bbsKeyGen failed"
163+
else do
164+
sk <- packPtr skPtr bbsSkLen
165+
pk <- packPtr pkPtr bbsPkLen
166+
pure (BBSSecretKey sk, BBSPublicKey pk)
153167

154168
bbsSign ::
155169
BBSSecretKey ->
@@ -159,16 +173,30 @@ bbsSign ::
159173
IO (Either String BBSSignature)
160174
bbsSign (BBSSecretKey sk) (BBSPublicKey pk) (BBSHeader header) msgs = do
161175
cs <- getCiphersuite
162-
sig <- BI.create bbsSigLen $ \_ -> pure ()
163-
withBS sk $ \skPtr _ ->
164-
withBS pk $ \pkPtr _ ->
165-
withBS header $ \hdrPtr hdrLen ->
166-
withBS sig $ \sigPtr _ ->
176+
allocaBytes bbsSigLen $ \sigPtr ->
177+
withBS sk $ \skPtr _ ->
178+
withBS pk $ \pkPtr _ ->
179+
withBS header $ \hdrPtr hdrLen ->
167180
withMessages msgs $ \msgsPtr lensPtr n -> do
168181
rc <- c_bbs_sign cs skPtr pkPtr sigPtr hdrPtr hdrLen n msgsPtr lensPtr
169-
pure $ if rc == 0
170-
then Right (BBSSignature sig)
171-
else Left "bbsSign failed"
182+
if rc /= 0
183+
then pure $ Left "bbsSign failed"
184+
else Right . BBSSignature <$> packPtr sigPtr bbsSigLen
185+
186+
bbsVerify ::
187+
BBSPublicKey ->
188+
BBSSignature ->
189+
BBSHeader ->
190+
[ByteString] ->
191+
IO Bool
192+
bbsVerify (BBSPublicKey pk) (BBSSignature sig) (BBSHeader header) msgs = do
193+
cs <- getCiphersuite
194+
withBS pk $ \pkPtr _ ->
195+
withBS sig $ \sigPtr _ ->
196+
withBS header $ \hdrPtr hdrLen ->
197+
withMessages msgs $ \msgsPtr lensPtr n -> do
198+
rc <- c_bbs_verify cs pkPtr sigPtr hdrPtr hdrLen n msgsPtr lensPtr
199+
pure (rc == 0)
172200

173201
bbsProofGen ::
174202
BBSPublicKey ->
@@ -182,18 +210,17 @@ bbsProofGen (BBSPublicKey pk) (BBSSignature sig) (BBSHeader header) (BBSPresHead
182210
cs <- getCiphersuite
183211
let numUndisclosed = length msgs - length disclosedIdxs
184212
proofSz = bbsProofLen numUndisclosed
185-
proof <- BI.create proofSz $ \_ -> pure ()
186-
withBS pk $ \pkPtr _ ->
187-
withBS sig $ \sigPtr _ ->
188-
withBS proof $ \proofPtr _ ->
213+
allocaBytes proofSz $ \proofPtr ->
214+
withBS pk $ \pkPtr _ ->
215+
withBS sig $ \sigPtr _ ->
189216
withBS header $ \hdrPtr hdrLen ->
190217
withBS ph $ \phPtr phLen ->
191218
withIndexes disclosedIdxs $ \idxsPtr idxsLen ->
192219
withMessages msgs $ \msgsPtr lensPtr n -> do
193220
rc <- c_bbs_proof_gen cs pkPtr sigPtr proofPtr hdrPtr hdrLen phPtr phLen idxsPtr idxsLen n msgsPtr lensPtr
194-
pure $ if rc == 0
195-
then Right (BBSProof proof)
196-
else Left "bbsProofGen failed"
221+
if rc /= 0
222+
then pure $ Left "bbsProofGen failed"
223+
else Right . BBSProof <$> packPtr proofPtr proofSz
197224

198225
bbsProofVerify ::
199226
BBSPublicKey ->
@@ -214,4 +241,3 @@ bbsProofVerify (BBSPublicKey pk) (BBSProof proof) (BBSHeader header) (BBSPresHea
214241
withMessages disclosedMsgs $ \msgsPtr lensPtr _ -> do
215242
rc <- c_bbs_proof_verify cs pkPtr proofPtr proofLen hdrPtr hdrLen phPtr phLen idxsPtr idxsLen (fromIntegral numMessages) msgsPtr lensPtr
216243
pure (rc == 0)
217-

tests/CoreTests/CryptoTests.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,15 @@ bbsDisclosedMsgs = ["2026-07-31", "supporter"]
298298
testBBSSignVerify :: IO ()
299299
testBBSSignVerify = do
300300
(sk, pk) <- bbsKeyGen
301-
Right sig <- bbsSign sk pk bbsHeader bbsMessages
302301
let BBSSecretKey skBs = sk
303302
BBSPublicKey pkBs = pk
304-
BBSSignature sigBs = sig
305303
B.length skBs `shouldBe` 32
306304
B.length pkBs `shouldBe` 96
305+
Right sig <- bbsSign sk pk bbsHeader bbsMessages
306+
let BBSSignature sigBs = sig
307307
B.length sigBs `shouldBe` 80
308+
bbsVerify pk sig bbsHeader bbsMessages >>= (`shouldBe` True)
309+
bbsVerify pk sig bbsHeader ["wrong", "2026-07-31", "supporter"] >>= (`shouldBe` False)
308310

309311
testBBSProofRoundtrip :: IO ()
310312
testBBSProofRoundtrip = do

0 commit comments

Comments
 (0)