mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
use std::io;
|
|
|
|
use halo2_gadgets::primitives::{
|
|
poseidon,
|
|
poseidon::{ConstantLength, P128Pow5T3},
|
|
};
|
|
use pasta_curves::{arithmetic::FieldExt, pallas};
|
|
|
|
use crate::{
|
|
crypto::keypair::SecretKey,
|
|
serial::{Decodable, Encodable, ReadExt, WriteExt},
|
|
Result,
|
|
};
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Nullifier(pub(crate) pallas::Base);
|
|
|
|
impl Nullifier {
|
|
pub fn new(secret: SecretKey, serial: pallas::Base) -> Self {
|
|
let nullifier = [secret.0, serial];
|
|
let nullifier = poseidon::Hash::init(P128Pow5T3, ConstantLength::<2>).hash(nullifier);
|
|
Nullifier(nullifier)
|
|
}
|
|
|
|
pub fn from_bytes(bytes: &[u8; 32]) -> Self {
|
|
pallas::Base::from_bytes(bytes).map(Nullifier).unwrap()
|
|
}
|
|
|
|
pub fn to_bytes(self) -> [u8; 32] {
|
|
self.0.to_bytes()
|
|
}
|
|
|
|
pub(crate) fn inner(&self) -> pallas::Base {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl Encodable for Nullifier {
|
|
fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
|
|
s.write_slice(&self.to_bytes()[..])?;
|
|
Ok(32)
|
|
}
|
|
}
|
|
|
|
impl Decodable for Nullifier {
|
|
fn decode<D: io::Read>(mut d: D) -> Result<Self> {
|
|
let mut bytes = [0u8; 32];
|
|
d.read_slice(&mut bytes)?;
|
|
let result = Self::from_bytes(&bytes);
|
|
Ok(result)
|
|
}
|
|
}
|