Skip to content

Commit

Permalink
Add #![no_std] (fixes #16)
Browse files Browse the repository at this point in the history
Use ::core in lieu of ::std, allowing this crate to be usable in #![no_std]
environments.

Gates features that presently depend on ::std (presently just rand) behind a
"std" cargo feature, which is enabled by default.
  • Loading branch information
tonychain committed Jan 18, 2017
1 parent 42c2578 commit 6330655
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ exclude = [
".gitignore"
]

[dependencies]
rand = "0.3"
arrayref = "0.3.2"
[dependencies.rand]
optional = true
version = "0.3"

[dependencies.arrayref]
version = "0.3.2"

[features]
default = ["std"]
std = ["rand"]

# The development profile, used for `cargo build`.
[profile.dev]
Expand Down
20 changes: 10 additions & 10 deletions src/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
// affine and projective cakes and eat both of them too.
#![allow(non_snake_case)]

use std::fmt::Debug;
use std::iter::Iterator;
use std::ops::{Add, Sub, Neg, Index};
use std::cmp::{PartialEq, Eq};
use core::fmt::Debug;
use core::iter::Iterator;
use core::ops::{Add, Sub, Neg, Index};
use core::cmp::{PartialEq, Eq};

use constants;
use field::FieldElement;
Expand All @@ -100,7 +100,7 @@ use util::bytes_equal_ct;
pub struct CompressedPoint(pub [u8; 32]);

impl Debug for CompressedPoint {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "CompressedPoint: {:?}", &self.0[..])
}
}
Expand Down Expand Up @@ -763,35 +763,35 @@ impl ExtendedPoint {
// ------------------------------------------------------------------------

impl Debug for ExtendedPoint {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "ExtendedPoint(\n\tX: {:?},\n\tY: {:?},\n\tZ: {:?},\n\tT: {:?}\n)",
&self.X, &self.Y, &self.Z, &self.T)
}
}

impl Debug for ProjectivePoint {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "ProjectivePoint(\n\tX: {:?},\n\tY: {:?},\n\tZ: {:?}\n)",
&self.X, &self.Y, &self.Z)
}
}

impl Debug for CompletedPoint {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "CompletedPoint(\n\tX: {:?},\n\tY: {:?},\n\tZ: {:?},\n\tT: {:?}\n)",
&self.X, &self.Y, &self.Z, &self.T)
}
}

impl Debug for PreComputedPoint {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "PreComputedPoint(\n\ty_plus_x: {:?},\n\ty_minus_x: {:?},\n\txy2d: {:?}\n)",
&self.y_plus_x, &self.y_minus_x, &self.xy2d)
}
}

impl Debug for CachedPoint {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "CachedPoint(\n\tY_plus_X: {:?},\n\tY_minus_X: {:?},\n\tZ: {:?},\n\tT2d: {:?}\n)",
&self.Y_plus_X, &self.Y_minus_X, &self.Z, &self.T2d)
}
Expand Down
18 changes: 9 additions & 9 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
//! Based on Adam Langley's curve25519-donna and (Golang) ed25519
//! implementations.
use std::clone::Clone;
use std::fmt::Debug;
use std::ops::{Add, AddAssign};
use std::ops::{Sub, SubAssign};
use std::ops::{Mul, MulAssign};
use std::ops::{Index, IndexMut};
use std::cmp::{Eq, PartialEq};
use std::ops::Neg;
use core::clone::Clone;
use core::fmt::Debug;
use core::ops::{Add, AddAssign};
use core::ops::{Sub, SubAssign};
use core::ops::{Mul, MulAssign};
use core::ops::{Index, IndexMut};
use core::cmp::{Eq, PartialEq};
use core::ops::Neg;

use util::byte_is_nonzero;

Expand Down Expand Up @@ -65,7 +65,7 @@ impl PartialEq for FieldElement {
impl Eq for FieldElement {}

impl Debug for FieldElement {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "FieldElement: {:?}", &self.0[..])
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// - Isis Agora Lovecruft <[email protected]>
// - Henry de Valence <[email protected]>

#![no_std]
#![allow(unused_features)]
#![feature(test)]
#![deny(missing_docs)] // refuse to compile if documentation is missing
Expand Down Expand Up @@ -37,6 +38,7 @@ extern crate test;
#[macro_use]
extern crate arrayref;

#[cfg(feature = "std")]
extern crate rand;

// Modules for low-level operations directly on field elements and curve points.
Expand Down
6 changes: 4 additions & 2 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
//! However, in contrast to `FieldElement`s, `Scalar`s are stored in
//! memory as bytes, allowing easy access to the bits of the `Scalar`.
use std::clone::Clone;
use std::ops::{Index, IndexMut};
use core::clone::Clone;
use core::ops::{Index, IndexMut};

#[cfg(feature = "std")]
use rand::Rng;

use field::{load3, load4};
Expand Down Expand Up @@ -73,6 +74,7 @@ impl Scalar {
/// # Returns
///
/// A random scalar within ℤ/lℤ.
#[cfg(feature = "std")]
pub fn random<T: Rng>(csprng: &mut T) -> Self {
let mut scalar_bytes = [0u8; 64];
csprng.fill_bytes(&mut scalar_bytes);
Expand Down

0 comments on commit 6330655

Please sign in to comment.