2021-02-25 08:06:54 -08:00
|
|
|
// -*- mode: rust; -*-
|
|
|
|
//
|
|
|
|
// This file is part of redjubjub.
|
|
|
|
// Copyright (c) 2019-2021 Zcash Foundation
|
|
|
|
// See LICENSE for licensing information.
|
|
|
|
//
|
|
|
|
// Authors:
|
|
|
|
// - Deirdre Connolly <deirdre@zfnd.org>
|
|
|
|
// - Henry de Valence <hdevalence@hdevalence.ca>
|
|
|
|
|
2020-07-03 15:23:28 -07:00
|
|
|
use blake2b_simd::{Params, State};
|
2021-03-01 06:54:52 -08:00
|
|
|
use jubjub::Scalar;
|
2019-12-03 19:54:31 -08:00
|
|
|
|
|
|
|
/// Provides H^star, the hash-to-scalar function used by RedJubjub.
|
|
|
|
pub struct HStar {
|
|
|
|
state: State,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for HStar {
|
|
|
|
fn default() -> Self {
|
|
|
|
let state = Params::new()
|
|
|
|
.hash_length(64)
|
|
|
|
.personal(b"Zcash_RedJubjubH")
|
|
|
|
.to_state();
|
|
|
|
Self { state }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HStar {
|
2019-12-03 19:19:36 -08:00
|
|
|
/// Add `data` to the hash, and return `Self` for chaining.
|
2020-07-03 15:23:28 -07:00
|
|
|
pub fn update(&mut self, data: impl AsRef<[u8]>) -> &mut Self {
|
|
|
|
self.state.update(data.as_ref());
|
2019-12-03 19:19:36 -08:00
|
|
|
self
|
2019-12-03 19:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume `self` to compute the hash output.
|
2020-07-03 15:23:28 -07:00
|
|
|
pub fn finalize(&self) -> Scalar {
|
2019-12-03 19:54:31 -08:00
|
|
|
Scalar::from_bytes_wide(self.state.finalize().as_array())
|
|
|
|
}
|
|
|
|
}
|