Skip to content

Commit ddfbe11

Browse files
committed
no std changes
1 parent 6362ab4 commit ddfbe11

File tree

11 files changed

+126
-52
lines changed

11 files changed

+126
-52
lines changed

Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ repository = "https://github.com/iron-fish/ironfish-frost"
1212
blake3 = { version = "1.5.0", optional = true }
1313
chacha20 = "0.9.1"
1414
chacha20poly1305 = "0.10.1"
15-
ed25519-dalek = { version = "2.1.0", features = ["rand_core"] }
15+
ed25519-dalek = { version = "2.1.0", default-features = false, features = ["rand_core", "alloc"] }
1616
rand_chacha = { version = "0.3.1", optional = true }
17-
rand_core = "0.6.4"
18-
reddsa = { git = "https://github.com/ZcashFoundation/reddsa.git", rev = "b9c3107e6ec5333a89a7fa064f2d10f749a90cce", features = ["frost", "frost-rerandomized"] }
19-
siphasher = { version = "1.0.0", optional = true }
20-
x25519-dalek = { version = "2.0.0", features = ["reusable_secrets", "static_secrets"] }
17+
rand_core = { version = "0.6.4", default-features = false, features = ["alloc"] }
18+
reddsa = { git = "https://github.com/ZcashFoundation/reddsa.git", rev="9ac52c5c60e454b0032d78a22c05fb79aae1d51e", features = ["frost"], default-features = false }
19+
siphasher = { version = "1.0.0", default-features = false }
20+
x25519-dalek = { version = "2.0.0", default-features = false, features = ["reusable_secrets", "static_secrets"] }
2121

2222
[dev-dependencies]
2323
hex-literal = "0.4.1"
2424
rand = "0.8.5"
2525

2626
[features]
27-
default = ["std", "signing"]
27+
default = ["signing"]
2828

2929
std = []
30-
signing = ["dep:blake3", "dep:rand_chacha", "dep:siphasher", "std"]
31-
dkg = ["std", "signing"]
30+
signing = ["dep:blake3", "dep:rand_chacha", "std"]
31+
dkg = []

src/checksum.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

5-
use std::error;
6-
use std::fmt;
5+
use core::fmt;
76

87
use siphasher::sip::SipHasher24;
8+
pub(crate) type ChecksumHasher = SipHasher24;
99

1010
pub(crate) const CHECKSUM_LEN: usize = 8;
1111

1212
pub(crate) type Checksum = u64;
1313

14-
pub(crate) type ChecksumHasher = SipHasher24;
15-
1614
#[derive(Clone, Debug)]
1715
pub enum ChecksumError {
1816
SigningCommitmentError,
@@ -33,4 +31,7 @@ impl fmt::Display for ChecksumError {
3331
}
3432
}
3533

34+
#[cfg(feature = "std")]
35+
use std::error;
36+
#[cfg(feature = "std")]
3637
impl error::Error for ChecksumError {}

src/dkg/group_key.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

5+
use crate::io;
56
use crate::multienc;
67
use crate::participant::Identity;
78
use crate::participant::Secret;
89
use rand_core::CryptoRng;
910
use rand_core::RngCore;
10-
use std::io;
11+
12+
#[cfg(not(feature = "std"))]
13+
extern crate alloc;
14+
#[cfg(not(feature = "std"))]
15+
use alloc::vec::Vec;
1116

1217
pub const GROUP_SECRET_KEY_LEN: usize = 32;
1318

src/dkg/round1.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::frost::keys::VerifiableSecretSharingCommitment;
1414
use crate::frost::Field;
1515
use crate::frost::Identifier;
1616
use crate::frost::JubjubScalarField;
17+
use crate::io;
1718
use crate::multienc;
1819
use crate::participant;
1920
use crate::participant::Identity;
@@ -23,12 +24,18 @@ use crate::serde::read_variable_length_bytes;
2324
use crate::serde::write_u16;
2425
use crate::serde::write_variable_length;
2526
use crate::serde::write_variable_length_bytes;
27+
use core::borrow::Borrow;
2628
use rand_core::CryptoRng;
2729
use rand_core::RngCore;
28-
use std::borrow::Borrow;
29-
use std::hash::Hasher;
30-
use std::io;
31-
use std::mem;
30+
31+
use core::hash::Hasher;
32+
use core::mem;
33+
34+
#[cfg(not(feature = "std"))]
35+
extern crate alloc;
36+
37+
#[cfg(not(feature = "std"))]
38+
use alloc::vec::Vec;
3239

3340
type Scalar = <JubjubScalarField as Field>::Scalar;
3441

