From 33f3de3b681d3011be40460e57675949e496db5a Mon Sep 17 00:00:00 2001 From: Ian Goldberg Date: Sat, 1 May 2021 15:33:45 -0400 Subject: [PATCH] Dates should just be u32, not u64 --- crates/lox-library/src/lib.rs | 13 ++++++++++++- crates/lox-library/src/proto/trust_promotion.rs | 8 ++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/lox-library/src/lib.rs b/crates/lox-library/src/lib.rs index 2b481c9..8ada29e 100644 --- a/crates/lox-library/src/lib.rs +++ b/crates/lox-library/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 { 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 { + // 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 diff --git a/crates/lox-library/src/proto/trust_promotion.rs b/crates/lox-library/src/proto/trust_promotion.rs index b84a80b..6c4ddac 100644 --- a/crates/lox-library/src/proto/trust_promotion.rs +++ b/crates/lox-library/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), };