Skip to content

Commit 4204689

Browse files
authored
sm2: add UncompressedPoint type alias (#1604)
Adds a type alias for a byte `Array` the size of an uncompressed curve point, and uses that to simplify the SM2PKE implementation
1 parent e60e991 commit 4204689

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

sm2/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ pub use elliptic_curve::pkcs8;
7575

7676
use elliptic_curve::{
7777
FieldBytesEncoding,
78-
array::{Array, typenum::U33},
78+
array::Array,
7979
bigint::{ArrayEncoding, Odd},
80-
consts::U32,
80+
consts::{U32, U33, U65},
8181
};
8282

8383
#[cfg(feature = "dsa")]
@@ -121,6 +121,9 @@ impl pkcs8::AssociatedOid for Sm2 {
121121
/// Compressed SEC1-encoded curve point.
122122
pub type CompressedPoint = Array<u8, U33>;
123123

124+
/// Uncompressed SEC1-encoded curve point.
125+
pub type UncompressedPoint = Array<u8, U65>;
126+
124127
/// SEC1 encoded point.
125128
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<Sm2>;
126129

sm2/src/pke/decrypting.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::fmt::{self, Debug};
22

33
use crate::{
44
AffinePoint, EncodedPoint, FieldBytes, NonZeroScalar, PublicKey, Scalar, SecretKey,
5-
arithmetic::field::FieldElement,
5+
UncompressedPoint, arithmetic::field::FieldElement,
66
};
77

88
use alloc::{borrow::ToOwned, vec::Vec};
@@ -159,11 +159,11 @@ fn decrypt(
159159
hasher: &mut dyn DynDigest,
160160
ciphertext: &[u8],
161161
) -> Result<Vec<u8>> {
162-
let q = U256::from_be_hex(FieldElement::MODULUS);
163-
let c1_len = q.bits().div_ceil(8) * 2 + 1;
164-
165162
// B1: get 𝐶1 from 𝐶
166-
let (c1, c) = ciphertext.split_at_checked(c1_len as usize).ok_or(Error)?;
163+
let (c1, c) = ciphertext
164+
.split_at_checked(size_of::<UncompressedPoint>())
165+
.ok_or(Error)?;
166+
167167
let encoded_c1 = EncodedPoint::from_bytes(c1).map_err(Error::from)?;
168168

169169
// verify that point c1 satisfies the elliptic curve

sm2/src/pke/encrypting.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::fmt::Debug;
22

33
use crate::{
4-
AffinePoint, FieldBytesSize, NonZeroScalar, ProjectivePoint, PublicKey, Scalar,
4+
AffinePoint, NonZeroScalar, ProjectivePoint, PublicKey, Scalar, UncompressedPoint,
55
arithmetic::field::FieldElement,
66
pke::{kdf, vec},
77
};
@@ -10,7 +10,6 @@ use crate::{
1010
use alloc::{borrow::ToOwned, boxed::Box, vec::Vec};
1111
use elliptic_curve::{
1212
Error, Generate, Group, Result,
13-
array::typenum::Unsigned,
1413
bigint::{U256, Uint},
1514
ops::Reduce,
1615
pkcs8::der::Encode,
@@ -153,7 +152,7 @@ fn encrypt<R: TryCryptoRng + ?Sized>(
153152
digest: &mut dyn DynDigest,
154153
msg: &[u8],
155154
) -> Result<Vec<u8>> {
156-
let mut c1 = [0; FieldBytesSize::USIZE * 2 + 1];
155+
let mut c1 = [0; size_of::<UncompressedPoint>()];
157156
let mut c2 = msg.to_owned();
158157
let mut hpb: AffinePoint;
159158
loop {

0 commit comments

Comments
 (0)