Ver código fonte

crypto: Implement Encodable and Decodable for some structs.

parazyd 4 anos atrás
pai
commit
5bbbd5851e

+ 24 - 0
src/crypto/coin.rs

@@ -1,5 +1,13 @@
+use std::io;
+
 use pasta_curves::{arithmetic::FieldExt, pallas};
 
+use crate::{
+    serial::{Decodable, Encodable, ReadExt, WriteExt},
+    Result,
+};
+
+#[derive(Clone, Copy, Debug)]
 pub struct Coin(pallas::Base);
 
 impl Coin {
@@ -15,3 +23,19 @@ impl Coin {
         self.0
     }
 }
+
+impl Encodable for Coin {
+    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+        s.write_slice(&self.to_bytes()[..])?;
+        Ok(32)
+    }
+}
+
+impl Decodable for Coin {
+    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)
+    }
+}

+ 0 - 2
src/crypto/merkle_old.rs

@@ -1,7 +1,6 @@
 //! Implementation of a Merkle tree of commitments used to prove the existence
 //! of notes.
 
-/*
 //use byteorder::{LittleEndian, ReadBytesExt};
 use crate::serial::{Decodable, Encodable, VarInt};
 use crate::{Error, Result};
@@ -452,4 +451,3 @@ impl<Node: Hashable> MerklePath<Node> {
             )
     }
 }
-*/

+ 6 - 5
src/crypto/mod.rs

@@ -11,17 +11,18 @@ pub mod schnorr;
 pub mod spend_proof;
 pub mod util;
 
-/*
-use crate::types::*;
+use incrementalmerkletree::bridgetree::Frontier as BridgeFrontier;
+
+use crate::types::DrkSecretKey;
 
 #[derive(Clone)]
 pub struct OwnCoin {
-    pub coin: DrkCoin,
+    pub coin: coin::Coin,
     pub note: note::Note,
     pub secret: DrkSecretKey,
     //pub witness: merkle::IncrementalWitness<merkle_node::MerkleNode>,
-    pub nullifier: DrkNullifier,
+    //pub witness: BridgeFrontier<merkle::MerkleHash, 32>,
+    pub nullifier: nullifier::Nullifier,
 }
 
 pub type OwnCoins = Vec<OwnCoin>;
-*/

+ 5 - 5
src/crypto/note.rs

@@ -18,7 +18,7 @@ pub const NOTE_PLAINTEXT_SIZE: usize = 32 +    // serial
     8 +     // value
     32 +    // token_id
     32 +    // coin_blind
-    32; // valcom_blind
+    32; // value_blind
 pub const AEAD_TAG_SIZE: usize = 16;
 pub const ENC_CIPHERTEXT_SIZE: usize = NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE;
 
@@ -28,7 +28,7 @@ pub struct Note {
     pub value: u64,
     pub token_id: DrkTokenId,
     pub coin_blind: DrkCoinBlind,
-    pub valcom_blind: DrkValueBlind,
+    pub value_blind: DrkValueBlind,
 }
 
 impl Encodable for Note {
@@ -38,7 +38,7 @@ impl Encodable for Note {
         len += self.value.encode(&mut s)?;
         len += self.token_id.encode(&mut s)?;
         len += self.coin_blind.encode(&mut s)?;
-        len += self.valcom_blind.encode(&mut s)?;
+        len += self.value_blind.encode(&mut s)?;
         Ok(len)
     }
 }
@@ -50,7 +50,7 @@ impl Decodable for Note {
             value: Decodable::decode(&mut d)?,
             token_id: Decodable::decode(&mut d)?,
             coin_blind: Decodable::decode(&mut d)?,
-            valcom_blind: Decodable::decode(d)?,
+            value_blind: Decodable::decode(d)?,
         })
     }
 }
@@ -138,7 +138,7 @@ fn test_note_encdec() {
         value: 110,
         token_id: DrkTokenId::random(&mut OsRng),
         coin_blind: DrkCoinBlind::random(&mut OsRng),
-        valcom_blind: DrkValueBlind::random(&mut OsRng),
+        value_blind: DrkValueBlind::random(&mut OsRng),
     };
 
     let secret = DrkSecretKey::random(&mut OsRng);

+ 24 - 0
src/crypto/nullifier.rs

@@ -1,5 +1,13 @@
+use std::io;
+
 use pasta_curves::{arithmetic::FieldExt, pallas};
 
+use crate::{
+    serial::{Decodable, Encodable, ReadExt, WriteExt},
+    Result,
+};
+
+#[derive(Clone, Copy, Debug)]
 pub struct Nullifier(pallas::Base);
 
 impl Nullifier {
@@ -15,3 +23,19 @@ impl Nullifier {
         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)
+    }
+}

+ 23 - 4
src/crypto/proof.rs

@@ -1,14 +1,17 @@
-use pasta_curves::vesta;
-// TODO: Alias vesta::Affine to something
+use std::io;
 
+// TODO: Alias vesta::Affine to something
 use halo2::{
     plonk,
     plonk::Circuit,
     poly::commitment,
     transcript::{Blake2bRead, Blake2bWrite},
 };
+use pasta_curves::vesta;
 
+use crate::serial::{Decodable, Encodable, ReadExt, VarInt, WriteExt};
 use crate::types::*;
+use crate::Result;
 
 #[derive(Debug)]
 pub struct VerifyingKey {
@@ -53,7 +56,7 @@ impl Proof {
         pk: &ProvingKey,
         circuits: &[impl Circuit<DrkCircuitField>],
         pubinputs: &[DrkCircuitField],
-    ) -> Result<Self, plonk::Error> {
+    ) -> std::result::Result<Self, plonk::Error> {
         let mut transcript = Blake2bWrite::<_, vesta::Affine, _>::init(vec![]);
 
         plonk::create_proof(
@@ -71,7 +74,7 @@ impl Proof {
         &self,
         vk: &VerifyingKey,
         pubinputs: &[DrkCircuitField],
-    ) -> Result<(), plonk::Error> {
+    ) -> std::result::Result<(), plonk::Error> {
         let msm = vk.params.empty_msm();
         let mut transcript = Blake2bRead::init(&self.0[..]);
         let guard = plonk::verify_proof(&vk.params, &vk.vk, msm, &[&[pubinputs]], &mut transcript)?;
@@ -88,3 +91,19 @@ impl Proof {
         Proof(bytes)
     }
 }
+
+impl Encodable for Proof {
+    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+        s.write_slice(&self.as_ref()[..])?;
+        Ok(self.as_ref().len())
+    }
+}
+
+impl Decodable for Proof {
+    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+        let len = VarInt::decode(&mut d)?.0 as usize;
+        let mut r = vec![0u8; len];
+        d.read_slice(&mut r)?;
+        Ok(Proof::new(r))
+    }
+}