Skip to content

Commit

Permalink
changed the ExposeSecret trait to way more general, finally making it…
Browse files Browse the repository at this point in the history
… compile
  • Loading branch information
jymchng committed Nov 20, 2023
1 parent 5981cd8 commit 6ed0068
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ repos:
rev: v1.0
hooks:
- id: fmt
args: ['--verbose', '--edition', '2018', '--']
- id: cargo-check
- id: clippy
57 changes: 27 additions & 30 deletions src/secret.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,71 @@
use std::marker::PhantomData;

use crate::traits::ExposeSecret;
use typenum::{
consts::{U0, U1},
type_operators::IsLess,
Bit, IsGreater, Same, True, Unsigned, B0, B1,
Sum, True, Unsigned,
};
use zeroize::Zeroize;
use zeroize::{DefaultIsZeroes, Zeroize};

pub type AddU1<A> = <A as core::ops::Add<U1>>::Output;

pub struct Secret<
T: Zeroize,
MEC: Unsigned,
EC: core::ops::Add<typenum::U1> + typenum::IsLess<MEC> + Unsigned = U0,
>(
T,
core::marker::PhantomData<MEC>,
core::marker::PhantomData<EC>,
);

pub struct ExposedSecret<'brand, T: Zeroize, MEC: Unsigned, EC: Unsigned>(
T,
::core::marker::PhantomData<fn(&'brand ()) -> &'brand ()>,
::core::marker::PhantomData<MEC>,
::core::marker::PhantomData<EC>,
);
>(T, core::marker::PhantomData<(MEC, EC)>);

pub struct ExposedSecret<'brand, T>(T, ::core::marker::PhantomData<fn(&'brand ()) -> &'brand ()>);

impl<T: Zeroize, MEC: Unsigned> Secret<T, MEC, U0>
where
U0: IsLess<MEC>,
{
#[inline(always)]
pub fn new(value: T) -> Self {
Self(value, <_>::default(), <_>::default())
Self(value, PhantomData)
}
}

impl<
'max,
T: Zeroize,
MEC: Unsigned,
EC: core::ops::Add<typenum::U1> + Unsigned + typenum::IsLess<MEC>,
> ExposeSecret<T, MEC, EC> for Secret<T, MEC, EC>
> ExposeSecret<'max, &'max T, MEC, EC> for Secret<T, MEC, EC>
{
type Exposed<'brand> = ExposedSecret<'brand, &'brand T>
where
'max: 'brand;

type Next = Secret<T, MEC, Sum<EC, U1>>
where
EC: core::ops::Add<U1> + Unsigned + typenum::IsLess<MEC>,
Sum<EC, U1>: Unsigned + IsLess<MEC> + core::ops::Add<typenum::U1>,
T: Zeroize;

#[inline(always)]
fn expose_secret<ReturnType>(
fn expose_secret<ReturnType, ClosureType>(
self,
scope: impl FnOnce(ExposedSecret<'_, T, MEC, EC>) -> (ExposedSecret<'_, T, MEC, EC>, ReturnType),
scope: ClosureType,
) -> (Secret<T, MEC, AddU1<EC>>, ReturnType)
where
AddU1<EC>: core::ops::Add<typenum::U1> + Unsigned + typenum::IsLess<MEC>,
EC: IsLess<MEC, Output = True>,
for<'brand> ClosureType: FnOnce(ExposedSecret<'brand, &'brand T>) -> ReturnType,
{
let (witness, returned_value) = scope(ExposedSecret(
self.0,
<_>::default(),
<_>::default(),
<_>::default(),
));
(
Secret(witness.0, <_>::default(), <_>::default()),
returned_value,
)
let returned_value = scope(ExposedSecret(&self.0, PhantomData));
(Secret(self.0, PhantomData), returned_value)
}
}

impl<T: Zeroize, MEC: Unsigned, EC: Unsigned> ::core::ops::Deref for ExposedSecret<'_, T, MEC, EC> {
impl<T: Zeroize + DefaultIsZeroes> ::core::ops::Deref for ExposedSecret<'_, &'_ T> {
type Target = T;

#[inline(always)]
fn deref(&self) -> &T {
&self.0
self.0
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::secret::{AddU1, ExposedSecret, Secret};
use typenum::{consts::U1, Bit, IsLess, True, Unsigned};
use zeroize::Zeroize;
use typenum::{consts::U1, IsLess, Sum, True, Unsigned};

pub trait ExposeSecret<
T: Zeroize,
MEC: Unsigned,
EC: core::ops::Add<typenum::U1> + Unsigned + typenum::IsLess<MEC>,
>
{
fn expose_secret<ReturnType>(
self,
scope: impl FnOnce(ExposedSecret<T, MEC, EC>) -> (ExposedSecret<T, MEC, EC>, ReturnType),
) -> (Secret<T, MEC, AddU1<EC>>, ReturnType)
pub trait ExposeSecret<'max, T, MEC: Unsigned, EC: Unsigned>: Sized {
type Exposed<'brand>
where
AddU1<EC>: core::ops::Add<typenum::U1> + Unsigned + typenum::IsLess<MEC>,
EC: IsLess<MEC, Output = True>;
'max: 'brand;

type Next: ExposeSecret<'max, T, MEC, Sum<EC, U1>>
where
EC: core::ops::Add<U1> + Unsigned + typenum::IsLess<MEC>,
Sum<EC, U1>: Unsigned + IsLess<MEC> + core::ops::Add<typenum::U1>;

fn expose_secret<ReturnType, ClosureType>(self, scope: ClosureType) -> (Self::Next, ReturnType)
where
for<'brand> ClosureType: FnOnce(Self::Exposed<'brand>) -> ReturnType,
EC: core::ops::Add<U1> + IsLess<MEC, Output = True>,
Sum<EC, U1>: Unsigned + core::ops::Add<U1> + IsLess<MEC>;
}
2 changes: 1 addition & 1 deletion tests/trybuild_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ fn test_compile_fails() {
t.compile_fail("trybuild_tests/test_compile_fail_one.rs");
t.compile_fail("trybuild_tests/test_compile_fail_two.rs");
t.compile_fail("trybuild_tests/test_compile_fail_three.rs");
}
}

0 comments on commit 6ed0068

Please sign in to comment.