lib.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // We really want points to be capital letters and scalars to be
  2. // lowercase letters
  3. #![allow(non_snake_case)]
  4. #![doc = include_str!("../README.md")]
  5. pub use cmz_derive::*;
  6. use core::any::Any;
  7. use ff::{Field, PrimeField};
  8. use generic_static::StaticTypeMap;
  9. use group::prime::PrimeGroup;
  10. use group::{Group, GroupEncoding, WnafBase, WnafScalar};
  11. use lazy_static::lazy_static;
  12. use rand::RngCore;
  13. use serde::{Deserialize, Deserializer, Serialize, Serializer};
  14. pub use serde_with::serde_as;
  15. use serde_with::{DeserializeAs, SerializeAs};
  16. use sigma_compiler::*;
  17. pub use sigma_compiler::{self};
  18. use thiserror::Error;
  19. // We need wrappers for group::Group and ff::PrimeField elements to be
  20. // handled by serde
  21. //
  22. // Pattern from https://docs.rs/serde_with/3.12.0/serde_with/guide/serde_as/index.html
  23. mod group_serde;
  24. /// A wrapper for serializing and deserializing a `Scalar`
  25. pub struct SerdeScalar;
  26. impl<F: PrimeField> SerializeAs<F> for SerdeScalar {
  27. fn serialize_as<S>(value: &F, serializer: S) -> Result<S::Ok, S::Error>
  28. where
  29. S: Serializer,
  30. {
  31. group_serde::serialize_scalar(value, serializer)
  32. }
  33. }
  34. impl<'de, F: PrimeField> DeserializeAs<'de, F> for SerdeScalar {
  35. fn deserialize_as<D>(deserializer: D) -> Result<F, D::Error>
  36. where
  37. D: Deserializer<'de>,
  38. {
  39. group_serde::deserialize_scalar(deserializer)
  40. }
  41. }
  42. /// A wrapper for serializing and deserializing a `Point` (a group
  43. /// element)
  44. pub struct SerdePoint;
  45. impl<G: Group + GroupEncoding> SerializeAs<G> for SerdePoint {
  46. fn serialize_as<S>(value: &G, serializer: S) -> Result<S::Ok, S::Error>
  47. where
  48. S: Serializer,
  49. {
  50. group_serde::serialize_point(value, serializer)
  51. }
  52. }
  53. impl<'de, G: Group + GroupEncoding> DeserializeAs<'de, G> for SerdePoint {
  54. fn deserialize_as<D>(deserializer: D) -> Result<G, D::Error>
  55. where
  56. D: Deserializer<'de>,
  57. {
  58. group_serde::deserialize_point(deserializer)
  59. }
  60. }
  61. /// The CMZMac struct represents a MAC on a CMZ credential.
  62. #[serde_as]
  63. #[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
  64. pub struct CMZMac<G: PrimeGroup> {
  65. #[serde_as(as = "SerdePoint")]
  66. pub P: G,
  67. #[serde_as(as = "SerdePoint")]
  68. pub Q: G,
  69. }
  70. /// The CMZPrivkey struct represents a CMZ private key
  71. #[serde_as]
  72. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
  73. pub struct CMZPrivkey<G: PrimeGroup> {
  74. // Is this key for µCMZ or classic CMZ14?
  75. pub muCMZ: bool,
  76. #[serde_as(as = "SerdeScalar")]
  77. pub x0: <G as Group>::Scalar,
  78. // The next field is xr for µCMZ, and serves the role of x0tilde for
  79. // CMZ14
  80. #[serde_as(as = "SerdeScalar")]
  81. pub xr: <G as Group>::Scalar,
  82. // The elements of x correspond to the attributes of the credential
  83. #[serde_as(as = "Vec<SerdeScalar>")]
  84. pub x: Vec<<G as Group>::Scalar>,
  85. }
  86. /// The CMZPubkey struct represents a CMZ public key
  87. #[serde_as]
  88. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
  89. pub struct CMZPubkey<G: PrimeGroup> {
  90. #[serde_as(as = "Option<SerdePoint>")]
  91. pub X0: Option<G>,
  92. // Xr is only used for µCMZ, not CMZ14 (where it will be None)
  93. #[serde_as(as = "Option<SerdePoint>")]
  94. pub Xr: Option<G>,
  95. // The elements of X correspond to the attributes of the credential
  96. #[serde_as(as = "Vec<SerdePoint>")]
  97. pub X: Vec<G>,
  98. }
  99. // The size of the WNAF windows. Larger sizes take more memory, but
  100. // result in faster multiplications.
  101. const WNAF_SIZE: usize = 6;
  102. /// A struct (generic over G) holding the two CMZ bases, and their Wnaf
  103. /// basepoint tables
  104. #[derive(Clone)]
  105. pub struct CMZBasepoints<G: Group> {
  106. A_: G,
  107. B_: G,
  108. A_TABLE: WnafBase<G, WNAF_SIZE>,
  109. B_TABLE: WnafBase<G, WNAF_SIZE>,
  110. }
  111. impl<G: Group> CMZBasepoints<G> {
  112. pub fn init(generator_A: G) -> Self {
  113. let A_ = generator_A;
  114. let B_ = G::generator();
  115. let A_TABLE = WnafBase::new(A_);
  116. let B_TABLE = WnafBase::new(B_);
  117. CMZBasepoints {
  118. A_,
  119. B_,
  120. A_TABLE,
  121. B_TABLE,
  122. }
  123. }
  124. pub fn mulA(&self, s: &G::Scalar) -> G {
  125. let wnaf_s = WnafScalar::<G::Scalar, WNAF_SIZE>::new(s);
  126. &self.A_TABLE * &wnaf_s
  127. }
  128. pub fn mulB(&self, s: &G::Scalar) -> G {
  129. let wnaf_s = WnafScalar::<G::Scalar, WNAF_SIZE>::new(s);
  130. &self.B_TABLE * &wnaf_s
  131. }
  132. pub fn keypairA(&self, rng: &mut impl RngCore) -> (G::Scalar, G) {
  133. let x = G::Scalar::random(&mut *rng);
  134. (x, self.mulA(&x))
  135. }
  136. pub fn keypairB(&self, rng: &mut impl RngCore) -> (G::Scalar, G) {
  137. let x = G::Scalar::random(&mut *rng);
  138. (x, self.mulB(&x))
  139. }
  140. pub fn A(&self) -> G {
  141. self.A_
  142. }
  143. pub fn B(&self) -> G {
  144. self.B_
  145. }
  146. }
  147. // What's going on here needs some explanation. For each group G, we
  148. // want to pre-compute the WnafBase tables in a [`CMZBasepoints`] struct,
  149. // and we want that pre-computed struct to remain globally accessible.
  150. // So ideally, we'd just have a generic static CMZBasepoints<G> struct,
  151. // and instantiate it once for each G that we use.
  152. //
  153. // The tricky bit is that we don't know what group(s) G the programmer
  154. // (the person using this cmz crate) will end up using, and Rust doesn't
  155. // support generic statics.
  156. //
  157. // So what we'd like is a non-generic static _map_ that maps a group
  158. // type G to the precomputed CMZBasepoints<G> struct. But types aren't
  159. // values that can be mapped by a normal HashMap. Luckily, there's a
  160. // generic_static crate that provides a StaticTypeMap that has the
  161. // ability to map types to objects.
  162. //
  163. // However, all of those *mapped-to* objects have to all be of the same
  164. // type, whereas we want the type G to map to a struct of type
  165. // CMZBasepoints<G>, which is different for each value of G.
  166. //
  167. // So we make a non-generic trait CMZbp that all instantiations of
  168. // CMZBasepoints<G> implement (for all group types G), and have the
  169. // StaticTypeMap map each type G to a trait object Box<dyn CMZbp>.
  170. //
  171. // Then to read the CMZBasepoints<G> back out, we look up the trait
  172. // object in the StaticTypeMap, yielding a Box<dyn CMZbp>. We now need
  173. // to downcast this trait object to the concrete type CMZBasepoints<G>,
  174. // for a _specific_ G. Rust provides downcasting, but only from &dyn Any
  175. // to the original concrete type, not from other things like &dyn CMZbp.
  176. // So first we need to upcast the trait object to &dyn Any, which we do
  177. // with an "as_any()" function in the CMZbp trait, and then downcast the
  178. // result to a CMZBasepoints<G> struct.
  179. //
  180. // The up/down casting pattern is from
  181. // https://stackoverflow.com/questions/33687447/how-to-get-a-reference-to-a-concrete-type-from-a-trait-object
  182. // Static objects have to be Sync + Send, so enforce that as part of the
  183. // CMXBP trait
  184. trait CMZbp: Sync + Send {
  185. fn as_any(&self) -> &dyn Any;
  186. }
  187. impl<G: Group> CMZbp for CMZBasepoints<G> {
  188. fn as_any(&self) -> &dyn Any {
  189. self
  190. }
  191. }
  192. // The StaticTypeMap mapping group types G to trait objects Box<dyn CMZbp>
  193. lazy_static! {
  194. static ref basepoints_map: StaticTypeMap<Box<dyn CMZbp>> = StaticTypeMap::new();
  195. }
  196. /// For a given group type `G`, if `bp` is `Some(b)`, then load the
  197. /// mapping from `G` to `b` into the `basepoints_map`. (If a mapping
  198. /// from `G` already exists, the old one will be kept and the new one
  199. /// ignored.) Whether `bp` is `Some(b)` or `None`, this function
  200. /// returns the (possibly new) target of the `basepoints_map`, as a
  201. /// `&'static CMZBasepoints<G>`.
  202. fn load_bp<G: Group>(bp: Option<CMZBasepoints<G>>) -> &'static CMZBasepoints<G> {
  203. match bp {
  204. Some(b) => basepoints_map.call_once::<Box<dyn CMZbp>, _>(|| Box::new(b.clone())),
  205. None => {
  206. basepoints_map.call_once::<Box<dyn CMZbp>, _>(|| panic!("basepoints uninitialized"))
  207. }
  208. }
  209. .as_any()
  210. .downcast_ref::<CMZBasepoints<G>>()
  211. .unwrap()
  212. }
  213. /// Initialize the required second generator for a `PrimeGroup`.
  214. ///
  215. /// CMZ credentials require two generators, `A` and `B`. `B` is the
  216. /// "standard" generator. A can be any other generator (that is, any
  217. /// other non-identity point in a prime-order group), but it is required
  218. /// that no one know the discrete log between `A` and `B`. So you can't
  219. /// generate `A` by multiplying `B` by some scalar, for example. If your
  220. /// group has a hash_from_bytes function, then you can use that to generate
  221. /// `A`. For example, if your group is a curve25519 group, you can
  222. ///
  223. /// ```
  224. /// use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT as B;
  225. /// use curve25519_dalek::ristretto::RistrettoPoint as G;
  226. /// use sha2::Sha512;
  227. /// let A = G::hash_from_bytes::<Sha512>(b"CMZ Generator A");
  228. /// assert_ne!(A, B);
  229. /// ```
  230. ///
  231. /// Otherwise, you're possibly on your own to generate an appropriate
  232. /// generator `A`. Everyone who uses a given credential type with a
  233. /// given group will need to use the same `A`. You need to call this
  234. /// before doing any operations with a credential.
  235. pub fn cmz_group_init<G: PrimeGroup>(generator_A: G) {
  236. let bp = CMZBasepoints::<G>::init(generator_A);
  237. load_bp(Some(bp));
  238. }
  239. /// Get the loaded CMZBasepoints for the given group
  240. pub fn cmz_basepoints<G: PrimeGroup>() -> &'static CMZBasepoints<G> {
  241. load_bp(None)
  242. }
  243. /// Compute a public key from a private key
  244. pub fn cmz_privkey_to_pubkey<G: PrimeGroup>(privkey: &CMZPrivkey<G>) -> CMZPubkey<G> {
  245. let bp = load_bp::<G>(None);
  246. let X0: Option<G> = if privkey.muCMZ {
  247. Some(bp.mulB(&privkey.x0))
  248. } else {
  249. Some(bp.mulA(&privkey.xr) + bp.mulB(&privkey.x0))
  250. };
  251. let Xr: Option<G> = if privkey.muCMZ {
  252. Some(bp.mulA(&privkey.xr))
  253. } else {
  254. None
  255. };
  256. let X: Vec<G> = privkey.x.iter().map(|x| bp.mulA(x)).collect();
  257. CMZPubkey { X0, Xr, X }
  258. }
  259. /// The CMZCredential trait implemented by all CMZ credential struct types.
  260. pub trait CMZCredential
  261. where
  262. Self: Default + Sized,
  263. {
  264. /// The type of attributes for this credential
  265. type Scalar: PrimeField;
  266. /// The type of the coordinates of the MAC for this credential
  267. type Point: PrimeGroup;
  268. /// Produce a vector of strings containing the names of the
  269. /// attributes of this credential. (The MAC is not included.)
  270. fn attrs() -> Vec<&'static str>;
  271. /// The number of attributes in this credential
  272. fn num_attrs() -> usize;
  273. /// The attribute number for a given name as a string
  274. fn attr_num(name: &str) -> usize;
  275. /// Get a reference to one of the attributes, specified by name as a
  276. /// string.
  277. fn attr(&self, name: &str) -> &Option<Self::Scalar>;
  278. /// Get a mutable reference to one of the attributes, specified by
  279. /// name as a string.
  280. fn attr_mut(&mut self, name: &str) -> &mut Option<Self::Scalar>;
  281. /// Set the public key for this credential.
  282. fn set_pubkey(&mut self, pubkey: &CMZPubkey<Self::Point>) -> &mut Self;
  283. /// Get a copy of the public key for this credential. If the public
  284. /// key has not yet been set or computed, a pubkey with X0 == None
  285. /// will be returned.
  286. fn get_pubkey(&self) -> &CMZPubkey<Self::Point>;
  287. /// Set the private key for this credential. The public key will
  288. /// automatically be computed from the private key.
  289. fn set_privkey(&mut self, privkey: &CMZPrivkey<Self::Point>) -> &mut Self;
  290. /// Get a copy of the private key for this credential. If the
  291. /// private key has not yet been set, a privkey with an empty x
  292. /// vector will be returned.
  293. fn get_privkey(&self) -> &CMZPrivkey<Self::Point>;
  294. /// Get the element of the privkey x vector associated with the
  295. /// given field name
  296. fn privkey_x(&self, name: &str) -> Self::Scalar;
  297. /// Get the element of the pubkey X vector associated with the given
  298. /// field name
  299. fn pubkey_X(&self, name: &str) -> Self::Point;
  300. /// Generate random private and public keys for this credential
  301. /// type. muCMZ should be true if this credential will be issued
  302. /// with muCMZ protocols (and _not_ classic CMZ14 protocols).
  303. fn gen_keys(
  304. rng: &mut impl RngCore,
  305. muCMZ: bool,
  306. ) -> (CMZPrivkey<Self::Point>, CMZPubkey<Self::Point>);
  307. /// Convenience functions for the above
  308. fn cmz14_gen_keys(rng: &mut impl RngCore) -> (CMZPrivkey<Self::Point>, CMZPubkey<Self::Point>) {
  309. Self::gen_keys(rng, false)
  310. }
  311. fn mucmz_gen_keys(rng: &mut impl RngCore) -> (CMZPrivkey<Self::Point>, CMZPubkey<Self::Point>) {
  312. Self::gen_keys(rng, true)
  313. }
  314. /// Convenience function for creating a new Self, and loading the
  315. /// given private key (which will also compute the public key).
  316. fn using_privkey(privkey: &CMZPrivkey<Self::Point>) -> Self {
  317. let mut slf = Self::default();
  318. slf.set_privkey(privkey);
  319. slf
  320. }
  321. /// Convenience function for creating a new Self, and loading the
  322. /// given public key.
  323. fn using_pubkey(pubkey: &CMZPubkey<Self::Point>) -> Self {
  324. let mut slf = Self::default();
  325. slf.set_pubkey(pubkey);
  326. slf
  327. }
  328. /// Create the MAC for this credential, given the private key.
  329. fn create_MAC(
  330. &mut self,
  331. rng: &mut impl RngCore,
  332. privkey: &CMZPrivkey<Self::Point>,
  333. ) -> Result<(), ()>;
  334. /// Compute the coefficient component of the MAC (the Scalar you
  335. /// would multiply P by to get Q), given the private key.
  336. fn compute_MAC_coeff(&self, privkey: &CMZPrivkey<Self::Point>) -> Result<Self::Scalar, ()>;
  337. /// Verify the MAC in this credential, given the private key. This
  338. /// is mainly useful for debugging, since the client will not have
  339. /// the private key and the issuer will typically not have the
  340. /// complete credential.
  341. fn verify_MAC(&self, privkey: &CMZPrivkey<Self::Point>) -> Result<(), ()>;
  342. /// Create a fake MAC for this credential. This is useful, for
  343. /// example, when you're doing an OR proof, and in some arms of the
  344. /// disjunction, the credential does not have to be valid.
  345. fn fake_MAC(&mut self, rng: &mut impl RngCore);
  346. }
  347. /// The CMZ macro for declaring CMZ credentials.
  348. ///
  349. /// Use this macro to declare a CMZ credential struct type.
  350. ///
  351. /// use cmz::*;
  352. /// use group::Group;
  353. /// use rand::{CryptoRng, RngCore};
  354. /// use curve25519_dalek::ristretto::RistrettoPoint as Grp;
  355. /// CMZ!{ Name<Grp>: attr1, attr2, attr3 }
  356. ///
  357. /// will declare a struct type called `Name`, containing one field for each
  358. /// of the listed attributes. The attribute fields will be of type
  359. /// `Option<Scalar>`. It will also automatically add a field called `MAC`
  360. /// of type [`CMZMac`], and an implementation (via the `CMZCred` derive) of
  361. /// the [`CMZCredential`] trait. The mathematical group used (the field for
  362. /// the values of the attributes and the private key elements, and the group
  363. /// elements for the commitments, MAC components, and public key elements)
  364. /// is `Grp`. If `Grp` is omitted, the macro will default to using a
  365. /// group called `G`, which you can define, for example, as:
  366. ///
  367. /// use curve25519_dalek::ristretto::RistrettoPoint as G;
  368. ///
  369. /// or:
  370. ///
  371. /// use curve25519_dalek::ristretto::RistrettoPoint;
  372. /// type G = RistrettoPoint;
  373. ///
  374. /// The group must implement the trait [`PrimeGroup`](https://docs.rs/group/latest/group/prime/trait.PrimeGroup.html).
  375. #[macro_export]
  376. macro_rules! CMZ {
  377. ( $name: ident < $G: ident > : $( $id: ident ),+ ) => {
  378. #[serde_as]
  379. #[derive(CMZCred,Clone,Debug,Default,serde::Serialize,serde::Deserialize)]
  380. #[cmzcred_group(group = $G)]
  381. pub struct $name {
  382. $(
  383. #[serde_as(as="Option<SerdeScalar>")]
  384. pub $id: Option<<$G as Group>::Scalar>,
  385. )+
  386. pub MAC: CMZMac<$G>,
  387. privkey: CMZPrivkey<$G>,
  388. pubkey: CMZPubkey<$G>,
  389. }
  390. };
  391. ( $name: ident : $( $id: ident ),+ ) => {
  392. #[serde_as]
  393. #[derive(CMZCred,Clone,Debug,Default,serde::Serialize,serde::Deserialize)]
  394. #[cmzcred_group(group = G)]
  395. pub struct $name {
  396. $(
  397. #[serde_as(as="Option<SerdeScalar>")]
  398. pub $id: Option<<G as Group>::Scalar>,
  399. )+
  400. pub MAC: CMZMac<G>,
  401. privkey: CMZPrivkey<G>,
  402. pubkey: CMZPubkey<G>,
  403. }
  404. };
  405. }
  406. /// The type for errors generated by the prepare, handle, and finalize
  407. /// functions generated by the CMZProtocol family of macros
  408. #[non_exhaustive]
  409. #[derive(Error, Debug)]
  410. pub enum CMZError {
  411. #[error("Hide attribute {1} of credential {0} was not passed to prepare")]
  412. HideAttrMissing(&'static str, &'static str),
  413. #[error("Reveal attribute {1} of credential {0} was not passed to prepare")]
  414. RevealAttrMissing(&'static str, &'static str),
  415. #[error("Implicit attribute {1} of credential {0} was not passed to prepare")]
  416. ImplicitAttrCliMissing(&'static str, &'static str),
  417. #[error("Implicit attribute {1} of credential {0} was not set by fill_creds")]
  418. ImplicitAttrIssMissing(&'static str, &'static str),
  419. #[error("Set attribute {1} of credential {0} was not set by fill_creds")]
  420. SetAttrMissing(&'static str, &'static str),
  421. #[error("private key for credential {0} was not set by fill_creds")]
  422. PrivkeyMissing(&'static str),
  423. #[error("public key for credential {0} was not passed to prepare")]
  424. PubkeyMissing(&'static str),
  425. #[error("credential initialized with wrong protocol")]
  426. WrongProtocol(&'static str),
  427. #[error("client proof did not verify")]
  428. CliProofFailed,
  429. #[error("issuer proof did not verify")]
  430. IssProofFailed,
  431. #[error("unknown CMZ proof error")]
  432. Unknown,
  433. }
  434. #[cfg(test)]
  435. mod tests {
  436. use super::*;
  437. #[test]
  438. fn lox_credential_test() {
  439. use curve25519_dalek::ristretto::RistrettoPoint as G;
  440. CMZ! { Lox:
  441. id,
  442. bucket,
  443. trust_level,
  444. level_since,
  445. invites_remaining,
  446. blockages
  447. }
  448. println!("{:#?}", Lox::attrs());
  449. let mut L = Lox::default();
  450. println!("{:#?}", L);
  451. L.bucket = Some(<G as Group>::Scalar::ONE);
  452. println!("{:#?}", L);
  453. println!("{:#?}", L.attr("bucket"));
  454. *L.attr_mut("id") = Some(<G as Group>::Scalar::ONE);
  455. println!("{:#?}", L);
  456. }
  457. }