From 6ed7596ecb5739689fada36a4fed0b06045a7f22 Mon Sep 17 00:00:00 2001 From: Neil Mayhew Date: Tue, 19 May 2026 16:05:30 -0600 Subject: [PATCH] Strengthen `Eq` constraint on `CertVRF` in `VRFAlgorithm` to `Ord` Also add `Ord` instances to the `CertVRF`s of the three `VRFAlgorithm` instances --- cardano-crypto-class/CHANGELOG.md | 3 +++ cardano-crypto-class/src/Cardano/Crypto/VRF/Class.hs | 5 +++-- cardano-crypto-class/src/Cardano/Crypto/VRF/Simple.hs | 6 +++++- cardano-crypto-praos/src/Cardano/Crypto/VRF/Praos.hs | 6 +++++- .../src/Cardano/Crypto/VRF/PraosBatchCompat.hs | 6 +++++- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cardano-crypto-class/CHANGELOG.md b/cardano-crypto-class/CHANGELOG.md index b812ce035..4cef150d0 100644 --- a/cardano-crypto-class/CHANGELOG.md +++ b/cardano-crypto-class/CHANGELOG.md @@ -2,6 +2,9 @@ ## 2.5.0.0 +* Add `Ord` superclass constraint for the `CertVRF` associated type in `VRFAlgorithm` +* Add `Ord` instances for `CertVRF {SimpleVRF,PraosVRF,PraosBatchCompatVRF}` +* Add `Ord` instances for `Cardano.Crypto.VRF.Praos{,BatchCompat}.Proof` * Add `NFData` superclass constraints for the `VerKeyVRF`, `SignKeyVRF`, and `CertVRF` associated types in `VRFAlgorithm`. * Remove constructors of `BLS12381SignContext` from export; use `minSigPoPDST` or `minVerKeyPoPDST` for the standard PoP ciphersuites. diff --git a/cardano-crypto-class/src/Cardano/Crypto/VRF/Class.hs b/cardano-crypto-class/src/Cardano/Crypto/VRF/Class.hs index 449e673fc..0888c7a9f 100644 --- a/cardano-crypto-class/src/Cardano/Crypto/VRF/Class.hs +++ b/cardano-crypto-class/src/Cardano/Crypto/VRF/Class.hs @@ -84,7 +84,7 @@ class , Show (SignKeyVRF v) , NFData (SignKeyVRF v) , Show (CertVRF v) - , Eq (CertVRF v) + , Ord (CertVRF v) , NFData (CertVRF v) , NoThunks (CertVRF v) , NoThunks (VerKeyVRF v) @@ -321,8 +321,9 @@ data CertifiedVRF v a = CertifiedVRF } deriving (Generic) -deriving instance VRFAlgorithm v => Show (CertifiedVRF v a) deriving instance VRFAlgorithm v => Eq (CertifiedVRF v a) +deriving instance VRFAlgorithm v => Ord (CertifiedVRF v a) +deriving instance VRFAlgorithm v => Show (CertifiedVRF v a) instance VRFAlgorithm v => NoThunks (CertifiedVRF v a) diff --git a/cardano-crypto-class/src/Cardano/Crypto/VRF/Simple.hs b/cardano-crypto-class/src/Cardano/Crypto/VRF/Simple.hs index 696ec8a59..8bbdd03e3 100644 --- a/cardano-crypto-class/src/Cardano/Crypto/VRF/Simple.hs +++ b/cardano-crypto-class/src/Cardano/Crypto/VRF/Simple.hs @@ -68,6 +68,10 @@ instance ToCBOR Point where instance FromCBOR Point where fromCBOR = Point . pointFromMaybe <$> fromCBOR +-- This isn't mathematically meaningful, but we want to be able to store Points in a Set +instance Ord Point where + compare (Point p) (Point r) = compare (pointToMaybe p) (pointToMaybe r) + instance Semigroup Point where Point p <> Point r = Point $ C.pointAdd curve p r @@ -118,7 +122,7 @@ instance VRFAlgorithm SimpleVRF where , certC :: !Natural -- md5 hash, so 16 bytes , certS :: !Integer -- at most q, so 15 bytes, round up to 16 } - deriving stock (Show, Eq, Generic) + deriving stock (Show, Eq, Ord, Generic) deriving anyclass (NoThunks) deriving anyclass (NFData) diff --git a/cardano-crypto-praos/src/Cardano/Crypto/VRF/Praos.hs b/cardano-crypto-praos/src/Cardano/Crypto/VRF/Praos.hs index cfac6f78b..36a2f5c1e 100644 --- a/cardano-crypto-praos/src/Cardano/Crypto/VRF/Praos.hs +++ b/cardano-crypto-praos/src/Cardano/Crypto/VRF/Praos.hs @@ -71,6 +71,7 @@ import Data.ByteString (ByteString) import qualified Data.ByteString as BS import qualified Data.ByteString.Unsafe as BS import Data.Coerce (coerce) +import Data.Ord (comparing) import Data.Primitive.ByteArray ( ByteArray, copyPtrToMutableByteArray, @@ -292,6 +293,9 @@ instance Show Proof where instance Eq Proof where a == b = proofBytes a == proofBytes b +instance Ord Proof where + compare = comparing proofBytes + instance ToCBOR Proof where toCBOR = toCBOR . proofBytes encodedSizeExpr _ _ = @@ -526,7 +530,7 @@ instance VRFAlgorithm PraosVRF where deriving newtype (NFData) newtype CertVRF PraosVRF = CertPraosVRF Proof - deriving stock (Show, Eq, Generic) + deriving stock (Show, Eq, Ord, Generic) deriving newtype (ToCBOR, FromCBOR) deriving (NoThunks) via OnlyCheckWhnfNamed "CertKeyVRF PraosVRF" Proof deriving newtype (NFData) diff --git a/cardano-crypto-praos/src/Cardano/Crypto/VRF/PraosBatchCompat.hs b/cardano-crypto-praos/src/Cardano/Crypto/VRF/PraosBatchCompat.hs index d5b126494..f13103677 100644 --- a/cardano-crypto-praos/src/Cardano/Crypto/VRF/PraosBatchCompat.hs +++ b/cardano-crypto-praos/src/Cardano/Crypto/VRF/PraosBatchCompat.hs @@ -77,6 +77,7 @@ import Control.Monad (void, (<$!>)) import Data.ByteString (ByteString) import qualified Data.ByteString as BS import Data.Coerce (coerce) +import Data.Ord (comparing) import Data.Primitive.ByteArray ( ByteArray, copyPtrToMutableByteArray, @@ -314,6 +315,9 @@ instance Show Proof where instance Eq Proof where a == b = proofBytes a == proofBytes b +instance Ord Proof where + compare = comparing proofBytes + instance ToCBOR Proof where toCBOR = toCBOR . proofBytes encodedSizeExpr _ _ = @@ -537,7 +541,7 @@ instance VRFAlgorithm PraosBatchCompatVRF where deriving newtype (NFData) newtype CertVRF PraosBatchCompatVRF = CertPraosBatchCompatVRF Proof - deriving stock (Show, Eq, Generic) + deriving stock (Show, Eq, Ord, Generic) deriving newtype (ToCBOR, FromCBOR) deriving (NoThunks) via OnlyCheckWhnfNamed "CertKeyVRF PraosBatchCompatVRF" Proof deriving newtype (NFData)