From 47bc240f722bc1c95d90aa26fbb6e0dec0d9ed41 Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Mon, 16 Oct 2023 19:24:53 +0200 Subject: [PATCH] bindings/rust/src/lib.rs: add scalar inversion test. --- bindings/rust/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 3d315383..68c5f407 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -2026,3 +2026,35 @@ mod fp12_test { ); } } + +#[cfg(test)] +mod sk_test { + use super::*; + use rand::{RngCore, SeedableRng}; + use rand_chacha::ChaCha20Rng; + + #[test] + fn inverse() { + let mut bytes = [0u8; 64]; + ChaCha20Rng::from_entropy().fill_bytes(bytes.as_mut()); + + let mut sk = blst_scalar::default(); + let mut p1 = blst_p1::default(); + let mut p2 = blst_p2::default(); + + unsafe { + blst_scalar_from_be_bytes(&mut sk, bytes.as_ptr(), bytes.len()); + + blst_p1_mult(&mut p1, blst_p1_generator(), sk.b.as_ptr(), 255); + blst_sk_inverse(&mut sk, &sk); + blst_p1_mult(&mut p1, &p1, sk.b.as_ptr(), 255); + + blst_p2_mult(&mut p2, blst_p2_generator(), sk.b.as_ptr(), 255); + blst_sk_inverse(&mut sk, &sk); + blst_p2_mult(&mut p2, &p2, sk.b.as_ptr(), 255); + } + + assert_eq!(p1, unsafe { *blst_p1_generator() }); + assert_eq!(p2, unsafe { *blst_p2_generator() }); + } +}