From 28ff658a53726f637f18278d91d4e2a1cb940bf5 Mon Sep 17 00:00:00 2001 From: armfazh Date: Mon, 21 Oct 2019 11:47:34 -0700 Subject: [PATCH] Adding conditional move for sjcl.bn --- core/bn.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/bn.js b/core/bn.js index 3d5acc5a..36c62cd1 100644 --- a/core/bn.js +++ b/core/bn.js @@ -88,6 +88,24 @@ sjcl.bn.prototype = { return (greater | ~less) >>> 31; }, + /** + * Constant time conditional move. + * If b=false, returns x; otherwise, returns y. + */ + cmov: function(x, y, b) { + var z = new x._class(0); + var m = x.radixMask; + var m0 = m & (m + b); + var m1 = m & (m + (!b)); + x.fullReduce(); + y.fullReduce(); + for (var i = Math.max(x.limbs.length, y.limbs.length) - 1; i >= 0; i--) { + z.limbs.unshift((x.getLimb(i) & m0) ^ (y.getLimb(i) & m1)); + } + z.trim(); + return z; + }, + /** * Convert to a hex string. */