Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec735ed

Browse files
committed
utils: leak less information ingetNAF()
1 parent71e4e8e commitec735ed

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

‎lib/elliptic/curve/base.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ function BaseCurve(type, conf) {
2828
this._wnafT3=newArray(4);
2929
this._wnafT4=newArray(4);
3030

31+
this._bitLength=this.n ?this.n.bitLength() :0;
32+
3133
// Generalized Greg Maxwell's trick
3234
varadjustCount=this.n&&this.p.div(this.n);
3335
if(!adjustCount||adjustCount.cmpn(100)>0){
@@ -51,7 +53,7 @@ BaseCurve.prototype._fixedNafMul = function _fixedNafMul(p, k) {
5153
assert(p.precomputed);
5254
vardoubles=p._getDoubles();
5355

54-
varnaf=getNAF(k,1);
56+
varnaf=getNAF(k,1,this._bitLength);
5557
varI=(1<<(doubles.step+1))-(doubles.step%2===0 ?2 :1);
5658
I/=3;
5759

@@ -88,7 +90,7 @@ BaseCurve.prototype._wnafMul = function _wnafMul(p, k) {
8890
varwnd=nafPoints.points;
8991

9092
// Get NAF form
91-
varnaf=getNAF(k,w);
93+
varnaf=getNAF(k,w,this._bitLength);
9294

9395
// Add `this`*(N+1) for every w-NAF index
9496
varacc=this.jpoint(null,null,null);
@@ -144,8 +146,8 @@ BaseCurve.prototype._wnafMulAdd = function _wnafMulAdd(defW,
144146
vara=i-1;
145147
varb=i;
146148
if(wndWidth[a]!==1||wndWidth[b]!==1){
147-
naf[a]=getNAF(coeffs[a],wndWidth[a]);
148-
naf[b]=getNAF(coeffs[b],wndWidth[b]);
149+
naf[a]=getNAF(coeffs[a],wndWidth[a],this._bitLength);
150+
naf[b]=getNAF(coeffs[b],wndWidth[b],this._bitLength);
149151
max=Math.max(naf[a].length,max);
150152
max=Math.max(naf[b].length,max);
151153
continue;

‎lib/elliptic/utils.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ utils.toHex = minUtils.toHex;
1212
utils.encode=minUtils.encode;
1313

1414
// Represent num in a w-NAF form
15-
functiongetNAF(num,w){
16-
varnaf=[];
15+
functiongetNAF(num,w,bits){
16+
varnaf=newArray(Math.max(num.bitLength(),bits)+1);
17+
naf.fill(0);
18+
1719
varws=1<<(w+1);
1820
vark=num.clone();
19-
while(k.cmpn(1)>=0){
21+
22+
for(vari=0;i<naf.length;i++){
2023
varz;
24+
varmod=k.andln(ws-1);
2125
if(k.isOdd()){
22-
varmod=k.andln(ws-1);
2326
if(mod>(ws>>1)-1)
2427
z=(ws>>1)-mod;
2528
else
@@ -28,13 +31,9 @@ function getNAF(num, w) {
2831
}else{
2932
z=0;
3033
}
31-
naf.push(z);
3234

33-
// Optimization, shift by word if possible
34-
varshift=(k.cmpn(0)!==0&&k.andln(ws-1)===0) ?(w+1) :1;
35-
for(vari=1;i<shift;i++)
36-
naf.push(0);
37-
k.iushrn(shift);
35+
naf[i]=z;
36+
k.iushrn(1);
3837
}
3938

4039
returnnaf;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp