sp-core: Rename VrfOutput to VrfPreOutput (#2534)

This will make more sense after
https://github.com/paritytech/polkadot-sdk/pull/2524 since the
schnorrkel type for VRF outputs is also renamed in the latest version.
Can be reviewed independently though.

Can be merged after https://github.com/paritytech/polkadot-sdk/pull/1577
so that there is less pain for @davxy.

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
André Silva
2023-12-06 00:19:56 +00:00
committed by GitHub
parent 91cbe087de
commit 1f023deab8
19 changed files with 193 additions and 172 deletions
+27 -25
View File
@@ -628,36 +628,36 @@ pub mod vrf {
/// VRF signature data
#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct VrfSignature {
/// VRF output.
pub output: VrfOutput,
/// VRF pre-output.
pub pre_output: VrfPreOutput,
/// VRF proof.
pub proof: VrfProof,
}
/// VRF output type suitable for schnorrkel operations.
/// VRF pre-output type suitable for schnorrkel operations.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VrfOutput(pub schnorrkel::vrf::VRFOutput);
pub struct VrfPreOutput(pub schnorrkel::vrf::VRFOutput);
impl Encode for VrfOutput {
impl Encode for VrfPreOutput {
fn encode(&self) -> Vec<u8> {
self.0.as_bytes().encode()
}
}
impl Decode for VrfOutput {
impl Decode for VrfPreOutput {
fn decode<R: codec::Input>(i: &mut R) -> Result<Self, codec::Error> {
let decoded = <[u8; VRF_OUTPUT_LENGTH]>::decode(i)?;
Ok(Self(schnorrkel::vrf::VRFOutput::from_bytes(&decoded).map_err(convert_error)?))
}
}
impl MaxEncodedLen for VrfOutput {
impl MaxEncodedLen for VrfPreOutput {
fn max_encoded_len() -> usize {
<[u8; VRF_OUTPUT_LENGTH]>::max_encoded_len()
}
}
impl TypeInfo for VrfOutput {
impl TypeInfo for VrfPreOutput {
type Identity = [u8; VRF_OUTPUT_LENGTH];
fn type_info() -> scale_info::Type {
@@ -699,7 +699,7 @@ pub mod vrf {
#[cfg(feature = "full_crypto")]
impl VrfCrypto for Pair {
type VrfInput = VrfTranscript;
type VrfOutput = VrfOutput;
type VrfPreOutput = VrfPreOutput;
type VrfSignData = VrfSignData;
type VrfSignature = VrfSignature;
}
@@ -717,18 +717,18 @@ pub mod vrf {
let proof = self.0.dleq_proove(extra, &inout, true).0;
VrfSignature { output: VrfOutput(inout.to_output()), proof: VrfProof(proof) }
VrfSignature { pre_output: VrfPreOutput(inout.to_output()), proof: VrfProof(proof) }
}
fn vrf_output(&self, input: &Self::VrfInput) -> Self::VrfOutput {
let output = self.0.vrf_create_hash(input.0.clone()).to_output();
VrfOutput(output)
fn vrf_pre_output(&self, input: &Self::VrfInput) -> Self::VrfPreOutput {
let pre_output = self.0.vrf_create_hash(input.0.clone()).to_output();
VrfPreOutput(pre_output)
}
}
impl VrfCrypto for Public {
type VrfInput = VrfTranscript;
type VrfOutput = VrfOutput;
type VrfPreOutput = VrfPreOutput;
type VrfSignData = VrfSignData;
type VrfSignature = VrfSignature;
}
@@ -739,7 +739,7 @@ pub mod vrf {
let public = schnorrkel::PublicKey::from_bytes(self)?;
let inout =
signature.output.0.attach_input_hash(&public, data.transcript.0.clone())?;
signature.pre_output.0.attach_input_hash(&public, data.transcript.0.clone())?;
let extra = data
.extra
@@ -802,19 +802,21 @@ pub mod vrf {
&self,
context: &[u8],
input: &VrfInput,
output: &VrfOutput,
pre_output: &VrfPreOutput,
) -> Result<[u8; N], codec::Error>
where
[u8; N]: Default,
{
let pubkey = schnorrkel::PublicKey::from_bytes(&self.0).map_err(convert_error)?;
let inout =
output.0.attach_input_hash(&pubkey, input.0.clone()).map_err(convert_error)?;
let inout = pre_output
.0
.attach_input_hash(&pubkey, input.0.clone())
.map_err(convert_error)?;
Ok(inout.make_bytes::<[u8; N]>(context))
}
}
impl VrfOutput {
impl VrfPreOutput {
/// Generate output bytes from the given VRF configuration.
pub fn make_bytes<const N: usize>(
&self,
@@ -1097,10 +1099,10 @@ mod tests {
let input = VrfTranscript::new(b"label", &[(b"domain1", b"data1")]);
let output = pair.vrf_output(&input);
let pre_output = pair.vrf_pre_output(&input);
let out1 = pair.make_bytes::<32>(ctx, &input);
let out2 = output.make_bytes::<32>(ctx, &input, &public).unwrap();
let out2 = pre_output.make_bytes::<32>(ctx, &input, &public).unwrap();
assert_eq!(out1, out2);
let extra = VrfTranscript::new(b"extra", &[(b"domain2", b"data2")]);
@@ -1108,7 +1110,7 @@ mod tests {
let signature = pair.vrf_sign(&data);
assert!(public.vrf_verify(&data, &signature));
let out3 = public.make_bytes::<32>(ctx, &input, &signature.output).unwrap();
let out3 = public.make_bytes::<32>(ctx, &input, &signature.pre_output).unwrap();
assert_eq!(out2, out3);
}
@@ -1126,7 +1128,7 @@ mod tests {
assert!(public.vrf_verify(&data, &signature));
let out1 = pair.make_bytes::<32>(ctx, &input);
let out2 = public.make_bytes::<32>(ctx, &input, &signature.output).unwrap();
let out2 = public.make_bytes::<32>(ctx, &input, &signature.pre_output).unwrap();
assert_eq!(out1, out2);
// Direct call to backend version of sign after check with extra params
@@ -1139,9 +1141,9 @@ mod tests {
})
.unwrap();
let signature2 =
VrfSignature { output: VrfOutput(inout.to_output()), proof: VrfProof(proof) };
VrfSignature { pre_output: VrfPreOutput(inout.to_output()), proof: VrfProof(proof) };
assert!(public.vrf_verify(&data, &signature2));
assert_eq!(signature.output, signature2.output);
assert_eq!(signature.pre_output, signature2.pre_output);
}
}