diff --git a/bindings/blst.hpp b/bindings/blst.hpp index 41eb3135..a84c1f00 100644 --- a/bindings/blst.hpp +++ b/bindings/blst.hpp @@ -808,6 +808,8 @@ class PT { PT* mul(const PT& p) { blst_fp12_mul(&value, &value, p); return this; } PT* final_exp() { blst_final_exp(&value, &value); return this; } bool in_group() const { return blst_fp12_in_group(&value); } + void to_bendian(byte out[48*12]) const + { blst_bendian_from_fp12(out, &value); } static bool finalverify(const PT& gt1, const PT& gt2) { return blst_fp12_finalverify(gt1, gt2); } diff --git a/bindings/blst_aux.h b/bindings/blst_aux.h index 41c2901b..cd22afad 100644 --- a/bindings/blst_aux.h +++ b/bindings/blst_aux.h @@ -75,5 +75,6 @@ void blst_p2_unchecked_mult(blst_p2 *out, const blst_p2 *p, const byte *scalar, void blst_pairing_raw_aggregate(blst_pairing *ctx, const blst_p2_affine *q, const blst_p1_affine *p); blst_fp12 *blst_pairing_as_fp12(blst_pairing *ctx); +void blst_bendian_from_fp12(byte out[48*12], const blst_fp12 *a); #endif diff --git a/bindings/go/blst.go b/bindings/go/blst.go index 88a245ac..3e6e285e 100644 --- a/bindings/go/blst.go +++ b/bindings/go/blst.go @@ -207,6 +207,12 @@ func (pt *Fp12) InGroup() bool { return bool(C.blst_fp12_in_group(pt)) } +func (pt *Fp12) ToBendian() []byte { + var out [BLST_FP_BYTES * 12]byte + C.blst_bendian_from_fp12((*C.byte)(&out[0]), pt) + return out[:] +} + // // MIN-PK // diff --git a/bindings/go/blst.tgo b/bindings/go/blst.tgo index 4ab1bf9b..530a9602 100644 --- a/bindings/go/blst.tgo +++ b/bindings/go/blst.tgo @@ -196,3 +196,9 @@ func (pt *Fp12) FinalExp() { func (pt *Fp12) InGroup() bool { return bool(C.blst_fp12_in_group(pt)) } + +func (pt *Fp12) ToBendian() []byte { + var out [BLST_FP_BYTES*12]byte + C.blst_bendian_from_fp12((*C.byte)(&out[0]), pt) + return out[:] +} diff --git a/bindings/rust/src/bindings.rs b/bindings/rust/src/bindings.rs index b9cc3c64..29a65823 100644 --- a/bindings/rust/src/bindings.rs +++ b/bindings/rust/src/bindings.rs @@ -7,7 +7,7 @@ macro_rules! offsetof { } }; } -/* automatically generated by rust-bindgen 0.59.1 */ +/* automatically generated by rust-bindgen 0.59.2 */ #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -1293,3 +1293,6 @@ extern "C" { extern "C" { pub fn blst_pairing_as_fp12(ctx: *mut blst_pairing) -> *mut blst_fp12; } +extern "C" { + pub fn blst_bendian_from_fp12(out: *mut byte, a: *const blst_fp12); +} diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 5c3714ad..4cea2973 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -156,6 +156,14 @@ impl blst_fp12 { pub fn finalverify(a: &Self, b: &Self) -> bool { unsafe { blst_fp12_finalverify(a, b) } } + + pub fn to_bendian(&self) -> [u8; 48 * 12] { + let mut out = MaybeUninit::<[u8; 48 * 12]>::uninit(); + unsafe { + blst_bendian_from_fp12(out.as_mut_ptr() as *mut u8, self); + out.assume_init() + } + } } impl blst_scalar { diff --git a/build/win64/blst.def b/build/win64/blst.def index 95f2f07a..5c2e0528 100644 --- a/build/win64/blst.def +++ b/build/win64/blst.def @@ -200,4 +200,5 @@ EXPORTS blst_p2_unchecked_mult blst_pairing_raw_aggregate blst_pairing_as_fp12 + blst_bendian_from_fp12 diff --git a/src/fp12_tower.c b/src/fp12_tower.c index 037b7db1..a345432a 100644 --- a/src/fp12_tower.c +++ b/src/fp12_tower.c @@ -769,3 +769,18 @@ int blst_fp12_is_one(const vec384fp12 a) const vec384fp12 *blst_fp12_one(void) { return (const vec384fp12 *)BLS12_381_Rx.p12; } + +void blst_bendian_from_fp12(unsigned char ret[48*12], const vec384fp12 a) +{ + size_t i, j; + vec384 out; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { + from_fp(out, a[j][i][0]); + be_bytes_from_limbs(ret, out, sizeof(vec384)); ret += 48; + from_fp(out, a[j][i][1]); + be_bytes_from_limbs(ret, out, sizeof(vec384)); ret += 48; + } + } +}