From 4817b5cf672e1c9ec1a336a6c9c9b3fe867448b4 Mon Sep 17 00:00:00 2001 From: Jay Geng Date: Wed, 23 Oct 2024 14:38:30 -0400 Subject: [PATCH] add bytesn conversion --- soroban-sdk/src/crypto/bls12_381.rs | 8 ++++++++ soroban-sdk/src/tests/crypto_bls12_381.rs | 12 +++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/soroban-sdk/src/crypto/bls12_381.rs b/soroban-sdk/src/crypto/bls12_381.rs index 26338cee..ba66723a 100644 --- a/soroban-sdk/src/crypto/bls12_381.rs +++ b/soroban-sdk/src/crypto/bls12_381.rs @@ -198,6 +198,14 @@ impl Fr { &self.0 } + pub fn from_bytes(bytes: BytesN<32>) -> Self { + U256::from_be_bytes(bytes.env(), bytes.as_ref()).into() + } + + pub fn to_bytes(&self) -> BytesN<32> { + self.as_u256().to_be_bytes().try_into().unwrap_optimized() + } + pub fn as_val(&self) -> &Val { self.0.as_val() } diff --git a/soroban-sdk/src/tests/crypto_bls12_381.rs b/soroban-sdk/src/tests/crypto_bls12_381.rs index 6b6761d0..a8efef6f 100644 --- a/soroban-sdk/src/tests/crypto_bls12_381.rs +++ b/soroban-sdk/src/tests/crypto_bls12_381.rs @@ -80,10 +80,16 @@ fn test_bls_g2() { let vp: Vec = vec![&env, one.clone(), one.clone()]; let vs: Vec = vec![ &env, - U256::from_u32(&env, 1).into(), - U256::from_u32(&env, 0).into(), + Fr::from_bytes(bytesn!( + &env, + 0x0000000000000000000000000000000000000000000000000000000000000001 + )), + Fr::from_bytes(bytesn!( + &env, + 0x0000000000000000000000000000000000000000000000000000000000000000 + )), ]; - let res = bls12_381.g2_msm(vp, vs); + let res = bls12_381.g2_msm(vp.clone(), vs); assert_eq!(res, one); // map to curve (test case from https://datatracker.ietf.org/doc/html/rfc9380)