halo2curves_axiom/bls12_381/hash_to_curve/
map_scalar.rs

1//! Implementation of hash-to-field for Scalar values
2//!
3//! Source: <https://github.com/privacy-scaling-explorations/bls12_381>
4
5use super::HashToField;
6use crate::bls12_381::generic_array::{typenum::U48, GenericArray};
7use crate::bls12_381::scalar::Scalar;
8
9impl HashToField for Scalar {
10    // ceil(log2(p)) = 255, m = 1, k = 128.
11    type InputLength = U48;
12
13    fn from_okm(okm: &GenericArray<u8, U48>) -> Scalar {
14        let mut bs = [0u8; 64];
15        bs[16..].copy_from_slice(okm);
16        bs.reverse(); // into little endian
17        Scalar::from_bytes_wide(&bs)
18    }
19}
20
21#[test]
22fn test_hash_to_scalar() {
23    let tests: &[(&[u8], &str)] = &[
24        (
25            &[0u8; 48],
26            "0x0000000000000000000000000000000000000000000000000000000000000000",
27        ),
28        (
29            b"aaaaaabbbbbbccccccddddddeeeeeeffffffgggggghhhhhh",
30            "0x2228450bf55d8fe62395161bd3677ff6fc28e45b89bc87e02a818eda11a8c5da",
31        ),
32        (
33            b"111111222222333333444444555555666666777777888888",
34            "0x4aa543cbd2f0c8f37f8a375ce2e383eb343e7e3405f61e438b0a15fb8899d1ae",
35        ),
36    ];
37    for (input, expected) in tests {
38        let output = format!("{:?}", Scalar::from_okm(GenericArray::from_slice(input)));
39        assert_eq!(&output, expected);
40    }
41}