1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! Serde support
//!
//! ## Default serialization format
//!
//! By default, serialization format is excessive. Points are serialized without compression.
//! Points and scalars have an extra field "curve" that specifies which curve this point/scalar
//! belongs to.
//!
//! ```rust
//! # fn main() -> Result<(), serde_json::Error> {
//! use generic_ec::{Curve, Point, Scalar, curves::Secp256k1};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize)]
//! #[serde(bound = "")]
//! pub struct ZkProof<E: Curve> {
//!     some_point: Point<E>,
//!     some_scalar: Scalar<E>,
//! }
//!
//! let proof = ZkProof::<Secp256k1> {
//!     some_point: Point::generator().to_point(),
//!     some_scalar: Scalar::one(),
//! };
//! assert_eq!(serde_json::to_string_pretty(&proof)?, r#"{
//!   "some_point": {
//!     "curve": "secp256k1",
//!     "point": "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"
//!   },
//!   "some_scalar": {
//!     "curve": "secp256k1",
//!     "scalar": "0000000000000000000000000000000000000000000000000000000000000001"
//!   }
//! }"#);
//! # Ok(()) }
//! ```
//!
//! Excessive serialization format enables better security as it makes it harder to misuse the library.
//! E.g. if by some mistake you parse a point that was initially generated on another curve, you'll get
//! instant error. Without this field, behavior is uncertain and difficult to debug: point from one curve
//! can happen to be a valid point on another curve.
//!
//! ## Compact serialization format
//!
//! You may opt for compact serialization format. If you do that, points are seialized in compressed form, and
//! extra "curve" field is dropped.
//!
//! Compact serialization format can be enabled using [serde_with] crate and [`Compact`] helper struct:
//!
//! ```rust
//! # fn main() -> Result<(), serde_json::Error> {
//! use generic_ec::{Curve, Point, Scalar, curves::Secp256k1};
//! use serde::{Serialize, Deserialize};
//! use serde_with::serde_as;
//!
//! #[serde_as]
//! #[derive(Serialize, Deserialize)]
//! #[serde(bound = "")]
//! pub struct ZkProof<E: Curve> {
//!     #[serde_as(as = "generic_ec::serde::Compact")]
//!     some_point: Point<E>,
//!     #[serde_as(as = "generic_ec::serde::Compact")]
//!     some_scalar: Scalar<E>,
//! }
//!
//! let proof = ZkProof::<Secp256k1> {
//!     some_point: Point::generator().to_point(),
//!     some_scalar: Scalar::one(),
//! };
//! assert_eq!(serde_json::to_string_pretty(&proof)?, r#"{
//!   "some_point": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
//!   "some_scalar": "0000000000000000000000000000000000000000000000000000000000000001"
//! }"#);
//! # Ok(()) }
//! ```

#![cfg(feature = "serde")]
use core::{convert::TryInto, fmt};

use phantom_type::PhantomType;

use crate::core::Curve;

#[cfg(feature = "serde")]
impl<E: Curve> serde::Serialize for crate::Point<E> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        models::PointUncompressed::from(self).serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, E: Curve> serde::Deserialize<'de> for crate::Point<E> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        models::PointUncompressed::deserialize(deserializer)?
            .try_into()
            .map_err(<D::Error as serde::de::Error>::custom)
    }
}

#[cfg(feature = "serde")]
impl<E: Curve> serde::Serialize for crate::Scalar<E> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        models::ScalarUncompressed::from(self).serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, E: Curve> serde::Deserialize<'de> for crate::Scalar<E> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        models::ScalarUncompressed::deserialize(deserializer)?
            .try_into()
            .map_err(<D::Error as serde::de::Error>::custom)
    }
}

#[cfg(feature = "serde")]
impl<E: Curve> serde::Serialize for crate::SecretScalar<E> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.as_ref().serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, E: Curve> serde::Deserialize<'de> for crate::SecretScalar<E> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Ok(crate::SecretScalar::new(&mut crate::Scalar::deserialize(
            deserializer,
        )?))
    }
}

#[cfg(feature = "serde")]
/// Compact serialization format
pub struct Compact;

#[cfg(feature = "serde")]
impl<E: Curve> serde_with::SerializeAs<crate::Point<E>> for Compact {
    fn serialize_as<S>(source: &crate::Point<E>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::Serialize;
        models::PointCompact::from(source).serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, E: Curve> serde_with::DeserializeAs<'de, crate::Point<E>> for Compact {
    fn deserialize_as<D>(deserializer: D) -> Result<crate::Point<E>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::Deserialize;
        models::PointCompact::deserialize(deserializer)?
            .try_into()
            .map_err(<D::Error as serde::de::Error>::custom)
    }
}

#[cfg(feature = "serde")]
impl<E: Curve> serde_with::SerializeAs<crate::Scalar<E>> for Compact {
    fn serialize_as<S>(source: &crate::Scalar<E>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::Serialize;
        models::ScalarCompact::from(source).serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, E: Curve> serde_with::DeserializeAs<'de, crate::Scalar<E>> for Compact {
    fn deserialize_as<D>(deserializer: D) -> Result<crate::Scalar<E>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::Deserialize;
        models::ScalarCompact::deserialize(deserializer)?
            .try_into()
            .map_err(<D::Error as serde::de::Error>::custom)
    }
}

#[cfg(feature = "serde")]
impl<E: Curve> serde_with::SerializeAs<crate::SecretScalar<E>> for Compact {
    fn serialize_as<S>(source: &crate::SecretScalar<E>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::Serialize;
        models::ScalarCompact::from(source.as_ref()).serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, E: Curve> serde_with::DeserializeAs<'de, crate::SecretScalar<E>> for Compact {
    fn deserialize_as<D>(deserializer: D) -> Result<crate::SecretScalar<E>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let mut scalar =
            <Compact as serde_with::DeserializeAs<'de, crate::Scalar<E>>>::deserialize_as(
                deserializer,
            )?;
        Ok(crate::SecretScalar::new(&mut scalar))
    }
}

/// A guard type asserting that deserialized value belongs to curve `E`
///
/// It implements [serde::Serialize] and [serde::Deserialize] traits if `serde` feature is
/// enabled. When serialized, `CurveName` is converted into string containing curve name.
/// When deserialized, it parses a string and requires it to match curve name, otherwise
/// deserialization error is returned.
///
/// This structure is present even if `serde` feature is disabled.
///
/// ## Example
///
/// ```rust
/// # fn main() -> Result<(), serde_json::Error> {
/// use generic_ec::{serde::CurveName, curves::{Secp256k1, Secp256r1}};
/// use serde_json::Value;
///
/// let curve_name = CurveName::<Secp256k1>::new();
/// let value = serde_json::to_value(&curve_name)?;
/// assert_eq!(value, Value::String("secp256k1".into()));
///
/// // `value` can be deserialized back to `CurveName<Secp256k1>`
/// let _curve_name: CurveName<Secp256k1> = serde_json::from_value(value.clone())?;
///
/// // but it can't be deserialized to `CurveName<Secp256r1>`
/// let serialization_fails = serde_json::from_value::<CurveName<Secp256r1>>(value);
/// assert!(serialization_fails.is_err());
/// # Ok(()) }
/// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct CurveName<E: Curve>(PhantomType<E>);

impl<E: Curve> CurveName<E> {
    /// Construct a `CurveName` guard
    pub fn new() -> Self {
        Self(PhantomType::new())
    }
}

impl<E: Curve> Default for CurveName<E> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "serde")]
impl<E: Curve> serde::Serialize for CurveName<E> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(E::CURVE_NAME)
    }
}

#[cfg(feature = "serde")]
impl<'de, E: Curve> serde::Deserialize<'de> for CurveName<E> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        pub struct CurveNameVisitor<E: Curve>(PhantomType<E>);
        impl<'de, E: Curve> serde::de::Visitor<'de> for CurveNameVisitor<E> {
            type Value = CurveName<E>;
            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "curve {name}", name = E::CURVE_NAME)
            }
            fn visit_str<Error>(self, v: &str) -> Result<Self::Value, Error>
            where
                Error: serde::de::Error,
            {
                if v == E::CURVE_NAME {
                    Ok(CurveName::default())
                } else {
                    Err(Error::custom(error_msg::ExpectedCurve {
                        expected: E::CURVE_NAME,
                        got: v,
                    }))
                }
            }
        }
        deserializer.deserialize_str(CurveNameVisitor(PhantomType::new()))
    }
}

#[cfg(feature = "serde")]
mod models {
    use core::convert::TryFrom;

