note.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use crypto_api_chachapoly::ChachaPolyIetf;
  2. use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable};
  3. use rand::rngs::OsRng;
  4. use crate::{
  5. crypto::{
  6. diffie_hellman::{kdf_sapling, sapling_ka_agree},
  7. keypair::{PublicKey, SecretKey},
  8. types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValueBlind},
  9. },
  10. Error, Result,
  11. };
  12. pub const AEAD_TAG_SIZE: usize = 16;
  13. #[derive(Clone, Debug, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  14. pub struct Note {
  15. pub serial: DrkSerial,
  16. pub value: u64,
  17. pub token_id: DrkTokenId,
  18. pub coin_blind: DrkCoinBlind,
  19. pub value_blind: DrkValueBlind,
  20. pub token_blind: DrkValueBlind,
  21. pub memo: Vec<u8>,
  22. }
  23. impl Note {
  24. pub fn encrypt(&self, public: &PublicKey) -> Result<EncryptedNote> {
  25. let ephem_secret = SecretKey::random(&mut OsRng);
  26. let ephem_public = PublicKey::from_secret(ephem_secret);
  27. let shared_secret = sapling_ka_agree(&ephem_secret, public);
  28. let key = kdf_sapling(&shared_secret, &ephem_public);
  29. let mut input = Vec::new();
  30. self.encode(&mut input)?;
  31. let mut ciphertext = vec![0; input.len() + AEAD_TAG_SIZE];
  32. assert_eq!(
  33. ChachaPolyIetf::aead_cipher()
  34. .seal_to(&mut ciphertext, &input, &[], key.as_ref(), &[0u8; 12])
  35. .unwrap(),
  36. input.len() + AEAD_TAG_SIZE
  37. );
  38. Ok(EncryptedNote { ciphertext, ephem_public })
  39. }
  40. }
  41. #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  42. pub struct EncryptedNote {
  43. ciphertext: Vec<u8>,
  44. ephem_public: PublicKey,
  45. }
  46. impl EncryptedNote {
  47. pub fn decrypt(&self, secret: &SecretKey) -> Result<Note> {
  48. let shared_secret = sapling_ka_agree(secret, &self.ephem_public);
  49. let key = kdf_sapling(&shared_secret, &self.ephem_public);
  50. let mut plaintext = vec![0; self.ciphertext.len()];
  51. assert_eq!(
  52. ChachaPolyIetf::aead_cipher()
  53. .open_to(&mut plaintext, &self.ciphertext, &[], key.as_ref(), &[0u8; 12])
  54. .map_err(|_| Error::NoteDecryptionFailed)?,
  55. self.ciphertext.len() - AEAD_TAG_SIZE
  56. );
  57. let note = Note::decode(&plaintext[..])?;
  58. Ok(note)
  59. }
  60. }
  61. #[cfg(test)]
  62. mod tests {
  63. use super::*;
  64. use crate::crypto::keypair::Keypair;
  65. use pasta_curves::group::ff::Field;
  66. #[test]
  67. fn test_note_encdec() {
  68. let note = Note {
  69. serial: DrkSerial::random(&mut OsRng),
  70. value: 110,
  71. token_id: DrkTokenId::random(&mut OsRng),
  72. coin_blind: DrkCoinBlind::random(&mut OsRng),
  73. value_blind: DrkValueBlind::random(&mut OsRng),
  74. token_blind: DrkValueBlind::random(&mut OsRng),
  75. memo: vec![32, 223, 231, 3, 1, 1],
  76. };
  77. let keypair = Keypair::random(&mut OsRng);
  78. let encrypted_note = note.encrypt(&keypair.public).unwrap();
  79. let note2 = encrypted_note.decrypt(&keypair.secret).unwrap();
  80. assert_eq!(note.value, note2.value);
  81. assert_eq!(note.token_id, note2.token_id);
  82. assert_eq!(note.token_blind, note2.token_blind);
  83. assert_eq!(note.memo, note2.memo);
  84. }
  85. }