zebra_chain/orchard/sinsemilla.rs
1//! Sinsemilla hash functions and helpers.
2
3use bitvec::prelude::*;
4use halo2::pasta::pallas;
5use sinsemilla::HashDomain;
6
7/// Sinsemilla Hash Function
8///
9/// "SinsemillaHash is an algebraic hash function with collision resistance (for
10/// fixed input length) derived from assumed hardness of the Discrete Logarithm
11/// Problem. It is designed by Sean Bowe and Daira Hopwood. The motivation for
12/// introducing a new discrete-log-based hash function (rather than using
13/// PedersenHash) is to make efficient use of the lookups available in recent
14/// proof systems including Halo 2."
15///
16/// SinsemillaHash: B^Y^\[N\] ร B[{0 .. ๐ยท๐}] โ P_๐ฅ โช {โฅ}
17///
18/// <https://zips.z.cash/protocol/nu5.pdf#concretesinsemillahash>
19#[allow(non_snake_case)]
20pub fn sinsemilla_hash(D: &[u8], M: &BitVec<u8, Lsb0>) -> Option<pallas::Base> {
21 let domain = std::str::from_utf8(D).expect("must be valid UTF-8");
22 let hash_domain = HashDomain::new(domain);
23
24 hash_domain.hash(M.iter().map(|b| *b.as_ref())).into()
25}
26
27#[cfg(test)]
28mod tests {
29
30 use super::*;
31 use crate::orchard::tests::vectors;
32
33 // Checks Sinsemilla hashes to point and to bytes (aka the x-coordinate
34 // bytes of a point) with:
35 // - One of two domains.
36 // - Random message lengths between 0 and 255 bytes.
37 // - Random message bits.
38 #[test]
39 #[allow(non_snake_case)]
40 fn sinsemilla_hackworks_test_vectors() {
41 use halo2::pasta::group::ff::PrimeField;
42
43 for tv in tests::vectors::SINSEMILLA.iter() {
44 let D = tv.domain.as_slice();
45 let M: &BitVec<u8, Lsb0> = &tv.msg.iter().collect();
46
47 assert_eq!(
48 sinsemilla_hash(D, M).expect("should not fail per Theorem 5.4.4"),
49 pallas::Base::from_repr(tv.hash).unwrap()
50 )
51 }
52 }
53}