Skip to content

Commit

Permalink
chore: remove inhouse base64 impl
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVikasRushi committed Oct 14, 2024
1 parent 6ea59a2 commit 29368f2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sha2 = { version = "0.10", default-features = false }
p256 = "0.13.2"
hex = "0.4.3"
serde = { version = "1.0",features = ["derive"] }
base64 = "0.22.1"

[patch.crates-io]
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-v0.10.8" }
44 changes: 7 additions & 37 deletions lib/src/sxg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
test_cases::{DATA_TO_VERIFY, FINAL_PAYLOAD, PAYLOAD},
verify_ecdsa_p256_r_s,
};
use base64::Engine;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -49,41 +50,6 @@ fn calculate_integrity(input: &[u8], record_size: usize) -> [u8; 32] {
proofs[0]
}

fn base64_encode_mice(input: &[u8]) -> String {
let base64_chars: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut result = String::from("mi-sha256-03=");
let mut i = 0;

while i < input.len() {
let n = if i + 3 <= input.len() {
((input[i] as u32) << 16) | ((input[i + 1] as u32) << 8) | (input[i + 2] as u32)
} else if i + 2 == input.len() {
((input[i] as u32) << 16) | ((input[i + 1] as u32) << 8)
} else {
(input[i] as u32) << 16
};

result.push(base64_chars[((n >> 18) & 63) as usize] as char);
result.push(base64_chars[((n >> 12) & 63) as usize] as char);

if i + 1 < input.len() {
result.push(base64_chars[((n >> 6) & 63) as usize] as char);
} else {
result.push('=');
}

if i + 2 < input.len() {
result.push(base64_chars[(n & 63) as usize] as char);
} else {
result.push('=');
}

i += 3;
}

result
}

impl SXGInput {
pub fn verify(&self) -> Result<bool, Box<dyn std::error::Error>> {
if self.payload[self.data_to_verify_start_index
Expand All @@ -93,8 +59,12 @@ impl SXGInput {
return Ok(false);
}

let mice = base64_encode_mice(&calculate_integrity(&self.payload, 16384));
let mice_bytes = mice.as_bytes();
let prefix = (b"mi-sha256-03=").to_vec();
let payload = calculate_integrity(&self.payload, 16384).to_vec();

let mice_payload = base64::prelude::BASE64_STANDARD.encode(payload);
let mice = mice_payload.as_bytes();
let mice_bytes = [prefix, mice.to_vec()].concat();

if self.final_payload
[self.integrity_start_index..self.integrity_start_index + mice_bytes.len()]
Expand Down

0 comments on commit 29368f2

Please sign in to comment.