p256/
arithmetic.rs

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
//! Pure Rust implementation of group operations on secp256r1.

pub(crate) mod affine;
pub(crate) mod field;
#[cfg(feature = "hash2curve")]
mod hash2curve;
pub(crate) mod projective;
pub(crate) mod scalar;
pub(crate) mod util;

use affine::AffinePoint;
use field::{FieldElement, MODULUS};
use projective::ProjectivePoint;
use scalar::Scalar;

/// a = -3
const CURVE_EQUATION_A: FieldElement = FieldElement::ZERO
    .subtract(&FieldElement::ONE)
    .subtract(&FieldElement::ONE)
    .subtract(&FieldElement::ONE);

/// b = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B
const CURVE_EQUATION_B: FieldElement = FieldElement([
    0xd89c_df62_29c4_bddf,
    0xacf0_05cd_7884_3090,
    0xe5a2_20ab_f721_2ed6,
    0xdc30_061d_0487_4834,
]);

#[cfg(test)]
mod tests {
    use super::{CURVE_EQUATION_A, CURVE_EQUATION_B};
    use hex_literal::hex;

    const CURVE_EQUATION_A_BYTES: &[u8] =
        &hex!("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");

    const CURVE_EQUATION_B_BYTES: &[u8] =
        &hex!("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");

    #[test]
    fn verify_constants() {
        assert_eq!(
            CURVE_EQUATION_A.to_bytes().as_slice(),
            CURVE_EQUATION_A_BYTES
        );
        assert_eq!(
            CURVE_EQUATION_B.to_bytes().as_slice(),
            CURVE_EQUATION_B_BYTES
        );
    }

    #[test]
    fn generate_secret_key() {
        use crate::SecretKey;
        use elliptic_curve::rand_core::OsRng;

        let key = SecretKey::random(&mut OsRng);

        // Sanity check
        assert!(!key.to_be_bytes().iter().all(|b| *b == 0))
    }
}