{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Ouroboros.Consensus.Committee.Crypto
(
PrivateKey
, PublicKey
, ElectionId
, VoteCandidate
, CryptoSupportsVoteSigning (..)
, CryptoSupportsAggregateVoteSigning (..)
, TrivialAggregateVoteVerificationKey (..)
, TrivialAggregateVoteSignature (..)
, trivialLiftVoteVerificationKey
, trivialLiftVoteSignature
, trivialVerifyAggregateVoteSignature
, VRFPoolContext (..)
, NormalizedVRFOutput (..)
, CryptoSupportsVRF (..)
, CryptoSupportsAggregateVRF (..)
, TrivialAggregateVRFVerificationKey (..)
, TrivialAggregateVRFOutput (..)
, trivialLiftVRFVerificationKey
, trivialLiftVRFOutput
, trivialVerifyAggregateVRFOutput
) where
import Cardano.Ledger.BaseTypes (Nonce)
import Data.Containers.NonEmpty (HasNonEmpty (..))
import Data.Either (partitionEithers)
import Data.Kind (Type)
import Data.List (intercalate)
import qualified Data.List.NonEmpty as NonEmpty
import Data.Proxy (Proxy)
type family PrivateKey crypto :: Type
type family PublicKey crypto :: Type
type family ElectionId crypto :: Type
type family VoteCandidate crypto :: Type
class CryptoSupportsVoteSigning crypto where
type VoteSigningKey crypto :: Type
type VoteVerificationKey crypto :: Type
data VoteSignature crypto :: Type
getVoteSigningKey ::
Proxy crypto ->
PrivateKey crypto ->
VoteSigningKey crypto
getVoteVerificationKey ::
Proxy crypto ->
PublicKey crypto ->
VoteVerificationKey crypto
signVote ::
VoteSigningKey crypto ->
ElectionId crypto ->
VoteCandidate crypto ->
VoteSignature crypto
verifyVoteSignature ::
VoteVerificationKey crypto ->
ElectionId crypto ->
VoteCandidate crypto ->
VoteSignature crypto ->
Either String ()
class
( Semigroup (AggregateVoteVerificationKey crypto)
, Semigroup (AggregateVoteSignature crypto)
) =>
CryptoSupportsAggregateVoteSigning crypto
where
type AggregateVoteVerificationKey crypto :: Type
type AggregateVoteSignature crypto :: Type
liftVoteVerificationKey ::
Proxy crypto ->
VoteVerificationKey crypto ->
AggregateVoteVerificationKey crypto
liftVoteSignature ::
Proxy crypto ->
VoteSignature crypto ->
AggregateVoteSignature crypto
verifyAggregateVoteSignature ::
Proxy crypto ->
AggregateVoteVerificationKey crypto ->
ElectionId crypto ->
VoteCandidate crypto ->
AggregateVoteSignature crypto ->
Either String ()
newtype TrivialAggregateVoteVerificationKey crypto
= TrivialAggregateVoteVerificationKey (NE [VoteVerificationKey crypto])
deriving newtype NonEmpty (TrivialAggregateVoteVerificationKey crypto)
-> TrivialAggregateVoteVerificationKey crypto
TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
(TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto)
-> (NonEmpty (TrivialAggregateVoteVerificationKey crypto)
-> TrivialAggregateVoteVerificationKey crypto)
-> (forall b.
Integral b =>
b
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto)
-> Semigroup (TrivialAggregateVoteVerificationKey crypto)
forall b.
Integral b =>
b
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
forall crypto.
NonEmpty (TrivialAggregateVoteVerificationKey crypto)
-> TrivialAggregateVoteVerificationKey crypto
forall crypto.
TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall crypto b.
Integral b =>
b
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
$c<> :: forall crypto.
TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
<> :: TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
$csconcat :: forall crypto.
NonEmpty (TrivialAggregateVoteVerificationKey crypto)
-> TrivialAggregateVoteVerificationKey crypto
sconcat :: NonEmpty (TrivialAggregateVoteVerificationKey crypto)
-> TrivialAggregateVoteVerificationKey crypto
$cstimes :: forall crypto b.
Integral b =>
b
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
stimes :: forall b.
Integral b =>
b
-> TrivialAggregateVoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
Semigroup
newtype TrivialAggregateVoteSignature crypto
= TrivialAggregateVoteSignature (NE [VoteSignature crypto])
deriving newtype NonEmpty (TrivialAggregateVoteSignature crypto)
-> TrivialAggregateVoteSignature crypto
TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
(TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto)
-> (NonEmpty (TrivialAggregateVoteSignature crypto)
-> TrivialAggregateVoteSignature crypto)
-> (forall b.
Integral b =>
b
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto)
-> Semigroup (TrivialAggregateVoteSignature crypto)
forall b.
Integral b =>
b
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
forall crypto.
NonEmpty (TrivialAggregateVoteSignature crypto)
-> TrivialAggregateVoteSignature crypto
forall crypto.
TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall crypto b.
Integral b =>
b
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
$c<> :: forall crypto.
TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
<> :: TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
$csconcat :: forall crypto.
NonEmpty (TrivialAggregateVoteSignature crypto)
-> TrivialAggregateVoteSignature crypto
sconcat :: NonEmpty (TrivialAggregateVoteSignature crypto)
-> TrivialAggregateVoteSignature crypto
$cstimes :: forall crypto b.
Integral b =>
b
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
stimes :: forall b.
Integral b =>
b
-> TrivialAggregateVoteSignature crypto
-> TrivialAggregateVoteSignature crypto
Semigroup
trivialLiftVoteVerificationKey ::
Proxy crypto ->
VoteVerificationKey crypto ->
TrivialAggregateVoteVerificationKey crypto
trivialLiftVoteVerificationKey :: forall crypto.
Proxy crypto
-> VoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
trivialLiftVoteVerificationKey Proxy crypto
_ =
NonEmpty (VoteVerificationKey crypto)
-> TrivialAggregateVoteVerificationKey crypto
NE [VoteVerificationKey crypto]
-> TrivialAggregateVoteVerificationKey crypto
forall crypto.
NE [VoteVerificationKey crypto]
-> TrivialAggregateVoteVerificationKey crypto
TrivialAggregateVoteVerificationKey
(NonEmpty (VoteVerificationKey crypto)
-> TrivialAggregateVoteVerificationKey crypto)
-> (VoteVerificationKey crypto
-> NonEmpty (VoteVerificationKey crypto))
-> VoteVerificationKey crypto
-> TrivialAggregateVoteVerificationKey crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VoteVerificationKey crypto -> NonEmpty (VoteVerificationKey crypto)
forall a. a -> NonEmpty a
NonEmpty.singleton
trivialLiftVoteSignature ::
Proxy crypto ->
VoteSignature crypto ->
TrivialAggregateVoteSignature crypto
trivialLiftVoteSignature :: forall crypto.
Proxy crypto
-> VoteSignature crypto -> TrivialAggregateVoteSignature crypto
trivialLiftVoteSignature Proxy crypto
_ =
NonEmpty (VoteSignature crypto)
-> TrivialAggregateVoteSignature crypto
NE [VoteSignature crypto] -> TrivialAggregateVoteSignature crypto
forall crypto.
NE [VoteSignature crypto] -> TrivialAggregateVoteSignature crypto
TrivialAggregateVoteSignature
(NonEmpty (VoteSignature crypto)
-> TrivialAggregateVoteSignature crypto)
-> (VoteSignature crypto -> NonEmpty (VoteSignature crypto))
-> VoteSignature crypto
-> TrivialAggregateVoteSignature crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VoteSignature crypto -> NonEmpty (VoteSignature crypto)
forall a. a -> NonEmpty a
NonEmpty.singleton
trivialVerifyAggregateVoteSignature ::
CryptoSupportsVoteSigning crypto =>
Proxy crypto ->
TrivialAggregateVoteVerificationKey crypto ->
ElectionId crypto ->
VoteCandidate crypto ->
TrivialAggregateVoteSignature crypto ->
Either String ()
trivialVerifyAggregateVoteSignature :: forall crypto.
CryptoSupportsVoteSigning crypto =>
Proxy crypto
-> TrivialAggregateVoteVerificationKey crypto
-> ElectionId crypto
-> VoteCandidate crypto
-> TrivialAggregateVoteSignature crypto
-> Either String ()
trivialVerifyAggregateVoteSignature
Proxy crypto
_
(TrivialAggregateVoteVerificationKey NE [VoteVerificationKey crypto]
keys)
ElectionId crypto
electionId
VoteCandidate crypto
candidate
(TrivialAggregateVoteSignature NE [VoteSignature crypto]
signatures)
| NonEmpty (VoteVerificationKey crypto) -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length NonEmpty (VoteVerificationKey crypto)
NE [VoteVerificationKey crypto]
keys Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= NonEmpty (VoteSignature crypto) -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length NonEmpty (VoteSignature crypto)
NE [VoteSignature crypto]
signatures =
String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$
String
"Aggregate vote signature verification failed: "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"number of keys and signatures do not match"
| Bool -> Bool
not ([String] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
errors) =
String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$
String
"Aggregate vote signature verification failed: "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"; " [String]
errors
| Bool
otherwise =
() -> Either String ()
forall a b. b -> Either a b
Right ()
where
([String]
errors, [()]
_) =
[Either String ()] -> ([String], [()])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either String ()] -> ([String], [()]))
-> [Either String ()] -> ([String], [()])
forall a b. (a -> b) -> a -> b
$
(VoteVerificationKey crypto
-> VoteSignature crypto -> Either String ())
-> [VoteVerificationKey crypto]
-> [VoteSignature crypto]
-> [Either String ()]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith
( \VoteVerificationKey crypto
key VoteSignature crypto
sig ->
VoteVerificationKey crypto
-> ElectionId crypto
-> VoteCandidate crypto
-> VoteSignature crypto
-> Either String ()
forall crypto.
CryptoSupportsVoteSigning crypto =>
VoteVerificationKey crypto
-> ElectionId crypto
-> VoteCandidate crypto
-> VoteSignature crypto
-> Either String ()
verifyVoteSignature VoteVerificationKey crypto
key ElectionId crypto
electionId VoteCandidate crypto
candidate VoteSignature crypto
sig
)
(NonEmpty (VoteVerificationKey crypto)
-> [VoteVerificationKey crypto]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty (VoteVerificationKey crypto)
NE [VoteVerificationKey crypto]
keys)
(NonEmpty (VoteSignature crypto) -> [VoteSignature crypto]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty (VoteSignature crypto)
NE [VoteSignature crypto]
signatures)
data VRFPoolContext crypto
=
VRFSignContext (VRFSigningKey crypto)
|
VRFVerifyContext (VRFVerificationKey crypto) (VRFOutput crypto)
newtype NormalizedVRFOutput = NormalizedVRFOutput
{ NormalizedVRFOutput -> Rational
unNormalizedVRFOutput :: Rational
}
deriving (NormalizedVRFOutput -> NormalizedVRFOutput -> Bool
(NormalizedVRFOutput -> NormalizedVRFOutput -> Bool)
-> (NormalizedVRFOutput -> NormalizedVRFOutput -> Bool)
-> Eq NormalizedVRFOutput
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NormalizedVRFOutput -> NormalizedVRFOutput -> Bool
== :: NormalizedVRFOutput -> NormalizedVRFOutput -> Bool
$c/= :: NormalizedVRFOutput -> NormalizedVRFOutput -> Bool
/= :: NormalizedVRFOutput -> NormalizedVRFOutput -> Bool
Eq, Int -> NormalizedVRFOutput -> String -> String
[NormalizedVRFOutput] -> String -> String
NormalizedVRFOutput -> String
(Int -> NormalizedVRFOutput -> String -> String)
-> (NormalizedVRFOutput -> String)
-> ([NormalizedVRFOutput] -> String -> String)
-> Show NormalizedVRFOutput
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> NormalizedVRFOutput -> String -> String
showsPrec :: Int -> NormalizedVRFOutput -> String -> String
$cshow :: NormalizedVRFOutput -> String
show :: NormalizedVRFOutput -> String
$cshowList :: [NormalizedVRFOutput] -> String -> String
showList :: [NormalizedVRFOutput] -> String -> String
Show)
class CryptoSupportsVRF crypto where
type VRFSigningKey crypto :: Type
type VRFVerificationKey crypto :: Type
data VRFElectionInput crypto :: Type
data VRFOutput crypto :: Type
getVRFSigningKey ::
Proxy crypto ->
PrivateKey crypto ->
VRFSigningKey crypto
getVRFVerificationKey ::
Proxy crypto ->
PublicKey crypto ->
VRFVerificationKey crypto
mkVRFElectionInput ::
Nonce ->
ElectionId crypto ->
VRFElectionInput crypto
evalVRF ::
VRFPoolContext crypto ->
VRFElectionInput crypto ->
Either String (VRFOutput crypto)
normalizeVRFOutput ::
VRFOutput crypto ->
NormalizedVRFOutput
class
( Semigroup (AggregateVRFVerificationKey crypto)
, Semigroup (AggregateVRFOutput crypto)
) =>
CryptoSupportsAggregateVRF crypto
where
type AggregateVRFVerificationKey crypto :: Type
type AggregateVRFOutput crypto :: Type
liftVRFVerificationKey ::
Proxy crypto ->
VRFVerificationKey crypto ->
AggregateVRFVerificationKey crypto
liftVRFOutput ::
Proxy crypto ->
VRFOutput crypto ->
AggregateVRFOutput crypto
verifyAggregateVRFOutput ::
AggregateVRFVerificationKey crypto ->
VRFElectionInput crypto ->
AggregateVRFOutput crypto ->
Either String ()
newtype TrivialAggregateVRFVerificationKey crypto
= TrivialAggregateVRFVerificationKey (NE [VRFVerificationKey crypto])
deriving newtype NonEmpty (TrivialAggregateVRFVerificationKey crypto)
-> TrivialAggregateVRFVerificationKey crypto
TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
(TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto)
-> (NonEmpty (TrivialAggregateVRFVerificationKey crypto)
-> TrivialAggregateVRFVerificationKey crypto)
-> (forall b.
Integral b =>
b
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto)
-> Semigroup (TrivialAggregateVRFVerificationKey crypto)
forall b.
Integral b =>
b
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
forall crypto.
NonEmpty (TrivialAggregateVRFVerificationKey crypto)
-> TrivialAggregateVRFVerificationKey crypto
forall crypto.
TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall crypto b.
Integral b =>
b
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
$c<> :: forall crypto.
TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
<> :: TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
$csconcat :: forall crypto.
NonEmpty (TrivialAggregateVRFVerificationKey crypto)
-> TrivialAggregateVRFVerificationKey crypto
sconcat :: NonEmpty (TrivialAggregateVRFVerificationKey crypto)
-> TrivialAggregateVRFVerificationKey crypto
$cstimes :: forall crypto b.
Integral b =>
b
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
stimes :: forall b.
Integral b =>
b
-> TrivialAggregateVRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
Semigroup
newtype TrivialAggregateVRFOutput crypto
= TrivialAggregateVRFOutput (NE [VRFOutput crypto])
deriving newtype NonEmpty (TrivialAggregateVRFOutput crypto)
-> TrivialAggregateVRFOutput crypto
TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
(TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto)
-> (NonEmpty (TrivialAggregateVRFOutput crypto)
-> TrivialAggregateVRFOutput crypto)
-> (forall b.
Integral b =>
b
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto)
-> Semigroup (TrivialAggregateVRFOutput crypto)
forall b.
Integral b =>
b
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
forall crypto.
NonEmpty (TrivialAggregateVRFOutput crypto)
-> TrivialAggregateVRFOutput crypto
forall crypto.
TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall crypto b.
Integral b =>
b
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
$c<> :: forall crypto.
TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
<> :: TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
$csconcat :: forall crypto.
NonEmpty (TrivialAggregateVRFOutput crypto)
-> TrivialAggregateVRFOutput crypto
sconcat :: NonEmpty (TrivialAggregateVRFOutput crypto)
-> TrivialAggregateVRFOutput crypto
$cstimes :: forall crypto b.
Integral b =>
b
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
stimes :: forall b.
Integral b =>
b
-> TrivialAggregateVRFOutput crypto
-> TrivialAggregateVRFOutput crypto
Semigroup
trivialLiftVRFVerificationKey ::
Proxy crypto ->
VRFVerificationKey crypto ->
TrivialAggregateVRFVerificationKey crypto
trivialLiftVRFVerificationKey :: forall crypto.
Proxy crypto
-> VRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
trivialLiftVRFVerificationKey Proxy crypto
_ =
NonEmpty (VRFVerificationKey crypto)
-> TrivialAggregateVRFVerificationKey crypto
NE [VRFVerificationKey crypto]
-> TrivialAggregateVRFVerificationKey crypto
forall crypto.
NE [VRFVerificationKey crypto]
-> TrivialAggregateVRFVerificationKey crypto
TrivialAggregateVRFVerificationKey
(NonEmpty (VRFVerificationKey crypto)
-> TrivialAggregateVRFVerificationKey crypto)
-> (VRFVerificationKey crypto
-> NonEmpty (VRFVerificationKey crypto))
-> VRFVerificationKey crypto
-> TrivialAggregateVRFVerificationKey crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VRFVerificationKey crypto -> NonEmpty (VRFVerificationKey crypto)
forall a. a -> NonEmpty a
NonEmpty.singleton
trivialLiftVRFOutput ::
Proxy crypto ->
VRFOutput crypto ->
TrivialAggregateVRFOutput crypto
trivialLiftVRFOutput :: forall crypto.
Proxy crypto
-> VRFOutput crypto -> TrivialAggregateVRFOutput crypto
trivialLiftVRFOutput Proxy crypto
_ =
NonEmpty (VRFOutput crypto) -> TrivialAggregateVRFOutput crypto
NE [VRFOutput crypto] -> TrivialAggregateVRFOutput crypto
forall crypto.
NE [VRFOutput crypto] -> TrivialAggregateVRFOutput crypto
TrivialAggregateVRFOutput
(NonEmpty (VRFOutput crypto) -> TrivialAggregateVRFOutput crypto)
-> (VRFOutput crypto -> NonEmpty (VRFOutput crypto))
-> VRFOutput crypto
-> TrivialAggregateVRFOutput crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VRFOutput crypto -> NonEmpty (VRFOutput crypto)
forall a. a -> NonEmpty a
NonEmpty.singleton
trivialVerifyAggregateVRFOutput ::
CryptoSupportsVRF crypto =>
TrivialAggregateVRFVerificationKey crypto ->
VRFElectionInput crypto ->
TrivialAggregateVRFOutput crypto ->
Either String ()
trivialVerifyAggregateVRFOutput :: forall crypto.
CryptoSupportsVRF crypto =>
TrivialAggregateVRFVerificationKey crypto
-> VRFElectionInput crypto
-> TrivialAggregateVRFOutput crypto
-> Either String ()
trivialVerifyAggregateVRFOutput
(TrivialAggregateVRFVerificationKey NE [VRFVerificationKey crypto]
keys)
VRFElectionInput crypto
vrfInput
(TrivialAggregateVRFOutput NE [VRFOutput crypto]
vrfOutputs)
| NonEmpty (VRFVerificationKey crypto) -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length NonEmpty (VRFVerificationKey crypto)
NE [VRFVerificationKey crypto]
keys Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= NonEmpty (VRFOutput crypto) -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length NonEmpty (VRFOutput crypto)
NE [VRFOutput crypto]
vrfOutputs =
String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$
String
"Aggregate VRF output verification failed: "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"number of keys and outputs do not match"
| Bool -> Bool
not ([String] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
errors) =
String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$
String
"Aggregate VRF output verification failed: "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"; " [String]
errors
| Bool
otherwise =
() -> Either String ()
forall a b. b -> Either a b
Right ()
where
([String]
errors, [VRFOutput crypto]
_) =
[Either String (VRFOutput crypto)]
-> ([String], [VRFOutput crypto])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either String (VRFOutput crypto)]
-> ([String], [VRFOutput crypto]))
-> [Either String (VRFOutput crypto)]
-> ([String], [VRFOutput crypto])
forall a b. (a -> b) -> a -> b
$
(VRFVerificationKey crypto
-> VRFOutput crypto -> Either String (VRFOutput crypto))
-> [VRFVerificationKey crypto]
-> [VRFOutput crypto]
-> [Either String (VRFOutput crypto)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith
( \VRFVerificationKey crypto
key VRFOutput crypto
vrfOutput ->
VRFPoolContext crypto
-> VRFElectionInput crypto -> Either String (VRFOutput crypto)
forall crypto.
CryptoSupportsVRF crypto =>
VRFPoolContext crypto
-> VRFElectionInput crypto -> Either String (VRFOutput crypto)
evalVRF (VRFVerificationKey crypto
-> VRFOutput crypto -> VRFPoolContext crypto
forall crypto.
VRFVerificationKey crypto
-> VRFOutput crypto -> VRFPoolContext crypto
VRFVerifyContext VRFVerificationKey crypto
key VRFOutput crypto
vrfOutput) VRFElectionInput crypto
vrfInput
)
(NonEmpty (VRFVerificationKey crypto) -> [VRFVerificationKey crypto]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty (VRFVerificationKey crypto)
NE [VRFVerificationKey crypto]
keys)
(NonEmpty (VRFOutput crypto) -> [VRFOutput crypto]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty (VRFOutput crypto)
NE [VRFOutput crypto]
vrfOutputs)