note.rs 3.1 KB

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