    use serde::{Deserialize, Serialize};
    use serde_with::serde_as;

    use crate::core::{CompressedEncoding, IntegerEncoding, UncompressedEncoding};
    use crate::{as_raw::AsRaw, Curve, Point, Scalar};

    use super::{
        error_msg::{InvalidPoint, InvalidScalar},
        CurveName,
    };

    #[serde_as]
    #[derive(Serialize, Deserialize)]
    #[serde(bound = "")]
    pub struct PointUncompressed<E: Curve> {
        curve: CurveName<E>,
        #[serde_as(as = "super::utils::Bytes")]
        point: E::UncompressedPointArray,
    }
    impl<E: Curve> From<&Point<E>> for PointUncompressed<E> {
        fn from(p: &Point<E>) -> Self {
            let bytes = p.as_raw().to_bytes_uncompressed();
            Self {
                curve: CurveName::new(),
                point: bytes,
            }
        }
    }
    impl<E: Curve> TryFrom<PointUncompressed<E>> for Point<E> {
        type Error = InvalidPoint;
        fn try_from(value: PointUncompressed<E>) -> Result<Self, Self::Error> {
            Point::from_bytes(value.point).or(Err(InvalidPoint))
        }
    }

    #[serde_as]
    #[derive(Serialize, Deserialize)]
    #[serde(bound = "")]
    pub struct PointCompact<E: Curve>(
        #[serde_as(as = "super::utils::Bytes")] E::CompressedPointArray,
    );
    impl<E: Curve> From<&Point<E>> for PointCompact<E> {
        fn from(p: &Point<E>) -> Self {
            let bytes = p.as_raw().to_bytes_compressed();
            Self(bytes)
        }
    }
    impl<E: Curve> TryFrom<PointCompact<E>> for Point<E> {
        type Error = InvalidPoint;
        fn try_from(value: PointCompact<E>) -> Result<Self, Self::Error> {
            Point::from_bytes(value.0).or(Err(InvalidPoint))
        }
    }

    #[serde_as]
    #[derive(Serialize, Deserialize)]
    #[serde(bound = "")]
    pub struct ScalarUncompressed<E: Curve> {
        curve: CurveName<E>,
        #[serde_as(as = "super::utils::Bytes")]
        scalar: E::ScalarArray,
    }
    impl<E: Curve> From<&Scalar<E>> for ScalarUncompressed<E> {
        fn from(s: &Scalar<E>) -> Self {
            let bytes = s.as_raw().to_be_bytes();
            Self {
                curve: CurveName::new(),
                scalar: bytes,
            }
        }
    }
    impl<E: Curve> TryFrom<ScalarUncompressed<E>> for Scalar<E> {
        type Error = InvalidScalar;
        fn try_from(value: ScalarUncompressed<E>) -> Result<Self, Self::Error> {
            Scalar::from_be_bytes(value.scalar).or(Err(InvalidScalar))
        }
    }

    #[serde_as]
    #[derive(Serialize, Deserialize)]
    #[serde(bound = "")]
    pub struct ScalarCompact<E: Curve>(#[serde_as(as = "super::utils::Bytes")] E::ScalarArray);
    impl<E: Curve> From<&Scalar<E>> for ScalarCompact<E> {
        fn from(s: &Scalar<E>) -> Self {
            let bytes = s.as_raw().to_be_bytes();
            Self(bytes)
        }
    }
    impl<E: Curve> TryFrom<ScalarCompact<E>> for Scalar<E> {
        type Error = InvalidScalar;
        fn try_from(value: ScalarCompact<E>) -> Result<Self, Self::Error> {
            Scalar::from_be_bytes(&value.0).or(Err(InvalidScalar))
        }
    }
}

#[cfg(feature = "serde")]
mod utils {
    use core::fmt;

    use serde::de::{self, Visitor};
    use serde_with::{DeserializeAs, SerializeAs};

    use crate::core::ByteArray;

    pub struct Bytes;

