Skip to content

Commit eb4ded5

Browse files
committed
revert changes to std
1 parent 9a8d28f commit eb4ded5

File tree

10 files changed

+53
-120
lines changed

10 files changed

+53
-120
lines changed

Cargo.toml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +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", default-features = false, features = ["rand_core", "alloc"] }
15+
ed25519-dalek = { version = "2.1.0", features = ["rand_core"] }
1616
rand_chacha = { version = "0.3.1", optional = true }
17-
rand_core = { version = "0.6.4", default-features = false, features = ["alloc"] }
18-
reddsa = { path = "../reddsa", default-features = false, features = ["frost", "alloc"]}
19-
frost-core = { git = "https://github.com/ZcashFoundation/frost.git", package = "frost-core", default-features = false, features = ["serialization", "serde", "cheater-detection"] }
20-
frost-rerandomized = { git = "https://github.com/ZcashFoundation/frost.git", package = "frost-rerandomized", default-features = false }
21-
siphasher = { version = "1.0.0", default-features = false }
22-
x25519-dalek = { version = "2.0.0", default-features = false, features = ["reusable_secrets", "static_secrets"] }
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"] }
2321

2422
[dev-dependencies]
2523
hex-literal = "0.4.1"
2624
rand = "0.8.5"
2725

2826
[features]
29-
default = ["dkg"]
27+
default = ["std", "signing"]
3028

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

src/checksum.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +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 core::fmt;
5+
use std::error;
6+
use std::fmt;
67

78
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+
1416
#[derive(Clone, Debug)]
1517
pub enum ChecksumError {
1618
SigningCommitmentError,
@@ -30,3 +32,5 @@ impl fmt::Display for ChecksumError {
3032
}
3133
}
3234
}
35+
36+
impl error::Error for ChecksumError {}

src/dkg/group_key.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
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;
65
use crate::multienc;
76
use crate::participant::Identity;
87
use crate::participant::Secret;
98
use rand_core::CryptoRng;
109
use rand_core::RngCore;
11-
12-
#[cfg(not(feature = "std"))]
13-
extern crate alloc;
14-
#[cfg(not(feature = "std"))]
15-
use alloc::vec::Vec;
10+
use std::io;
1611

1712
pub const GROUP_SECRET_KEY_LEN: usize = 32;
1813

src/dkg/round1.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::frost::keys::VerifiableSecretSharingCommitment;
1414
use crate::frost::Field;
1515
use crate::frost::Identifier;
1616
use crate::frost::JubjubScalarField;
17-
use crate::io;
1817
use crate::multienc;
1918
use crate::multienc::read_encrypted_blob;
2019
use crate::participant;
@@ -25,17 +24,12 @@ use crate::serde::read_variable_length_bytes;
2524
use crate::serde::write_u16;
2625
use crate::serde::write_variable_length;
2726
use crate::serde::write_variable_length_bytes;
28-
use core::borrow::Borrow;
2927
use rand_core::CryptoRng;
3028
use rand_core::RngCore;
31-
32-
use core::hash::Hasher;
33-
use core::mem;
34-
35-
#[cfg(not(feature = "std"))]
36-
extern crate alloc;
37-
#[cfg(not(feature = "std"))]
38-
use alloc::vec::Vec;
29+
use std::borrow::Borrow;
30+
use std::hash::Hasher;
31+
use std::io;
32+
use std::mem;
3933

4034
type Scalar = <JubjubScalarField as Field>::Scalar;
4135

@@ -268,7 +262,7 @@ impl PublicPackage {
268262
let frost_package = read_variable_length_bytes(&mut reader)?;
269263
let frost_package = Package::deserialize(&frost_package)?;
270264

271-
let group_secret_key_shard_encrypted = read_encrypted_blob(&mut reader)?;
265+
let group_secret_key_shard_encrypted = read_variable_length_bytes(&mut reader)?;
272266

273267
let mut checksum = [0u8; CHECKSUM_LEN];
274268
reader.read_exact(&mut checksum)?;
@@ -303,8 +297,8 @@ where
303297
return Err(IronfishFrostError::InvalidInput);
304298
}
305299

306-
let max_signers =
307-
u16::try_from(participants.len()).map_err(|_| IronfishFrostError::InvalidInput)?;
300+
let max_signers = u16::try_from(participants.len())
301+
.map_err(|_| IronfishFrostError::InvalidInput("too many participants".to_string()))?;
308302

