|
| 1 | +use pallas_crypto::hash::Hash; |
| 2 | +use thiserror::Error; |
| 3 | +use vrf_dalek::vrf03::{PublicKey03, SecretKey03, VrfProof03}; |
| 4 | + |
| 5 | +/// error that can be returned if the verification of a [`VrfProof`] fails |
| 6 | +/// see [`VrfProof::verify`] |
| 7 | +/// |
| 8 | +#[derive(Error, Debug)] |
| 9 | +#[error("VRF Proof Verification failed.")] |
| 10 | +pub struct VerificationError( |
| 11 | + #[from] |
| 12 | + #[source] |
| 13 | + vrf_dalek::errors::VrfError, |
| 14 | +); |
| 15 | + |
| 16 | +pub const VRF_SEED_SIZE: usize = 32; |
| 17 | +pub const VRF_PROOF_SIZE: usize = 80; |
| 18 | +pub const VRF_PUBLIC_KEY_SIZE: usize = 32; |
| 19 | +pub const VRF_SECRET_KEY_SIZE: usize = 32; |
| 20 | +pub const VRF_PROOF_HASH_SIZE: usize = 64; |
| 21 | + |
| 22 | +pub type VrfSeedBytes = [u8; VRF_SEED_SIZE]; |
| 23 | +pub type VrfProofBytes = [u8; VRF_PROOF_SIZE]; |
| 24 | +pub type VrfPublicKeyBytes = [u8; VRF_PUBLIC_KEY_SIZE]; |
| 25 | +pub type VrfSecretKeyBytes = [u8; VRF_SECRET_KEY_SIZE]; |
| 26 | +pub type VrfProofHashBytes = [u8; VRF_PROOF_HASH_SIZE]; |
| 27 | + |
| 28 | +// Wrapper for VRF secret key |
| 29 | +pub struct VrfSecretKey { |
| 30 | + secret_key_03: SecretKey03, |
| 31 | +} |
| 32 | + |
| 33 | +// Wrapper for VRF public key |
| 34 | +pub struct VrfPublicKey { |
| 35 | + public_key_03: PublicKey03, |
| 36 | +} |
| 37 | + |
| 38 | +// Wrapper for VRF proof |
| 39 | +pub struct VrfProof { |
| 40 | + proof_03: VrfProof03, |
| 41 | +} |
| 42 | + |
| 43 | +// Create a VrfSecretKey from a slice |
| 44 | +impl From<&VrfSecretKeyBytes> for VrfSecretKey { |
| 45 | + fn from(slice: &VrfSecretKeyBytes) -> Self { |
| 46 | + VrfSecretKey { |
| 47 | + secret_key_03: SecretKey03::from_bytes(slice), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Create a VrfPublicKey from a slice |
| 53 | +impl From<&VrfPublicKeyBytes> for VrfPublicKey { |
| 54 | + fn from(slice: &VrfPublicKeyBytes) -> Self { |
| 55 | + VrfPublicKey { |
| 56 | + public_key_03: PublicKey03::from_bytes(slice), |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Create a VrfProof from a slice |
| 62 | +impl From<&VrfProofBytes> for VrfProof { |
| 63 | + fn from(slice: &VrfProofBytes) -> Self { |
| 64 | + VrfProof { |
| 65 | + proof_03: VrfProof03::from_bytes(slice).expect("Infallible"), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Create a VrfPublicKey from a VrfSecretKey |
| 71 | +impl From<&VrfSecretKey> for VrfPublicKey { |
| 72 | + fn from(secret_key: &VrfSecretKey) -> Self { |
| 73 | + VrfPublicKey { |
| 74 | + public_key_03: PublicKey03::from(&secret_key.secret_key_03), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl VrfSecretKey { |
| 80 | + /// Sign a challenge message value with a vrf secret key and produce a proof signature |
| 81 | + pub fn prove(&self, challenge: &[u8]) -> VrfProof { |
| 82 | + let pk = PublicKey03::from(&self.secret_key_03); |
| 83 | + let proof = VrfProof03::generate(&pk, &self.secret_key_03, challenge); |
| 84 | + VrfProof { proof_03: proof } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl VrfProof { |
| 89 | + /// Return the created proof signature |
| 90 | + pub fn signature(&self) -> [u8; VRF_PROOF_SIZE] { |
| 91 | + self.proof_03.to_bytes() |
| 92 | + } |
| 93 | + |
| 94 | + /// Convert a proof signature to a hash |
| 95 | + pub fn to_hash(&self) -> Hash<VRF_PROOF_HASH_SIZE> { |
| 96 | + Hash::from(self.proof_03.proof_to_hash()) |
| 97 | + } |
| 98 | + |
| 99 | + /// Verify a proof signature with a vrf public key. This will return a hash to compare with the original |
| 100 | + /// signature hash, but any non-error result is considered a successful verification without needing |
| 101 | + /// to do the extra comparison check. |
| 102 | + pub fn verify( |
| 103 | + &self, |
| 104 | + public_key: &VrfPublicKey, |
| 105 | + seed: &[u8], |
| 106 | + ) -> Result<Hash<VRF_PROOF_HASH_SIZE>, VerificationError> { |
| 107 | + Ok(Hash::from( |
| 108 | + self.proof_03.verify(&public_key.public_key_03, seed)?, |
| 109 | + )) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + use rand::{thread_rng, Rng}; |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn vrf_prove_and_verify() { |
| 120 | + // Node operational VRF-Verification-Key: pool.vrf.vkey |
| 121 | + // { |
| 122 | + // "type": "VrfVerificationKey_PraosVRF", |
| 123 | + // "description": "VRF Verification Key", |
| 124 | + // "cborHex": "5820e0ff2371508ac339431b50af7d69cde0f120d952bb876806d3136f9a7fda4381" |
| 125 | + // } |
| 126 | + // |
| 127 | + // Node operational VRF-Signing-Key: pool.vrf.skey |
| 128 | + // { |
| 129 | + // "type": "VrfSigningKey_PraosVRF", |
| 130 | + // "description": "VRF Signing Key", |
| 131 | + // "cborHex": "5840adb9c97bec60189aa90d01d113e3ef405f03477d82a94f81da926c90cd46a374e0ff2371508ac339431b50af7d69cde0f120d952bb876806d3136f9a7fda4381" |
| 132 | + // } |
| 133 | + let raw_vrf_skey: Vec<u8> = hex::decode("adb9c97bec60189aa90d01d113e3ef405f03477d82a94f81da926c90cd46a374e0ff2371508ac339431b50af7d69cde0f120d952bb876806d3136f9a7fda4381").unwrap(); |
| 134 | + let raw_vrf_vkey: Vec<u8> = |
| 135 | + hex::decode("e0ff2371508ac339431b50af7d69cde0f120d952bb876806d3136f9a7fda4381") |
| 136 | + .unwrap(); |
| 137 | + |
| 138 | + let vrf_skey = VrfSecretKey::from(&raw_vrf_skey[..VRF_SECRET_KEY_SIZE].try_into().unwrap()); |
| 139 | + let vrf_vkey = |
| 140 | + VrfPublicKey::from(&raw_vrf_vkey[..VRF_PUBLIC_KEY_SIZE].try_into().unwrap() |
| 141 | + as &[u8; VRF_PUBLIC_KEY_SIZE]); |
| 142 | + |
| 143 | + let calculated_vrf_vkey = VrfPublicKey::from(&vrf_skey); |
| 144 | + assert_eq!( |
| 145 | + vrf_vkey.public_key_03.as_bytes(), |
| 146 | + calculated_vrf_vkey.public_key_03.as_bytes() |
| 147 | + ); |
| 148 | + |
| 149 | + // random challenge to sign with vrf_skey |
| 150 | + let mut challenge = [0u8; 64]; |
| 151 | + thread_rng().fill(&mut challenge); |
| 152 | + |
| 153 | + // create a proof signature and hash of the seed |
| 154 | + let proof = vrf_skey.prove(&challenge); |
| 155 | + let proof_hash = proof.to_hash(); |
| 156 | + |
| 157 | + // verify the proof signature with the public vrf public key |
| 158 | + let verified_hash = proof.verify(&vrf_vkey, &challenge).unwrap(); |
| 159 | + assert_eq!(proof_hash, verified_hash); |
| 160 | + } |
| 161 | +} |
0 commit comments