Explorar el Código

Add using_privkey and using_pubkey convenience functions to trait CMZCredential

These functions act like default(), but also load the given private or
public key
Ian Goldberg hace 11 meses
padre
commit
7fb14f8a23
Se han modificado 3 ficheros con 28 adiciones y 9 borrados
  1. 5 2
      cmzcred_derive/src/lib.rs
  2. 22 3
      src/lib.rs
  3. 1 4
      tests/basic.rs

+ 5 - 2
cmzcred_derive/src/lib.rs

@@ -77,17 +77,20 @@ fn impl_cmzcred_derive(ast: &syn::DeriveInput, group_ident: &Ident) -> TokenStre
                 }
             }
 
-            fn set_pubkey(&mut self, pubkey: &CMZPubkey<Self::Point>) {
+            fn set_pubkey(&mut self, pubkey: &CMZPubkey<Self::Point>) -> &mut Self {
                 self.pubkey = pubkey.clone();
+                self
             }
 
             fn get_pubkey(&self) -> CMZPubkey<Self::Point> {
                 self.pubkey.clone()
             }
 
-            fn set_privkey(&mut self, privkey: &CMZPrivkey<Self::Point>) {
+            fn set_privkey(&mut self, privkey: &CMZPrivkey<Self::Point>)
+            -> &mut Self {
                 self.pubkey = cmz_privkey_to_pubkey(&privkey);
                 self.privkey = privkey.clone();
+                self
             }
 
             fn get_privkey(&self) -> CMZPrivkey<Self::Point> {

+ 22 - 3
src/lib.rs

@@ -169,7 +169,10 @@ pub fn cmz_privkey_to_pubkey<G: PrimeGroup>(privkey: &CMZPrivkey<G>) -> CMZPubke
 }
 
 /// The CMZCredential trait implemented by all CMZ credential struct types.
-pub trait CMZCredential<G: PrimeGroup> {
+pub trait CMZCredential<G: PrimeGroup>
+where
+    Self: Default + Sized,
+{
     /// The type of attributes for this credential
     type Scalar: PrimeField;
 
@@ -192,7 +195,7 @@ pub trait CMZCredential<G: PrimeGroup> {
     fn attr_mut(&mut self, name: &str) -> &mut Option<Self::Scalar>;
 
     /// Set the public key for this credential.
-    fn set_pubkey(&mut self, pubkey: &CMZPubkey<G>);
+    fn set_pubkey(&mut self, pubkey: &CMZPubkey<G>) -> &mut Self;
 
     /// Get a copy of the public key for this credential.  If the public
     /// key has not yet been set or computed, a pubkey with X0 == None
@@ -201,7 +204,7 @@ pub trait CMZCredential<G: PrimeGroup> {
 
     /// Set the private key for this credential.  The public key will
     /// automatically be computed from the private key.
-    fn set_privkey(&mut self, privkey: &CMZPrivkey<G>);
+    fn set_privkey(&mut self, privkey: &CMZPrivkey<G>) -> &mut Self;
 
     /// Get a copy of the private key for this credential.  If the
     /// private key has not yet been set, a privkey with an empty x
@@ -211,6 +214,22 @@ pub trait CMZCredential<G: PrimeGroup> {
     /// Generate random private and public keys for this credential
     /// type.
     fn gen_keys(rng: &mut impl RngCore) -> (CMZPrivkey<G>, CMZPubkey<G>);
+
+    /// Convenience function for creating a new Self, and loading the
+    /// given private key (which will also compute the public key).
+    fn using_privkey(privkey: &CMZPrivkey<G>) -> Self {
+        let mut slf = Self::default();
+        slf.set_privkey(privkey);
+        slf
+    }
+
+    /// Convenience function for creating a new Self, and loading the
+    /// given public key.
+    fn using_pubkey(pubkey: &CMZPubkey<G>) -> Self {
+        let mut slf = Self::default();
+        slf.set_pubkey(pubkey);
+        slf
+    }
 }
 
 /** The CMZ macro for declaring CMZ credentials.

+ 1 - 4
tests/basic.rs

@@ -15,12 +15,9 @@ fn test_basic() {
     cmz_group_init(RistrettoPoint::hash_from_bytes::<Sha512>(
         b"CMZ Generator A",
     ));
-    let mut basic_cred = Basic::default();
-
-    println!("{:#?}", basic_cred);
 
     let (privkey, pubkey) = Basic::gen_keys(&mut rng);
-    basic_cred.set_privkey(&privkey);
+    let basic_cred = Basic::using_privkey(&privkey);
 
     println!("{:#?}", basic_cred);
 }