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

Add endian argument to toString method #272

Open
mahnunchik opened this issue Feb 26, 2021 · 0 comments
Open

Add endian argument to toString method #272

mahnunchik opened this issue Feb 26, 2021 · 0 comments

Comments

@mahnunchik
Copy link

Feature request: add endian (le or be) argument to toString method.

Motivation: working with ed25519 curve from elliptic module there is an inconvenience.

ed25519 uses numbers in little endian order:

const EdDSA = elliptic.eddsa;
const ec = new EdDSA('ed25519');

// hex string in little endian order
const input = '70ee7dd55b7ae8922ed345adc0f5fcc6ccb93c673f38ca7900186efb277337ff';

const num = ec.decodeInt(input);
// utils.intFromLE(bytes)
// new BN(bytes, 'hex', 'le');
// used under hood

// make reduce by n
const reduced = num.umod(ec.curve.n);

There is no simple way to convert reduced number to hex string in little endian order.

// option 1
const output = ec.encodeInt(reduced);
// produces array of numbers in little endian

// option 2
const output = reduced.toString('hex');
// produces hex string but in big endian order

Current solution

const output = reduced.toBuffer('le', 32).toString('hex');

Yep, it works, but it is a bit wired.

Additional context

It would be consistent to have endian argument across all to* method: a.toArray(endian, length), a.toArrayLike(type, endian, length), a.toBuffer(endian, length).

I think it is better to have a look at this issue in case of different bytes order: Wrong padding in toString() function #198

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

No branches or pull requests

1 participant