From 4f1de2d3b5451290e6ada98a239f707638760632 Mon Sep 17 00:00:00 2001 From: microproofs Date: Fri, 20 Sep 2024 13:41:58 -0400 Subject: [PATCH] Add test for flat error when encoding bls constant --- crates/uplc/src/flat.rs | 56 ++++++++++--------- examples/acceptance_tests/113/aiken.toml | 9 +++ .../acceptance_tests/113/validators/foo.ak | 21 +++++++ 3 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 examples/acceptance_tests/113/aiken.toml create mode 100644 examples/acceptance_tests/113/validators/foo.ak diff --git a/crates/uplc/src/flat.rs b/crates/uplc/src/flat.rs index 78a5e051..fb363cc2 100644 --- a/crates/uplc/src/flat.rs +++ b/crates/uplc/src/flat.rs @@ -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::::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::::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, d: &mut Decoder) -> Result { let p1 = Vec::::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::::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(), diff --git a/examples/acceptance_tests/113/aiken.toml b/examples/acceptance_tests/113/aiken.toml new file mode 100644 index 00000000..c1ee3b1c --- /dev/null +++ b/examples/acceptance_tests/113/aiken.toml @@ -0,0 +1,9 @@ +name = "aiken-lang/acceptance_test_113" +version = "0.0.0" +license = "Apache-2.0" +description = "Aiken contracts for project 'aiken-lang/113'" + +[repository] +user = "aiken-lang" +project = "113" +platform = "github" diff --git a/examples/acceptance_tests/113/validators/foo.ak b/examples/acceptance_tests/113/validators/foo.ak new file mode 100644 index 00000000..4a7a3d69 --- /dev/null +++ b/examples/acceptance_tests/113/validators/foo.ak @@ -0,0 +1,21 @@ +use aiken/builtin + +pub const generator_g1: G1Element = + #"97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb" + +pub const generator_g2: G2Element = + #"93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8" + +validator here { + mint(_rdr, _policy_id, _tx) { + let g1 = builtin.bls12_381_g1_compress(generator_g1) + + let g2 = builtin.bls12_381_g2_compress(generator_g2) + + g1 != g2 + } + + else(_) { + fail + } +}