Browse Source

Dates should just be u32, not u64

Ian Goldberg 3 years ago
parent
commit
8f489c81ff
2 changed files with 16 additions and 5 deletions
  1. 12 1
      src/lib.rs
  2. 4 4
      src/proto/trust_promotion.rs

+ 12 - 1
src/lib.rs

@@ -249,8 +249,9 @@ impl BridgeAuth {
     }
 
     /// Get today's (real or simulated) date
-    fn today(&self) -> u64 {
+    fn today(&self) -> u32 {
         // We will not encounter negative Julian dates (~6700 years ago)
+        // or ones larger than 32 bits
         (time::OffsetDateTime::now_utc().date() + self.time_offset)
             .julian_day()
             .try_into()
@@ -308,6 +309,16 @@ pub fn scalar_u64(s: &Scalar) -> Option<u64> {
     Some(u64::from_le_bytes(sbytes[..8].try_into().unwrap()))
 }
 
+/// Try to extract a u32 from a Scalar
+pub fn scalar_u32(s: &Scalar) -> Option<u32> {
+    // Check that the top 28 bytes of the Scalar are 0
+    let sbytes = s.as_bytes();
+    if sbytes[4..].ct_eq(&[0u8; 28]).unwrap_u8() == 0 {
+        return None;
+    }
+    Some(u32::from_le_bytes(sbytes[..4].try_into().unwrap()))
+}
+
 /// Double a Scalar
 pub fn scalar_dbl(s: &Scalar) -> Scalar {
     s + s

+ 4 - 4
src/proto/trust_promotion.rs

@@ -41,7 +41,7 @@ use std::collections::HashMap;
 use super::super::cred;
 use super::super::dup_filter::SeenType;
 use super::super::migration_table;
-use super::super::{pt_dbl, scalar_dbl, scalar_u64};
+use super::super::{pt_dbl, scalar_dbl, scalar_u32};
 use super::super::{BridgeAuth, IssuerPubKey};
 use super::super::{CMZ_A, CMZ_A_TABLE, CMZ_B, CMZ_B_TABLE};
 
@@ -52,7 +52,7 @@ use super::super::{CMZ_A, CMZ_A_TABLE, CMZ_B, CMZ_B_TABLE};
 /// The implementation also puts an upper bound of UNTRUSTED_INTERVAL +
 /// 511 days, which is not unreasonable; we want users to be engaging
 /// with the system in order to move up trust levels.
-pub const UNTRUSTED_INTERVAL: u64 = 30;
+pub const UNTRUSTED_INTERVAL: u32 = 30;
 
 pub struct Request {
     // Fields for blind showing the Lox credential
@@ -157,7 +157,7 @@ define_proof! {
 pub fn request(
     lox_cred: &cred::Lox,
     lox_pub: &IssuerPubKey,
-    today: u64,
+    today: u32,
 ) -> Result<(Request, State), ProofError> {
     let A: &RistrettoPoint = &CMZ_A;
     let B: &RistrettoPoint = &CMZ_B;
@@ -166,7 +166,7 @@ pub fn request(
 
     // Ensure the credential can be correctly shown: it must be the case
     // that level_since + UNTRUSTED_INTERVAL <= today.
-    let level_since: u64 = match scalar_u64(&lox_cred.level_since) {
+    let level_since: u32 = match scalar_u32(&lox_cred.level_since) {
         Some(v) => v,
         None => return Err(ProofError::VerificationFailure),
     };