SageMath中椭圆曲线上某点的指数运算过快

时间:2019-05-30 04:21:00

标签: runtime sage elliptic-curve

我正在处理鼠尾草的椭圆曲线。我试图收集用于NIST P-256椭圆曲线上点的组运算和指数求和的基准。当我尝试对曲线上的2个点执行分组操作时,大约需要2微秒。当我尝试对具有随机指数的椭圆曲线上的某点执行幂运算时,只需3微秒。这怎么可能?由于我使用256位值求幂,因此这至少应花费256个组操作所需的时间,即大于0.5ms。我担心我的代码是否错误!

p = 115792089210356248762697446949407573530086143415290314195533631308867097853951 
order = 115792089210356248762697446949407573529996955224135760342422259061068512044369 
b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b 
F = GF(p) 
E = EllipticCurve(F, [-3,b]) 
runs = 10000 
G = E.abelian_group()

F2 = GF(order) 
exponent = [F2.random_element() for i in range(runs)]
e2 = [G.random_element() for i in range(runs)]

t1 = time() 
for i in range(runs):   
      e = Integer(exponent[i])*e2[i] 
t2 = time() 
print  "Time per operation = ", (t2 - t1)/runs , " seconds"

e1 = [G.random_element() for i in range(runs)] 
e2 = [G.random_element() for i in range(runs)] 
t1 = time() 
for i in range(runs):   
         e = e1[i]+e2[i] 
t2 = time() 
print  "Time per operation = ", (t2 - t1)/runs , " seconds"

1 个答案:

答案 0 :(得分:1)

如果您的目标是计时椭圆曲线标量乘法,请不要使用E.abelian_group()

sage: P = G.random_element()
sage: P.parent()
Additive abelian group isomorphic to Z/115792089210356248762697446949407573529996955224135760342422259061068512044369 embedded in Abelian group of points on Elliptic Curve defined by y^2 = x^3 + 115792089210356248762697446949407573530086143415290314195533631308867097853948*x + 41058363725152142129326129780047268409114441015993725554835256314039467401291 over Finite Field of size 115792089210356248762697446949407573530086143415290314195533631308867097853951
sage: P.__class__
<class 'sage.groups.additive_abelian.additive_abelian_wrapper.AdditiveAbelianGroupWrapper_with_category.element_class'>
sage: Q = E.random_element()
sage: Q.parent()
Abelian group of points on Elliptic Curve defined by y^2 = x^3 + 115792089210356248762697446949407573530086143415290314195533631308867097853948*x + 41058363725152142129326129780047268409114441015993725554835256314039467401291 over Finite Field of size 115792089210356248762697446949407573530086143415290314195533631308867097853951
sage: Q.__class__
<class 'sage.schemes.elliptic_curves.ell_point.EllipticCurvePoint_finite_field'>

E.abelian_group()E(_p)的离散日志表示:为该组选择了一个(或多个)生成器:

sage: G.gens()
((20722840521025081260844746572646324413063607211611059363846436737341554936251 : 92859506829486198345561119925850006904261672023969849576492780649068338418688 : 1),)

和点表示为指数向量:

sage: P.vector()
(115792089210356248762697446949407573529996955224135760342422259061068512044368)

因此c*P只需将指数乘以c并减少曲线阶数的模数。

使用E.random_element()获取曲线的点并执行真正的椭圆曲线操作:

sage: c = 2^100
sage: %timeit c*Q
100 loops, best of 3: 3.88 ms per loop
sage: c = 2^1000
sage: %timeit c*Q
10 loops, best of 3: 32.4 ms per loop
sage: c = 2^10000
sage: %timeit c*Q
1 loop, best of 3: 321 ms per loop