arkeddsa/src/signature.rs
src/signature.rsRaw
1use ark_ec::{AffineRepr, CurveGroup};
2use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
3
4/// `SignatureComponents` contains the realized parts of a signature
5#[derive(Copy, Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
6pub struct Signature<C: CurveGroup + Clone> {
7 pub r: C::Affine,
8 pub s: C::ScalarField,
9}
10
11impl<C: CurveGroup + Clone> Signature<C> {
12 /// Serializes the signature components to bytes as uncompressed.
13 /// Expect output size to be `size_of(C::BaseField) * 2 + size_of(C::ScalarField)`
14 pub fn to_bytes(&self) -> Vec<u8> {
15 let mut bytes = Vec::new();
16 self.r.serialize_uncompressed(&mut bytes).unwrap();
17 self.s.serialize_uncompressed(&mut bytes).unwrap();
18 bytes
19 }
20
21 /// Checked deserialization of the signature components from bytes.
22 /// Expects input size to be `size_of(C::BaseField) * 2 + size_of(C::ScalarField)`
23 pub fn from_bytes(bytes: &[u8]) -> Result<Self, Box<dyn ark_std::error::Error>> {
24 let point_size = C::Affine::generator().serialized_size(ark_serialize::Compress::No);
25 (bytes.len() == 32 + point_size)
26 .then_some(true)
27 .ok_or(ark_serialize::SerializationError::InvalidData)?;
28
29 let off1 = point_size;
30 let off2 = off1 + 32;
31
32 let r = C::Affine::deserialize_uncompressed(&bytes[00..off1])?;
33 let s = C::ScalarField::deserialize_uncompressed(&bytes[off1..off2])?;
34 Ok(Signature { r, s })
35 }
36
37 pub fn new(r: C::Affine, s: C::ScalarField) -> Self {
38 Self { r, s }
39 }
40
41 pub fn r(&self) -> &C::Affine {
42 &self.r
43 }
44
45 pub fn s(&self) -> &C::ScalarField {
46 &self.s
47 }
48}