arnaucube
arnaucube/cryptofunPublic Self-hosted repository
cryptofun/bn128/README.md
bn128/README.mdRaw
  1## Bn128
  2**[not finished]**
  3
  4This is implemented followng the implementations and info from:
  5- https://github.com/zcash/zcash/tree/master/src/snark
  6- https://github.com/iden3/snarkjs
  7- https://github.com/ethereum/py_ecc/tree/master/py_ecc/bn128
  8- `Multiplication and Squaring on Pairing-Friendly
  9Fields`, Augusto Jun Devegili, Colm Ó hÉigeartaigh, Michael Scott, and Ricardo Dahab https://pdfs.semanticscholar.org/3e01/de88d7428076b2547b60072088507d881bf1.pdf
 10- `Optimal Pairings`, Frederik Vercauteren https://www.cosic.esat.kuleuven.be/bcrypt/optimal.pdf
 11- `Double-and-Add with Relative Jacobian
 12Coordinates`, Björn Fay https://eprint.iacr.org/2014/1014.pdf
 13- `Fast and Regular Algorithms for Scalar Multiplication
 14over Elliptic Curves`, Matthieu Rivain https://eprint.iacr.org/2011/338.pdf
 15
 16- [x] Fq, Fq2, Fq6, Fq12 operations
 17- [x] G1, G2 operations
 18
 19
 20#### Usage
 21First let's define three basic functions to convert integer compositions to big integer compositions:
 22```go
 23func iToBig(a int) *big.Int {
 24	return big.NewInt(int64(a))
 25}
 26
 27func iiToBig(a, b int) [2]*big.Int {
 28	return [2]*big.Int{iToBig(a), iToBig(b)}
 29}
 30
 31func iiiToBig(a, b int) [2]*big.Int {
 32	return [2]*big.Int{iToBig(a), iToBig(b)}
 33}
 34```
 35
 36- Finite Fields (1, 2, 6, 12) operations
 37```go
 38// new finite field of order 1
 39fq1 := NewFq(iToBig(7))
 40
 41// basic operations of finite field 1
 42res := fq1.Add(iToBig(4), iToBig(4))
 43res = fq1.Double(iToBig(5))
 44res = fq1.Sub(iToBig(5), iToBig(7))
 45res = fq1.Neg(iToBig(5))
 46res = fq1.Mul(iToBig(5), iToBig(11))
 47res = fq1.Inverse(iToBig(4))
 48res = fq1.Square(iToBig(5))
 49
 50// new finite field of order 2
 51nonResidueFq2str := "-1" // i / Beta
 52nonResidueFq2, ok := new(big.Int).SetString(nonResidueFq2str, 10)
 53fq2 := Fq2{fq1, nonResidueFq2}
 54nonResidueFq6 := iiToBig(9, 1)
 55
 56// basic operations of finite field of order 2
 57res := fq2.Add(iiToBig(4, 4), iiToBig(3, 4))
 58res = fq2.Double(iiToBig(5, 3))
 59res = fq2.Sub(iiToBig(5, 3), iiToBig(7, 2))
 60res = fq2.Neg(iiToBig(4, 4))
 61res = fq2.Mul(iiToBig(4, 4), iiToBig(3, 4))
 62res = fq2.Inverse(iiToBig(4, 4))
 63res = fq2.Div(iiToBig(4, 4), iiToBig(3, 4))
 64res = fq2.Square(iiToBig(4, 4))
 65
 66
 67// new finite field of order 6
 68nonResidueFq6 := iiToBig(9, 1) // TODO
 69fq6 := Fq6{fq2, nonResidueFq6}
 70
 71// define two new values of Finite Field 6, in order to be able to perform the operations
 72a := [3][2]*big.Int{
 73	iiToBig(1, 2),
 74	iiToBig(3, 4),
 75	iiToBig(5, 6)}
 76b := [3][2]*big.Int{
 77	iiToBig(12, 11),
 78	iiToBig(10, 9),
 79	iiToBig(8, 7)}
 80
 81// basic operations of finite field order 6
 82res := fq6.Add(a, b)
 83res = fq6.Sub(a, b)
 84res = fq6.Mul(a, b)
 85divRes := fq6.Div(mulRes, b)
 86
 87
 88// new finite field of order 12
 89q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
 90if !ok {
 91	fmt.Println("error parsing string to big integer")
 92}
 93
 94fq1 := NewFq(q)
 95nonResidueFq2, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
 96assert.True(t, ok)
 97nonResidueFq6 := iiToBig(9, 1)
 98
 99fq2 := Fq2{fq1, nonResidueFq2}
100fq6 := Fq6{fq2, nonResidueFq6}
101fq12 := Fq12{fq6, fq2, nonResidueFq6}
102
103```
104
105- G1 operations
106```go
107bn128, err := NewBn128()
108assert.Nil(t, err)
109
110r1 := big.NewInt(int64(33))
111r2 := big.NewInt(int64(44))
112
113gr1 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r1))
114gr2 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r2))
115
116grsum1 := bn128.G1.Add(gr1, gr2)
117r1r2 := bn128.Fq1.Add(r1, r2)
118grsum2 := bn128.G1.MulScalar(bn128.G1.G, r1r2)
119
120a := bn128.G1.Affine(grsum1)
121b := bn128.G1.Affine(grsum2)
122assert.Equal(t, a, b)
123assert.Equal(t, "0x2f978c0ab89ebaa576866706b14787f360c4d6c3869efe5a72f7c3651a72ff00", utils.BytesToHex(a[0].Bytes()))
124assert.Equal(t, "0x12e4ba7f0edca8b4fa668fe153aebd908d322dc26ad964d4cd314795844b62b2", utils.BytesToHex(a[1].Bytes()))
125```
126
127- G2 operations
128```go
129bn128, err := NewBn128()
130assert.Nil(t, err)
131
132r1 := big.NewInt(int64(33))
133r2 := big.NewInt(int64(44))
134
135gr1 := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(r1))
136gr2 := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(r2))
137
138grsum1 := bn128.G2.Add(gr1, gr2)
139r1r2 := bn128.Fq1.Add(r1, r2)
140grsum2 := bn128.G2.MulScalar(bn128.G2.G, r1r2)
141
142a := bn128.G2.Affine(grsum1)
143b := bn128.G2.Affine(grsum2)
144assert.Equal(t, a, b)
145```