Skip to content

Commit

Permalink
tests passed #37
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Feb 18, 2024
1 parent 1d2e16d commit 3510e33
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
20 changes: 18 additions & 2 deletions src/runtime/common.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
use crate::runtime::traits::MinimallyRepresentableUInt;
use crate::runtime::traits::{MinimallyRepresentableUInt, __private};
use core::ops::Sub;

use typenum::{Exp, U1, U16, U2, U256, U32, U4294967296, U64, U65536, U8};
use typenum::{Exp, Unsigned, U1, U16, U2, U256, U32, U4294967296, U64, U65536, U8};

impl MinimallyRepresentableUInt for U8 {
type Type = u8;
type UIntMaxValueAsType = <U256 as Sub<U1>>::Output;
const MIN: Self::Type = Self::Type::MIN;
const ONE: Self::Type = 1;

fn cast_unsigned_to_self_type<T: typenum::Unsigned>(_: __private::SealedToken) -> Self::Type {
<T as Unsigned>::U8
}
}

impl MinimallyRepresentableUInt for U16 {
type Type = u16;
type UIntMaxValueAsType = <U65536 as Sub<U1>>::Output;
const MIN: Self::Type = Self::Type::MIN;
const ONE: Self::Type = 1;

fn cast_unsigned_to_self_type<T: typenum::Unsigned>(_: __private::SealedToken) -> Self::Type {
<T as Unsigned>::U16
}
}

impl MinimallyRepresentableUInt for U32 {
type Type = u32;
type UIntMaxValueAsType = <U4294967296 as Sub<U1>>::Output;
const MIN: Self::Type = Self::Type::MIN;
const ONE: Self::Type = 1;

fn cast_unsigned_to_self_type<T: typenum::Unsigned>(_: __private::SealedToken) -> Self::Type {
<T as Unsigned>::U32
}
}

impl MinimallyRepresentableUInt for U64 {
type Type = u64;
type UIntMaxValueAsType = <Exp<U2, U64> as Sub<U1>>::Output;
const MIN: Self::Type = Self::Type::MIN;
const ONE: Self::Type = 1;

fn cast_unsigned_to_self_type<T: typenum::Unsigned>(_: __private::SealedToken) -> Self::Type {
<T as Unsigned>::U64
}
}
9 changes: 5 additions & 4 deletions src/runtime/secret.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use core::{cell::UnsafeCell, marker::PhantomData, ops::Deref};

use crate::runtime::{error, traits};
use num_traits::{AsPrimitive, ConstOne};
use crate::runtime::{
error,
traits::{self, __private},
};
use typenum::{IsLessOrEqual, True, Unsigned, U64};

pub struct RTSecret<T, MEC: Unsigned, SIZE: traits::MinimallyRepresentableUInt = U64>(
Expand Down Expand Up @@ -71,8 +73,7 @@ impl<'secret, T, MEC: Unsigned, SIZE: traits::MinimallyRepresentableUInt>
// SAFETY: All tuple fields of `RTSecret` are private, there are no setter to them.
// `RTSecret` is also not `Sync` so it is not possible to have multithreading race condition.
let ec_mut = unsafe { &mut *self.1.get() };
let ec_mut_usize: usize = ec_mut.as_();
if ec_mut_usize >= MEC::USIZE {
if *ec_mut >= SIZE::cast_unsigned_to_self_type::<MEC>(__private::SealedToken {}) {
return Err(error::ExposeSecretError::ExposeMoreThanMaximallyAllow(
error::ExposeMoreThanMaximallyAllowError {
mec: MEC::USIZE,
Expand Down
14 changes: 11 additions & 3 deletions src/runtime/traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use core::{
cmp::PartialOrd,
fmt::{Debug, Display},
ops::AddAssign,
};

pub use crate::runtime::error;
use num_traits::AsPrimitive;
use typenum::Unsigned;

pub trait RTExposeSecret<'secret, T, SIZE: MinimallyRepresentableUInt> {
Expand All @@ -24,9 +24,17 @@ pub trait RTExposeSecret<'secret, T, SIZE: MinimallyRepresentableUInt> {
for<'brand> ClosureType: FnOnce(Self::Exposed<'brand>) -> ReturnType;
}

pub(crate) trait MinimallyRepresentableUInt: Unsigned {
type Type: AddAssign + AsPrimitive<usize> + Debug + Display;
pub(crate) mod __private {

pub struct SealedToken {}
}

pub trait MinimallyRepresentableUInt: Unsigned {
// indeed, `u8`, `u16`, `u32` and `u64` all satisfy these bounds
type Type: AddAssign + PartialOrd + Debug + Display + Copy;
type UIntMaxValueAsType;
const MIN: Self::Type;
const ONE: Self::Type;

fn cast_unsigned_to_self_type<T: Unsigned>(_: __private::SealedToken) -> Self::Type;
}
2 changes: 1 addition & 1 deletion tests/extern_bin_rt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sosecrets_rs::{
prelude::typenum::{U0, U1, U2},
prelude::typenum::{U1, U2},
runtime::{
secret::{RTExposedSecret, RTSecret},
traits::RTExposeSecret,
Expand Down

0 comments on commit 3510e33

Please sign in to comment.