Browse Source

Use WnafBase and WnafScalar for Migration Cred

onyinyang 7 months ago
parent
commit
63ccdfe8ed
3 changed files with 27 additions and 35 deletions
  1. 23 28
      src/migration_table.rs
  2. 2 3
      src/proto/check_blockage.rs
  3. 2 4
      src/proto/trust_promotion.rs

+ 23 - 28
src/migration_table.rs

@@ -8,31 +8,28 @@ that the credentials contain the bucket attributes, which include both
 the id and the bucket decryption key, but the table just contains the
 the id and the bucket decryption key, but the table just contains the
 bucket ids.) */
 bucket ids.) */
 
 
-#[cfg(feature = "bridgeauth")]
-use cmz::CMZPrivkey;
-use cmz::{CMZCredential, CMZPubkey};
+use cmz::*;
 use curve25519_dalek::ristretto::CompressedRistretto;
 use curve25519_dalek::ristretto::CompressedRistretto;
-#[cfg(feature = "bridgeauth")]
-use curve25519_dalek::ristretto::RistrettoBasepointTable;
-use curve25519_dalek::ristretto::RistrettoPoint as G;
-use curve25519_dalek::scalar::Scalar;
-
-use sha2::Digest;
-use sha2::Sha256;
-
+//#[cfg(feature = "bridgeauth")]
 use aes_gcm::aead::{generic_array::GenericArray, Aead};
 use aes_gcm::aead::{generic_array::GenericArray, Aead};
 use aes_gcm::{Aes128Gcm, KeyInit};
 use aes_gcm::{Aes128Gcm, KeyInit};
-#[cfg(feature = "bridgeauth")]
+use ff::PrimeField;
+use group::{WnafBase, WnafScalar};
+use sha2::Digest;
+use sha2::Sha256;
+//#[cfg(feature = "bridgeauth")]
 use rand::RngCore;
 use rand::RngCore;
 
 
 use std::collections::HashMap;
 use std::collections::HashMap;
 
 
-#[cfg(feature = "bridgeauth")]
-use serde::{Deserialize, Serialize};
-
+//#[cfg(feature = "bridgeauth")]
 #[cfg(feature = "bridgeauth")]
 #[cfg(feature = "bridgeauth")]
 use super::bridge_table;
 use super::bridge_table;
 use super::lox_creds::{Migration, MigrationKey};
 use super::lox_creds::{Migration, MigrationKey};
+use super::{Scalar, G};
+use serde::{Deserialize, Serialize};
+
+pub const WNAF_SIZE: usize = 6;
 
 
 /// Each (plaintext) entry in the returned migration table is serialized
 /// Each (plaintext) entry in the returned migration table is serialized
 /// into this many bytes
 /// into this many bytes
@@ -51,7 +48,7 @@ pub enum MigrationType {
     Blockage,
     Blockage,
 }
 }
 
 
