Start work implementing sprout note commitment function
This commit is contained in:
parent
0e21a70b88
commit
8b78a55c71
|
@ -6,3 +6,9 @@ pub mod sprout;
|
||||||
/// The randomness used in the Pedersen Hash for note commitment.
|
/// The randomness used in the Pedersen Hash for note commitment.
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
pub struct NoteCommitmentRandomness(pub [u8; 32]);
|
pub struct NoteCommitmentRandomness(pub [u8; 32]);
|
||||||
|
|
||||||
|
impl AsRef<[u8]> for NoteCommitmentRandomness {
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,33 +1,48 @@
|
||||||
//!
|
//!
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use super::{memo::Memo, *};
|
||||||
|
use crate::serde_helpers;
|
||||||
|
use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
|
||||||
|
use crate::types::amount::{Amount, NonNegative};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
io::{self},
|
io::{self},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use proptest::{collection::vec, prelude::*};
|
|
||||||
|
|
||||||
use crate::serde_helpers;
|
|
||||||
use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
|
|
||||||
|
|
||||||
use super::{memo::Memo, *};
|
|
||||||
|
|
||||||
///
|
///
|
||||||
pub struct Note {
|
pub struct Note {
|
||||||
// TODO: refine type as a SHA-256d output derived from a spending key.
|
// TODO: refine type as a SHA-256d output derived from a spending key.
|
||||||
paying_key: [u8; 32],
|
paying_key: [u8; 32],
|
||||||
value: u64,
|
value: Amount<NonNegative>,
|
||||||
// TODO: refine type as the input to the PRF that results in a nullifier.
|
// TODO: refine type as the input to the PRF that results in a nullifier.
|
||||||
nullifier_seed: [u8; 32],
|
nullifier_seed: [u8; 32],
|
||||||
note_commitment_randomness: NoteCommitmentRandomness,
|
note_commitment_randomness: NoteCommitmentRandomness,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Note {
|
||||||
|
pub fn note_commitment(&self) -> NoteCommitment {
|
||||||
|
let leading_byte: u8 = 0xB0;
|
||||||
|
let mut hasher = Sha256::default();
|
||||||
|
hasher.input([leading_byte]);
|
||||||
|
hasher.input(self.paying_key);
|
||||||
|
hasher.input(self.value.to_bytes());
|
||||||
|
hasher.input(self.nullifier_seed);
|
||||||
|
hasher.input(self.note_commitment_randomness);
|
||||||
|
let hash = hasher.result().into();
|
||||||
|
NoteCommitment { hash }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NoteCommitment {
|
||||||
|
hash: [u8; 32],
|
||||||
|
}
|
||||||
|
|
||||||
/// The decrypted form of encrypted Sprout notes on the blockchain.
|
/// The decrypted form of encrypted Sprout notes on the blockchain.
|
||||||
pub struct NotePlaintext {
|
pub struct NotePlaintext {
|
||||||
value: u64,
|
value: Amount<NonNegative>,
|
||||||
// TODO: refine type
|
// TODO: refine type
|
||||||
rho: [u8; 32],
|
rho: [u8; 32],
|
||||||
// TODO: refine as jub-jub appropriate in the base field.
|
// TODO: refine as jub-jub appropriate in the base field.
|
||||||
|
@ -82,6 +97,9 @@ impl ZcashDeserialize for EncryptedCiphertext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use proptest::{collection::vec, prelude::*};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
impl Arbitrary for EncryptedCiphertext {
|
impl Arbitrary for EncryptedCiphertext {
|
||||||
type Parameters = ();
|
type Parameters = ();
|
||||||
|
|
|
@ -21,6 +21,10 @@ impl<C> Amount<C> {
|
||||||
{
|
{
|
||||||
self.0.try_into()
|
self.0.try_into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_bytes(&self) -> [u8; 8] {
|
||||||
|
self.0.to_le_bytes()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C> std::ops::Add<Amount<C>> for Amount<C>
|
impl<C> std::ops::Add<Amount<C>> for Amount<C>
|
||||||
|
|
Loading…
Reference in New Issue