Browse Source

Add create_MAC and compute_MAC_coeff functions to CMZCred

Ian Goldberg 4 months ago
parent
commit
324e24d42a
2 changed files with 25 additions and 2 deletions
  1. 14 2
      cmzcred_derive/src/lib.rs
  2. 11 0
      src/lib.rs

+ 14 - 2
cmzcred_derive/src/lib.rs

@@ -146,8 +146,7 @@ fn impl_cmzcred_derive(ast: &syn::DeriveInput, group_ident: &Ident) -> TokenStre
                 (privkey, pubkey)
             }
 
-            fn verify_MAC(&self, privkey: &CMZPrivkey<Self::Point>) ->
-                    Result<(),()> {
+            fn compute_MAC_coeff(&self, privkey: &CMZPrivkey<Self::Point>) -> Result<Self::Scalar, ()> {
                 if privkey.x.len() != Self::num_attrs() {
                     return Err(());
                 }
@@ -159,6 +158,19 @@ fn impl_cmzcred_derive(ast: &syn::DeriveInput, group_ident: &Ident) -> TokenStre
                     let attr_val = self.attr(field).ok_or(())?;
                     coeff += attr_val * privkey.x[Self::attr_num(field)];
                 }
+                Ok(coeff)
+            }
+
+            fn create_MAC(&mut self, rng: &mut impl RngCore, privkey: &CMZPrivkey<Self::Point>) -> Result<(),()> {
+                let coeff = self.compute_MAC_coeff(privkey)?;
+                self.MAC.P = <Self::Point as group::Group>::random(&mut *rng);
+                self.MAC.Q = coeff * self.MAC.P;
+                Ok(())
+            }
+
+            fn verify_MAC(&self, privkey: &CMZPrivkey<Self::Point>) ->
+                    Result<(),()> {
+                let coeff = self.compute_MAC_coeff(privkey)?;
                 if !bool::from(self.MAC.P.is_identity()) && coeff * self.MAC.P == self.MAC.Q {
                     Ok(())
                 } else {

+ 11 - 0
src/lib.rs

@@ -359,6 +359,17 @@ where
         slf
     }
 
+    /// Create the MAC for this credential, given the private key.
+    fn create_MAC(
+        &mut self,
+        rng: &mut impl RngCore,
+        privkey: &CMZPrivkey<Self::Point>,
+    ) -> Result<(), ()>;
+
+    /// Compute the coefficient component of the MAC (the Scalar you
+    /// would multiply P by to get Q), given the private key.
+    fn compute_MAC_coeff(&self, privkey: &CMZPrivkey<Self::Point>) -> Result<Self::Scalar, ()>;
+
     /// Verify the MAC in this credential, given the private key.  This
     /// is mainly useful for debugging, since the client will not have
     /// the private key and the issuer will typically not have the