crypto_bigint/uint/encoding/
der.rs1use crate::{hybrid_array::Array, ArrayEncoding, Uint};
4use ::der::{
5 asn1::{AnyRef, UintRef},
6 DecodeValue, EncodeValue, FixedTag, Length, Tag,
7};
8
9impl<'a, const LIMBS: usize> TryFrom<AnyRef<'a>> for Uint<LIMBS>
10where
11 Uint<LIMBS>: ArrayEncoding,
12{
13 type Error = der::Error;
14
15 fn try_from(any: AnyRef<'a>) -> der::Result<Uint<LIMBS>> {
16 UintRef::try_from(any)?.try_into()
17 }
18}
19
20impl<'a, const LIMBS: usize> TryFrom<UintRef<'a>> for Uint<LIMBS>
21where
22 Uint<LIMBS>: ArrayEncoding,
23{
24 type Error = der::Error;
25
26 fn try_from(bytes: UintRef<'a>) -> der::Result<Uint<LIMBS>> {
27 let mut array = Array::default();
28 let offset = array.len().saturating_sub(bytes.len().try_into()?);
29 array[offset..].copy_from_slice(bytes.as_bytes());
30 Ok(Uint::from_be_byte_array(array))
31 }
32}
33
34impl<'a, const LIMBS: usize> DecodeValue<'a> for Uint<LIMBS>
35where
36 Uint<LIMBS>: ArrayEncoding,
37{
38 fn decode_value<R: der::Reader<'a>>(reader: &mut R, header: der::Header) -> der::Result<Self> {
39 UintRef::decode_value(reader, header)?.try_into()
40 }
41}
42
43impl<const LIMBS: usize> EncodeValue for Uint<LIMBS>
44where
45 Uint<LIMBS>: ArrayEncoding,
46{
47 fn value_len(&self) -> der::Result<Length> {
48 let array = self.to_be_byte_array();
50 UintRef::new(&array)?.value_len()
51 }
52
53 fn encode_value(&self, encoder: &mut impl der::Writer) -> der::Result<()> {
54 let array = self.to_be_byte_array();
55 UintRef::new(&array)?.encode_value(encoder)
56 }
57}
58
59impl<const LIMBS: usize> FixedTag for Uint<LIMBS>
60where
61 Uint<LIMBS>: ArrayEncoding,
62{
63 const TAG: Tag = Tag::Integer;
64}