feat(bls): pretty printing for g1 and g1 element

Co-authored-by: Kasey White <kwhitemsg@gmail.com>
This commit is contained in:
rvcas
2023-11-01 23:17:56 -04:00
committed by Lucas
parent 0d2ac952d0
commit f101581813
2 changed files with 49 additions and 22 deletions

View File

@@ -32,8 +32,6 @@ const BLST_P1_COMPRESSED_SIZE: usize = 48;
const BLST_P2_COMPRESSED_SIZE: usize = 96;
// 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001;
//#[derive(std::cmp::PartialEq)]
//pub enum EvalMode {
// Immediate,
@@ -1231,7 +1229,6 @@ impl DefaultFunction {
blst::blst_p1_mult(
&mut out as *mut _,
// This was true in the Cardano code
arg2 as *const _,
scalar.b.as_ptr() as *const _,
size_scalar * 8,
@@ -1255,11 +1252,7 @@ impl DefaultFunction {
DefaultFunction::Bls12_381_G1_Compress => {
let arg1 = args[0].unwrap_bls12_381_g1_element();
let mut out = [0; BLST_P1_COMPRESSED_SIZE];
unsafe {
blst::blst_p1_compress(&mut out as *mut _, arg1);
};
let out = arg1.compress();
let constant = Constant::ByteString(out.to_vec());
@@ -1389,7 +1382,6 @@ impl DefaultFunction {
blst::blst_p2_mult(
&mut out as *mut _,
// This was true in the Cardano code
arg2 as *const _,
scalar.b.as_ptr() as *const _,
size_scalar * 8,
@@ -1413,11 +1405,7 @@ impl DefaultFunction {
DefaultFunction::Bls12_381_G2_Compress => {
let arg1 = args[0].unwrap_bls12_381_g2_element();
let mut out = [0; BLST_P2_COMPRESSED_SIZE];
unsafe {
blst::blst_p2_compress(&mut out as *mut _, arg1);
};
let out = arg1.compress();
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> {
if (121..=127).contains(&tag) {
Some(tag - 121)