Add test for flat error when encoding bls constant

This commit is contained in:
microproofs
2024-09-20 13:41:58 -04:00
parent c3af748b76
commit 4f1de2d3b5
3 changed files with 60 additions and 26 deletions

View File

@@ -547,19 +547,19 @@ impl Encode for Constant {
cbor.encode(e)?;
}
Constant::Bls12_381G1Element(b) => {
Constant::Bls12_381G1Element(_) => {
encode_constant(&[9], e)?;
let x = b.compress();
x.encode(e)?;
return Err(en::Error::Message(
"BLS12-381 G1 points are not supported for flat encoding".to_string(),
));
}
Constant::Bls12_381G2Element(b) => {
Constant::Bls12_381G2Element(_) => {
encode_constant(&[10], e)?;
let x = b.compress();
x.encode(e)?;
return Err(en::Error::Message(
"BLS12-381 G2 points are not supported for flat encoding".to_string(),
));
}
Constant::Bls12_381MlResult(_) => {
encode_constant(&[11], e)?;
@@ -597,16 +597,12 @@ fn encode_constant_value(x: &Constant, e: &mut Encoder) -> Result<(), en::Error>
cbor.encode(e)
}
Constant::Bls12_381G1Element(b) => {
let x = b.compress();
x.encode(e)
}
Constant::Bls12_381G2Element(b) => {
let x = b.compress();
x.encode(e)
}
Constant::Bls12_381G1Element(_) => Err(en::Error::Message(
"BLS12-381 G1 points are not supported for flat encoding".to_string(),
)),
Constant::Bls12_381G2Element(_) => Err(en::Error::Message(
"BLS12-381 G2 points are not supported for flat encoding".to_string(),
)),
Constant::Bls12_381MlResult(_) => Err(en::Error::Message(
"BLS12-381 ML results are not supported for flat encoding".to_string(),
)),
@@ -676,21 +672,25 @@ impl<'b> Decode<'b> for Constant {
[9] => {
let p1 = Vec::<u8>::decode(d)?;
let p1 = blst::blst_p1::uncompress(&p1).map_err(|err| {
let _p1 = blst::blst_p1::uncompress(&p1).map_err(|err| {
de::Error::Message(format!("Failed to uncompress p1: {}", err))
})?;
Ok(Constant::Bls12_381G1Element(p1.into()))
Err(de::Error::Message(format!(
"BLS12-381 G1 points are not supported for flat decoding."
)))
}
[10] => {
let p2 = Vec::<u8>::decode(d)?;
let p2 = blst::blst_p2::uncompress(&p2).map_err(|err| {
let _p2 = blst::blst_p2::uncompress(&p2).map_err(|err| {
de::Error::Message(format!("Failed to uncompress p2: {}", err))
})?;
Ok(Constant::Bls12_381G2Element(p2.into()))
Err(de::Error::Message(format!(
"BLS12-381 G2 points are not supported for flat decoding."
)))
}
[11] => Err(de::Error::Message(
"BLS12-381 ML results are not supported for flat decoding".to_string(),
@@ -737,18 +737,22 @@ fn decode_constant_value(typ: Rc<Type>, d: &mut Decoder) -> Result<Constant, de:
Type::Bls12_381G1Element => {
let p1 = Vec::<u8>::decode(d)?;
let p1 = blst::blst_p1::uncompress(&p1)
let _p1 = blst::blst_p1::uncompress(&p1)
.map_err(|err| de::Error::Message(format!("Failed to uncompress p1: {}", err)))?;
Ok(Constant::Bls12_381G1Element(p1.into()))
Err(de::Error::Message(format!(
"BLS12-381 G1 points are not supported for flat decoding."
)))
}
Type::Bls12_381G2Element => {
let p2 = Vec::<u8>::decode(d)?;
let p2 = blst::blst_p2::uncompress(&p2)
let _p2 = blst::blst_p2::uncompress(&p2)
.map_err(|err| de::Error::Message(format!("Failed to uncompress p2: {}", err)))?;
Ok(Constant::Bls12_381G2Element(p2.into()))
Err(de::Error::Message(format!(
"BLS12-381 G2 points are not supported for flat decoding."
)))
}
Type::Bls12_381MlResult => Err(de::Error::Message(
"BLS12-381 ML results are not supported for flat decoding".to_string(),