generated from succinctlabs/sp1-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdde55a
commit 11e7b72
Showing
8 changed files
with
880 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
use crate::{sha256_hash, verify_ecdsa_p256_r_s}; | ||
|
||
pub struct SXGInput { | ||
pub final_payload: Vec<u8>, | ||
pub data_to_verify: Vec<u8>, | ||
pub data_to_verify_start_index: usize, | ||
pub integrity_start_index: usize, | ||
pub payload: Vec<u8>, | ||
pub r: [u8; 32], | ||
pub s: [u8; 32], | ||
pub px: [u8; 32], | ||
pub py: [u8; 32], | ||
} | ||
|
||
fn calculate_integrity(input: &[u8], record_size: usize) -> [u8; 32] { | ||
if input.is_empty() { | ||
return sha256_hash(&[]); | ||
} | ||
|
||
let actual_record_size = record_size.min(input.len()); | ||
let mut records: Vec<&[u8]> = Vec::new(); | ||
let mut i = 0; | ||
|
||
while i < input.len() { | ||
let chunk_size = (i + actual_record_size).min(input.len()) - i; | ||
records.push(&input[i..i + chunk_size]); | ||
i += actual_record_size; | ||
} | ||
|
||
let mut proofs: Vec<[u8; 32]> = Vec::new(); | ||
for record in records.into_iter().rev() { | ||
let mut to_hash = Vec::from(record); | ||
if !proofs.is_empty() { | ||
to_hash.extend_from_slice(&proofs[0]); | ||
to_hash.push(1); | ||
} else { | ||
to_hash.push(0); | ||
} | ||
let hash_result = sha256_hash(&to_hash); | ||
proofs.insert(0, hash_result); | ||
} | ||
|
||
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 | ||
} | ||
|
||
pub fn sxg_verify(input: SXGInput) -> Result<bool, Box<dyn std::error::Error>> { | ||
let SXGInput { | ||
final_payload, | ||
data_to_verify, | ||
data_to_verify_start_index, | ||
integrity_start_index, | ||
payload, | ||
r, | ||
s, | ||
px, | ||
py, | ||
} = input; | ||
|
||
if payload[data_to_verify_start_index..data_to_verify_start_index + data_to_verify.len()] | ||
!= data_to_verify | ||
{ | ||
return Ok(false); | ||
} | ||
|
||
let mice = base64_encode_mice(&calculate_integrity(&payload, 16384)); | ||
let mice_bytes = mice.as_bytes(); | ||
|
||
if final_payload[integrity_start_index..integrity_start_index + mice_bytes.len()] | ||
!= mice_bytes[..] | ||
{ | ||
return Ok(false); | ||
} | ||
|
||
Ok(verify_ecdsa_p256_r_s(&final_payload, &r, &s, &px, &py).is_ok()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::constants::{DATA_TO_VERIFY, FINAL_PAYLOAD, PAYLOAD}; | ||
|
||
#[test] | ||
fn test_sxg() { | ||
let final_payload = FINAL_PAYLOAD; | ||
let data_to_verify = DATA_TO_VERIFY; | ||
let payload = PAYLOAD; | ||
|
||
let data_to_verify_start_index = 0; | ||
let integrity_start_index = 694 / 2; | ||
|
||
let px = "45E3943B0705F9EF69B53A4EFB8C668E6A9F90124E9BCF917662CFADEA56C0C1"; | ||
let py = "F3703834F92F6FE70A004BA4098D079BFB5F927E042991EFD5A1572E8F9D39D6"; | ||
|
||
let r = "9970818CBCA38C196795EEAD295BDED48311702DF7DDB0C2BB448276894C393D"; | ||
let s = "729B2F9229D545A553F0F7CBC1792E9A6185E539DBF667FE5BC38D673D90C014"; | ||
|
||
let r = hex::decode(r).unwrap(); | ||
let s = hex::decode(s).unwrap(); | ||
|
||
let px = hex::decode(px).unwrap(); | ||
let py = hex::decode(py).unwrap(); | ||
|
||
let input = super::SXGInput { | ||
final_payload: final_payload.to_vec(), | ||
data_to_verify: data_to_verify.to_vec(), | ||
data_to_verify_start_index, | ||
integrity_start_index, | ||
payload: payload.to_vec(), | ||
r: r.try_into().unwrap(), | ||
s: s.try_into().unwrap(), | ||
px: px.try_into().unwrap(), | ||
py: py.try_into().unwrap(), | ||
}; | ||
|
||
assert!(super::sxg_verify(input).unwrap()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters