Эх сурвалжийг харах

switch code to using nullifier type

narodnik 5 жил өмнө
parent
commit
8b511a79b1

+ 7 - 5
src/bin/tx.rs

@@ -34,8 +34,8 @@ impl ProgramState for MemoryState {
     fn is_valid_merkle(&self, merkle: &bls12_381::Scalar) -> bool {
     fn is_valid_merkle(&self, merkle: &bls12_381::Scalar) -> bool {
         self.merkle_roots.iter().any(|m| *m == *merkle)
         self.merkle_roots.iter().any(|m| *m == *merkle)
     }
     }
-    fn nullifier_exists(&self, nullifier: &[u8; 32]) -> bool {
-        self.nullifiers.iter().any(|n| n.repr == *nullifier)
+    fn nullifier_exists(&self, nullifier: &Nullifier) -> bool {
+        self.nullifiers.iter().any(|n| n.repr == nullifier.repr)
     }
     }
 
 
     fn mint_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12> {
     fn mint_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12> {
@@ -47,7 +47,7 @@ impl ProgramState for MemoryState {
 }
 }
 
 
 impl MemoryState {
 impl MemoryState {
-    async fn apply(&mut self, mut updates: StateUpdates) {
+    fn apply(&mut self, mut updates: StateUpdates) {
         self.nullifiers.append(&mut updates.nullifiers);
         self.nullifiers.append(&mut updates.nullifiers);
 
 
         // Update merkle tree and witnesses
         // Update merkle tree and witnesses
@@ -184,8 +184,10 @@ fn main() {
         let tx = tx::Transaction::decode(&tx_data[..]).unwrap();
         let tx = tx::Transaction::decode(&tx_data[..]).unwrap();
 
 
         let update = state_transition(&state, tx).expect("step 2 state transition failed");
         let update = state_transition(&state, tx).expect("step 2 state transition failed");
-
-        smol::block_on(state.apply(update));
+        // Our state impl is memory online for this demo
+        // but in the real version, this function will be async
+        // and using the databases.
+        state.apply(update);
     }
     }
 
 
     // Wallet1 has received payment from the cashier.
     // Wallet1 has received payment from the cashier.

+ 18 - 0
src/crypto/nullifier.rs

@@ -1,3 +1,7 @@
+use std::io;
+
+use crate::{error::Result, serial::{Decodable, Encodable}};
+
 pub struct Nullifier {
 pub struct Nullifier {
     pub repr: [u8; 32],
     pub repr: [u8; 32],
 }
 }
@@ -7,3 +11,17 @@ impl Nullifier {
         Self { repr }
         Self { repr }
     }
     }
 }
 }
+
+impl Encodable for Nullifier {
+    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+        Ok(self.repr.encode(s)?)
+    }
+}
+
+impl Decodable for Nullifier {
+    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+        Ok(Self {
+            repr: Decodable::decode(d)?,
+        })
+    }
+}

+ 4 - 2
src/crypto/spend_proof.rs

@@ -12,10 +12,11 @@ use super::node::merkle_hash;
 use crate::circuit::spend_contract::SpendContract;
 use crate::circuit::spend_contract::SpendContract;
 use crate::error::Result;
 use crate::error::Result;
 use crate::serial::{Decodable, Encodable};
 use crate::serial::{Decodable, Encodable};
+use super::nullifier::Nullifier;
 
 
 pub struct SpendRevealedValues {
 pub struct SpendRevealedValues {
     pub value_commit: jubjub::SubgroupPoint,
     pub value_commit: jubjub::SubgroupPoint,
-    pub nullifier: [u8; 32],
+    pub nullifier: Nullifier,
     // This should not be here, we just have it for debugging
     // This should not be here, we just have it for debugging
     //coin: [u8; 32],
     //coin: [u8; 32],
     pub merkle_root: bls12_381::Scalar,
     pub merkle_root: bls12_381::Scalar,
@@ -48,6 +49,7 @@ impl SpendRevealedValues {
                 .finalize()
                 .finalize()
                 .as_bytes(),
                 .as_bytes(),
         );
         );
+        let nullifier = Nullifier::new(nullifier);
 
 
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
         let signature_public =
         let signature_public =
@@ -108,7 +110,7 @@ impl SpendRevealedValues {
         // NF
         // NF
         {
         {
             // Pack the hash as inputs for proof verification.
             // Pack the hash as inputs for proof verification.
-            let hash = multipack::bytes_to_bits_le(&self.nullifier);
+            let hash = multipack::bytes_to_bits_le(&self.nullifier.repr);
             let hash = multipack::compute_multipacking(&hash);
             let hash = multipack::compute_multipacking(&hash);
 
 
             // There are 2 chunks for a blake hash
             // There are 2 chunks for a blake hash

+ 2 - 2
src/state.rs

@@ -10,7 +10,7 @@ use crate::{
 pub trait ProgramState {
 pub trait ProgramState {
     fn is_valid_cashier_public_key(&self, public: &jubjub::SubgroupPoint) -> bool;
     fn is_valid_cashier_public_key(&self, public: &jubjub::SubgroupPoint) -> bool;
     fn is_valid_merkle(&self, merkle: &bls12_381::Scalar) -> bool;
     fn is_valid_merkle(&self, merkle: &bls12_381::Scalar) -> bool;
-    fn nullifier_exists(&self, nullifier: &[u8; 32]) -> bool;
+    fn nullifier_exists(&self, nullifier: &Nullifier) -> bool;
 
 
     fn mint_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12>;
     fn mint_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12>;
     fn spend_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12>;
     fn spend_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12>;
@@ -100,7 +100,7 @@ pub fn state_transition<S: ProgramState>(
 
 
     let mut nullifiers = vec![];
     let mut nullifiers = vec![];
     for input in tx.inputs {
     for input in tx.inputs {
-        nullifiers.push(Nullifier::new(input.revealed.nullifier));
+        nullifiers.push(input.revealed.nullifier);
     }
     }
 
 
     // Newly created coins for this tx
     // Newly created coins for this tx