bls12_381_serde.rs
1 pub mod scalar { 2 use bls12_381::Scalar; 3 use serde::de::Error; 4 use serde::{Deserializer, Serializer}; 5 6 pub fn serialize<S: Serializer>(scalar: &Scalar, s: S) -> Result<S::Ok, S::Error> { 7 serdect::array::serialize_hex_lower_or_bin(&scalar.to_bytes(), s) 8 } 9 10 pub fn deserialize<'d, D: Deserializer<'d>>(d: D) -> Result<Scalar, D::Error> { 11 let mut byte_array = [0; 32]; 12 13 serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; 14 15 Option::from(Scalar::from_bytes(&byte_array)) 16 .ok_or_else(|| Error::custom("Could not decode scalar")) 17 } 18 } 19 20 pub mod g1 { 21 use bls12_381::G1Affine; 22 use serde::de::Error; 23 use serde::{Deserializer, Serializer}; 24 25 pub fn serialize<S: Serializer>(point: &G1Affine, s: S) -> Result<S::Ok, S::Error> { 26 serdect::array::serialize_hex_lower_or_bin(&point.to_compressed(), s) 27 } 28 29 pub fn deserialize<'d, D: Deserializer<'d>>(d: D) -> Result<G1Affine, D::Error> { 30 let mut byte_array = [0; 48]; 31 32 serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; 33 34 Option::from(G1Affine::from_compressed(&byte_array)) 35 .ok_or_else(|| Error::custom("Could not decode compressed group element")) 36 } 37 } 38 39 pub mod g2 { 40 use bls12_381::G2Affine; 41 use serde::de::Error; 42 use serde::{Deserializer, Serializer}; 43 44 pub fn serialize<S: Serializer>(point: &G2Affine, s: S) -> Result<S::Ok, S::Error> { 45 serdect::array::serialize_hex_lower_or_bin(&point.to_compressed(), s) 46 } 47 48 pub fn deserialize<'d, D: Deserializer<'d>>(d: D) -> Result<G2Affine, D::Error> { 49 let mut byte_array = [0; 96]; 50 51 serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; 52 53 Option::from(G2Affine::from_compressed(&byte_array)) 54 .ok_or_else(|| Error::custom("Could not decode compressed group element")) 55 } 56 }