Przeglądaj źródła

sdk/util: rename Fp.to_str() to Fp.to_string(), and encode/deocde hex strings with 0x prefix.

zero 2 lat temu
rodzic
commit
61661052ce
2 zmienionych plików z 13 dodań i 14 usunięć
  1. 7 8
      src/sdk/src/crypto/util.rs
  2. 6 6
      src/zk/debug.rs

+ 7 - 8
src/sdk/src/crypto/util.rs

@@ -89,8 +89,8 @@ pub fn fp_to_u64(value: pallas::Base) -> Option<u64> {
 
 // Not allowed to implement external traits for external crates
 pub trait FieldElemAsStr: PrimeField<Repr = [u8; 32]> {
-    fn to_str(&self) -> String {
-        let mut repr = String::new();
+    fn to_string(&self) -> String {
+        let mut repr = "0x".to_string();
         for &b in self.to_repr().iter().rev() {
             repr += &format!("{:02x}", b);
         }
@@ -98,18 +98,17 @@ pub trait FieldElemAsStr: PrimeField<Repr = [u8; 32]> {
     }
 
     fn from_str(hex: &str) -> GenericResult<Self> {
-        if hex.len() != 32 * 2 {
+        if hex.len() != 33 * 2 {
             return Err(ContractError::HexFmtErr)
         }
 
+        let hex = hex.strip_prefix("0x").ok_or(ContractError::HexFmtErr)?;
+
         let mut bytes = [0u8; 32];
         for i in 0..32 {
             // Bytes are little endian but str repr is big endian
-            bytes[32 - i - 1] = if let Ok(byte) = u8::from_str_radix(&hex[2 * i..2 * i + 2], 16) {
-                byte
-            } else {
-                return Err(ContractError::HexFmtErr)
-            };
+            bytes[32 - i - 1] = u8::from_str_radix(&hex[2 * i..2 * i + 2], 16)
+                .map_err(|_| ContractError::HexFmtErr)?;
         }
 
         let value = Self::from_repr(bytes);

+ 6 - 6
src/zk/debug.rs

@@ -48,13 +48,13 @@ pub fn export_witness_json<P: AsRef<Path>>(
         match witness {
             Witness::Base(value) => {
                 value.map(|w| {
-                    value_json.insert("Base".to_string(), JsonStr(w.to_str()));
+                    value_json.insert("Base".to_string(), JsonStr(w.to_string()));
                     w
                 });
             }
             Witness::Scalar(value) => {
                 value.map(|w| {
-                    value_json.insert("Scalar".to_string(), JsonStr(w.to_str()));
+                    value_json.insert("Scalar".to_string(), JsonStr(w.to_string()));
                     w
                 });
             }
@@ -68,7 +68,7 @@ pub fn export_witness_json<P: AsRef<Path>>(
                 let mut path = Vec::new();
                 value.map(|w| {
                     for node in w {
-                        path.push(JsonStr(node.inner().to_str()));
+                        path.push(JsonStr(node.inner().to_string()));
                     }
                     w
                 });
@@ -78,7 +78,7 @@ pub fn export_witness_json<P: AsRef<Path>>(
                 let mut path = Vec::new();
                 value.map(|w| {
                     for node in w {
-                        path.push(JsonStr(node.to_str()));
+                        path.push(JsonStr(node.to_string()));
                     }
                     w
                 });
@@ -91,7 +91,7 @@ pub fn export_witness_json<P: AsRef<Path>>(
                     (x, y) = (*coords.x(), *coords.y());
                     w
                 });
-                let coords = vec![JsonStr(x.to_str()), JsonStr(y.to_str())];
+                let coords = vec![JsonStr(x.to_string()), JsonStr(y.to_string())];
                 value_json.insert("EcNiPoint".to_string(), JsonArray(coords));
             }
             _ => unimplemented!(),
@@ -101,7 +101,7 @@ pub fn export_witness_json<P: AsRef<Path>>(
 
     let mut instances = Vec::new();
     for instance in public_inputs {
-        instances.push(JsonStr(instance.to_str()));
+        instances.push(JsonStr(instance.to_string()));
     }
 
     let witnesses_json = JsonArray(witnesses);