309303
let (secret_package, public_package) = frost::keys::dkg::part1(
310304
self_identity.to_frost_identifier(),

src/dkg/round2.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::frost::keys::VerifiableSecretSharingCommitment;
1616
use crate::frost::Field;
1717
use crate::frost::Identifier;
1818
use crate::frost::JubjubScalarField;
19-
use crate::io;
2019
use crate::multienc;
2120
use crate::participant;
2221
use crate::participant::Identity;
@@ -26,22 +25,13 @@ use crate::serde::read_variable_length_bytes;
2625
use crate::serde::write_u16;
2726
use crate::serde::write_variable_length;
2827
use crate::serde::write_variable_length_bytes;
29-
use core::borrow::Borrow;
30-
use core::hash::Hasher;
31-
use core::mem;
3228
use rand_core::CryptoRng;
3329
use rand_core::RngCore;
34-
// use log::info;
35-
36-
#[cfg(feature = "std")]
30+
use std::borrow::Borrow;
3731
use std::collections::BTreeMap;
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;
32+
use std::hash::Hasher;
33+
use std::io;
34+
use std::mem;
4535

4636
type Scalar = <JubjubScalarField as Field>::Scalar;
4737

src/dkg/round3.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ 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;
1615
use crate::participant::Identity;
1716
use crate::participant::Secret;
1817
use crate::serde::read_u16;
@@ -21,20 +20,10 @@ use crate::serde::read_variable_length_bytes;
2120
use crate::serde::write_u16;
2221
use crate::serde::write_variable_length;
2322
use crate::serde::write_variable_length_bytes;
24-
use core::borrow::Borrow;
2523
use reddsa::frost::redjubjub::VerifyingKey;
26-
27-
#[cfg(feature = "std")]
24+
use std::borrow::Borrow;
2825
use std::collections::BTreeMap;
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::string::ToString;
36-
#[cfg(not(feature = "std"))]
37-
use alloc::vec::Vec;
26+
use std::io;
3827

3928
#[derive(Clone, Eq, PartialEq, Debug)]
4029
pub struct PublicKeyPackage {
@@ -83,6 +72,7 @@ impl PublicKeyPackage {
8372
bytes
8473
}
8574

75+
#[cfg(feature = "std")]
8676
pub fn serialize_into<W: io::Write>(&self, mut writer: W) -> Result<(), IronfishFrostError> {
8777
let frost_public_key_package = self.frost_public_key_package.serialize()?;
8878
write_variable_length_bytes(&mut writer, &frost_public_key_package)?;
@@ -94,6 +84,7 @@ impl PublicKeyPackage {
9484
Ok(())
9585
}
9686

87+
#[cfg(feature = "std")]
9788
pub fn deserialize_from<R: io::Read>(mut reader: R) -> Result<Self, IronfishFrostError> {
9889
let frost_public_key_package = read_variable_length_bytes(&mut reader)?;
9990
let frost_public_key_package =

src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
use reddsa::frost::redjubjub::frost::Error as FrostError;
66
use reddsa::frost::redjubjub::JubjubBlake2b512;
77

8-
use crate::checksum::ChecksumError;
98
use crate::io;
109

10+
#[cfg(feature = "std")]
11+
use crate::checksum::ChecksumError;
12+
1113
#[derive(Debug)]
1214
pub enum IronfishFrostError {
1315
InvalidInput,
1416
StdError,
1517
IoError(io::Error),
1618
FrostError(FrostError<JubjubBlake2b512>),
1719
SignatureError(ed25519_dalek::SignatureError),
20+
#[cfg(feature = "std")]
1821
ChecksumError(ChecksumError),
1922
}
2023

src/lib.rs

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

1212
mod serde;
1313

14+
#[cfg(feature = "signing")]
1415
mod checksum;
1516

1617
pub mod error;
@@ -38,11 +39,6 @@ mod io {
3839
pub(crate) use std::io::Write;
3940
}
4041

41-
#[cfg(not(feature = "std"))]
42-
#[macro_use]
43-
#[cfg(not(feature = "std"))]
44-
extern crate alloc;
45-
4642
#[cfg(not(feature = "std"))]
4743
mod io {
4844
use core::cmp;
@@ -57,7 +53,7 @@ mod io {
5753
}
5854
}
5955

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

6258
pub trait Read {
6359
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
@@ -78,13 +74,6 @@ mod io {
7874
Err(Error)
7975
}
8076
}
81-
82-
fn by_ref(&mut self) -> &mut Self
83-
where
84-
Self: Sized,
85-
{
86-
self
87-
}
8877
}
8978

9079
impl<R: Read> Read for &mut R {
@@ -148,19 +137,3 @@ mod io {
148137
}
149138
}
150139
}
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: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ 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-
3023
pub const HEADER_SIZE: usize = 56;
3124
pub const KEY_SIZE: usize = 32;
3225

@@ -36,30 +29,31 @@ pub const fn metadata_size(num_recipients: usize) -> usize {
3629
HEADER_SIZE + KEY_SIZE * num_recipients
3730
}
3831

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

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

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

5952
Ok(result)
6053
}
6154

6255
#[must_use]
56+
#[cfg(feature = "std")]
6357
pub fn encrypt<'a, I, R>(data: &[u8], recipients: I, csrng: R) -> Vec<u8>
6458
where
6559
I: IntoIterator<Item = &'a Identity>,
@@ -146,21 +140,15 @@ where
146140
///
147141
/// This method expects the ciphertext and the metadata to be concatenated in one slice. Use
148142
/// [`decrypt_in_place`] if you have two separate slices.
143+
#[cfg(feature = "std")]
149144
pub fn decrypt(secret: &Secret, data: &[u8]) -> io::Result<Vec<u8>> {
150145
let header = Header::deserialize_from(data)?;
151146
let metadata_len = metadata_size(header.num_recipients);
152147
let total_len = metadata_len
153148
.checked_add(header.data_len)
154149
.ok_or_else(|| io::Error::other("overflow when calculating data size"))?;
155150
if data.len() < total_len {
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-
}
151+
return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
164152
}
165153

166154
let (metadata, ciphertext) = data.split_at(metadata_len);
@@ -253,6 +241,7 @@ impl Header {
253241
write_usize(&mut writer, self.data_len)
254242
}
255243

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

277266
#[cfg(test)]
278267
mod tests {
268+
#[cfg(feature = "std")]
279269
mod detached {
280270
use crate::multienc::decrypt;
281271
use crate::multienc::encrypt;

0 commit comments

Comments
 (0)