Javascript椭圆点乘法算法

时间:2018-12-01 16:41:11

标签: javascript elliptic-curve

以下是椭圆曲线Point Multiplication的实现,但未按预期工作(使用带有BigInt的最新Chrome / Node进行说明):

const bi0 = BigInt(0)
const bi1 = BigInt(1)
const bi2 = BigInt(2)
const bi3 = BigInt(3)

const absMod = (n, p) => n < bi0 ? (n % p) + p : n % p

export function pointAdd (xp, yp, xq, yq, p) {
  const lambda = (yq - yp) / (xq - xp)
  const x = absMod(lambda ** bi2 - xp - xq, p)
  const y = absMod(lambda * (xp - x) - yp, p)
  return { x, y }
}

export function pointDouble (xp, yp, a, p) {
  const numer = bi3 * xp ** bi2 + a
  const denom = (bi2 * yp) ** (p - bi2)
  const lambda = (numer * denom) % p
  const x = absMod(lambda ** bi2 - bi2 * xp, p)
  const y = absMod(lambda * (xp - x) - yp, p)
  return { x, y }
}

export function pointMultiply (d, xp, yp, a, p) {
  const add = (xp, yp, { x, y }) => pointAdd(xp, yp, x, y, p)
  const double = (x, y) => pointDouble(x, y, a, p)
  const recur = ({ x, y }, n) => {
    if (n === bi0) { return { x: bi0, y: bi0 } }
    if (n === bi1) { return { x, y } }
    if (n % bi2 === bi1) { return add(x, y, recur({ x, y }, n - bi1)) }
    return recur(double(x, y), n / bi2)
  }
  return recur({ x: xp, y: yp }, d)
}

给出具有属性的known curve,以上操作对于P2-P5点成功,但从P6开始失败:

const p = BigInt('17')
const a = BigInt('2')
const p1 = { x: BigInt(5), y: BigInt(1) }

const d = BigInt(6)
const p6 = pointMultiply(d, p1.x, p1.y, a, p)
p6.x === BigInt(16) // incorrect value of 8 was returned
p6.y === BigInt(13) // incorrect value of 14 was returned

已知曲线的值:

P   X    Y
—————————— 
1   5    1
2   6    3
3  10    6
4   3    1
5   9   16
6  16   13
7   0    6
8  13    7
9   7    6
10  7   11

我不确定是我误解了算法还是在实现中出错了。

1 个答案:

答案 0 :(得分:1)

我不太了解javascript,因此代码使我感到困惑。但是...

在函数std::shuffle中以及其他所有地方,必须除法mod p。显然,您在std::string Scramble(const std::string& plaintext) { static std::random_device rd; static std::mt19937 g(rd()); std::stringstream ss; ss << plaintext; std::vector<std::string> words{}; std::string cur_word; while(std::getline(ss, cur_word, ' ')) { if(cur_word.empty()) continue; words.push_back(cur_word); } std::for_each(std::begin(words), std::end(words), [](std::string& word) { if(word.size() <= 3) { return; } auto old_word = word; while(old_word == word) { std::shuffle(std::begin(word) + 1, std::end(word) - 1, g); } }); ss.clear(); ss.seekg(0); ss.seekp(0); ss.str(""); for(const auto& word : words) { ss << word << ' '; } return ss.str(); } 中正确地执行了此操作,但在pointAdd中却没有正确执行。在pointDouble中,使用相同的模式:代替

pointAdd

pointAdd

尽管我认为仅具有模块化的逆函数而不是在需要的地方计算X p-2 会更清楚,更容易出错。