starknet-rs icon indicating copy to clipboard operation
starknet-rs copied to clipboard

`compute_hash_on_elements` could accept any iterator on `FieldElement`

Open tdelabro opened this issue 1 year ago • 3 comments

https://github.com/xJonathanLEI/starknet-rs/blob/d980869d44ef86249b4077f4ec809165f470678d/starknet-core/src/crypto.rs#L49

Right now the signature requires the input to be &[FieldElement] but it's implementation would still be valid if the input were to be a more generic T: Iter<FieldElement>. It would allow more flexibility for the lib users

tdelabro avatar Mar 25 '24 15:03 tdelabro

The issue with Iterator<FieldElement> is then you can't use say slices they yield &FieldElement instead of FieldELement.

xJonathanLEI avatar Mar 27 '24 03:03 xJonathanLEI

What we could do is maybe T: AsRef<[FieldElement]>? This way you can send in anything that can be referenced as a slice, but then... if you can reference it as a slice you could have just passed in the slice like you do right now.

The only benefit of this change, as far as I know, is that you can now also pass in owned values, which could be useful in a threading scenario. Not that it's not useful in certain cases, but it's probably not what you're looking for.

xJonathanLEI avatar Mar 27 '24 04:03 xJonathanLEI

Oh the other hand, we can indeed provide a hasher type which would actually work better with iterators? Such a hasher would probably be rather generic and won't hash the arrary length at the end though.

xJonathanLEI avatar Mar 27 '24 04:03 xJonathanLEI

https://rust-lang.github.io/api-guidelines/flexibility.html#functions-minimize-assumptions-about-parameters-by-using-generics-c-generic

T: IntoIterator<Item = FieldElement> requires you to have ownership of the value, if you want to pass a slice, you can do so:

compute_hash_on_elements(my_slice.into_iter().cloned())

This ensures you own the value you are passing in.

tdelabro avatar Apr 18 '24 15:04 tdelabro