1use crate::Error;
9
10use p256::{P256Point, P256Scalar};
11
12#[allow(non_upper_case_globals, unused)]
13const identifier: &[u8] = b"P256-SHA256";
14
15pub type P256SerializedPoint = [u8; 33];
16
17pub fn serialize_element(p: &P256Point) -> P256SerializedPoint {
21 p256::serialize_point(p)
22}
23
24#[allow(unused)]
25pub fn deserialize_element(pm: P256SerializedPoint) -> Result<P256Point, Error> {
26 p256::deserialize_point(pm).map_err(|e| e.into())
27}
28
29pub fn identity() -> P256Point {
30 P256Point::AtInfinity
31}
32
33pub fn scalar_inverse(s: P256Scalar) -> P256Scalar {
34 use hacspec_lib::hacspec_helper::NatMod;
35 s.inv()
36}
37
38pub fn hash_to_scalar(bytes: &[u8], context_string: &[u8]) -> Result<P256Scalar, Error> {
42 let mut dst: Vec<u8> = "HashToScalar-".into(); dst.extend_from_slice(context_string);
44 hash_to_scalar_dst(bytes, &dst, context_string)
45}
46
47pub fn hash_to_scalar_dst(
48 bytes: &[u8],
49 dst: &[u8],
50 context_string: &[u8],
51) -> Result<P256Scalar, Error> {
52 let mut dst = dst.to_vec();
53 dst.extend_from_slice(context_string);
54
55 hash_to_curve::p256_hash::hash_to_scalar(bytes, &dst, 1)
56 .map(|v| v[0])
57 .map_err(|e| e.into())
58}
59
60pub fn hash_to_group(bytes: &[u8], context_string: &[u8]) -> Result<P256Point, Error> {
64 let mut dst: Vec<u8> = b"HashToGroup-".to_vec();
65 dst.extend_from_slice(context_string);
66
67 hash_to_curve::p256_hash::hash_to_curve(bytes, &dst).map_err(|e| e.into())
68}
69
70#[test]
71fn serialize_deserialize() {
72 use hacspec_lib::Randomness;
73 let p: P256Point = p256::p256_point_mul_base(
74 p256::random_scalar(&mut Randomness::new(vec![0xab; 32]), b"OPRF-Test").unwrap(),
75 )
76 .unwrap();
77
78 debug_assert_eq!(p, deserialize_element(serialize_element(&p)).unwrap());
79}