arnaucube
arnaucube/cryptofunPublic Self-hosted repository
cryptofun/bn128/bn128.go
bn128/bn128.goRaw
 1package bn128
 2
 3import (
 4	"errors"
 5	"math/big"
 6)
 7
 8type Bn128 struct {
 9	Q             *big.Int
10	R             *big.Int
11	Gg1           [2]*big.Int
12	Gg2           [2][2]*big.Int
13	NonResidueFq2 *big.Int
14	NonResidueFq6 [2]*big.Int
15	Fq1           Fq
16	Fq2           Fq2
17	Fq6           Fq6
18	Fq12          Fq12
19	G1            G1
20	G2            G2
21}
22
23func NewBn128() (Bn128, error) {
24	var b Bn128
25	q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
26	if !ok {
27		return b, errors.New("err with q")
28	}
29	b.Q = q
30	r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10) // i
31	if !ok {
32		return b, errors.New("err with r")
33	}
34	b.R = r
35
36	b.Gg1 = [2]*big.Int{
37		big.NewInt(int64(1)),
38		big.NewInt(int64(2)),
39	}
40
41	g2_00, ok := new(big.Int).SetString("10857046999023057135944570762232829481370756359578518086990519993285655852781", 10)
42	if !ok {
43		return b, errors.New("err with g2_00")
44	}
45	g2_01, ok := new(big.Int).SetString("11559732032986387107991004021392285783925812861821192530917403151452391805634", 10)
46	if !ok {
47		return b, errors.New("err with g2_00")
48	}
49	g2_10, ok := new(big.Int).SetString("8495653923123431417604973247489272438418190587263600148770280649306958101930", 10)
50	if !ok {
51		return b, errors.New("err with g2_00")
52	}
53	g2_11, ok := new(big.Int).SetString("4082367875863433681332203403145435568316851327593401208105741076214120093531", 10)
54	if !ok {
55		return b, errors.New("err with g2_00")
56	}
57	g2_0 := [2]*big.Int{
58		g2_00,
59		g2_01,
60	}
61	g2_1 := [2]*big.Int{
62		g2_10,
63		g2_11,
64	}
65	b.Gg2 = [2][2]*big.Int{
66		g2_0,
67		g2_1,
68	}
69
70	b.Fq1 = NewFq(q)
71	b.NonResidueFq2, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
72	if !ok {
73		return b, errors.New("err with nonResidueFq2")
74	}
75	b.NonResidueFq6 = [2]*big.Int{
76		big.NewInt(int64(9)),
77		big.NewInt(int64(1)),
78	}
79
80	b.Fq2 = Fq2{b.Fq1, b.NonResidueFq2}
81	b.Fq6 = Fq6{b.Fq2, b.NonResidueFq6}
82	b.Fq12 = Fq12{b.Fq6, b.Fq2, b.NonResidueFq6}
83
84	b.G1 = NewG1(b.Fq1, b.Gg1)
85	b.G2 = NewG2(b.Fq2, b.Gg2)
86
87	return b, nil
88}