47 lines
1.0 KiB
Rust
Raw Normal View History

2018-03-08 00:41:47 -07:00
use jubjub::{
JubjubEngine,
PrimeOrder,
edwards
};
use ff::{
PrimeField
2018-03-08 00:41:47 -07:00
};
use blake2_rfc::blake2s::Blake2s;
use constants;
2018-03-06 22:26:03 -07:00
/// Produces a random point in the Jubjub curve.
/// The point is guaranteed to be prime order
/// and not the identity.
2017-12-18 11:00:10 -07:00
pub fn group_hash<E: JubjubEngine>(
tag: &[u8],
personalization: &[u8],
2017-12-18 11:00:10 -07:00
params: &E::Params
) -> Option<edwards::Point<E, PrimeOrder>>
2017-12-18 11:00:10 -07:00
{
assert_eq!(personalization.len(), 8);
2017-12-18 11:00:10 -07:00
// Check to see that scalar field is 255 bits
assert!(E::Fr::NUM_BITS == 255);
let mut h = Blake2s::with_params(32, &[], &[], personalization);
h.update(constants::GH_FIRST_BLOCK);
h.update(tag);
let h = h.finalize().as_ref().to_vec();
2017-12-18 11:00:10 -07:00
assert!(h.len() == 32);
match edwards::Point::<E, _>::read(&h[..], params) {
Ok(p) => {
2017-12-18 11:00:10 -07:00
let p = p.mul_by_cofactor(params);
if p != edwards::Point::zero() {
Some(p)
} else {
None
}
},
Err(_) => None
2017-12-18 11:00:10 -07:00
}
}