    impl<T> SerializeAs<T> for Bytes
    where
        T: AsRef<[u8]>,
    {
        fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
        {
            if serializer.is_human_readable() {
                // We only support serialization of byte arrays up to 128 bytes. It can be generalized when
                // Rust has better support of const generics
                let mut buf = [0u8; 256];

                if source.as_ref().len() * 2 > buf.len() {
                    return Err(<S::Error as serde::ser::Error>::custom(
                        super::error_msg::ByteArrayTooLarge {
                            len: source.as_ref().len(),
                            supported_len: buf.len() / 2,
                        },
                    ));
                }
                let buf = &mut buf[..2 * source.as_ref().len()];
                hex::encode_to_slice(source, buf)
                    .map_err(<S::Error as serde::ser::Error>::custom)?;
                let buf_str = core::str::from_utf8(buf).map_err(|e| {
                    <S::Error as serde::ser::Error>::custom(super::error_msg::MalformedHex(e))
                })?;
                serializer.serialize_str(buf_str)
            } else {
                serializer.serialize_bytes(source.as_ref())
            }
        }
    }

    impl<'de, T> DeserializeAs<'de, T> for Bytes
    where
        T: ByteArray,
    {
        fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            pub struct BytesVisitor<T>(T);
            impl<'de, T: AsMut<[u8]>> Visitor<'de> for BytesVisitor<T> {
                type Value = T;
                fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                    write!(f, "bytes")
                }
                fn visit_str<E>(mut self, v: &str) -> Result<Self::Value, E>
                where
                    E: serde::de::Error,
                {
                    hex::decode_to_slice(v, self.0.as_mut()).map_err(E::custom)?;
                    Ok(self.0)
                }
                fn visit_bytes<E>(mut self, v: &[u8]) -> Result<Self::Value, E>
                where
                    E: serde::de::Error,
                {
                    let expected_len = self.0.as_mut().len();
                    if v.len() != expected_len {
                        return Err(E::invalid_length(
                            v.len(),
                            &super::error_msg::ExpectedLen(expected_len),
                        ));
                    }
                    self.0.as_mut().copy_from_slice(v);
                    Ok(self.0)
                }
                fn visit_seq<A>(mut self, mut seq: A) -> Result<Self::Value, A::Error>
                where
                    A: serde::de::SeqAccess<'de>,
                {
                    let expected_len = self.0.as_mut().len();
                    let bytes = self.0.as_mut().iter_mut().enumerate();

                    for (i, byte_i) in bytes {
                        let byte_parsed = seq.next_element()?.ok_or_else(|| {
                            <A::Error as de::Error>::invalid_length(
                                i,
                                &super::error_msg::ExpectedLen(expected_len),
                            )
                        })?;
                        *byte_i = byte_parsed;
                    }

                    let mut unparsed_bytes = 0;
                    while seq.next_element::<serde::de::IgnoredAny>()?.is_some() {
                        unparsed_bytes += 1
                    }

                    if unparsed_bytes > 0 {
                        Err(<A::Error as de::Error>::invalid_length(
                            expected_len + unparsed_bytes,
                            &super::error_msg::ExpectedLen(expected_len),
                        ))
                    } else {
                        Ok(self.0)
                    }
                }
            }
            let visitor = BytesVisitor(T::zeroes());
            if deserializer.is_human_readable() {
                deserializer.deserialize_str(visitor)
            } else {
                deserializer.deserialize_bytes(visitor)
            }
        }
    }
}

#[cfg(feature = "serde")]
mod error_msg {
    use core::fmt;

    use serde::de::Expected;

    pub struct ExpectedCurve<'g> {
        pub expected: &'static str,
        pub got: &'g str,
    }

    impl<'g> fmt::Display for ExpectedCurve<'g> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(
                f,
                "expected {e} curve, got {g}",
                e = self.expected,
                g = self.got
            )
        }
    }

    pub struct ExpectedLen(pub usize);

    impl Expected for ExpectedLen {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{} bytes", self.0)
        }
    }

    pub struct InvalidPoint;
    impl fmt::Display for InvalidPoint {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "invalid point")
        }
    }

    pub struct InvalidScalar;
    impl fmt::Display for InvalidScalar {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "invalid scalar")
        }
    }

    pub struct MalformedHex(pub core::str::Utf8Error);
    impl fmt::Display for MalformedHex {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "malformed hex: {}", self.0)
        }
    }

    pub struct ByteArrayTooLarge {
        pub len: usize,
        pub supported_len: usize,
    }
    impl fmt::Display for ByteArrayTooLarge {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "byte array is too large: its length is {} bytes, but only up to {} bytes can be serialized", self.len, self.supported_len)
        }
    }
}