orchard/src/spec.rs

124 lines
4.2 KiB
Rust
Raw Normal View History

2021-03-05 15:25:45 -08:00
//! Helper functions defined in the Zcash Protocol Specification.
use std::iter;
use blake2b_simd::Params;
2021-03-05 15:25:45 -08:00
use ff::PrimeField;
use group::{Curve, Group};
use halo2::arithmetic::{CurveAffine, CurveExt, FieldExt};
use pasta_curves::pallas;
2021-03-05 15:25:45 -08:00
use crate::{constants::L_ORCHARD_BASE, primitives::sinsemilla};
2021-03-05 15:25:45 -08:00
const PRF_EXPAND_PERSONALIZATION: &[u8; 16] = b"Zcash_ExpandSeed";
/// $\mathsf{ToBase}^\mathsf{Orchard}(x) := LEOS2IP_{\ell_\mathsf{PRFexpand}}(x) (mod q_P)$
///
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
2021-03-05 15:25:45 -08:00
///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
pub(crate) fn to_base(x: [u8; 64]) -> pallas::Base {
pallas::Base::from_bytes_wide(&x)
2021-03-05 15:25:45 -08:00
}
/// $\mathsf{ToScalar}^\mathsf{Orchard}(x) := LEOS2IP_{\ell_\mathsf{PRFexpand}}(x) (mod r_P)$
///
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
2021-03-05 15:25:45 -08:00
///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
pub(crate) fn to_scalar(x: [u8; 64]) -> pallas::Scalar {
pallas::Scalar::from_bytes_wide(&x)
2021-03-05 15:25:45 -08:00
}
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
2021-03-05 15:25:45 -08:00
///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
2021-03-05 15:25:45 -08:00
pub(crate) fn commit_ivk(
ak: &pallas::Base,
2021-03-05 15:25:45 -08:00
nk: &pallas::Base,
rivk: &pallas::Scalar,
) -> pallas::Scalar {
// We rely on the API contract that to_le_bits() returns at least PrimeField::NUM_BITS
// bits, which is equal to L_ORCHARD_BASE.
let domain = sinsemilla::CommitDomain::new(&"z.cash:Orchard-CommitIvk");
let ivk = domain.short_commit(
2021-03-05 15:25:45 -08:00
iter::empty()
.chain(ak.to_le_bits().iter().by_val().take(L_ORCHARD_BASE))
.chain(nk.to_le_bits().iter().by_val().take(L_ORCHARD_BASE)),
2021-03-05 15:25:45 -08:00
rivk,
);
// Convert from pallas::Base to pallas::Scalar. This requires no modular reduction
// because Pallas' base field is smaller than its scalar field.
pallas::Scalar::from_repr(ivk.to_repr()).unwrap()
}
/// Defined in [Zcash Protocol Spec § 5.4.1.6: DiversifyHash^Sapling and DiversifyHash^Orchard Hash Functions][concretediversifyhash].
2021-03-05 15:25:45 -08:00
///
/// [concretediversifyhash]: https://zips.z.cash/protocol/nu5.pdf#concretediversifyhash
pub(crate) fn diversify_hash(d: &[u8; 11]) -> pallas::Point {
let hasher = pallas::Point::hash_to_curve("z.cash:Orchard-gd");
let pk_d = hasher(d);
if pk_d.is_identity().into() {
// If the identity occurs, we replace it with a different fixed point.
hasher(&[])
} else {
pk_d
}
2021-03-05 15:25:45 -08:00
}
/// $PRF^\mathsf{expand}(sk, t) := BLAKE2b-512("Zcash_ExpandSeed", sk || t)$
///
/// Defined in [Zcash Protocol Spec § 5.4.2: Pseudo Random Functions][concreteprfs].
2021-03-05 15:25:45 -08:00
///
2021-03-17 12:20:40 -07:00
/// [concreteprfs]: https://zips.z.cash/protocol/nu5.pdf#concreteprfs
pub(crate) fn prf_expand(sk: &[u8], t: &[u8]) -> [u8; 64] {
2021-03-05 15:25:45 -08:00
prf_expand_vec(sk, &[t])
}
pub(crate) fn prf_expand_vec(sk: &[u8], ts: &[&[u8]]) -> [u8; 64] {
2021-03-05 15:25:45 -08:00
let mut h = Params::new()
.hash_length(64)
.personal(PRF_EXPAND_PERSONALIZATION)
.to_state();
h.update(sk);
for t in ts {
h.update(t);
}
*h.finalize().as_array()
2021-03-05 15:25:45 -08:00
}
2021-03-17 12:20:40 -07:00
/// Defined in [Zcash Protocol Spec § 5.4.5.5: Orchard Key Agreement][concreteorchardkeyagreement].
2021-03-05 15:25:45 -08:00
///
/// [concreteorchardkeyagreement]: https://zips.z.cash/protocol/nu5.pdf#concreteorchardkeyagreement
2021-03-05 15:25:45 -08:00
pub(crate) fn ka_orchard(sk: &pallas::Scalar, b: &pallas::Point) -> pallas::Point {
b * sk
}
2021-03-17 12:20:40 -07:00
/// Coordinate extractor for Pallas.
2021-03-05 15:25:45 -08:00
///
2021-03-17 12:20:40 -07:00
/// Defined in [Zcash Protocol Spec § 5.4.9.7: Coordinate Extractor for Pallas][concreteextractorpallas].
///
/// [concreteextractorpallas]: https://zips.z.cash/protocol/nu5.pdf#concreteextractorpallas
2021-03-05 15:25:45 -08:00
pub(crate) fn extract_p(point: &pallas::Point) -> pallas::Base {
if let Some((x, _)) = point.to_affine().get_xy().into() {
x
} else {
pallas::Base::zero()
}
}
#[cfg(test)]
mod tests {
use group::Group;
use halo2::arithmetic::CurveExt;
use pasta_curves::pallas;
#[test]
fn diversify_hash_substitution() {
assert!(!bool::from(
pallas::Point::hash_to_curve("z.cash:Orchard-gd")(&[]).is_identity()
));
}
}