Help with using Signature::from_scalars method in crate ecdsa

Hi,
I am trying to use the crate ecdsa - Rust for verifying ecdsa signatures. Specifically I am trying to load a signature using the below method,
ecdsa::Signature - Rust
Tried two methods to load some test data,

    let buf1: [u8; 16] = [0x00, 0x42, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0xc3, 0x00, 0x00, 0x30, 0x00, 0xc3, 0x00, 0x00];
    let buf2: [u8; 16] = [0x00, 0x42, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0xc3, 0x00, 0x00, 0x30, 0x00, 0xc3, 0x00, 0x00];
    let test_signature = Signature::from_scalars(buf1, buf2).unwrap();
    let bytes2: GenericArray<u8, U16> = GenericArray::clone_from_slice(&buf2[0..16]);
    let test_signature = Signature::from_scalars(bytes1, bytes2).unwrap();```

Both cases I get error:
the trait bound ```GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>>: From<[u8; 16]>``` is not satisfied
The implementation of the method looks like below, but I am finding it a bit difficult to understand how to pass r and s parameters to the method.

    pub fn from_scalars(
        r: impl Into<FieldBytes<C>>,
        s: impl Into<FieldBytes<C>>,
    ) -> Result<Self, Error> {
        Self::try_from(r.into().concat(s.into()).as_slice())
    }

Any guidance will be of great help and much appreciated.

Please format your code properly using three-backticks. (``` <Code goes here> ```)

It looks like you're using a 256-bit elliptic curve. You'll need to use 32-byte arrays for both of the scalar values.

Thank you. That solved the compilation issue in the first version of Signature::from_scalars usage. I am just wondering was there any clue in the compilation error that helped to spot the issue?

the trait bound GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>>: From<[u8; 16]> is not satisfied

These crates all use the generic-array crate, a trick for working with arrays of generic size before const generics were stabilized in Rust 1.51.0. The size of the array is encoded in the UInt chain as a binary number: each B1 represents a 1 bit, and each B0 represents a 0 bit. Thus, the error message is saying that Signature::from_scalars() requires a GenericArray of size 0b100000, or 32, and that it cannot convert a [u8; 16] into that. Since the From impls for GenericArray require an array of matching size, it makes sense that a [u8; 32] should work instead.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.