src/dkg/round2.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::frost::keys::VerifiableSecretSharingCommitment;
1616
use crate::frost::Field;
1717
use crate::frost::Identifier;
1818
use crate::frost::JubjubScalarField;
19+
use crate::io;
1920
use crate::multienc;
2021
use crate::participant;
2122
use crate::participant::Identity;
@@ -25,13 +26,22 @@ use crate::serde::read_variable_length_bytes;
2526
use crate::serde::write_u16;
2627
use crate::serde::write_variable_length;
2728
use crate::serde::write_variable_length_bytes;
29+
use core::borrow::Borrow;
30+
use core::hash::Hasher;
31+
use core::mem;
2832
use rand_core::CryptoRng;
2933
use rand_core::RngCore;
30-
use std::borrow::Borrow;
34+
// use log::info;
35+
36+
#[cfg(feature = "std")]
3137
use std::collections::BTreeMap;
32-
use std::hash::Hasher;
33-
use std::io;
34-
use std::mem;
38+
39+
#[cfg(not(feature = "std"))]
40+
extern crate alloc;
41+
#[cfg(not(feature = "std"))]
42+
use alloc::collections::BTreeMap;
43+
#[cfg(not(feature = "std"))]
44+
use alloc::vec::Vec;
3545

3646
type Scalar = <JubjubScalarField as Field>::Scalar;
3747

src/dkg/round3.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::error::IronfishFrostError;
1212
use crate::frost::keys::dkg::part3;
1313
use crate::frost::keys::KeyPackage;
1414
use crate::frost::keys::PublicKeyPackage as FrostPublicKeyPackage;
15+
use crate::io;
1516
use crate::participant::Identity;
1617
use crate::participant::Secret;
1718
use crate::serde::read_u16;
@@ -20,10 +21,18 @@ use crate::serde::read_variable_length_bytes;
2021
use crate::serde::write_u16;
2122
use crate::serde::write_variable_length;
2223
use crate::serde::write_variable_length_bytes;
24+
use core::borrow::Borrow;
2325
use reddsa::frost::redjubjub::VerifyingKey;
24-
use std::borrow::Borrow;
26+
27+
#[cfg(feature = "std")]
2528
use std::collections::BTreeMap;
26-
use std::io;
29+
30+
#[cfg(not(feature = "std"))]
31+
extern crate alloc;
32+
#[cfg(not(feature = "std"))]
33+
use alloc::collections::BTreeMap;
34+
#[cfg(not(feature = "std"))]
35+
use alloc::vec::Vec;
2736

2837
#[derive(Clone, Eq, PartialEq, Debug)]
2938
pub struct PublicKeyPackage {
@@ -72,9 +81,11 @@ impl PublicKeyPackage {
7281
bytes
7382
}
7483

75-
#[cfg(feature = "std")]
76-
pub fn serialize_into<W: io::Write>(&self, mut writer: W) -> Result<(), IronfishFrostError> {
77-
let frost_public_key_package = self.frost_public_key_package.serialize()?;
84+
pub fn serialize_into<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
85+
let frost_public_key_package = self
86+
.frost_public_key_package
87+
.serialize()
88+
.map_err(|_| io::Error::other("public key package serialization failed"))?;
7889
write_variable_length_bytes(&mut writer, &frost_public_key_package)?;
7990
write_variable_length(&mut writer, &self.identities, |writer, identity| {
8091
identity.serialize_into(writer)
@@ -84,7 +95,6 @@ impl PublicKeyPackage {
8495
Ok(())
8596
}
8697

87-
#[cfg(feature = "std")]
8898
pub fn deserialize_from<R: io::Read>(mut reader: R) -> Result<Self, IronfishFrostError> {
8999
let frost_public_key_package = read_variable_length_bytes(&mut reader)?;
90100
let frost_public_key_package =
@@ -171,7 +181,7 @@ where
171181
// inputs
172182
round1_frost_packages
173183
.remove(&identity.to_frost_identifier())
174-
.ok_or_else(|| IronfishFrostError::InvalidInput)?;
184+
.ok_or(IronfishFrostError::InvalidInput)?;
175185

176186
let expected_round2_checksum =
177187
round2::input_checksum(round1_public_packages.iter().map(Borrow::borrow));

src/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use reddsa::frost::redjubjub::JubjubBlake2b512;
77

88
use crate::io;
99

10-
#[cfg(feature = "signing")]
1110
use crate::checksum::ChecksumError;
1211

1312
#[derive(Debug)]
@@ -17,7 +16,6 @@ pub enum IronfishFrostError {
1716
IoError(io::Error),
1817
FrostError(FrostError<JubjubBlake2b512>),
1918
SignatureError(ed25519_dalek::SignatureError),
20-
#[cfg(feature = "signing")]
2119
ChecksumError(ChecksumError),
2220
}
2321

src/lib.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
mod serde;
1313

14-
#[cfg(feature = "signing")]
1514
mod checksum;
1615

1716
pub mod error;
@@ -39,6 +38,11 @@ mod io {
3938
pub(crate) use std::io::Write;
4039
}
4140

41+
#[cfg(not(feature = "std"))]
42+
#[macro_use]
43+
#[cfg(not(feature = "std"))]
44+
extern crate alloc;
45+
4246
#[cfg(not(feature = "std"))]
4347
mod io {
4448
use core::cmp;
@@ -53,7 +57,7 @@ mod io {
5357
}
5458
}
5559

56-
pub type Result<T> = core::result::Result<T, Error>;
60+
pub(crate) type Result<T> = core::result::Result<T, Error>;
5761

5862
pub trait Read {
5963
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
@@ -74,6 +78,13 @@ mod io {
7478
Err(Error)
7579
}
7680
}
81+
82+
fn by_ref(&mut self) -> &mut Self
83+
where
84+
Self: Sized,
85+
{
86+
self
87+
}
7788
}
7889

7990
impl<R: Read> Read for &mut R {
@@ -137,3 +148,19 @@ mod io {
137148
}
138149
}
139150
}
151+
152+
#[cfg(not(feature = "std"))]
153+
use alloc::vec::Vec;
154+
155+
#[cfg(not(feature = "std"))]
156+
impl io::Write for Vec<u8> {
157+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
158+
self.extend_from_slice(buf);
159+
Ok(buf.len())
160+
}
161+
162+
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
163+
self.extend_from_slice(buf);
164+
Ok(())
165+
}
166+
}

src/multienc.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ use rand_core::RngCore;
2020
use x25519_dalek::PublicKey;
2121
use x25519_dalek::ReusableSecret;
2222

23+
#[cfg(not(feature = "std"))]
24+
extern crate alloc;
25+
#[cfg(not(feature = "std"))]
26+
use crate::alloc::borrow::ToOwned;
27+
#[cfg(not(feature = "std"))]
28+
use alloc::vec::Vec;
29+
2330
pub const HEADER_SIZE: usize = 56;
2431
pub const KEY_SIZE: usize = 32;
2532

@@ -29,31 +36,30 @@ pub const fn metadata_size(num_recipients: usize) -> usize {
2936
HEADER_SIZE + KEY_SIZE * num_recipients
3037
}
3138

32-
#[cfg(feature = "std")]
33-
pub fn read_encrypted_blob<R>(mut reader: R) -> io::Result<Vec<u8>>
39+
pub fn read_encrypted_blob<R>(reader: &mut R) -> Result<Vec<u8>, io::Error>
3440
where
35-
R: io::Read,
41+
R: crate::io::Read,
3642
{
37-
use std::io::Read;
38-
3943
let mut result = Vec::new();
40-
let reader = reader.by_ref();
4144

42-
reader.take(HEADER_SIZE as u64).read_to_end(&mut result)?;
45+
let mut header_bytes = [0u8; HEADER_SIZE];
46+
reader.read_exact(&mut header_bytes)?;
47+
let header: Header = Header::deserialize_from(&header_bytes[..])?;
4348

44-
let header = Header::deserialize_from(&result[..])?;
4549
for _ in 0..header.num_recipients {
46-
reader.take(KEY_SIZE as u64).read_to_end(&mut result)?;
50+
let mut key_bytes = vec![0u8; KEY_SIZE];
51+
reader.read_exact(&mut key_bytes)?;
52+
result.extend(key_bytes);
4753
}
48-
reader
49-
.take(header.data_len as u64)
50-
.read_to_end(&mut result)?;
54+
55+
let mut data_bytes = vec![0u8; header.data_len];
56+
reader.read_exact(&mut data_bytes)?;
57+
result.extend(data_bytes);
5158

5259
Ok(result)
5360
}
5461

5562
#[must_use]
56-
#[cfg(feature = "std")]
5763
pub fn encrypt<'a, I, R>(data: &[u8], recipients: I, csrng: R) -> Vec<u8>
5864
where
5965
I: IntoIterator<Item = &'a Identity>,
@@ -140,15 +146,21 @@ where
140146
///
141147
/// This method expects the ciphertext and the metadata to be concatenated in one slice. Use
142148
/// [`decrypt_in_place`] if you have two separate slices.
143-
#[cfg(feature = "std")]
144149
pub fn decrypt(secret: &Secret, data: &[u8]) -> io::Result<Vec<u8>> {
145150
let header = Header::deserialize_from(data)?;
146151
let metadata_len = metadata_size(header.num_recipients);
147152
let total_len = metadata_len
148153
.checked_add(header.data_len)
149154
.ok_or_else(|| io::Error::other("overflow when calculating data size"))?;
150155
if data.len() < total_len {
151-
return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
156+
#[cfg(feature = "std")]
157+
{
158+
return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
159+
}
160+
#[cfg(not(feature = "std"))]
161+
{
162+
return Err(io::Error);
163+
}
152164
}
153165

154166
let (metadata, ciphertext) = data.split_at(metadata_len);
@@ -241,7 +253,6 @@ impl Header {
241253
write_usize(&mut writer, self.data_len)
242254
}
243255

244-
#[cfg(feature = "std")]
245256
fn deserialize_from<R: io::Read>(mut reader: R) -> io::Result<Self> {
246257
let mut agreement_key = [0u8; 32];
247258
reader.read_exact(&mut agreement_key)?;
@@ -265,7 +276,6 @@ impl Header {
265276

266277
#[cfg(test)]
267278
mod tests {
268-
#[cfg(feature = "std")]
269279
mod detached {
270280
use crate::multienc::decrypt;
271281
use crate::multienc::encrypt;

0 commit comments

Comments
 (0)