bign256/
lib.rs

1// #![no_std]
2#![no_std]
3#![cfg_attr(docsrs, feature(doc_auto_cfg))]
4#![doc = include_str!("../README.md")]
5#![doc(
6    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
7    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
8)]
9#![forbid(unsafe_code)]
10#![warn(
11    clippy::mod_module_files,
12    clippy::cast_lossless,
13    clippy::cast_possible_truncation,
14    clippy::cast_possible_wrap,
15    clippy::cast_precision_loss,
16    clippy::cast_sign_loss,
17    clippy::checked_conversions,
18    clippy::implicit_saturating_sub,
19    clippy::arithmetic_side_effects,
20    clippy::panic,
21    clippy::panic_in_result_fn,
22    clippy::unwrap_used,
23    missing_docs,
24    rust_2018_idioms,
25    unused_lifetimes,
26    unused_qualifications
27)]
28
29#[cfg(feature = "alloc")]
30#[allow(unused_extern_crates)]
31extern crate alloc;
32
33#[cfg(feature = "arithmetic")]
34pub mod arithmetic;
35
36#[cfg(any(feature = "test-vectors", test))]
37pub mod test_vectors;
38
39#[cfg(feature = "dsa")]
40pub mod dsa;
41
42pub use elliptic_curve::{self, bigint::U256};
43
44#[cfg(feature = "arithmetic")]
45pub use arithmetic::{scalar::Scalar, AffinePoint, ProjectivePoint};
46
47#[cfg(feature = "pkcs8")]
48pub use elliptic_curve::pkcs8;
49
50use elliptic_curve::{
51    bigint::ArrayEncoding,
52    consts::{U32, U33},
53    generic_array::GenericArray,
54    FieldBytesEncoding,
55};
56
57#[cfg(feature = "dsa")]
58type Hash = belt_hash::digest::Output<belt_hash::BeltHash>;
59
60/// Order of BIGN P-256's elliptic curve group (i.e. scalar modulus) in hexadecimal.
61const ORDER_HEX: &str = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD95C8ED60DFB4DFC7E5ABF99263D6607";
62
63/// BIGN P-256 elliptic curve.
64///
65/// This curve is also known as bign-curve256v1
66/// and is specified in [STB 34.101.45-2013]:
67/// Recommendations for Discrete Logarithm-based Cryptography:
68/// Elliptic Curve Domain Parameters.
69///
70///
71/// Its equation is `y² = x³ + ax + b` over a ~256-bit prime field.
72///
73/// ```text
74/// a = 115792089237316195423570985008687907853269984665640564039457584007913129639744
75/// b = 54189945433829174764701416670523239872420438478408031144987871676190519198705
76/// ```
77///
78/// [STB 34.101.45-2013]: https://apmi.bsu.by/assets/files/std/bign-spec294.pdf
79#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
80pub struct BignP256;
81
82impl elliptic_curve::Curve for BignP256 {
83    /// 256-bit integer type used for internally representing field elements.
84    type FieldBytesSize = U32;
85    type Uint = U256;
86
87    /// Order of BIGN P-256's elliptic curve group (i.e. scalar modulus).
88    const ORDER: U256 = U256::from_be_hex(ORDER_HEX);
89}
90
91impl elliptic_curve::PrimeCurve for BignP256 {}
92
93impl elliptic_curve::point::PointCompression for BignP256 {
94    /// BIGN P-256 points are typically uncompressed.
95    const COMPRESS_POINTS: bool = false;
96}
97
98impl elliptic_curve::point::PointCompaction for BignP256 {
99    /// BIGN P-256 points are typically uncompressed.
100    const COMPACT_POINTS: bool = false;
101}
102
103#[cfg(feature = "pkcs8")]
104impl pkcs8::AssociatedOid for BignP256 {
105    const OID: pkcs8::ObjectIdentifier =
106        pkcs8::ObjectIdentifier::new_unwrap("1.2.112.0.2.0.34.101.45.1");
107}
108
109/// Compressed SEC1-encoded BIGN P256 curve point.
110pub type CompressedPoint = GenericArray<u8, U33>;
111
112/// BIGN P-256 field element serialized as bytes.
113///
114/// Byte array containing a serialized field element value (base field or scalar).
115pub type FieldBytes = elliptic_curve::FieldBytes<BignP256>;
116
117/// SEC1 encoded point.
118pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BignP256>;
119
120impl FieldBytesEncoding<BignP256> for U256 {
121    fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
122        U256::from_be_byte_array(*field_bytes)
123    }
124
125    fn encode_field_bytes(&self) -> FieldBytes {
126        self.to_be_byte_array()
127    }
128}
129
130/// Non-zero scalar field element.
131#[cfg(feature = "arithmetic")]
132pub type NonZeroScalar = elliptic_curve::NonZeroScalar<BignP256>;
133
134/// BIGN P-256 public key.
135#[cfg(feature = "arithmetic")]
136pub type PublicKey = elliptic_curve::PublicKey<BignP256>;
137
138/// BIGN P-256 secret key.
139pub type SecretKey = elliptic_curve::SecretKey<BignP256>;
140
141#[cfg(not(feature = "arithmetic"))]
142impl elliptic_curve::sec1::ValidatePublicKey for BignP256 {}
143
144/// Bit representation of a BIGN P-256 scalar field element.
145#[cfg(feature = "bits")]
146pub type ScalarBits = elliptic_curve::ScalarBits<BignP256>;