-impl From<MigrationType> for Scalar {
+impl From<MigrationType> for u128 {
     /// Convert a MigrationType into the Scalar value that represents
     /// Convert a MigrationType into the Scalar value that represents
     /// it in the Migration credential
     /// it in the Migration credential
     fn from(m: MigrationType) -> Self {
     fn from(m: MigrationType) -> Self {
@@ -65,7 +62,7 @@ impl From<MigrationType> for Scalar {
 
 
 /// The migration table
 /// The migration table
 #[derive(Default, Debug, Serialize, Deserialize)]
 #[derive(Default, Debug, Serialize, Deserialize)]
-#[cfg(feature = "bridgeauth")]
+//#[cfg(feature = "bridgeauth")]
 pub struct MigrationTable {
 pub struct MigrationTable {
     pub table: HashMap<u32, u32>,
     pub table: HashMap<u32, u32>,
     pub migration_type: Scalar,
     pub migration_type: Scalar,
@@ -83,13 +80,13 @@ pub struct MigrationTable {
 /// from_attr_i, Qk_i) and the encrypted Migration credential.  H1 and
 /// from_attr_i, Qk_i) and the encrypted Migration credential.  H1 and
 /// H2 are the first 16 bytes and the second 16 bytes respectively of
 /// H2 are the first 16 bytes and the second 16 bytes respectively of
 /// the SHA256 hash of the input.
 /// the SHA256 hash of the input.
-#[cfg(feature = "bridgeauth")]
+//#[cfg(feature = "bridgeauth")]
 pub fn encrypt_cred(
 pub fn encrypt_cred(
     id: Scalar,
     id: Scalar,
     from_bucket: Scalar,
     from_bucket: Scalar,
     to_bucket: Scalar,
     to_bucket: Scalar,
     migration_type: Scalar,
     migration_type: Scalar,
-    Pktable: &RistrettoBasepointTable,
+    Pktable: &WnafBase<G, WNAF_SIZE>,
     migration_priv: &CMZPrivkey<G>,
     migration_priv: &CMZPrivkey<G>,
     migrationkey_priv: &CMZPrivkey<G>,
     migrationkey_priv: &CMZPrivkey<G>,
 ) -> ([u8; 16], [u8; ENC_MIGRATION_BYTES]) {
 ) -> ([u8; 16], [u8; ENC_MIGRATION_BYTES]) {
@@ -100,10 +97,8 @@ pub fn encrypt_cred(
     let mut K = MigrationKey::using_privkey(migrationkey_priv);
     let mut K = MigrationKey::using_privkey(migrationkey_priv);
     K.lox_id = Some(id);
     K.lox_id = Some(id);
     K.from_bucket = Some(from_bucket);
     K.from_bucket = Some(from_bucket);
-    K.MAC.Q = &(migrationkey_priv.x[0]
-        + migrationkey_priv.x[1] * id
-        + migrationkey_priv.x[2] * from_bucket)
-        * Pktable;
+    let coeff: Scalar = K.compute_MAC_coeff(&migrationkey_priv).unwrap();
+    K.MAC.Q = Pktable * &WnafScalar::new(&coeff);
 
 
     // Compute a MAC (P, Q) on the Migration credential
     // Compute a MAC (P, Q) on the Migration credential
     let mut M = Migration::using_privkey(migration_priv);
     let mut M = Migration::using_privkey(migration_priv);
@@ -111,7 +106,7 @@ pub fn encrypt_cred(
     M.from_bucket = Some(from_bucket);
     M.from_bucket = Some(from_bucket);
     M.to_bucket = Some(to_bucket);
     M.to_bucket = Some(to_bucket);
     M.migration_type = Some(migration_type);
     M.migration_type = Some(migration_type);
-    // let b = Scalar::random(&mut rng);
+    let _ = M.create_MAC(&mut rng, migration_priv);
 
 
     // Serialize (to_bucket, P, Q)
     // Serialize (to_bucket, P, Q)
     let mut credbytes: [u8; MIGRATION_BYTES] = [0; MIGRATION_BYTES];
     let mut credbytes: [u8; MIGRATION_BYTES] = [0; MIGRATION_BYTES];
@@ -162,7 +157,7 @@ pub fn encrypt_cred_ids(
     to_id: u32,
     to_id: u32,
     migration_type: Scalar,
     migration_type: Scalar,
     bridgetable: &bridge_table::BridgeTable,
     bridgetable: &bridge_table::BridgeTable,
-    Pktable: &RistrettoBasepointTable,
+    Pktable: &WnafBase<G, WNAF_SIZE>,
     migration_priv: &CMZPrivkey<G>,
     migration_priv: &CMZPrivkey<G>,
     migrationkey_priv: &CMZPrivkey<G>,
     migrationkey_priv: &CMZPrivkey<G>,
 ) -> Option<([u8; 16], [u8; ENC_MIGRATION_BYTES])> {
 ) -> Option<([u8; 16], [u8; ENC_MIGRATION_BYTES])> {
@@ -186,7 +181,7 @@ impl MigrationTable {
     pub fn new(table_type: MigrationType) -> Self {
     pub fn new(table_type: MigrationType) -> Self {
         Self {
         Self {
             table: Default::default(),
             table: Default::default(),
-            migration_type: table_type.into(),
+            migration_type: Scalar::from_u128(table_type.into()),
         }
         }
     }
     }
 
 
@@ -197,7 +192,7 @@ impl MigrationTable {
         &self,
         &self,
         id: Scalar,
         id: Scalar,
         bridgetable: &bridge_table::BridgeTable,
         bridgetable: &bridge_table::BridgeTable,
-        Pktable: &RistrettoBasepointTable,
+        Pktable: &WnafBase<G, WNAF_SIZE>,
         migration_priv: &CMZPrivkey<G>,
         migration_priv: &CMZPrivkey<G>,
         migrationkey_priv: &CMZPrivkey<G>,
         migrationkey_priv: &CMZPrivkey<G>,
     ) -> HashMap<[u8; 16], [u8; ENC_MIGRATION_BYTES]> {
     ) -> HashMap<[u8; 16], [u8; ENC_MIGRATION_BYTES]> {
@@ -272,6 +267,6 @@ pub fn decrypt_cred(
     M.lox_id = mk_cred.lox_id;
     M.lox_id = mk_cred.lox_id;
     M.from_bucket = mk_cred.from_bucket;
     M.from_bucket = mk_cred.from_bucket;
     M.to_bucket = Some(to_bucket);
     M.to_bucket = Some(to_bucket);
-    M.migration_type = Some(migration_type.into());
+    M.migration_type = Some(Scalar::from_u128(migration_type.into()));
     Some(M)
     Some(M)
 }
 }

+ 2 - 3
src/proto/check_blockage.rs

@@ -39,9 +39,8 @@ use crate::migration_table;
 use crate::migration_table::ENC_MIGRATION_BYTES;
 use crate::migration_table::ENC_MIGRATION_BYTES;
 use cmz::*;
 use cmz::*;
 #[cfg(feature = "bridgeauth")]
 #[cfg(feature = "bridgeauth")]
-use curve25519_dalek::ristretto::RistrettoBasepointTable;
 use curve25519_dalek::ristretto::RistrettoPoint as G;
 use curve25519_dalek::ristretto::RistrettoPoint as G;
-use group::Group;
+use group::{Group, WnafBase};
 use rand_core::RngCore;
 use rand_core::RngCore;
 use sha2::Sha512;
 use sha2::Sha512;
 use std::collections::HashMap;
 use std::collections::HashMap;
@@ -137,7 +136,7 @@ impl BridgeAuth {
             },
             },
         ) {
         ) {
             Ok((response, (L_issuer, M_issuer))) => {
             Ok((response, (L_issuer, M_issuer))) => {
-                let Pktable = RistrettoBasepointTable::create(&M_issuer.MAC.P);
+                let Pktable: WnafBase<G, WNAF_SIZE> = WnafBase::new(M_issuer.MAC.P);
                 let enc_migration_table = self.blockage_migration_table.encrypt_table(
                 let enc_migration_table = self.blockage_migration_table.encrypt_table(
                     L_issuer.id.unwrap(),
                     L_issuer.id.unwrap(),
                     &self.bridge_table,
                     &self.bridge_table,

+ 2 - 4
src/proto/trust_promotion.rs

@@ -38,9 +38,8 @@ use crate::migration_table;
 use crate::migration_table::ENC_MIGRATION_BYTES;
 use crate::migration_table::ENC_MIGRATION_BYTES;
 use cmz::*;
 use cmz::*;
 #[cfg(feature = "bridgeauth")]
 #[cfg(feature = "bridgeauth")]
-use curve25519_dalek::ristretto::RistrettoBasepointTable;
 use curve25519_dalek::ristretto::RistrettoPoint as G;
 use curve25519_dalek::ristretto::RistrettoPoint as G;
-use group::Group;
+use group::{Group, WnafBase};
 use rand_core::RngCore;
 use rand_core::RngCore;
 use sha2::Sha512;
 use sha2::Sha512;
 use std::collections::HashMap;
 use std::collections::HashMap;
@@ -152,8 +151,7 @@ impl BridgeAuth {
             },
             },
         ) {
         ) {
             Ok((response, (L_issuer, M_issuer))) => {
             Ok((response, (L_issuer, M_issuer))) => {
-                let Pktable: RistrettoBasepointTable =
-                    RistrettoBasepointTable::create(&M_issuer.MAC.P);
+                let Pktable: WnafBase<G, WNAF_SIZE> = WnafBase::new(M_issuer.MAC.P);
                 let enc_migration_table = self.trustup_migration_table.encrypt_table(
                 let enc_migration_table = self.trustup_migration_table.encrypt_table(
                     L_issuer.id.unwrap(),
                     L_issuer.id.unwrap(),
                     &self.bridge_table,
                     &self.bridge_table,