Skip to content

Commit

Permalink
Merge pull request #134 from fjarri/simplify-secrets
Browse files Browse the repository at this point in the history
Make `SecretKey::try_from_be_bytes()` take just a slice reference
  • Loading branch information
fjarri committed Jan 29, 2024
2 parents b3eaa7a + 9a2ee66 commit 6f692dd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/umbral-pre.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
strategy:
matrix:
rust:
- 1.65.0 # MSRV
- 1.70.0 # MSRV
- stable
target:
- wasm32-unknown-unknown
Expand All @@ -45,7 +45,7 @@ jobs:
strategy:
matrix:
rust:
- 1.65.0 # MSRV
- 1.70.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -63,7 +63,7 @@ jobs:
strategy:
matrix:
rust:
- 1.65.0 # MSRV
- 1.70.0 # MSRV
- stable
steps:
- uses: actions/checkout@v2
Expand All @@ -79,7 +79,7 @@ jobs:
matrix:
include:
- target: x86_64-unknown-linux-gnu
rust: 1.65.0 # MSRV
rust: 1.70.0 # MSRV
- target: x86_64-unknown-linux-gnu
rust: stable

Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.12.0] - In development

### Changed

- `SecretKey::try_from_be_bytes()` takes just a slice reference instead of a `SecretBox`. ([#134])
- Bumped MSRV to 1.70. ([#134])


[#134]: https://github.com/nucypher/rust-umbral/pull/134


## [0.11.0] - 2023-08-01

### Added
Expand Down
12 changes: 2 additions & 10 deletions umbral-pre/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
// TODO (#30): ideally, we would write documentation for the bindings as docstrings here,
// and let Sphinx pick it up... but it's not great at doing so.
#![allow(missing_docs)]
// Clippy shows false positives in PyO3 methods.
// See https://github.com/rust-lang/rust-clippy/issues/8971
// Will probably be fixed by Rust 1.65
#![allow(clippy::borrow_deref_ref)]

use alloc::format;
use alloc::string::String;
Expand All @@ -24,7 +20,7 @@ use pyo3::wrap_pyfunction;
use sha2::{digest::Update, Digest, Sha256};

use crate as umbral_pre;
use crate::{curve::ScalarSize, DefaultDeserialize, DefaultSerialize, SecretBox};
use crate::{DefaultDeserialize, DefaultSerialize};

fn map_py_value_err<T: fmt::Display>(err: T) -> PyErr {
PyValueError::new_err(format!("{err}"))
Expand Down Expand Up @@ -101,11 +97,7 @@ impl SecretKey {

#[staticmethod]
pub fn from_be_bytes(data: &[u8]) -> PyResult<Self> {
let arr = SecretBox::new(
GenericArray::<u8, ScalarSize>::from_exact_iter(data.iter().cloned())
.ok_or_else(|| map_py_value_err("Invalid length of a curve scalar"))?,
);
umbral_pre::SecretKey::try_from_be_bytes(&arr)
umbral_pre::SecretKey::try_from_be_bytes(data)
.map_err(map_py_value_err)
.map(Self::from)
}
Expand Down
9 changes: 2 additions & 7 deletions umbral-pre/src/bindings_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

use generic_array::GenericArray;
use js_sys::{Error, Uint8Array};
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};
use wasm_bindgen::JsCast;
use wasm_bindgen_derive::TryFromJsValue;

use crate as umbral_pre;
use crate::{curve::ScalarSize, DefaultDeserialize, DefaultSerialize, SecretBox};
use crate::{DefaultDeserialize, DefaultSerialize};

#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -104,11 +103,7 @@ impl SecretKey {

#[wasm_bindgen(js_name = fromBEBytes)]
pub fn from_be_bytes(data: &[u8]) -> Result<SecretKey, Error> {
let arr = SecretBox::new(
GenericArray::<u8, ScalarSize>::from_exact_iter(data.iter().cloned())
.ok_or_else(|| map_js_err("Invalid length of a curve scalar"))?,
);
umbral_pre::SecretKey::try_from_be_bytes(&arr)
umbral_pre::SecretKey::try_from_be_bytes(data)
.map(Self)
.map_err(map_js_err)
}
Expand Down
10 changes: 6 additions & 4 deletions umbral-pre/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ impl SecretKey {
}

/// Deserializes the secret key from a scalar in the big-endian representation.
pub fn try_from_be_bytes(
bytes: &SecretBox<GenericArray<u8, ScalarSize>>,
) -> Result<Self, String> {
BackendSecretKey::<CurveType>::from_bytes(bytes.as_secret())
pub fn try_from_be_bytes(bytes: &[u8]) -> Result<Self, String> {
let arr = SecretBox::new(
GenericArray::<u8, ScalarSize>::from_exact_iter(bytes.iter().cloned())
.ok_or("Invalid length of a curve scalar")?,
);
BackendSecretKey::<CurveType>::from_bytes(arr.as_secret())
.map(Self::new)
.map_err(|err| format!("{err}"))
}
Expand Down

0 comments on commit 6f692dd

Please sign in to comment.