k256/
arithmetic.rs

1//! A pure-Rust implementation of group operations on secp256k1.
2
3pub(crate) mod affine;
4mod field;
5#[cfg(feature = "hash2curve")]
6mod hash2curve;
7mod mul;
8pub(crate) mod projective;
9pub(crate) mod scalar;
10
11#[cfg(test)]
12mod dev;
13
14pub use field::FieldElement;
15
16use self::{affine::AffinePoint, projective::ProjectivePoint, scalar::Scalar};
17use crate::Secp256k1;
18use elliptic_curve::CurveArithmetic;
19
20impl CurveArithmetic for Secp256k1 {
21    type AffinePoint = AffinePoint;
22    type ProjectivePoint = ProjectivePoint;
23    type Scalar = Scalar;
24}
25
26const CURVE_EQUATION_B_SINGLE: u32 = 7u32;
27
28#[rustfmt::skip]
29pub(crate) const CURVE_EQUATION_B: FieldElement = FieldElement::from_bytes_unchecked(&[
30    0, 0, 0, 0, 0, 0, 0, 0,
31    0, 0, 0, 0, 0, 0, 0, 0,
32    0, 0, 0, 0, 0, 0, 0, 0,
33    0, 0, 0, 0, 0, 0, 0, CURVE_EQUATION_B_SINGLE as u8,
34]);
35
36#[cfg(test)]
37mod tests {
38    use super::CURVE_EQUATION_B;
39    use hex_literal::hex;
40
41    const CURVE_EQUATION_B_BYTES: [u8; 32] =
42        hex!("0000000000000000000000000000000000000000000000000000000000000007");
43
44    #[test]
45    fn verify_constants() {
46        assert_eq!(CURVE_EQUATION_B.to_bytes(), CURVE_EQUATION_B_BYTES.into());
47    }
48
49    #[test]
50    fn generate_secret_key() {
51        use crate::SecretKey;
52        use elliptic_curve::rand_core::OsRng;
53        let key = SecretKey::random(&mut OsRng);
54
55        // Sanity check
56        assert!(!key.to_bytes().iter().all(|b| *b == 0))
57    }
58}