narodnik 5 лет назад
Родитель
Сommit
b20fbfb422
8 измененных файлов с 30 добавлено и 30 удалено
  1. 8 8
      src/bin/tx.rs
  2. 1 1
      src/crypto/merkle.rs
  3. 1 1
      src/crypto/mod.rs
  4. 13 13
      src/crypto/node.rs
  5. 2 2
      src/crypto/spend_proof.rs
  6. 3 3
      src/state.rs
  7. 1 1
      src/tx/builder.rs
  8. 1 1
      src/tx/partial.rs

+ 8 - 8
src/bin/tx.rs

@@ -8,7 +8,7 @@ use rand::rngs::OsRng;
 use std::path::Path;
 
 use sapvi::crypto::{
-    coin::{hash_coin, Coin},
+    node::{hash_coin, Node},
     create_mint_proof, create_spend_proof, load_params,
     merkle::{CommitmentTree, IncrementalWitness},
     note::{EncryptedNote, Note},
@@ -21,10 +21,10 @@ use sapvi::state::{state_transition, ProgramState, StateUpdates};
 use sapvi::tx;
 
 struct MemoryState {
-    tree: CommitmentTree<Coin>,
+    tree: CommitmentTree<Node>,
     merkle_roots: Vec<bls12_381::Scalar>,
     nullifiers: Vec<Nullifier>,
-    own_coins: Vec<(Coin, Note, jubjub::Fr, IncrementalWitness<Coin>)>,
+    own_coins: Vec<(Node, Note, jubjub::Fr, IncrementalWitness<Node>)>,
     mint_pvk: groth16::PreparedVerifyingKey<Bls12>,
     spend_pvk: groth16::PreparedVerifyingKey<Bls12>,
     cashier_public: jubjub::SubgroupPoint,
@@ -60,14 +60,14 @@ impl MemoryState {
 
             // Add the new coins to the merkle tree
             self.tree
-                .append(Coin::new(node.to_repr()))
+                .append(Node::new(node.to_repr()))
                 .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(Coin::new(node.to_repr()))
+                    .append(Node::new(node.to_repr()))
                     .expect("append to witness");
             }
             assert_eq!(self.own_coins.len(), 0);
@@ -172,7 +172,7 @@ fn main() {
         // Here we simulate 5 fake random coins, adding them to our tree.
         let tree = &mut state.tree;
         for i in 0..5 {
-            let cmu = Coin::new(bls12_381::Scalar::random(&mut OsRng).to_repr());
+            let cmu = Node::new(bls12_381::Scalar::random(&mut OsRng).to_repr());
             tree.append(cmu);
 
             let root = tree.root();
@@ -204,7 +204,7 @@ fn main() {
 
         // Add some more random coins in
         for i in 0..10 {
-            let cmu = Coin::new(bls12_381::Scalar::random(&mut OsRng).to_repr());
+            let cmu = Node::new(bls12_381::Scalar::random(&mut OsRng).to_repr());
             tree.append(cmu);
             witness.append(cmu);
             assert_eq!(tree.root(), witness.root());
@@ -228,7 +228,7 @@ fn main() {
         let root = tree.root();
         drop(tree);
         drop(witness);
-        assert_eq!(merkle_path.root(Coin::new(node)), root);
+        assert_eq!(merkle_path.root(Node::new(node)), root);
         let root = root.into();
         assert!(state.is_valid_merkle(&root));
 

+ 1 - 1
src/crypto/merkle.rs

@@ -5,7 +5,7 @@ use std::collections::VecDeque;
 use std::io::{self, Read, Write};
 
 //use crate::serialize::{Optional, Vector};
-use super::coin::SAPLING_COMMITMENT_TREE_DEPTH;
+use super::node::SAPLING_COMMITMENT_TREE_DEPTH;
 
 /// A hashable node within a Merkle tree.
 pub trait Hashable: Clone + Copy {

+ 1 - 1
src/crypto/mod.rs

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

+ 13 - 13
src/crypto/coin.rs → src/crypto/node.rs

@@ -61,21 +61,21 @@ pub fn hash_coin(coin: [u8; 32]) -> bls12_381::Scalar {
 
 /// A node within the Sapling commitment tree.
 #[derive(Clone, Copy, Debug, PartialEq)]
-pub struct Coin {
+pub struct Node {
     pub repr: [u8; 32],
 }
 
-impl Coin {
+impl Node {
     pub fn new(repr: [u8; 32]) -> Self {
-        Coin { repr }
+        Self { repr }
     }
 }
 
-impl Hashable for Coin {
+impl Hashable for Node {
     fn read<R: io::Read>(mut reader: R) -> io::Result<Self> {
         let mut repr = [0u8; 32];
         reader.read_exact(&mut repr)?;
-        Ok(Coin::new(repr))
+        Ok(Self::new(repr))
     }
 
     fn write<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
@@ -83,7 +83,7 @@ impl Hashable for Coin {
     }
 
     fn combine(depth: usize, lhs: &Self, rhs: &Self) -> Self {
-        Coin {
+        Self {
             repr: merkle_hash(depth, &lhs.repr, &rhs.repr).to_repr(),
         }
     }
@@ -92,7 +92,7 @@ impl Hashable for Coin {
         // The smallest u-coordinate that is not on the curve
         // is one.
         let uncommitted_note = bls12_381::Scalar::one();
-        Coin {
+        Self {
             repr: uncommitted_note.to_repr(),
         }
     }
@@ -102,17 +102,17 @@ impl Hashable for Coin {
     }
 }
 
-impl From<Coin> for bls12_381::Scalar {
-    fn from(coin: Coin) -> Self {
-        bls12_381::Scalar::from_repr(coin.repr).expect("Tree nodes should be in the prime field")
+impl From<Node> for bls12_381::Scalar {
+    fn from(node: Node) -> Self {
+        bls12_381::Scalar::from_repr(node.repr).expect("Tree nodes should be in the prime field")
     }
 }
 
 lazy_static! {
-    static ref EMPTY_ROOTS: Vec<Coin> = {
-        let mut v = vec![Coin::blank()];
+    static ref EMPTY_ROOTS: Vec<Node> = {
+        let mut v = vec![Node::blank()];
         for d in 0..SAPLING_COMMITMENT_TREE_DEPTH {
-            let next = Coin::combine(d, &v[d], &v[d]);
+            let next = Node::combine(d, &v[d], &v[d]);
             v.push(next);
         }
         v

+ 2 - 2
src/crypto/spend_proof.rs

@@ -8,7 +8,7 @@ use rand::rngs::OsRng;
 use std::io;
 use std::time::Instant;
 
-use super::coin::merkle_hash;
+use super::node::merkle_hash;
 use crate::circuit::spend_contract::SpendContract;
 use crate::error::Result;
 use crate::serial::{Decodable, Encodable};
@@ -211,7 +211,7 @@ pub fn create_spend_proof(
     assert_eq!(merkle_path.len(), 4);
     assert_eq!(
         merkle_path.len(),
-        super::coin::SAPLING_COMMITMENT_TREE_DEPTH
+        super::node::SAPLING_COMMITMENT_TREE_DEPTH
     );
     let c = SpendContract {
         value: Some(value),

+ 3 - 3
src/state.rs

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

+ 1 - 1
src/tx/builder.rs

@@ -10,7 +10,7 @@ use super::{
     Transaction, TransactionClearInput, TransactionInput, TransactionOutput,
 };
 use crate::crypto::{
-    coin::Coin,
+    node::Node,
     create_mint_proof, create_spend_proof, load_params,
     merkle::CommitmentTree,
     note::{EncryptedNote, Note},

+ 1 - 1
src/tx/partial.rs

@@ -7,7 +7,7 @@ use std::io;
 
 use super::{Transaction, TransactionClearInput, TransactionInput, TransactionOutput};
 use crate::crypto::{
-    coin::Coin,
+    node::Node,
     create_mint_proof, create_spend_proof, load_params,
     merkle::CommitmentTree,
     note::{EncryptedNote, Note},