mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-06 20:41:14 +00:00
Merge remote-tracking branch 'isislovecruft/master'
This commit is contained in:
commit
51e3763643
3 changed files with 22 additions and 0 deletions
|
|
@ -15,6 +15,7 @@ exclude = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
rand = "0.3"
|
||||||
arrayref = "0.3.2"
|
arrayref = "0.3.2"
|
||||||
|
|
||||||
# The development profile, used for `cargo build`.
|
# The development profile, used for `cargo build`.
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,8 @@ extern crate test;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate arrayref;
|
extern crate arrayref;
|
||||||
|
|
||||||
|
extern crate rand;
|
||||||
|
|
||||||
// Modules for low-level operations directly on field elements and curve points.
|
// Modules for low-level operations directly on field elements and curve points.
|
||||||
|
|
||||||
pub mod field;
|
pub mod field;
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,9 @@
|
||||||
use std::clone::Clone;
|
use std::clone::Clone;
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
|
use rand::OsRng;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
use field::{load3, load4};
|
use field::{load3, load4};
|
||||||
|
|
||||||
/// The `Scalar` struct represents an element in ℤ/lℤ, where
|
/// The `Scalar` struct represents an element in ℤ/lℤ, where
|
||||||
|
|
@ -60,6 +63,17 @@ impl IndexMut<usize> for Scalar {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Scalar {
|
impl Scalar {
|
||||||
|
/// Return a `Scalar` chosen uniformly at random using a CSPRNG.
|
||||||
|
/// Panics if the operating system's CSPRNG is unavailable.
|
||||||
|
pub fn random() -> Self {
|
||||||
|
// XXX is there a more efficient way than building
|
||||||
|
// the rng every time here?
|
||||||
|
let mut rng = OsRng::new().unwrap();
|
||||||
|
let mut scalar_bytes = [0u8; 64];
|
||||||
|
rng.fill_bytes(&mut scalar_bytes);
|
||||||
|
Scalar::reduce(&scalar_bytes)
|
||||||
|
}
|
||||||
|
|
||||||
/// Construct the additive identity
|
/// Construct the additive identity
|
||||||
pub fn zero() -> Self {
|
pub fn zero() -> Self {
|
||||||
Scalar([0u8; 32])
|
Scalar([0u8; 32])
|
||||||
|
|
@ -417,6 +431,11 @@ mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use test::Bencher;
|
use test::Bencher;
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn bench_scalar_random(b: &mut Bencher) {
|
||||||
|
b.iter(|| Scalar::random());
|
||||||
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_scalar_multiply_add(b: &mut Bencher) {
|
fn bench_scalar_multiply_add(b: &mut Bencher) {
|
||||||
b.iter(|| Scalar::multiply_add(&X, &Y, &Z) );
|
b.iter(|| Scalar::multiply_add(&X, &Y, &Z) );
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue