Jelajahi Sumber

add coin to the merkle tree and get the path

narodnik 4 tahun lalu
induk
melakukan
1746ea1a36
4 mengubah file dengan 87 tambahan dan 12 penghapusan
  1. 77 5
      src/bin/tx2.rs
  2. 1 1
      src/crypto/coin.rs
  3. 8 5
      src/crypto/mint_proof.rs
  4. 1 1
      src/state.rs

+ 77 - 5
src/bin/tx2.rs

@@ -93,7 +93,7 @@ mod tx2 {
         pallas,
     };
 
-    use super::{VerifyFailed, VerifyResult};
+    use super::{VerifyFailed, VerifyResult, MerkleNode};
     use drk::{
         crypto::{
             mint_proof::{create_mint_proof, verify_mint_proof, MintRevealedValues},
@@ -119,7 +119,11 @@ mod tx2 {
         pub signature_secret: pallas::Base,
     }
 
-    pub struct TransactionBuilderInputInfo {}
+    pub struct TransactionBuilderInputInfo {
+        pub merkle_path: Vec<MerkleNode>,
+        pub secret: pallas::Base,
+        pub note: Note,
+    }
 
     pub struct TransactionBuilderOutputInfo {
         pub value: u64,
@@ -167,7 +171,46 @@ mod tx2 {
                 clear_inputs.push(clear_input);
             }
 
+            let mut inputs = vec![];
             let mut input_blinds = vec![];
+            let mut signature_secrets = vec![];
+            for input in &self.inputs {
+                input_blinds.push(input.note.value_blind);
+
+                let signature_secret = pallas::Base::random(&mut OsRng);
+
+                /*
+                // TODO: Some stupid glue code. Need to sort this out
+                let auth_path: Vec<(bls12_381::Scalar, bool)> = input
+                    .merkle_path
+                    .auth_path
+                    .iter()
+                    .map(|(node, b)| ((*node).into(), *b))
+                    .collect();
+                */
+
+                //let (proof, revealed) = create_spend_proof(
+                //    input.note.value,
+                //    input.note.token_id,
+                //    input.note.value_blind,
+                //    token_blind,
+                //    input.note.serial,
+                //    input.note.coin_blind,
+                //    input.secret,
+                //    auth_path,
+                //    signature_secret,
+                //)?;
+
+                //// First we make the tx then sign after
+                //let signature_secret = schnorr::SecretKey(signature_secret);
+                signature_secrets.push(signature_secret);
+
+                let input = PartialTransactionInput {
+                    //spend_proof: proof,
+                    //revealed,
+                };
+                inputs.push(input);
+            }
 
             let mut outputs = vec![];
             let mut output_blinds = vec![];
@@ -215,7 +258,7 @@ mod tx2 {
 
             let partial_tx = PartialTransaction {
                 clear_inputs,
-                //inputs,
+                inputs,
                 outputs,
             };
 
@@ -236,7 +279,7 @@ mod tx2 {
 
     pub struct PartialTransaction {
         pub clear_inputs: Vec<PartialTransactionClearInput>,
-        //pub inputs: Vec<PartialTransactionInput>,
+        pub inputs: Vec<PartialTransactionInput>,
         pub outputs: Vec<TransactionOutput>,
     }
 
@@ -248,6 +291,11 @@ mod tx2 {
         pub signature_public: pallas::Point,
     }
 
+    pub struct PartialTransactionInput {
+        //pub spend_proof: Proof,
+        //pub revealed: SpendRevealedValues,
+    }
+
     pub struct Transaction {
         pub clear_inputs: Vec<TransactionClearInput>,
         pub outputs: Vec<TransactionOutput>,
@@ -426,7 +474,7 @@ pub fn state_transition<S: ProgramState>(
     let mut enc_notes = vec![];
     for output in tx.outputs {
         // Gather all the coins
-        coins.push(Coin::from_bytes(&output.revealed.coin));
+        coins.push(Coin(output.revealed.coin.clone()));
         enc_notes.push(output.enc_note);
     }
 
@@ -552,9 +600,33 @@ fn main() -> std::result::Result<(), failure::Error> {
 
     let tx = builder.build()?;
 
+    let mut tree = BridgeTree::<MerkleNode, 2>::new(100);
+    let node = MerkleNode(tx.outputs[0].revealed.coin.clone());
+    tree.append(&node);
+    tree.witness();
+    let (merkle_position, merkle_path) = tree.authentication_path(&node).unwrap();
+
+    let note = tx.outputs[0].enc_note.decrypt(&secret)?;
+
     let update = state_transition(&state, tx)?;
     state.apply(update);
 
+    // Now spend
+
+    let builder = tx2::TransactionBuilder {
+        clear_inputs: vec![],
+        inputs: vec![tx2::TransactionBuilderInputInfo {
+            merkle_path,
+            secret,
+            note,
+        }],
+        outputs: vec![tx2::TransactionBuilderOutputInfo {
+            value: 110,
+            token_id,
+            public,
+        }],
+    };
+
     let mut tree = BridgeTree::<MerkleNode, 2>::new(100);
     let coin1 = MerkleNode(pallas::Base::random(&mut OsRng));
     let coin2 = MerkleNode(pallas::Base::random(&mut OsRng));

+ 1 - 1
src/crypto/coin.rs

@@ -8,7 +8,7 @@ use crate::{
 };
 
 #[derive(Clone, Copy, Debug)]
-pub struct Coin(pallas::Base);
+pub struct Coin(pub pallas::Base);
 
 impl Coin {
     pub fn from_bytes(bytes: &[u8; 32]) -> Self {

+ 8 - 5
src/crypto/mint_proof.rs

@@ -7,6 +7,7 @@ use log::debug;
 use pasta_curves::{
     arithmetic::{CurveAffine, FieldExt},
     group::Curve,
+    pallas,
 };
 
 use super::{
@@ -23,7 +24,8 @@ use crate::{
 pub struct MintRevealedValues {
     pub value_commit: DrkValueCommit,
     pub token_commit: DrkValueCommit,
-    pub coin: [u8; 32],
+    //pub coin: [u8; 32],
+    pub coin: pallas::Base,
 }
 
 impl MintRevealedValues {
@@ -46,12 +48,12 @@ impl MintRevealedValues {
             [serial, coin_blind],
         ];
 
-        let mut hash = DrkCoin::zero();
+        let mut coin = DrkCoin::zero();
         for msg in messages.iter() {
-            hash += primitives::poseidon::Hash::init(P128Pow5T3, ConstantLength::<2>).hash(*msg);
+            coin += primitives::poseidon::Hash::init(P128Pow5T3, ConstantLength::<2>).hash(*msg);
         }
 
-        let coin = hash.to_bytes();
+        //let coin = hash.to_bytes();
 
         MintRevealedValues {
             value_commit,
@@ -65,7 +67,8 @@ impl MintRevealedValues {
         let token_coords = self.token_commit.to_affine().coordinates().unwrap();
 
         vec![
-            DrkCircuitField::from_bytes(&self.coin).unwrap(),
+            //DrkCircuitField::from_bytes(&self.coin).unwrap(),
+            self.coin.clone(),
             *value_coords.x(),
             *value_coords.y(),
             *token_coords.x(),

+ 1 - 1
src/state.rs

@@ -119,7 +119,7 @@ pub fn state_transition<S: ProgramState>(
     let mut enc_notes = vec![];
     for output in tx.outputs {
         // Gather all the coins
-        coins.push(Coin::from_bytes(&output.revealed.coin));
+        coins.push(Coin(output.revealed.coin.clone()));
         enc_notes.push(output.enc_note);
     }