mirror of
https://github.com/saymrwulf/risc0-curve25519-dalek-source.git
synced 2026-09-07 20:30:39 +00:00
Merge remote-tracking branch 'hdevalence/feature/hash-to-scalar' into develop
This commit is contained in:
commit
c627bf25ce
3 changed files with 50 additions and 0 deletions
10
Cargo.toml
10
Cargo.toml
|
|
@ -22,6 +22,16 @@ version = "0.3.3"
|
||||||
optional = true
|
optional = true
|
||||||
version = "0.3"
|
version = "0.3"
|
||||||
|
|
||||||
|
[dependencies.digest]
|
||||||
|
version = "0.4"
|
||||||
|
|
||||||
|
[dependencies.generic-array]
|
||||||
|
# same version that digest depends on
|
||||||
|
version = "^0.6"
|
||||||
|
|
||||||
|
[dev-dependencies.sha2]
|
||||||
|
version = "0.4"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
std = ["rand"]
|
std = ["rand"]
|
||||||
|
|
|
||||||
|
|
@ -38,10 +38,15 @@ extern crate std;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
#[cfg(test)]
|
||||||
|
extern crate sha2;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate arrayref;
|
extern crate arrayref;
|
||||||
|
|
||||||
|
extern crate generic_array;
|
||||||
|
extern crate digest;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,10 @@ use core::ops::{Neg};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
use digest::Digest;
|
||||||
|
use generic_array::GenericArray;
|
||||||
|
use generic_array::typenum::U64;
|
||||||
|
|
||||||
use constants;
|
use constants;
|
||||||
use utils::{load3, load4};
|
use utils::{load3, load4};
|
||||||
use subtle::CTAssignable;
|
use subtle::CTAssignable;
|
||||||
|
|
@ -159,6 +163,37 @@ impl Scalar {
|
||||||
Scalar::reduce(&scalar_bytes)
|
Scalar::reduce(&scalar_bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Hash a slice of bytes into a scalar.
|
||||||
|
///
|
||||||
|
/// Takes a type parameter `D`, which is any `Digest` producing 64
|
||||||
|
/// bytes (512 bits) of output.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # extern crate curve25519_dalek;
|
||||||
|
/// # use curve25519_dalek::scalar::Scalar;
|
||||||
|
/// extern crate sha2;
|
||||||
|
/// use sha2::Sha512;
|
||||||
|
///
|
||||||
|
/// # // Need fn main() here in comment so the doctest compiles
|
||||||
|
/// # // See https://doc.rust-lang.org/book/documentation.html#documentation-as-tests
|
||||||
|
/// # fn main() {
|
||||||
|
/// let msg = "To really appreciate architecture, you may even need to commit a murder";
|
||||||
|
/// let s = Scalar::hash_from_bytes::<Sha512>(msg.as_bytes());
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
pub fn hash_from_bytes<D>(input: &[u8]) -> Scalar
|
||||||
|
where D: Digest<OutputSize=U64> + Default {
|
||||||
|
let mut hash = D::default();
|
||||||
|
hash.input(input);
|
||||||
|
// XXX this seems clumsy
|
||||||
|
let mut output = [0u8;64];
|
||||||
|
output.copy_from_slice(hash.result().as_slice());
|
||||||
|
Scalar::reduce(&output)
|
||||||
|
}
|
||||||
|
|
||||||
/// Construct the additive identity
|
/// Construct the additive identity
|
||||||
pub fn zero() -> Self {
|
pub fn zero() -> Self {
|
||||||
Scalar([0u8; 32])
|
Scalar([0u8; 32])
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue