From 86ebd3019a45ed17b5249827b3858d73657688f1 Mon Sep 17 00:00:00 2001 From: "Jason.Huang" Date: Wed, 12 Jun 2024 12:16:13 +0800 Subject: [PATCH] ecgfp5: change implementation of from/to hex string --- ecgfp5/src/curve/curve.rs | 41 ++++---------------------------- ecgfp5/src/curve/scalar_field.rs | 40 ++++--------------------------- 2 files changed, 10 insertions(+), 71 deletions(-) diff --git a/ecgfp5/src/curve/curve.rs b/ecgfp5/src/curve/curve.rs index 7b1f6cca0f..5631b6e4f0 100644 --- a/ecgfp5/src/curve/curve.rs +++ b/ecgfp5/src/curve/curve.rs @@ -288,43 +288,12 @@ impl Point { } pub fn to_hex_string(&self) -> String { - let mut buf: [u8; 5 * 8] = [0; 40]; - let dst_ptr = buf.as_mut_ptr(); - - let mut offset = 0; - - let encode = Point::encode(*self); - for e in encode.0 { - let bytes = e.to_canonical_u64().to_le_bytes(); - unsafe { - std::ptr::copy_nonoverlapping(bytes.as_ptr(), dst_ptr.add(offset), 8); - } - offset = offset + 8; - } - - let hex_string = hex::encode(&buf); - hex_string + hex::encode(self.to_le_bytes()) } - pub fn from_hex_string(input_hex_string: &str) -> Self { - let buf: Vec = hex::decode(input_hex_string).unwrap(); - let mut data: [GoldilocksField; 5] = [GoldilocksField::ZERO; 5]; - - let src_ptr = buf.as_ptr(); - let mut offset = 0; - for ele in data.iter_mut() { - unsafe { - let mut v_buf: [u8; 8] = [0; 8]; - std::ptr::copy_nonoverlapping(src_ptr.add(offset), v_buf.as_mut_ptr(), 8); - let v: u64 = u64::from_le_bytes(v_buf); - *ele = GoldilocksField::from_canonical_u64(v); - } - offset = offset + 8; - } - - let quintic = QuinticExtension::(data); - let decoded = Self::decode(quintic).unwrap(); - decoded + pub fn from_hex_string(input_hex_string: &str) -> Option { + let vec: Vec = hex::decode(input_hex_string).ok()?; + Self::from_le_bytes(vec.try_into().ok()?) } // General point addition. formulas are complete (no special case). @@ -1745,7 +1714,7 @@ mod tests { let p1 = Point::decode(w1).expect("w1 should successfully decode"); let hex_str = p1.to_hex_string(); - let recoverred = Point::from_hex_string(&hex_str); + let recoverred = Point::from_hex_string(&hex_str).unwrap(); assert_eq!(p1, recoverred); } diff --git a/ecgfp5/src/curve/scalar_field.rs b/ecgfp5/src/curve/scalar_field.rs index d5567f758d..344695f891 100644 --- a/ecgfp5/src/curve/scalar_field.rs +++ b/ecgfp5/src/curve/scalar_field.rs @@ -646,42 +646,12 @@ impl Scalar { } pub fn to_hex_string(&self) -> String { - let u64_array = self.0; - - let mut buf: [u8; 40] = [0; 40]; - let dst_ptr = buf.as_mut_ptr(); - - let mut offset = 0; - for e in u64_array { - let bytes = e.to_le_bytes(); - unsafe { - let src_ptr = bytes.as_ptr(); - std::ptr::copy_nonoverlapping(src_ptr, dst_ptr.add(offset), 8); - offset = offset + 8; - } - } - - let hex_string = hex::encode(&buf); - hex_string + hex::encode(self.encode()) } - pub fn from_hex_string(input_hex_string: &str) -> Self { - let buf: Vec = hex::decode(input_hex_string).unwrap(); - let mut data: [u64; 5] = [0; 5]; - - let src_ptr = buf.as_ptr(); - let mut offset = 0; - for ele in data.iter_mut() { - unsafe { - let mut v_buf: [u8; 8] = [0; 8]; - std::ptr::copy_nonoverlapping(src_ptr.add(offset), v_buf.as_mut_ptr(), 8); - let v: u64 = u64::from_le_bytes(v_buf); - *ele = v; - } - offset = offset + 8; - } - - Self(data) + pub fn from_hex_string(input_hex_string: &str) -> Option { + let buf: Vec = hex::decode(input_hex_string).ok()?; + Self::from_canonical_bytes(buf.try_into().ok()?) } } @@ -1144,7 +1114,7 @@ mod tests { let s4 = Scalar::from_noncanonical_bytes(&buf4[..]); let hex_str = s4.to_hex_string(); - let recoverred = Scalar::from_hex_string(&hex_str); + let recoverred = Scalar::from_hex_string(&hex_str).unwrap(); assert_eq!(s4, recoverred); }