Skip to content

Alternative for crypto_core_ed25519_scalar_invert #163

Answered by paulmillr
li0ard asked this question in Q&A
Discussion options

You must be logged in to vote

25519 field is little-endian, a bit painful

So, the scalars you're seeing are actually swapped:

import { ed25519 } from '@noble/curves/ed25519';
import { Field } from '@noble/curves/abstract/modular';

// Convert bigint to hex, pad with up to 64 zeros (32-byte)
function toHex(scalar) {
  return scalar.toString(16).padStart(64, '0');
}
// Swap big endian to little endian
// 0xbeef => 0xefbe
function swap_endianness_BE_LE(scalar) {
  const swapped = toHex(scalar).match(/\w{2}/g).reverse().join('');
  return BigInt('0x' + swapped);
}

const scalar = swap_endianness_BE_LE(0x7866d7873b9ca2ffe35ba0e05321f3786da8e3c9049bb6821ee1463a32304509n);
const Fn = Field(ed25519.CURVE.n);
const inverted = Fn.

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@li0ard
Comment options

@paulmillr
Comment options

@li0ard
Comment options

@paulmillr
Comment options

Answer selected by li0ard
@li0ard
Comment options

@paulmillr
Comment options

@li0ard
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants