0xPolygonID / polygonid-flutter-sdk

Flutter Plugin to use the Polygon ID SDK
Apache License 2.0
40 stars 27 forks source link

The Rust code for BabyJubJub and the prebuilt library in the Flutter project do not align perfectly #351

Closed yushihang closed 9 months ago

yushihang commented 10 months ago

https://github.com/0xPolygonID/polygonid-flutter-sdk/blob/15750e3093eee2b16513090241050110d9223292/rust/src/lib.rs#L211C58-L211C58

  1. unwrap() in the rust codes lacks a check for whether it is empty. When incorrect parameters are passed, it may result in a panic and cause the entire mobile app process to exit.

  2. The following functions are not found in the Rust code, but they exist in the prebuilt iOS/Android libraries provided by the Flutter project. Moreover, the Dart FFI-related code calls these functions that are absent in the Rust code.

    char *poseidon_hash2(const char *in1, const char *in2);

   char *poseidon_hash3(const char *in1, const char *in2, const char *in3);

   char *poseidon_hash4(const char *in1, const char *in2, const char *in3, const char *in4);
  1. poseidon_hash() in rust codes returns a string value in format 'Fr(0x29176100eaa962bdc1fe6c654d6a3c130e96a4d1168b33848b897dc502820133)' after compilation and execution.

However, when the same function is run in the Flutter project using the prebuilt library, the result is a decimal format string, like '18586133768512220936620570745912940619677854269274689475585506675881198879027'.

(The BigInt values represented by both formats are equivalent)."

#[no_mangle]
pub /*extern*/ fn poseidon_hash(input: *const c_char) -> *mut c_char {

    let input_str = unsafe { CStr::from_ptr(input) }.to_str().unwrap();
    let b0: Fr = Fr::from_str(input_str).unwrap();

    let hm_input = vec![b0.clone()];
    //let hm_input = vec![x.clone(), y.clone(), z.clone()];
    let poseidon = Poseidon::new();
    let hm = poseidon.hash(hm_input).unwrap();
    //hm.to_string: Fr(0x29176100eaa962bdc1fe6c654d6a3c130e96a4d1168b33848b897dc502820133)
    return CString::new(to_hex(&hm).as_str()).unwrap().into_raw();
    //return CString::new(hm.to_string()).unwrap().into_raw();
}
plusema86 commented 9 months ago

hi @yushihang , what you say is correct but what you see in the Flutter repo is a legacy code (outdated) that needs to be removed, as you said the Rust code is called directly from the iOS/Android prebuilt libraries. This is the repo that should be taken as reference https://github.com/arnaucube/babyjubjub-rs

yushihang commented 9 months ago

hi @yushihang , what you say is correct but what you see in the Flutter repo is a legacy code (outdated) that needs to be removed, as you said the Rust code is called directly from the iOS/Android prebuilt libraries. This is the repo that should be taken as reference https://github.com/arnaucube/babyjubjub-rs

Hi @plusema86 ,

Thank you for your response.

We are looking to compile a library that supports the iOS simulator. The prebuilt iOS library currently does not offer support for the iOS simulator, particularly for the arm64 architecture on Apple Silicon.

I have previously examined the code at https://github.com/arnaucube/babyjubjub-rs, and there are differences compared to the code in the prebuilt library.

Is it possible for us to obtain the latest Flutter SDK source code, including the Rust code for babyjubjub that matches the prebuilt library?