Skip to content

Commit aed6a04

Browse files
committed
Rework element de-serialization
1 parent 0efb3d9 commit aed6a04

File tree

6 files changed

+16
-36
lines changed

6 files changed

+16
-36
lines changed

src/group/mod.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use ristretto::Ristretto255;
2626
use subtle::ConstantTimeEq;
2727
use zeroize::Zeroize;
2828

29-
use crate::{Error, Result};
29+
use crate::Result;
3030

3131
/// A prime-order subgroup of a base field (EC, prime-order field ...). This
3232
/// subgroup is noted additively — as in the draft RFC — in this trait.
@@ -91,27 +91,9 @@ pub trait Group {
9191
/// The multiplicative inverse of this scalar
9292
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar;
9393

94-
/// Return an element from its fixed-length bytes representation. This is
95-
/// the unchecked version, which does not check for deserializing the
96-
/// identity element
97-
fn from_element_slice_unchecked(
98-
element_bits: &GenericArray<u8, Self::ElemLen>,
99-
) -> Result<Self::Elem>;
100-
10194
/// Return an element from its fixed-length bytes representation. If the
10295
/// element is the identity element, return an error.
103-
fn from_element_slice<'a>(
104-
element_bits: impl Into<&'a GenericArray<u8, Self::ElemLen>>,
105-
) -> Result<Self::Elem> {
106-
let elem = Self::from_element_slice_unchecked(element_bits.into())?;
107-
108-
if Self::Elem::ct_eq(&elem, &Self::identity()).into() {
109-
// found the identity element
110-
return Err(Error::PointError);
111-
}
112-
113-
Ok(elem)
114-
}
96+
fn deserialize_element(element_bits: &GenericArray<u8, Self::ElemLen>) -> Result<Self::Elem>;
11597

11698
/// Serializes the `self` group element
11799
fn to_arr(elem: Self::Elem) -> GenericArray<u8, Self::ElemLen>;

src/group/p256.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ use num_integer::Integer;
2525
use num_traits::{One, ToPrimitive, Zero};
2626
use once_cell::unsync::Lazy;
2727
use p256_::elliptic_curve::group::prime::PrimeCurveAffine;
28-
use p256_::elliptic_curve::group::GroupEncoding;
2928
use p256_::elliptic_curve::ops::Reduce;
3029
use p256_::elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint};
3130
use p256_::elliptic_curve::Field;
32-
use p256_::{AffinePoint, EncodedPoint, NistP256, ProjectivePoint, Scalar, SecretKey};
31+
use p256_::{AffinePoint, EncodedPoint, NistP256, ProjectivePoint, PublicKey, Scalar, SecretKey};
3332
use rand_core::{CryptoRng, RngCore};
3433
use subtle::{Choice, ConditionallySelectable};
3534

@@ -162,10 +161,10 @@ impl Group for NistP256 {
162161
Option::from(scalar.invert()).unwrap()
163162
}
164163

165-
fn from_element_slice_unchecked(
166-
element_bits: &GenericArray<u8, Self::ElemLen>,
167-
) -> Result<Self::Elem> {
168-
Option::from(ProjectivePoint::from_bytes(element_bits)).ok_or(Error::PointError)
164+
fn deserialize_element(element_bits: &GenericArray<u8, Self::ElemLen>) -> Result<Self::Elem> {
165+
PublicKey::from_sec1_bytes(element_bits)
166+
.map(|public_key| public_key.to_projective())
167+
.map_err(|_| Error::PointError)
169168
}
170169

171170
fn to_arr(elem: Self::Elem) -> GenericArray<u8, Self::ElemLen> {

src/group/ristretto.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ impl Group for Ristretto255 {
108108
scalar.invert()
109109
}
110110

111-
fn from_element_slice_unchecked(
112-
element_bits: &GenericArray<u8, Self::ElemLen>,
113-
) -> Result<Self::Elem> {
111+
fn deserialize_element(element_bits: &GenericArray<u8, Self::ElemLen>) -> Result<Self::Elem> {
114112
CompressedRistretto::from_slice(element_bits)
115113
.decompress()
114+
.filter(|point| point != &RistrettoPoint::identity())
116115
.ok_or(Error::PointError)
117116
}
118117

src/group/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn test_group_properties() -> Result<()> {
3636
// Checks that the identity element cannot be deserialized
3737
fn test_identity_element_error<G: Group>() -> Result<()> {
3838
let identity = G::identity();
39-
let result = G::from_element_slice(&G::to_arr(identity));
39+
let result = G::deserialize_element(&G::to_arr(identity));
4040
assert!(matches!(result, Err(Error::PointError)));
4141

4242
Ok(())

src/serialization.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> VerifiableClient<G,
6161
let mut input = input.iter().copied();
6262

6363
let blind = G::deserialize_scalar(&deserialize(&mut input)?)?;
64-
let blinded_element = G::from_element_slice(&deserialize(&mut input)?)?;
64+
let blinded_element = G::deserialize_element(&deserialize(&mut input)?)?;
6565

6666
Ok(Self {
6767
blind,
@@ -105,7 +105,7 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> VerifiableServer<G,
105105
let mut input = input.iter().copied();
106106

107107
let sk = G::deserialize_scalar(&deserialize(&mut input)?)?;
108-
let pk = G::from_element_slice(&deserialize(&mut input)?)?;
108+
let pk = G::deserialize_element(&deserialize(&mut input)?)?;
109109

110110
Ok(Self {
111111
sk,
@@ -150,7 +150,7 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> BlindedElement<G, H
150150
pub fn deserialize(input: &[u8]) -> Result<Self> {
151151
let mut input = input.iter().copied();
152152

153-
let value = G::from_element_slice(&deserialize(&mut input)?)?;
153+
let value = G::deserialize_element(&deserialize(&mut input)?)?;
154154

155155
Ok(Self {
156156
value,
@@ -169,7 +169,7 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> EvaluationElement<G
169169
pub fn deserialize(input: &[u8]) -> Result<Self> {
170170
let mut input = input.iter().copied();
171171

172-
let value = G::from_element_slice(&deserialize(&mut input)?)?;
172+
let value = G::deserialize_element(&deserialize(&mut input)?)?;
173173

174174
Ok(Self {
175175
value,

src/tests/voprf_test_vectors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn test_verifiable_finalize<G: Group, H: BlockSizeUser + Digest + FixedOutputRes
323323
for i in 0..parameters.input.len() {
324324
let client = VerifiableClient::<G, H>::from_blind_and_element(
325325
G::deserialize_scalar(&GenericArray::clone_from_slice(&parameters.blind[i]))?,
326-
G::from_element_slice(&GenericArray::clone_from_slice(
326+
G::deserialize_element(&GenericArray::clone_from_slice(
327327
&parameters.blinded_element[i],
328328
))?,
329329
);
@@ -341,7 +341,7 @@ fn test_verifiable_finalize<G: Group, H: BlockSizeUser + Digest + FixedOutputRes
341341
&clients,
342342
&messages,
343343
&Proof::deserialize(&parameters.proof)?,
344-
G::from_element_slice(GenericArray::from_slice(&parameters.pksm))?,
344+
G::deserialize_element(GenericArray::from_slice(&parameters.pksm))?,
345345
Some(&parameters.info),
346346
)?;
347347

0 commit comments

Comments
 (0)