ec/dsa: extract P_OVER_FOUR as a curve specific constant

This actually resolves a possible bug if multiple ecparams were used
(aka different values for P_OVER_FOUR, but only the cached was used).
This commit is contained in:
Daniel Cousens 2014-05-17 12:03:23 +10:00
parent 1e54c521d5
commit 0865f09d20
2 changed files with 8 additions and 8 deletions

View file

@ -343,11 +343,13 @@ ECPointFp.decodeFrom = function (curve, buffer) {
var p = curve.getQ() var p = curve.getQ()
// We precalculate (p + 1) / 4 where p is the field order // We precalculate (p + 1) / 4 where p is the field order
var P_OVER_FOUR = p.add(BigInteger.ONE).shiftRight(2) if (!curve.P_OVER_FOUR) {
curve.P_OVER_FOUR = p.add(BigInteger.ONE).shiftRight(2)
}
// Convert x to point // Convert x to point
var alpha = x.square().multiply(x).add(SEVEN).mod(p) var alpha = x.square().multiply(x).add(SEVEN).mod(p)
var beta = alpha.modPow(P_OVER_FOUR, p) var beta = alpha.modPow(curve.P_OVER_FOUR, p)
// If beta is even, but y isn't, or vice versa, then convert it, // If beta is even, but y isn't, or vice versa, then convert it,
// otherwise we're done and y == beta. // otherwise we're done and y == beta.

View file

@ -6,8 +6,6 @@ var ecparams = sec("secp256k1")
var BigInteger = require('bigi') var BigInteger = require('bigi')
var ECPointFp = require('./ec').ECPointFp var ECPointFp = require('./ec').ECPointFp
var P_OVER_FOUR = null
function implShamirsTrick(P, k, Q, l) { function implShamirsTrick(P, k, Q, l) {
var m = Math.max(k.bitLength(), l.bitLength()) var m = Math.max(k.bitLength(), l.bitLength())
var Z = P.add2D(Q) var Z = P.add2D(Q)
@ -257,9 +255,9 @@ var ecdsa = {
var a = curve.getA().toBigInteger() var a = curve.getA().toBigInteger()
var b = curve.getB().toBigInteger() var b = curve.getB().toBigInteger()
// We precalculate (p + 1) / 4 where p is if the field order // We precalculate (p + 1) / 4 where p is the field order
if (!P_OVER_FOUR) { if (!curve.P_OVER_FOUR) {
P_OVER_FOUR = p.add(BigInteger.ONE).divide(BigInteger.valueOf(4)) curve.P_OVER_FOUR = p.add(BigInteger.ONE).shiftRight(2)
} }
// 1.1 Compute x // 1.1 Compute x
@ -267,7 +265,7 @@ var ecdsa = {
// 1.3 Convert x to point // 1.3 Convert x to point
var alpha = x.multiply(x).multiply(x).add(a.multiply(x)).add(b).mod(p) var alpha = x.multiply(x).multiply(x).add(a.multiply(x)).add(b).mod(p)
var beta = alpha.modPow(P_OVER_FOUR, p) var beta = alpha.modPow(curve.P_OVER_FOUR, p)
// If beta is even, but y isn't, or vice versa, then convert it, // If beta is even, but y isn't, or vice versa, then convert it,
// otherwise we're done and y == beta. // otherwise we're done and y == beta.