Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: invalid type: string \"FROST-secp256k1-SHA256-v1\", expected a borrowed string #588

Closed
mimoo opened this issue Dec 1, 2023 · 5 comments · Fixed by #589
Closed

bug: invalid type: string \"FROST-secp256k1-SHA256-v1\", expected a borrowed string #588

mimoo opened this issue Dec 1, 2023 · 5 comments · Fixed by #589

Comments

@mimoo
Copy link
Contributor

mimoo commented Dec 1, 2023

I'm getting the following error:

error while reading file: Error("invalid type: string \"FROST-secp256k1-SHA256-v1\", expected a borrowed string", line: 4, column: 46)

after serializing and deserializing a frost::keys::KeyPackage with json_serde.

@mimoo
Copy link
Contributor Author

mimoo commented Dec 1, 2023

I think the bug is in src/serialization.rs of frost-core:

/// Deserialize a placeholder ciphersuite field, checking if it's the ciphersuite ID string.
#[cfg(feature = "serde")]
pub(crate) fn ciphersuite_deserialize<'de, D, C>(deserializer: D) -> Result<(), D::Error>
where
    D: serde::Deserializer<'de>,
    C: Ciphersuite,
{
    if deserializer.is_human_readable() {
-        let s: &str = serde::de::Deserialize::deserialize(deserializer)?;
+        let s: String = serde::de::Deserialize::deserialize(deserializer)?;
        if s != C::ID {
            Err(serde::de::Error::custom("wrong ciphersuite"))
        } else {
            Ok(())
        }
    } else {
        let buffer: [u8; 4] = serde::de::Deserialize::deserialize(deserializer)?;
        if buffer != short_id::<C>() {
            Err(serde::de::Error::custom("wrong ciphersuite"))
        } else {
            Ok(())
        }
    }
}

@conradoplg
Copy link
Contributor

Interesting. The fix seems correct but I can't come up with a test case that shows the issue. I'd like to add a test to exercise it. Can you share how are you serializing/deserializing it?

@mergify mergify bot closed this as completed in #589 Dec 4, 2023
@github-project-automation github-project-automation bot moved this to Done in FROST Dec 4, 2023
@mimoo
Copy link
Contributor Author

mimoo commented Dec 4, 2023

are you using serde_json in your test? I just use serde_json::{to_string, from_str}()

@conradoplg
Copy link
Contributor

are you using serde_json in your test? I just use serde_json::{to_string, from_str}()

Yes. This test passes:

#[test]
fn check_key_package_serialization2() {
    let json = r#"{
        "header": {
          "version": 0,
          "ciphersuite": "FROST-secp256k1-SHA256-v1"
        },
        "identifier": "000000000000000000000000000000000000000000000000000000000000002a",
        "signing_share": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9d1c9e899ca306ad27fe1945de0242b81",
        "verifying_share": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
        "verifying_key": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
        "min_signers": 2
      }"#;
    let key_package: KeyPackage = serde_json::from_str(json).unwrap();
    let json: String = serde_json::to_string(&key_package).unwrap();
    println!("{}", json);
    let _key_package: KeyPackage = serde_json::from_str(&json).unwrap();
}

I think it fails when it can't get a reference to the ciphersuite substring inside the JSON string, which usually happens if the string has some escape code, which isn't the case.

@mimoo
Copy link
Contributor Author

mimoo commented Dec 5, 2023

I was doing something similar to this but reading from a file instead. Not sure why your test pass when I couldn't do that on my end :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants