feat(bls): pretty printing for g1 and g1 element
Co-authored-by: Kasey White <kwhitemsg@gmail.com>
This commit is contained in:
		
							parent
							
								
									0d2ac952d0
								
							
						
					
					
						commit
						f101581813
					
				| 
						 | 
					@ -32,8 +32,6 @@ const BLST_P1_COMPRESSED_SIZE: usize = 48;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const BLST_P2_COMPRESSED_SIZE: usize = 96;
 | 
					const BLST_P2_COMPRESSED_SIZE: usize = 96;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//#[derive(std::cmp::PartialEq)]
 | 
					//#[derive(std::cmp::PartialEq)]
 | 
				
			||||||
//pub enum EvalMode {
 | 
					//pub enum EvalMode {
 | 
				
			||||||
//    Immediate,
 | 
					//    Immediate,
 | 
				
			||||||
| 
						 | 
					@ -1231,7 +1229,6 @@ impl DefaultFunction {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    blst::blst_p1_mult(
 | 
					                    blst::blst_p1_mult(
 | 
				
			||||||
                        &mut out as *mut _,
 | 
					                        &mut out as *mut _,
 | 
				
			||||||
                        // This was true in the Cardano code
 | 
					 | 
				
			||||||
                        arg2 as *const _,
 | 
					                        arg2 as *const _,
 | 
				
			||||||
                        scalar.b.as_ptr() as *const _,
 | 
					                        scalar.b.as_ptr() as *const _,
 | 
				
			||||||
                        size_scalar * 8,
 | 
					                        size_scalar * 8,
 | 
				
			||||||
| 
						 | 
					@ -1255,11 +1252,7 @@ impl DefaultFunction {
 | 
				
			||||||
            DefaultFunction::Bls12_381_G1_Compress => {
 | 
					            DefaultFunction::Bls12_381_G1_Compress => {
 | 
				
			||||||
                let arg1 = args[0].unwrap_bls12_381_g1_element();
 | 
					                let arg1 = args[0].unwrap_bls12_381_g1_element();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let mut out = [0; BLST_P1_COMPRESSED_SIZE];
 | 
					                let out = arg1.compress();
 | 
				
			||||||
 | 
					 | 
				
			||||||
                unsafe {
 | 
					 | 
				
			||||||
                    blst::blst_p1_compress(&mut out as *mut _, arg1);
 | 
					 | 
				
			||||||
                };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let constant = Constant::ByteString(out.to_vec());
 | 
					                let constant = Constant::ByteString(out.to_vec());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1389,7 +1382,6 @@ impl DefaultFunction {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    blst::blst_p2_mult(
 | 
					                    blst::blst_p2_mult(
 | 
				
			||||||
                        &mut out as *mut _,
 | 
					                        &mut out as *mut _,
 | 
				
			||||||
                        // This was true in the Cardano code
 | 
					 | 
				
			||||||
                        arg2 as *const _,
 | 
					                        arg2 as *const _,
 | 
				
			||||||
                        scalar.b.as_ptr() as *const _,
 | 
					                        scalar.b.as_ptr() as *const _,
 | 
				
			||||||
                        size_scalar * 8,
 | 
					                        size_scalar * 8,
 | 
				
			||||||
| 
						 | 
					@ -1413,11 +1405,7 @@ impl DefaultFunction {
 | 
				
			||||||
            DefaultFunction::Bls12_381_G2_Compress => {
 | 
					            DefaultFunction::Bls12_381_G2_Compress => {
 | 
				
			||||||
                let arg1 = args[0].unwrap_bls12_381_g2_element();
 | 
					                let arg1 = args[0].unwrap_bls12_381_g2_element();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let mut out = [0; BLST_P2_COMPRESSED_SIZE];
 | 
					                let out = arg1.compress();
 | 
				
			||||||
 | 
					 | 
				
			||||||
                unsafe {
 | 
					 | 
				
			||||||
                    blst::blst_p2_compress(&mut out as *mut _, arg1);
 | 
					 | 
				
			||||||
                };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let constant = Constant::ByteString(out.to_vec());
 | 
					                let constant = Constant::ByteString(out.to_vec());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1529,6 +1517,34 @@ impl DefaultFunction {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub trait Compressable {
 | 
				
			||||||
 | 
					    fn compress(&self) -> Vec<u8>;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Compressable for blst::blst_p1 {
 | 
				
			||||||
 | 
					    fn compress(&self) -> Vec<u8> {
 | 
				
			||||||
 | 
					        let mut out = [0; BLST_P1_COMPRESSED_SIZE];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        unsafe {
 | 
				
			||||||
 | 
					            blst::blst_p1_compress(&mut out as *mut _, self);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        out.to_vec()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Compressable for blst::blst_p2 {
 | 
				
			||||||
 | 
					    fn compress(&self) -> Vec<u8> {
 | 
				
			||||||
 | 
					        let mut out = [0; BLST_P2_COMPRESSED_SIZE];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        unsafe {
 | 
				
			||||||
 | 
					            blst::blst_p2_compress(&mut out as *mut _, self);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        out.to_vec()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn convert_tag_to_constr(tag: u64) -> Option<u64> {
 | 
					pub fn convert_tag_to_constr(tag: u64) -> Option<u64> {
 | 
				
			||||||
    if (121..=127).contains(&tag) {
 | 
					    if (121..=127).contains(&tag) {
 | 
				
			||||||
        Some(tag - 121)
 | 
					        Some(tag - 121)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    ast::{Constant, Program, Term, Type},
 | 
					    ast::{Constant, Program, Term, Type},
 | 
				
			||||||
    flat::Binder,
 | 
					    flat::Binder,
 | 
				
			||||||
 | 
					    machine::runtime::Compressable,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use pallas_primitives::babbage::{Constr, PlutusData};
 | 
					use pallas_primitives::babbage::{Constr, PlutusData};
 | 
				
			||||||
use pretty::RcDoc;
 | 
					use pretty::RcDoc;
 | 
				
			||||||
| 
						 | 
					@ -256,9 +257,15 @@ impl Constant {
 | 
				
			||||||
                .append(RcDoc::text("("))
 | 
					                .append(RcDoc::text("("))
 | 
				
			||||||
                .append(Self::to_doc_list_plutus_data(d))
 | 
					                .append(Self::to_doc_list_plutus_data(d))
 | 
				
			||||||
                .append(RcDoc::text(")")),
 | 
					                .append(RcDoc::text(")")),
 | 
				
			||||||
            Constant::Bls12_381G1Element(_) => todo!(),
 | 
					            Constant::Bls12_381G1Element(p1) => RcDoc::text("bls12_381_G1_element ")
 | 
				
			||||||
            Constant::Bls12_381G2Element(_) => todo!(),
 | 
					                .append(RcDoc::line())
 | 
				
			||||||
            Constant::Bls12_381MlResult(_) => todo!(),
 | 
					                .append(RcDoc::text("0x"))
 | 
				
			||||||
 | 
					                .append(RcDoc::text(hex::encode(p1.compress()))),
 | 
				
			||||||
 | 
					            Constant::Bls12_381G2Element(p2) => RcDoc::text("bls12_381_G2_element ")
 | 
				
			||||||
 | 
					                .append(RcDoc::line())
 | 
				
			||||||
 | 
					                .append(RcDoc::text("0x"))
 | 
				
			||||||
 | 
					                .append(RcDoc::text(hex::encode(p2.compress()))),
 | 
				
			||||||
 | 
					            Constant::Bls12_381MlResult(_) => panic!("cannot represent Bls12_381MlResult as text"),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -284,9 +291,13 @@ impl Constant {
 | 
				
			||||||
                .append(RcDoc::text(")")),
 | 
					                .append(RcDoc::text(")")),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Constant::Data(data) => Self::to_doc_list_plutus_data(data),
 | 
					            Constant::Data(data) => Self::to_doc_list_plutus_data(data),
 | 
				
			||||||
            Constant::Bls12_381G1Element(_) => todo!(),
 | 
					            Constant::Bls12_381G1Element(p1) => {
 | 
				
			||||||
            Constant::Bls12_381G2Element(_) => todo!(),
 | 
					                RcDoc::text("0x").append(RcDoc::text(hex::encode(p1.compress())))
 | 
				
			||||||
            Constant::Bls12_381MlResult(_) => todo!(),
 | 
					            }
 | 
				
			||||||
 | 
					            Constant::Bls12_381G2Element(p2) => {
 | 
				
			||||||
 | 
					                RcDoc::text("0x").append(RcDoc::text(hex::encode(p2.compress())))
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            Constant::Bls12_381MlResult(_) => panic!("cannot represent Bls12_381MlResult as text"),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -358,8 +369,8 @@ impl Type {
 | 
				
			||||||
                .append(r.to_doc())
 | 
					                .append(r.to_doc())
 | 
				
			||||||
                .append(")"),
 | 
					                .append(")"),
 | 
				
			||||||
            Type::Data => RcDoc::text("data"),
 | 
					            Type::Data => RcDoc::text("data"),
 | 
				
			||||||
            Type::Bls12_381G1Element => todo!(),
 | 
					            Type::Bls12_381G1Element => RcDoc::text("bls12_381_G1_element"),
 | 
				
			||||||
            Type::Bls12_381G2Element => todo!(),
 | 
					            Type::Bls12_381G2Element => RcDoc::text("bls12_381_G1_element"),
 | 
				
			||||||
            Type::Bls12_381MlResult => todo!(),
 | 
					            Type::Bls12_381MlResult => todo!(),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue