Implement needed getters and conversions in other modules.

This commit is contained in:
therealyingtong 2021-06-06 19:13:20 +08:00
parent bd28b46163
commit d16b83816b
7 changed files with 81 additions and 11 deletions

View File

@ -185,6 +185,11 @@ impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> Point<C,
.constrain_equal(&mut layouter, &self.inner, &other.inner)
}
/// Returns the inner point.
pub fn inner(&self) -> &EccChip::Point {
&self.inner
}
/// Extracts the x-coordinate of a point.
pub fn extract_p(&self) -> X<C, EccChip> {
X::from_inner(self.chip.clone(), EccChip::extract_p(&self.inner).clone())

View File

@ -10,7 +10,7 @@ use halo2::{
};
mod pow5t3;
pub use pow5t3::{Pow5T3Chip, Pow5T3Config};
pub use pow5t3::{Pow5T3Chip, Pow5T3Config, StateWord};
use crate::primitives::poseidon::{ConstantLength, Domain, Spec, Sponge, SpongeState, State};
@ -67,7 +67,7 @@ pub struct Word<
const T: usize,
const RATE: usize,
> {
inner: PoseidonChip::Word,
pub inner: PoseidonChip::Word,
}
fn poseidon_duplex<

View File

@ -8,6 +8,7 @@ use halo2::{
};
use super::{PoseidonDuplexInstructions, PoseidonInstructions};
use crate::circuit::gadget::utilities::{CellValue, Var};
use crate::primitives::poseidon::{Domain, Mds, Spec, SpongeState, State};
const WIDTH: usize = 3;
@ -210,7 +211,7 @@ impl<F: FieldExt> Pow5T3Chip<F> {
}
}
fn construct(config: Pow5T3Config<F>) -> Self {
pub fn construct(config: Pow5T3Config<F>) -> Self {
Pow5T3Chip { config }
}
}
@ -416,6 +417,18 @@ pub struct StateWord<F: FieldExt> {
value: Option<F>,
}
impl<F: FieldExt> StateWord<F> {
pub fn new(var: Cell, value: Option<F>) -> Self {
Self { var, value }
}
}
impl<F: FieldExt> From<StateWord<F>> for CellValue<F> {
fn from(state_word: StateWord<F>) -> CellValue<F> {
CellValue::new(state_word.var, state_word.value)
}
}
#[derive(Debug)]
struct Pow5T3State<F: FieldExt>([StateWord<F>; WIDTH]);

View File

@ -121,6 +121,12 @@ impl From<&SpendAuthorizingKey> for SpendValidatingKey {
}
}
impl From<&SpendValidatingKey> for pallas::Point {
fn from(spend_validating_key: &SpendValidatingKey) -> pallas::Point {
pallas::Point::from_bytes(&(&spend_validating_key.0).into()).unwrap()
}
}
impl PartialEq for SpendValidatingKey {
fn eq(&self, other: &Self) -> bool {
<[u8; 32]>::from(&self.0).eq(&<[u8; 32]>::from(&other.0))
@ -141,9 +147,17 @@ impl SpendValidatingKey {
/// [`Nullifier`]: crate::note::Nullifier
/// [`Note`]: crate::note::Note
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Debug, Clone)]
#[derive(Copy, Debug, Clone)]
pub(crate) struct NullifierDerivingKey(pallas::Base);
impl std::ops::Deref for NullifierDerivingKey {
type Target = pallas::Base;
fn deref(&self) -> &pallas::Base {
&self.0
}
}
impl From<&SpendingKey> for NullifierDerivingKey {
fn from(sk: &SpendingKey) -> Self {
NullifierDerivingKey(to_base(PrfExpand::OrchardNk.expand(&sk.0)))
@ -161,8 +175,8 @@ impl NullifierDerivingKey {
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Debug, Clone)]
struct CommitIvkRandomness(pallas::Scalar);
#[derive(Copy, Debug, Clone)]
pub(crate) struct CommitIvkRandomness(pallas::Scalar);
impl From<&SpendingKey> for CommitIvkRandomness {
fn from(sk: &SpendingKey) -> Self {
@ -170,6 +184,14 @@ impl From<&SpendingKey> for CommitIvkRandomness {
}
}
impl std::ops::Deref for CommitIvkRandomness {
type Target = pallas::Scalar;
fn deref(&self) -> &pallas::Scalar {
&self.0
}
}
/// A key that provides the capability to view incoming and outgoing transactions.
///
/// This key is useful anywhere you need to maintain accurate balance, but do not want the
@ -206,6 +228,10 @@ impl FullViewingKey {
&self.nk
}
pub(crate) fn rivk(&self) -> &CommitIvkRandomness {
&self.rivk
}
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents

View File

@ -18,7 +18,7 @@ pub(crate) mod nullifier;
pub use self::nullifier::Nullifier;
/// The ZIP 212 seed randomness for a note.
#[derive(Clone, Debug)]
#[derive(Copy, Clone, Debug)]
pub(crate) struct RandomSeed([u8; 32]);
impl RandomSeed {
@ -46,7 +46,7 @@ impl RandomSeed {
/// Defined in [Zcash Protocol Spec § 4.7.3: Sending Notes (Orchard)][orchardsend].
///
/// [orchardsend]: https://zips.z.cash/protocol/nu5.pdf#orchardsend
fn psi(&self, rho: &Nullifier) -> pallas::Base {
pub(crate) fn psi(&self, rho: &Nullifier) -> pallas::Base {
to_base(PrfExpand::Psi.with_ad(&self.0, &rho.to_bytes()[..]))
}
@ -70,7 +70,7 @@ impl RandomSeed {
/// Defined in [Zcash Protocol Spec § 4.7.3: Sending Notes (Orchard)][orchardsend].
///
/// [orchardsend]: https://zips.z.cash/protocol/nu5.pdf#orchardsend
fn rcm(&self, rho: &Nullifier) -> commitment::NoteCommitTrapdoor {
pub(crate) fn rcm(&self, rho: &Nullifier) -> commitment::NoteCommitTrapdoor {
commitment::NoteCommitTrapdoor(to_scalar(
PrfExpand::Rcm.with_ad(&self.0, &rho.to_bytes()[..]),
))
@ -187,6 +187,11 @@ impl Note {
EphemeralSecretKey(self.rseed.esk(&self.rho))
}
/// Returns rho of this note.
pub fn rho(&self) -> Nullifier {
self.rho
}
/// Derives the commitment to this note.
///
/// Defined in [Zcash Protocol Spec § 3.2: Notes][notes].

View File

@ -7,12 +7,21 @@ use subtle::{ConstantTimeEq, CtOption};
use crate::{constants::L_ORCHARD_BASE, primitives::sinsemilla, spec::extract_p, value::NoteValue};
pub(super) struct NoteCommitTrapdoor(pub(super) pallas::Scalar);
#[derive(Debug)]
pub(crate) struct NoteCommitTrapdoor(pub(super) pallas::Scalar);
/// A commitment to a note.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct NoteCommitment(pub(super) pallas::Point);
impl std::ops::Deref for NoteCommitment {
type Target = pallas::Point;
fn deref(&self) -> &pallas::Point {
&self.0
}
}
impl NoteCommitment {
/// $NoteCommit^Orchard$.
///

View File

@ -71,6 +71,10 @@ impl NoteValue {
Default::default()
}
pub(crate) fn inner(&self) -> u64 {
self.0
}
/// Creates a note value from its raw numeric value.
///
/// This only enforces that the value is an unsigned 64-bit integer. Callers should
@ -160,6 +164,14 @@ impl TryFrom<ValueSum> for i64 {
#[derive(Clone, Debug)]
pub struct ValueCommitTrapdoor(pallas::Scalar);
impl std::ops::Deref for ValueCommitTrapdoor {
type Target = pallas::Scalar;
fn deref(&self) -> &pallas::Scalar {
&self.0
}
}
impl Add<&ValueCommitTrapdoor> for ValueCommitTrapdoor {
type Output = ValueCommitTrapdoor;