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

Merge branch 'master' into rpc-rewrite

lunar-mining 4 лет назад
Родитель
Сommit
1eda31506a
4 измененных файлов с 87 добавлено и 13 удалено
  1. 80 0
      script/research/bltprf/naive.sage
  2. 1 9
      sql/cashier.sql
  3. 3 2
      src/service/btc.rs
  4. 3 2
      src/service/cashier.rs

+ 80 - 0
script/research/bltprf/naive.sage

@@ -0,0 +1,80 @@
+q = 0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
+K = GF(q)
+a = K(0x00)
+b = K(0x05)
+E = EllipticCurve(K, (a, b))
+G = E(0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000000, 0x02)
+
+p = 0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001
+assert E.order() == p
+Scalar = GF(p)
+
+a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 = (
+    Scalar(110), Scalar(56), Scalar(89), Scalar(6543), Scalar(2),
+    Scalar(110), Scalar(44), Scalar(78), Scalar(77), Scalar(4))
+
+G1, G2, G3, G4, G5, G6, G7, G8, G9, G10 = (
+    E.random_element(), E.random_element(), E.random_element(),
+    E.random_element(), E.random_element(), E.random_element(),
+    E.random_element(), E.random_element(), E.random_element(),
+    E.random_element())
+
+A = (int(a1) * G1 + int(a2) * G2 + int(a3) * G3 + int(a4) * G4
+     + int(a5) * G5 + int(a6) * G6 + int(a7) * G7 + int(a8) * G8
+     + int(a9) * G9 + int(a10) * G10)
+
+a1, a2, a3, a4, a5 = (a1, a2), (a3, a4), (a5, a6), (a7, a8), (a9, a10)
+G1, G2, G3, G4, G5 = (G1, G2), (G3, G4), (G5, G6), (G7, G8), (G9, G10)
+
+# a1 G1-\  a2 G1    a3 G1    a4 G1    a5 G1
+# a1 G2  \-a2 G2-\  a3 G2    a4 G2    a5 G2
+# a1 G3    a2 G3  \-a3 G3-\  a4 G3    a5 G3
+# a1 G4    a2 G4    a3 G4  \-a4 G4-\  a5 G4
+# a1 G5    a2 G5    a3 G5    a4 G5  \-a5 G5
+
+# Dot product
+def dot(x, y):
+    result = None
+    for x_i, y_i in zip(x, y):
+        if result is None:
+            result = int(x_i) * y_i
+        else:
+            result += int(x_i) * y_i
+    return result
+
+# Main diagonal is sum(a_i G_i) = A
+assert dot(a1, G1) + dot(a2, G2) + dot(a3, G3) + dot(a4, G4) + dot(a5, G5) == A
+
+# Sum all the diagonals of the grid above
+A_neg_4 = dot(a1, G5)
+A_neg_3 = dot(a1, G4) + dot(a2, G5)
+A_neg_2 = dot(a1, G3) + dot(a2, G4) + dot(a3, G5)
+A_neg_1 = dot(a1, G2) + dot(a2, G3) + dot(a3, G4) + dot(a4, G5)
+A_0 = A
+A_1 = dot(a2, G1) + dot(a3, G2) + dot(a4, G3) + dot(a5, G4)
+A_2 = dot(a3, G1) + dot(a4, G2) + dot(a5, G3)
+A_3 = dot(a4, G1) + dot(a5, G2)
+A_4 = dot(a5, G1)
+
+x = Scalar.random_element()
+a_prime = (x * vector(a1) + x^2 * vector(a2)
+           + x^3 * vector(a3) + x^4 * vector(a4)
+           + x^5 * vector(a5))
+# Sage cannot do this:
+#
+# G_prime = (int(x^-1) * vector(G1) + int(x^-2) * vector(G2)
+#            + int(x^-3) * vector(G3) + int(x^-4) * vector(G4)
+#            + int(x^-5) * vector(G5))
+G_prime = [(int(x^-1) * G1[0] + int(x^-2) * G2[0] + int(x^-3) * G3[0]
+            + int(x^-4) * G4[0] + int(x^-5) * G5[0]),
+           (int(x^-1) * G1[1] + int(x^-2) * G2[1] + int(x^-3) * G3[1]
+            + int(x^-4) * G4[1] + int(x^-5) * G5[1])]
+assert len(a_prime) == len(G_prime) == 2
+A_prime = dot(a_prime, G_prime)
+
+assert (int(x^-4) * A_neg_4 + int(x^-3) * A_neg_3 + int(x^-2) * A_neg_2
+        + int(x^-1) * A_neg_1
+        + A
+        + int(x) * A_1 + int(x^2) * A_2 + int(x^3) * A_3 + int(x^4) * A_4) \
+       == A_prime
+

+ 1 - 9
sql/cashier.sql

@@ -4,15 +4,7 @@ CREATE TABLE IF NOT EXISTS deposit_keypairs(
     coin_key_public BLOB NOT NULL,
 	asset_id BLOB NOT NULL
 );
-PRAGMA foreign_keys=on;
-CREATE TABLE IF NOT EXISTS btc_utxo(
-    tx_id BLOB PRIMARY KEY NOT NULL,
-    balance INTEGER NOT NULL,
-    btc_key_public BLOB NOT NULL,
-    FOREIGN KEY (btc_key_public)
-        REFERENCES deposit_keypairs (coin_key_public)
-        ON UPDATE CASCADE
-);
+
 CREATE TABLE IF NOT EXISTS withdraw_keypairs(
     coin_key_id BLOB PRIMARY KEY NOT NULL,
 	d_key_private BLOB NOT NULL,

+ 3 - 2
src/service/btc.rs

@@ -148,13 +148,13 @@ pub struct BtcClient {
 }
 
 impl BtcClient {
-    pub fn new( btc_endpoint: (bool, String) ) -> Result<Self> {
+    pub fn new( btc_endpoint: (Network, String) ) -> Result<Self> {
         let (network, client_address) = btc_endpoint;
         let client = ElectrumClient::new(&client_address)
             .map_err(|err| crate::Error::from(super::BtcFailed::from(err)))?;
         Ok(Self {
             client: Arc::new(client),
-            network: if network {Network::Testnet} else {Network::Bitcoin},
+            network: network,
         })
     }
 }
@@ -181,6 +181,7 @@ impl CoinClient for BtcClient {
 
     async fn send(&self, _address: Vec<u8>, _amount: u64) -> Result<()> {
         // TODO
+
         Ok(())
     }
 }

+ 3 - 2
src/service/cashier.rs

@@ -76,8 +76,9 @@ impl CashierService {
         cfg_if::cfg_if! {
             if #[cfg(feature = "default")]{
                 // TODO: the endpoint should be generic according to asset_id
-                let btc_endpoint: (bool, String) = 
-                    (true, String::from("ssl://blockstream.info:993"));
+                let btc_endpoint: (bitcoin::network::constants::Network, String) =
+                    (bitcoin::network::constants::Network::Bitcoin,
+                     String::from("ssl://blockstream.info:993"));
 
                 let btc_client = super::btc::BtcClient::new(btc_endpoint)?;
                 bridge.clone().add_clients(asset_id, Arc::new(btc_client)).await;