CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download

生成JubJub曲线参数

Views: 38
Image: ubuntu2204
def findGenPoint(prime, A, EC, N): Fp = GF(prime) for uInt in range(1, 1001): u = Fp(uInt) v2 = u^3+A*u^2+u if not v2.is_square(): continue v = v2.sqrt() point = EC([u, v]) pointOrder = point.order() if pointOrder == N: return point def findBasePoint(EC, h, u, v): return h*EC([u, v]) # Generator and base points of Baby Jubjub in Montgomery form prime = 52435875175126190479447740508185965837690552500527637822603658699938581184513 l = 6554484396890773809930967563523245729705921265872317281365359162392183254199 Fp = GF(prime) EC = EllipticCurve(GF(prime), [0,40962,0,1,0]) A = 40962 B = 1 n = 52435875175126190479447740508185965837647370126978538250922873299137466033592 h = 8 gen_u, gen_v, gen_w = findGenPoint(prime, A, EC, n) base_u , base_v, base_w = findBasePoint(EC, h, gen_u, gen_v) print(gen_u, gen_v) print(base_u, base_v) def mont_to_ted(u, v, prime): Fp = GF(prime) x = Fp(u / v) y = Fp((u-1)/(u+1)) return(x, y) def ted_to_mont(x, y , prime): Fp = GF(prime) u = Fp((1 + y )/ ( 1 - y)) v = Fp((1 + y ) / ( (1 - y) * x)) return(u, v) def is_on_ted(x, y, prime, a, d): Fp = GF(prime) return Fp(a*(x**2) + y**2 - 1 - d*(x**2)*(y**2)) == 0 # Conversion of Baby Jubjub to twisted Edwards a = Fp((A+2)/B) d = Fp((A-2)/B) print("a/d:") print(a) print(d) # Check we have a safe twist and discriminant != 0 assert(not d.is_square()) assert(a*d*(a-d)!=0) # Conversion of generator to twisted Edwards gen_x, gen_y = mont_to_ted(gen_u, gen_v, prime) assert(is_on_ted(gen_x , gen_y , prime , a , d)) # Sanity check: the inverse map returns the original point in Montgomery u , v = ted_to_mont(gen_x, gen_y, prime) assert (u == gen_u) assert (v == gen_v) # Conversion of base point to twisted Edwards base_x , base_y = mont_to_ted(base_u , base_v , prime) assert(is_on_ted(base_x ,base_y , prime , a , d)) print("twisted Edward") print(gen_x, gen_y) print(base_x, base_y)
10 4864016555691628132658688815665199693566189978262460729570611510575653263443 18662558417428907826588413824421338451096304318249267443782476967941530951665 10829583678449754674258847202008243338777477689250089851084221267577753407818 a/d: 40964 40960 twisted Edward 50210964013865202807697778192253911217867412634541528443086291916476367487291 9533795486386580087172316456033811970489191363732297785927937945443378397185 25048732578063176751608348748134148938511950496662102134944680124345451629928 37193546711722353718211339597021920218737743307281823076963067517006020382455