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

Compact form EIP-2098 signature recovery #1185

Closed
Braqzen opened this issue Aug 14, 2023 · 2 comments
Closed

Compact form EIP-2098 signature recovery #1185

Braqzen opened this issue Aug 14, 2023 · 2 comments
Labels
feat Issue is a feature

Comments

@Braqzen
Copy link

Braqzen commented Aug 14, 2023

Motivation

In Ethers-rs (and I assume it's also in Ethers-js but I have not gotten there yet), when a message is signed the return type is a Signature which when converted into a vector consists of 65 bytes that represent the r, s and v values.

The issue is that Sway uses 64 bytes.
There is a function that can be implemented to transform the representation here (to_compact()).

This is not the final representation because it's not a single vector of bytes but we can have that function and perhaps another which takes these two values and outputs a vector.

Possible implementations

This is an implementation I have written in Rust inspired from Ethers-rs. I have not tested this to prove that it works but I think it might. It may be taken as an example from Rust and ported to Ts (and also added into the Rust SDK).

fn compact(signature: &Signature) -> [u8; 64] {
    let shifted_parity = U256::from(signature.v - 27) << 255;

    let r = signature.r;
    let yParityAndS = shifted_parity | signature.s;

    let mut sig = [0u8; 64];
    let mut r_bytes = [0u8; 32];
    let mut s_bytes = [0u8; 32];
    r.to_big_endian(&mut r_bytes);
    yParityAndS.to_big_endian(&mut s_bytes);
    sig[..32].copy_from_slice(&r_bytes);
    sig[32..64].copy_from_slice(&s_bytes);
    return sig;
}

I haven't cleaned this up at all so there may be some canonical way of implementing this.

@Braqzen
Copy link
Author

Braqzen commented Aug 18, 2023

Potentially close this issue because of this comment

@camsjams
Copy link
Contributor

See: FuelLabs/fuel-vm#551

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

No branches or pull requests

2 participants