XRPLF / xrpl.js

A JavaScript/TypeScript API for interacting with the XRP Ledger in Node.js and the browser
https://xrpl.org/
1.2k stars 510 forks source link

ripple-iso-crypto #2306

Open sublimator opened 1 year ago

sublimator commented 1 year ago

Related to #2272 and #2273

ripple-iso-crypto/sha512 ripple-iso-crypto/sha256 ripple-iso-crypto/ripemd160 ripple-iso-crypto/utils (randomBytes, hexToBytes, bytesToHex)

Easily tree shakeable, pay for only what you use

TODO: more details/justification

sublimator commented 1 year ago

Maybe even put an ECC Sub Group implementation ? Enough to do key generation etc ? What is needed ? Point addition ? Scalar multiplication ?

Along these lines:

type HexString = string
type Bytes = HexString | Uint8Array 

interface Point {
    multiply(scalar: bigint): Point;
    add(other: Point): Point;
    encodeCanonical(): Uint8Array;
}

interface Group {
    Generator: Point;
    order: bigint
    decodePoint(bytes: Bytes): Point;
}

EDIT:

What is needed ? Point addition ? Scalar multiplication ?

Basically, the following:

interface Ed25519 {
  getPublicKey: (bytes: Uint8Array) => Uint8Array
  sign: (message: Uint8Array, privateKey: Uint8Array) => Uint8Array
  verify: (
    signature: Uint8Array,
    message: Uint8Array,
    publicKey: Uint8Array,
  ) => boolean
}

interface Point {
  // scalar multiplication
  multiply: (s: Scalar) => Point
  // point addition
  add: (elt: Point) => Point
  // In compressed form
  encode: () => Uint8Array
}

export interface Scalar {
  add: (s: Scalar) => Scalar
  // Can fold gt/lt into a gtZeroAndLtOrder (e.g. secp256k1_ec_seckey_verify)
  gt: (s: Scalar) => boolean
  lt: (s: Scalar) => boolean
  // as per % operator
  // If you made Scalar automatically % order transparently could get rid of mod
  mod: (s: Scalar) => Scalar
}

interface Secp256k1 {
  decodePoint: (bytes: Uint8Array) => Point
  decodeScalar: (bytes: Uint8Array) => Scalar
  Base: Point
  Scalars: {
    Zero: Scalar
  }
  Curve: {
    // See notes re: order near Scalar fields
    order: Scalar
  }
  sign: (messageHash: Uint8Array, privateKey: Scalar | Uint8Array) => Uint8Array
  verify: (
    sig: Uint8Array,
    messageHash: Uint8Array,
    publicKey: Uint8Array | Point,
  ) => boolean
}
ckniffen commented 1 year ago

This will be done in #2273 with the following entry points: