|
| 1 | +#!/usr/bin/env sage -python |
| 2 | + |
| 3 | +from sage.all import * |
| 4 | +import sys |
| 5 | +sys.path.append("../") |
| 6 | +import params_generator |
| 7 | + |
| 8 | +# Prime order of the subgroup we work in |
| 9 | +def r(x): |
| 10 | + return 36*(x**4) + 36*(x**3) + 18*(x**2) + 6*x + 1 |
| 11 | + |
| 12 | +# Prime used to generate the base finite field |
| 13 | +def q(x): |
| 14 | + return 36*(x**4) + 36*(x**3) + 24*(x**2) + 6*x + 1 |
| 15 | + |
| 16 | +# Compute G2 cofactor |
| 17 | +# See: Proposition 1, Section 3.3: https://eprint.iacr.org/2015/247.pdf |
| 18 | +def g2_h(x): |
| 19 | + return 36*x^4+ 36*x^3+ 30*x^2+ 6*x + 1 |
| 20 | + |
| 21 | +# Computes the order of G1, the safe subgroup of E/Fq |
| 22 | +def g1_order(curve_order): |
| 23 | + decomposition = factor(curve_order) |
| 24 | + # Factor returns the prime decomposition and orders prime |
| 25 | + # factors from smaller to biggest |
| 26 | + biggest_factor = decomposition[-1] |
| 27 | + assert(biggest_factor[1] == 1) |
| 28 | + return biggest_factor[0] |
| 29 | + |
| 30 | +def main(): |
| 31 | + print("Generating parameters for alt_bn128") |
| 32 | + # Curve parameter |
| 33 | + param = 0x44e992b44a6909f1 |
| 34 | + |
| 35 | + prime_r = r(param) |
| 36 | + assert(prime_r == 21888242871839275222246405745257275088548364400416034343698204186575808495617) |
| 37 | + |
| 38 | + prime_q = q(param) |
| 39 | + assert(prime_q == 21888242871839275222246405745257275088696311157297823662689037894645226208583) |
| 40 | + if (mod(prime_q, 6) != 1): |
| 41 | + raise BaseException("Unexpected: q should be = 1 (mod 6). See: https://eprint.iacr.org/2007/390.pdf") |
| 42 | + |
| 43 | + # Scalar field |
| 44 | + print('prime_r = {}'.format(prime_r)) |
| 45 | + #params_generator.generate_libff_Fp_model_params(prime_r) |
| 46 | + Fr = GF(prime_r) |
| 47 | + |
| 48 | + # Base field |
| 49 | + print('prime_q = {}'.format(prime_q)) |
| 50 | + #params_generator.generate_libff_Fp_model_params(prime_q) |
| 51 | + Fq = GF(prime_q) |
| 52 | + |
| 53 | + # E/Fq |
| 54 | + curve = EllipticCurve(Fq, [0, 3]) |
| 55 | + curve_order = curve.order() |
| 56 | + |
| 57 | + # Cofactors |
| 58 | + h1 = curve_order // g1_order(curve_order) |
| 59 | + # G1 cofactor should be 1 |
| 60 | + assert(h1 == 1) |
| 61 | + print('h1 = {}'.format(h1)) |
| 62 | + h2 = g2_h(param) |
| 63 | + print('h2 = {}'.format(h2)) |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + main() |
0 commit comments