implement decoding from raw representation

This commit is contained in:
Alex Vlasov 2019-06-01 14:08:44 +03:00
parent 9228d20862
commit 47948ef8a5
3 changed files with 86 additions and 0 deletions

@ -760,6 +760,45 @@ pub mod g1 {
res res
} }
fn from_raw_uncompressed_le_unchecked(
encoded: &Self::Uncompressed,
_infinity: bool
) -> Result<Self, GroupDecodingError> {
let copy = encoded.0;
if copy.iter().all(|b| *b == 0) {
return Ok(Self::zero());
}
let mut x = FqRepr([0; 6]);
let mut y = FqRepr([0; 6]);
{
let mut reader = &copy[..];
x.read_be(&mut reader).unwrap();
y.read_be(&mut reader).unwrap();
}
Ok(G1Affine {
x: Fq::from_raw_repr(x).map_err(|e| {
GroupDecodingError::CoordinateDecodingError("x coordinate", e)
})?,
y: Fq::from_raw_repr(y).map_err(|e| {
GroupDecodingError::CoordinateDecodingError("y coordinate", e)
})?,
infinity: false,
})
}
fn from_raw_uncompressed_le(encoded: &Self::Uncompressed, _infinity: bool) -> Result<Self, GroupDecodingError> {
let affine = Self::from_raw_uncompressed_le_unchecked(&encoded, _infinity)?;
if !affine.is_on_curve() {
Err(GroupDecodingError::NotOnCurve)
} else {
Ok(affine)
}
}
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]

@ -652,6 +652,47 @@ pub mod g1 {
res res
} }
/// Creates a point from raw encoded coordinates without checking on curve
fn from_raw_uncompressed_le_unchecked(
encoded: &Self::Uncompressed,
_infinity: bool
) -> Result<Self, GroupDecodingError> {
let copy = encoded.0;
if copy.iter().all(|b| *b == 0) {
return Ok(Self::zero());
}
let mut x = FqRepr([0; 4]);
let mut y = FqRepr([0; 4]);
{
let mut reader = &copy[..];
x.read_be(&mut reader).unwrap();
y.read_be(&mut reader).unwrap();
}
Ok(G1Affine {
x: Fq::from_raw_repr(x).map_err(|e| {
GroupDecodingError::CoordinateDecodingError("x coordinate", e)
})?,
y: Fq::from_raw_repr(y).map_err(|e| {
GroupDecodingError::CoordinateDecodingError("y coordinate", e)
})?,
infinity: false,
})
}
fn from_raw_uncompressed_le(encoded: &Self::Uncompressed, _infinity: bool) -> Result<Self, GroupDecodingError> {
let affine = Self::from_raw_uncompressed_le_unchecked(&encoded, _infinity)?;
if !affine.is_on_curve() {
Err(GroupDecodingError::NotOnCurve)
} else {
Ok(affine)
}
}
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]

@ -246,6 +246,12 @@ pub trait RawEncodable: CurveAffine {
/// Converts this element into its uncompressed encoding, so long as it's not /// Converts this element into its uncompressed encoding, so long as it's not
/// the point at infinity. Leaves coordinates in Montgommery form /// the point at infinity. Leaves coordinates in Montgommery form
fn into_raw_uncompressed_le(&self) -> Self::Uncompressed; fn into_raw_uncompressed_le(&self) -> Self::Uncompressed;
/// Creates a point from raw encoded coordinates without checking on curve
fn from_raw_uncompressed_le_unchecked(encoded: &Self::Uncompressed, infinity: bool) -> Result<Self, GroupDecodingError>;
/// Creates a point from raw encoded coordinates
fn from_raw_uncompressed_le(encoded: &Self::Uncompressed, infinity: bool) -> Result<Self, GroupDecodingError>;
} }
/// An encoded elliptic curve point, which should essentially wrap a `[u8; N]`. /// An encoded elliptic curve point, which should essentially wrap a `[u8; N]`.