Skip to content

Commit 122e791

Browse files
authored
Refactor EC public key construction to share more code (#14285)
1 parent 3b1a380 commit 122e791

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

ci-constraints-requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ coverage==7.6.1 ; python_full_version < '3.9'
4949
# via pytest-cov
5050
coverage==7.10.7 ; python_full_version == '3.9.*'
5151
# via pytest-cov
52-
coverage==7.13.1 ; python_full_version >= '3.10'
52+
coverage==7.13.4 ; python_full_version >= '3.10'
5353
# via pytest-cov
5454
dependency-groups==1.3.1
5555
# via nox
@@ -316,7 +316,7 @@ urllib3==2.2.3 ; python_full_version < '3.9'
316316
# via requests
317317
urllib3==2.6.3 ; python_full_version >= '3.9'
318318
# via requests
319-
uv==0.10.0
319+
uv==0.10.1
320320
# via nox
321321
virtualenv==20.36.1
322322
# via nox

src/rust/src/backend/ec.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,10 @@ pub(crate) fn public_key_from_pkey(
135135
) -> CryptographyResult<ECPublicKey> {
136136
let ec = pkey.ec_key()?;
137137
let curve = py_curve_from_curve(py, ec.group())?;
138-
check_key_infinity(&ec)?;
139-
Ok(ECPublicKey {
140-
pkey: pkey.to_owned(),
141-
curve: curve.into(),
142-
})
138+
139+
ECPublicKey::new(pkey.to_owned(), curve.into())
143140
}
141+
144142
#[pyo3::pyfunction]
145143
#[pyo3(signature = (curve, backend=None))]
146144
fn generate_private_key(
@@ -198,10 +196,7 @@ fn from_public_bytes(
198196
let ec = openssl::ec::EcKey::from_public_key(&curve, &point)?;
199197
let pkey = openssl::pkey::PKey::from_ec_key(ec)?;
200198

201-
Ok(ECPublicKey {
202-
pkey,
203-
curve: py_curve.into(),
204-
})
199+
ECPublicKey::new(pkey, py_curve.into())
205200
}
206201

207202
#[pyo3::pymethods]
@@ -382,6 +377,18 @@ impl ECPrivateKey {
382377
}
383378
}
384379

380+
impl ECPublicKey {
381+
fn new(
382+
pkey: openssl::pkey::PKey<openssl::pkey::Public>,
383+
curve: pyo3::Py<pyo3::PyAny>,
384+
) -> CryptographyResult<ECPublicKey> {
385+
let ec = pkey.ec_key()?;
386+
check_key_infinity(&ec)?;
387+
388+
Ok(ECPublicKey { pkey, curve })
389+
}
390+
}
391+
385392
#[pyo3::pymethods]
386393
impl ECPublicKey {
387394
#[getter]
@@ -628,10 +635,7 @@ impl EllipticCurvePublicNumbers {
628635

629636
let pkey = openssl::pkey::PKey::from_ec_key(public_key)?;
630637

631-
Ok(ECPublicKey {
632-
pkey,
633-
curve: self.curve.clone_ref(py),
634-
})
638+
ECPublicKey::new(pkey, self.curve.clone_ref(py))
635639
}
636640

637641
fn __eq__(

0 commit comments

Comments
 (0)