indutny / elliptic

Fast Elliptic Curve Cryptography in plain javascript
1.66k stars 359 forks source link

Update bn.js to 5.2.1 and fix test #304

Closed agalatan closed 4 months ago

agalatan commented 1 year ago

In bn.js@4.11.9, the string equals hello, and the function below executes with no error. Moreover, the test that i changed passes, due to pure luck.

// HELLO! string equals 'hello' when we test in "elliptic"
function parseHex4Bits (string, index) {
    var c = string.charCodeAt(index);
    // 'A' - 'F'
    if (c >= 65 && c <= 70) {
      return c - 55;
    // 'a' - 'f'
    } else if (c >= 97 && c <= 102) {
      return c - 87;
    // '0' - '9'
    } else {
      return (c - 48) & 0xf;
    }
  }

In bn.js@5.2.1, the string equals hello, and the assertion correctly throws

// HELLO! string equals 'hello' when we test in "elliptic"
function parseHex4Bits (string, index) {
    // string === 'hello'
    var c = string.charCodeAt(index);
    // '0' - '9'
    if (c >= 48 && c <= 57) {
      return c - 48;
    // 'A' - 'F'
    } else if (c >= 65 && c <= 70) {
      return c - 55;
    // 'a' - 'f'
    } else if (c >= 97 && c <= 102) {
      return c - 87;
    } else {
      assert(false, 'Invalid character in ' + string);
    }
  }