Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt ::aead to name change of NewAead to KeyInit #97

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ getrandom = ["rand_core/getrandom"]
# wasm-bindgen = ["getrandom/wasm-bindgen"]
# See https://github.com/rust-lang/cargo/issues/9210
# and https://github.com/w3f/schnorrkel/issues/65#issuecomment-786923588
aead = ["dep:aead", "getrandom"]
34 changes: 17 additions & 17 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ regarded as a pointer, not a recommendation.

// use rand_core::{RngCore,CryptoRng};

use aead::{NewAead, generic_array::{GenericArray}};
use aead::{KeyInit, KeySizeUser, generic_array::{GenericArray}};

use curve25519_dalek::digest::generic_array::typenum::{U32};

Expand All @@ -40,18 +40,18 @@ use crate::cert::AdaptorCertPublic;


fn make_aead<T,AEAD>(mut t: T) -> AEAD
where T: SigningTranscript,AEAD: NewAead
where T: SigningTranscript,AEAD: KeyInit
{
let mut key: GenericArray<u8, <AEAD as NewAead>::KeySize> = Default::default();
let mut key: GenericArray<u8, <AEAD as KeySizeUser>::KeySize> = Default::default();
t.challenge_bytes(b"",key.as_mut_slice());
AEAD::new(key)
AEAD::new(&key)
}

impl SecretKey {
/// Commit the results of a key exchange into a transcript
#[inline(always)]
pub(crate) fn raw_key_exchange(&self, public: &PublicKey) -> CompressedRistretto {
(&self.key * public.as_point()).compress()
(self.key * public.as_point()).compress()
}

/// Commit the results of a raw key exchange into a transcript
Expand All @@ -66,19 +66,19 @@ impl SecretKey {
///
/// Requires the AEAD have a 32 byte public key and does not support a context.
pub fn aead32_unauthenticated<AEAD>(&self, public: &PublicKey) -> AEAD
where AEAD: NewAead<KeySize=U32>
where AEAD: KeyInit<KeySize=U32>
{
let mut key: GenericArray<u8, <AEAD as NewAead>::KeySize> = Default::default();
let mut key: GenericArray<u8, <AEAD as KeySizeUser>::KeySize> = Default::default();
key.clone_from_slice( self.raw_key_exchange(public).as_bytes() );
AEAD::new(key)
AEAD::new(&key)
}
}

impl PublicKey {
/// Initialize an AEAD to the public key `self` using an ephemeral key exchange.
///
/// Returns the ephemeral public key and AEAD.
pub fn init_aead_unauthenticated<AEAD: NewAead>(&self, ctx: &[u8]) -> (CompressedRistretto,AEAD)
pub fn init_aead_unauthenticated<AEAD: KeyInit>(&self, ctx: &[u8]) -> (CompressedRistretto,AEAD)
{
let ephemeral = Keypair::generate();
let aead = ephemeral.aead_unauthenticated(ctx,self);
Expand All @@ -90,7 +90,7 @@ impl PublicKey {
/// Returns the ephemeral public key and AEAD.
/// Requires the AEAD have a 32 byte public key and does not support a context.
pub fn init_aead32_unauthenticated<AEAD>(&self) -> (CompressedRistretto,AEAD)
where AEAD: NewAead<KeySize=U32>
where AEAD: KeyInit<KeySize=U32>
{
let secret = SecretKey::generate();
let aead = secret.aead32_unauthenticated(self);
Expand All @@ -111,7 +111,7 @@ impl Keypair {
}

/// An AEAD from a key exchange with the specified public key.
pub fn aead_unauthenticated<AEAD: NewAead>(&self, ctx: &[u8], public: &PublicKey) -> AEAD {
pub fn aead_unauthenticated<AEAD: KeyInit>(&self, ctx: &[u8], public: &PublicKey) -> AEAD {
let mut t = merlin::Transcript::new(b"KEX");
t.append_message(b"ctx",ctx);
self.commit_key_exchange(&mut t,b"kex",public);
Expand All @@ -125,7 +125,7 @@ impl Keypair {
ephemeral_pk: &PublicKey,
static_pk: &PublicKey,
) -> AEAD
where T: SigningTranscript, AEAD: NewAead
where T: SigningTranscript, AEAD: KeyInit
{
self.commit_key_exchange(&mut t,b"epk",ephemeral_pk);
self.commit_key_exchange(&mut t,b"epk",static_pk);
Expand All @@ -138,10 +138,10 @@ impl Keypair {
mut t: T,
public: &PublicKey,
) -> (CompressedRistretto,AEAD)
where T: SigningTranscript, AEAD: NewAead
where T: SigningTranscript, AEAD: KeyInit
{
let key = t.witness_scalar(b"make_esk", &[&self.secret.nonce]);
let ekey = SecretKey { key, nonce: self.secret.nonce.clone() }.to_keypair();
let ekey = SecretKey { key, nonce: self.secret.nonce }.to_keypair();
ekey.commit_key_exchange(&mut t,b"epk",public);
self.commit_key_exchange(&mut t,b"epk",public);
(ekey.public.into_compressed(), make_aead(t))
Expand All @@ -158,7 +158,7 @@ impl Keypair {
cert_public: &AdaptorCertPublic,
public: &PublicKey,
) -> SignatureResult<AEAD>
where T: SigningTranscript, AEAD: NewAead
where T: SigningTranscript, AEAD: KeyInit
{
let epk = public.open_adaptor_cert(t,cert_public) ?;
Ok(self.aead_unauthenticated(b"",&epk))
Expand All @@ -169,10 +169,10 @@ impl Keypair {
/// Along with the AEAD, we return the implicit Adaptor certificate
/// from which the receiver recreates the ephemeral public key.
pub fn sender_aead_with_adaptor_cert<T,AEAD>(&self, t: T, public: &PublicKey) -> (AdaptorCertPublic,AEAD)
where T: SigningTranscript+Clone, AEAD: NewAead
where T: SigningTranscript+Clone, AEAD: KeyInit
{
let (cert,secret) = self.issue_self_adaptor_cert(t);
let aead = secret.to_keypair().aead_unauthenticated(b"",&public);
let aead = secret.to_keypair().aead_unauthenticated(b"", public);
(cert, aead)
}
}
2 changes: 1 addition & 1 deletion src/points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl PartialEq<Self> for RistrettoBoth {

impl PartialOrd<RistrettoBoth> for RistrettoBoth {
fn partial_cmp(&self, other: &RistrettoBoth) -> Option<::core::cmp::Ordering> {
self.compressed.0.partial_cmp(&other.compressed.0)
Some(self.cmp(other))
}
}

Expand Down
Loading