Просмотр исходного кода

Separate the Node and Coin types

narodnik 5 лет назад
Родитель
Сommit
411a62ed20
4 измененных файлов с 20 добавлено и 13 удалено
  1. 10 8
      src/bin/tx.rs
  2. 1 0
      src/crypto/mod.rs
  3. 6 2
      src/crypto/node.rs
  4. 3 3
      src/state.rs

+ 10 - 8
src/bin/tx.rs

@@ -5,6 +5,7 @@ use rand::rngs::OsRng;
 use std::path::Path;
 
 use sapvi::crypto::{
+    coin::Coin,
     load_params,
     merkle::{CommitmentTree, IncrementalWitness},
     node::{hash_coin, Node},
@@ -20,7 +21,7 @@ struct MemoryState {
     tree: CommitmentTree<Node>,
     merkle_roots: Vec<bls12_381::Scalar>,
     nullifiers: Vec<Nullifier>,
-    own_coins: Vec<(Node, Note, jubjub::Fr, IncrementalWitness<Node>)>,
+    own_coins: Vec<(Coin, Note, jubjub::Fr, IncrementalWitness<Node>)>,
     mint_pvk: groth16::PreparedVerifyingKey<Bls12>,
     spend_pvk: groth16::PreparedVerifyingKey<Bls12>,
     cashier_public: jubjub::SubgroupPoint,
@@ -52,18 +53,18 @@ impl MemoryState {
 
         // Update merkle tree and witnesses
         for (coin, enc_note) in updates.coins.into_iter().zip(updates.enc_notes.into_iter()) {
-            let node = hash_coin(coin.repr);
+            let node = Node::from_coin(&coin);
 
             // Add the new coins to the merkle tree
             self.tree
-                .append(Node::new(node.to_repr()))
+                .append(node)
                 .expect("Append to merkle tree");
 
             let root = self.tree.root();
             self.merkle_roots.push(root.into());
             for (_, _, _, witness) in self.own_coins.iter_mut() {
                 witness
-                    .append(Node::new(node.to_repr()))
+                    .append(node)
                     .expect("append to witness");
             }
             assert_eq!(self.own_coins.len(), 0);
@@ -197,8 +198,9 @@ fn main() {
 
     let auth_path = {
         let tree = &mut state.tree;
-        let coin = state.own_coins[0].0;
-        let witness = &mut state.own_coins[0].3;
+        //let coin: &Coin = &state.own_coins[0].0;
+        //let witness = &mut state.own_coins[0].3;
+        let (coin, _, _, witness) = &mut state.own_coins[0];
         // Check this is the 6th coin we added
         assert_eq!(witness.position(), 5);
         assert_eq!(tree.root(), witness.root());
@@ -224,12 +226,12 @@ fn main() {
             .map(|(node, b)| ((*node).into(), *b))
             .collect();
 
-        let node = hash_coin(coin.repr).to_repr();
+        let node = Node::from_coin(&coin);
 
         let root = tree.root();
         drop(tree);
         drop(witness);
-        assert_eq!(merkle_path.root(Node::new(node)), root);
+        assert_eq!(merkle_path.root(node), root);
         let root = root.into();
         assert!(state.is_valid_merkle(&root));
 

+ 1 - 0
src/crypto/mod.rs

@@ -1,3 +1,4 @@
+pub mod coin;
 pub mod diffie_hellman;
 pub mod fr_serial;
 pub mod merkle;

+ 6 - 2
src/crypto/node.rs

@@ -4,7 +4,7 @@ use group::Curve;
 use lazy_static::lazy_static;
 use std::io;
 
-use super::merkle::Hashable;
+use super::{coin::Coin, merkle::Hashable};
 
 pub const SAPLING_COMMITMENT_TREE_DEPTH: usize = 4;
 
@@ -42,7 +42,7 @@ pub fn merkle_hash(depth: usize, lhs: &[u8; 32], rhs: &[u8; 32]) -> bls12_381::S
     .get_u()
 }
 
-pub fn hash_coin(coin: [u8; 32]) -> bls12_381::Scalar {
+pub fn hash_coin(coin: &[u8; 32]) -> bls12_381::Scalar {
     let rhs = {
         let mut tmp = [false; 256];
         for (a, b) in tmp.iter_mut().zip(coin.as_bits::<Lsb0>()) {
@@ -69,6 +69,10 @@ impl Node {
     pub fn new(repr: [u8; 32]) -> Self {
         Self { repr }
     }
+
+    pub fn from_coin(coin: &Coin) -> Self {
+        Self { repr: hash_coin(&coin.repr).to_repr() }
+    }
 }
 
 impl Hashable for Node {

+ 3 - 3
src/state.rs

@@ -3,7 +3,7 @@ use bls12_381::Bls12;
 use std::fmt;
 
 use crate::{
-    crypto::{node::Node, note::EncryptedNote, nullifier::Nullifier},
+    crypto::{coin::Coin, node::Node, note::EncryptedNote, nullifier::Nullifier},
     tx,
 };
 
@@ -18,7 +18,7 @@ pub trait ProgramState {
 
 pub struct StateUpdates {
     pub nullifiers: Vec<Nullifier>,
-    pub coins: Vec<Node>,
+    pub coins: Vec<Coin>,
     pub enc_notes: Vec<EncryptedNote>,
 }
 
@@ -108,7 +108,7 @@ pub fn state_transition<S: ProgramState>(
     let mut enc_notes = vec![];
     for output in tx.outputs {
         // Gather all the coins
-        coins.push(Node::new(output.revealed.coin));
+        coins.push(Coin::new(output.revealed.coin));
         enc_notes.push(output.enc_note);
     }