212 b = arg.q
213 res = k(a) / k(b)
214 else:
215 res = resolve(arg, k)216 args.append(res)
217 return expression.func(*args)
218
181 )
182 with local(MultipleContext()) as ctx:
183 real_mult.init(secp128r1, point)
184 real_mult.multiply(scalar)185 return any(
186 map(lambda P: P.X == 0 or P.Y == 0, sum(ctx.parents.values(), []))
187 )
180 secp128r1.curve.coordinate_model, secp128r1.curve
181 )
182 with local(MultipleContext()) as ctx:
183 real_mult.init(secp128r1, point)184 real_mult.multiply(scalar)
185 return any(
186 map(lambda P: P.X == 0 or P.Y == 0, sum(ctx.parents.values(), []))
289
290 def map_point(a, b, pt, aff):
291 u = (pt.x - alpha) / pt.y
292 v = (s * (pt.x - alpha) - 1) / (s * (pt.x - alpha) + 1) * t293 return Point(aff, x=u, y=v)
294
295 return __map(params, ("a", "b"), map_parameters, map_point, EdwardsModel())
289
290 def map_point(a, b, pt, aff):
291 u = (pt.x - alpha) / pt.y
292 v = (s * (pt.x - alpha) - 1) / (s * (pt.x - alpha) + 1) * t293 return Point(aff, x=u, y=v)
294
295 return __map(params, ("a", "b"), map_parameters, map_point, EdwardsModel())
A variable used in a closure is defined in a loop. This will result in all closures using the same value for the closed-over variable, which can pave way for a hideous bug.
Consider the following code snippet:
# Motivation: Create 3 functions that return `x**2`, `x**3`, and `x**4`.
# Note: This will be using the loop variable to show the bug.
powers = [lambda x: x**i for i in range(2,5)]
So, powers
is supposed to contain 3 functions to return 2nd, 3rd and 4th powers of a given number.
On execution, these are the results:
In [1]: powers = [lambda x: x**i for i in range(2,5)]
In [2]: powers[0](2) # Expected result: 4 (2**2)
Out[2]: 16
In [3]: powers[1](2) # Expected result: 8 (2**3)
Out[3]: 16
In [4]: powers[2](2) # Expected result: 16 (2**4)
Out[4]: 16
This happens because i is not local to the lambdas, but is defined in the outer scope, and it is accessed when the lambda is called — not when it is defined. At the end of the loop, the value of i is 4, so all the functions now return x**4.
In order to avoid this, you need to save the values in variables local to the lambdas, so that they don’t rely on the value of the global i
:
In [1]: powers = [lambda x, n =i: x**n for i in range(2,5)]
In [2]: powers[0](2)
Out[2]: 4
In [3]: powers[1](2)
Out[3]: 8
In [4]: powers[2](2)
Out[4]: 16