diff --git a/Cargo.lock b/Cargo.lock index 8781d32..2426d5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2569,6 +2569,7 @@ name = "lib" version = "0.1.0" dependencies = [ "alloy-sol-types", + "base64 0.22.1", "hex", "p256", "serde", diff --git a/elf/riscv32im-succinct-zkvm-elf b/elf/riscv32im-succinct-zkvm-elf index c174359..1f2fee9 100755 Binary files a/elf/riscv32im-succinct-zkvm-elf and b/elf/riscv32im-succinct-zkvm-elf differ diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 906432d..6b9b176 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -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" } diff --git a/lib/src/sxg.rs b/lib/src/sxg.rs index 5199939..318b19a 100644 --- a/lib/src/sxg.rs +++ b/lib/src/sxg.rs @@ -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)] @@ -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> { if self.payload[self.data_to_verify_start_index @@ -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()]