Skip to content

Commit

Permalink
add bytesn conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Oct 23, 2024
1 parent 39556b0 commit 4817b5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions soroban-sdk/src/crypto/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
12 changes: 9 additions & 3 deletions soroban-sdk/src/tests/crypto_bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ fn test_bls_g2() {
let vp: Vec<G2Affine> = vec![&env, one.clone(), one.clone()];
let vs: Vec<Fr> = 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)
Expand Down

0 comments on commit 4817b5c

Please sign in to comment.