@ -1,173 +1,3 @@ |
|||||
## Bn128 |
## Bn128 |
||||
|
|
||||
This is implemented followng the info and the implementations from: |
|
||||
- `Multiplication and Squaring on Pairing-Friendly |
|
||||
Fields`, Augusto Jun Devegili, Colm Ó hÉigeartaigh, Michael Scott, and Ricardo Dahab https://pdfs.semanticscholar.org/3e01/de88d7428076b2547b60072088507d881bf1.pdf |
|
||||
- `Optimal Pairings`, Frederik Vercauteren https://www.cosic.esat.kuleuven.be/bcrypt/optimal.pdf , https://eprint.iacr.org/2008/096.pdf |
|
||||
- `Double-and-Add with Relative Jacobian |
|
||||
Coordinates`, Björn Fay https://eprint.iacr.org/2014/1014.pdf |
|
||||
- `Fast and Regular Algorithms for Scalar Multiplication |
|
||||
over Elliptic Curves`, Matthieu Rivain https://eprint.iacr.org/2011/338.pdf |
|
||||
- `High-Speed Software Implementation of the Optimal Ate Pairing over Barreto–Naehrig Curves`, Jean-Luc Beuchat, Jorge E. González-Díaz, Shigeo Mitsunari, Eiji Okamoto, Francisco Rodríguez-Henríquez, and Tadanori Teruya https://eprint.iacr.org/2010/354.pdf |
|
||||
- `New software speed records for cryptographic pairings`, Michael Naehrig, Ruben Niederhagen, Peter Schwabe https://cryptojedi.org/papers/dclxvi-20100714.pdf |
|
||||
- https://github.com/zcash/zcash/tree/master/src/snark |
|
||||
- https://github.com/iden3/snarkjs |
|
||||
- https://github.com/ethereum/py_ecc/tree/master/py_ecc/bn128 |
|
||||
|
|
||||
- [x] Fq, Fq2, Fq6, Fq12 operations |
|
||||
- [x] G1, G2 operations |
|
||||
- [x] preparePairing |
|
||||
- [x] PreComupteG1, PreComupteG2 |
|
||||
- [x] DoubleStep, AddStep |
|
||||
- [x] MillerLoop |
|
||||
- [x] Pairing |
|
||||
|
|
||||
|
|
||||
#### Usage |
|
||||
First let's assume that we have these three basic functions to convert integer compositions to big integer compositions: |
|
||||
```go |
|
||||
func iToBig(a int) *big.Int { |
|
||||
return big.NewInt(int64(a)) |
|
||||
} |
|
||||
|
|
||||
func iiToBig(a, b int) [2]*big.Int { |
|
||||
return [2]*big.Int{iToBig(a), iToBig(b)} |
|
||||
} |
|
||||
|
|
||||
func iiiToBig(a, b int) [2]*big.Int { |
|
||||
return [2]*big.Int{iToBig(a), iToBig(b)} |
|
||||
} |
|
||||
``` |
|
||||
|
|
||||
|
|
||||
- Pairing |
|
||||
```go |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
big25 := big.NewInt(int64(25)) |
|
||||
big30 := big.NewInt(int64(30)) |
|
||||
|
|
||||
g1a := bn128.G1.MulScalar(bn128.G1.G, big25) |
|
||||
g2a := bn128.G2.MulScalar(bn128.G2.G, big30) |
|
||||
|
|
||||
g1b := bn128.G1.MulScalar(bn128.G1.G, big30) |
|
||||
g2b := bn128.G2.MulScalar(bn128.G2.G, big25) |
|
||||
|
|
||||
pA, err := bn128.Pairing(g1a, g2a) |
|
||||
assert.Nil(t, err) |
|
||||
pB, err := bn128.Pairing(g1b, g2b) |
|
||||
assert.Nil(t, err) |
|
||||
assert.True(t, bn128.Fq12.Equal(pA, pB)) |
|
||||
``` |
|
||||
|
|
||||
- Finite Fields (1, 2, 6, 12) operations |
|
||||
```go |
|
||||
// new finite field of order 1 |
|
||||
fq1 := NewFq(iToBig(7)) |
|
||||
|
|
||||
// basic operations of finite field 1 |
|
||||
res := fq1.Add(iToBig(4), iToBig(4)) |
|
||||
res = fq1.Double(iToBig(5)) |
|
||||
res = fq1.Sub(iToBig(5), iToBig(7)) |
|
||||
res = fq1.Neg(iToBig(5)) |
|
||||
res = fq1.Mul(iToBig(5), iToBig(11)) |
|
||||
res = fq1.Inverse(iToBig(4)) |
|
||||
res = fq1.Square(iToBig(5)) |
|
||||
|
|
||||
// new finite field of order 2 |
|
||||
nonResidueFq2str := "-1" // i/j |
|
||||
nonResidueFq2, ok := new(big.Int).SetString(nonResidueFq2str, 10) |
|
||||
fq2 := Fq2{fq1, nonResidueFq2} |
|
||||
nonResidueFq6 := iiToBig(9, 1) |
|
||||
|
|
||||
// basic operations of finite field of order 2 |
|
||||
res := fq2.Add(iiToBig(4, 4), iiToBig(3, 4)) |
|
||||
res = fq2.Double(iiToBig(5, 3)) |
|
||||
res = fq2.Sub(iiToBig(5, 3), iiToBig(7, 2)) |
|
||||
res = fq2.Neg(iiToBig(4, 4)) |
|
||||
res = fq2.Mul(iiToBig(4, 4), iiToBig(3, 4)) |
|
||||
res = fq2.Inverse(iiToBig(4, 4)) |
|
||||
res = fq2.Div(iiToBig(4, 4), iiToBig(3, 4)) |
|
||||
res = fq2.Square(iiToBig(4, 4)) |
|
||||
|
|
||||
|
|
||||
// new finite field of order 6 |
|
||||
nonResidueFq6 := iiToBig(9, 1) // TODO |
|
||||
fq6 := Fq6{fq2, nonResidueFq6} |
|
||||
|
|
||||
// define two new values of Finite Field 6, in order to be able to perform the operations |
|
||||
a := [3][2]*big.Int{ |
|
||||
iiToBig(1, 2), |
|
||||
iiToBig(3, 4), |
|
||||
iiToBig(5, 6)} |
|
||||
b := [3][2]*big.Int{ |
|
||||
iiToBig(12, 11), |
|
||||
iiToBig(10, 9), |
|
||||
iiToBig(8, 7)} |
|
||||
|
|
||||
// basic operations of finite field order 6 |
|
||||
res := fq6.Add(a, b) |
|
||||
res = fq6.Sub(a, b) |
|
||||
res = fq6.Mul(a, b) |
|
||||
divRes := fq6.Div(mulRes, b) |
|
||||
|
|
||||
|
|
||||
// new finite field of order 12 |
|
||||
q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i |
|
||||
if !ok { |
|
||||
fmt.Println("error parsing string to big integer") |
|
||||
} |
|
||||
|
|
||||
fq1 := NewFq(q) |
|
||||
nonResidueFq2, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i |
|
||||
assert.True(t, ok) |
|
||||
nonResidueFq6 := iiToBig(9, 1) |
|
||||
|
|
||||
fq2 := Fq2{fq1, nonResidueFq2} |
|
||||
fq6 := Fq6{fq2, nonResidueFq6} |
|
||||
fq12 := Fq12{fq6, fq2, nonResidueFq6} |
|
||||
|
|
||||
``` |
|
||||
|
|
||||
- G1 operations |
|
||||
```go |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
r1 := big.NewInt(int64(33)) |
|
||||
r2 := big.NewInt(int64(44)) |
|
||||
|
|
||||
gr1 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r1)) |
|
||||
gr2 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r2)) |
|
||||
|
|
||||
grsum1 := bn128.G1.Add(gr1, gr2) |
|
||||
r1r2 := bn128.Fq1.Add(r1, r2) |
|
||||
grsum2 := bn128.G1.MulScalar(bn128.G1.G, r1r2) |
|
||||
|
|
||||
a := bn128.G1.Affine(grsum1) |
|
||||
b := bn128.G1.Affine(grsum2) |
|
||||
assert.Equal(t, a, b) |
|
||||
assert.Equal(t, "0x2f978c0ab89ebaa576866706b14787f360c4d6c3869efe5a72f7c3651a72ff00", utils.BytesToHex(a[0].Bytes())) |
|
||||
assert.Equal(t, "0x12e4ba7f0edca8b4fa668fe153aebd908d322dc26ad964d4cd314795844b62b2", utils.BytesToHex(a[1].Bytes())) |
|
||||
``` |
|
||||
|
|
||||
- G2 operations |
|
||||
```go |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
r1 := big.NewInt(int64(33)) |
|
||||
r2 := big.NewInt(int64(44)) |
|
||||
|
|
||||
gr1 := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(r1)) |
|
||||
gr2 := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(r2)) |
|
||||
|
|
||||
grsum1 := bn128.G2.Add(gr1, gr2) |
|
||||
r1r2 := bn128.Fq1.Add(r1, r2) |
|
||||
grsum2 := bn128.G2.MulScalar(bn128.G2.G, r1r2) |
|
||||
|
|
||||
a := bn128.G2.Affine(grsum1) |
|
||||
b := bn128.G2.Affine(grsum2) |
|
||||
assert.Equal(t, a, b) |
|
||||
``` |
|
||||
|
Implementation of the bn128 pairing. |
||||
|
Code moved to https://github.com/arnaucube/bn128 |
@ -1,407 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"bytes" |
|
||||
"errors" |
|
||||
"math/big" |
|
||||
) |
|
||||
|
|
||||
type Bn128 struct { |
|
||||
Q *big.Int |
|
||||
Gg1 [2]*big.Int |
|
||||
Gg2 [2][2]*big.Int |
|
||||
NonResidueFq2 *big.Int |
|
||||
NonResidueFq6 [2]*big.Int |
|
||||
Fq1 Fq |
|
||||
Fq2 Fq2 |
|
||||
Fq6 Fq6 |
|
||||
Fq12 Fq12 |
|
||||
G1 G1 |
|
||||
G2 G2 |
|
||||
LoopCount *big.Int |
|
||||
LoopCountNeg bool |
|
||||
|
|
||||
TwoInv *big.Int |
|
||||
CoefB *big.Int |
|
||||
TwistCoefB [2]*big.Int |
|
||||
Twist [2]*big.Int |
|
||||
FrobeniusCoeffsC11 *big.Int |
|
||||
TwistMulByQX [2]*big.Int |
|
||||
TwistMulByQY [2]*big.Int |
|
||||
FinalExp *big.Int |
|
||||
} |
|
||||
|
|
||||
func NewBn128() (Bn128, error) { |
|
||||
var b Bn128 |
|
||||
q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
|
|
||||
if !ok { |
|
||||
return b, errors.New("err with q") |
|
||||
} |
|
||||
b.Q = q |
|
||||
|
|
||||
b.Gg1 = [2]*big.Int{ |
|
||||
big.NewInt(int64(1)), |
|
||||
big.NewInt(int64(2)), |
|
||||
} |
|
||||
|
|
||||
g2_00, ok := new(big.Int).SetString("10857046999023057135944570762232829481370756359578518086990519993285655852781", 10) |
|
||||
if !ok { |
|
||||
return b, errors.New("err with g2_00") |
|
||||
} |
|
||||
g2_01, ok := new(big.Int).SetString("11559732032986387107991004021392285783925812861821192530917403151452391805634", 10) |
|
||||
if !ok { |
|
||||
return b, errors.New("err with g2_00") |
|
||||
} |
|
||||
g2_10, ok := new(big.Int).SetString("8495653923123431417604973247489272438418190587263600148770280649306958101930", 10) |
|
||||
if !ok { |
|
||||
return b, errors.New("err with g2_00") |
|
||||
} |
|
||||
g2_11, ok := new(big.Int).SetString("4082367875863433681332203403145435568316851327593401208105741076214120093531", 10) |
|
||||
if !ok { |
|
||||
return b, errors.New("err with g2_00") |
|
||||
} |
|
||||
|
|
||||
b.Gg2 = [2][2]*big.Int{ |
|
||||
[2]*big.Int{ |
|
||||
g2_00, |
|
||||
g2_01, |
|
||||
}, |
|
||||
[2]*big.Int{ |
|
||||
g2_10, |
|
||||
g2_11, |
|
||||
}, |
|
||||
} |
|
||||
|
|
||||
b.Fq1 = NewFq(q) |
|
||||
b.NonResidueFq2, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
|
|
||||
if !ok { |
|
||||
return b, errors.New("err with nonResidueFq2") |
|
||||
} |
|
||||
b.NonResidueFq6 = [2]*big.Int{ |
|
||||
big.NewInt(int64(9)), |
|
||||
big.NewInt(int64(1)), |
|
||||
} |
|
||||
|
|
||||
b.Fq2 = NewFq2(b.Fq1, b.NonResidueFq2) |
|
||||
b.Fq6 = NewFq6(b.Fq2, b.NonResidueFq6) |
|
||||
b.Fq12 = NewFq12(b.Fq6, b.Fq2, b.NonResidueFq6) |
|
||||
|
|
||||
b.G1 = NewG1(b.Fq1, b.Gg1) |
|
||||
b.G2 = NewG2(b.Fq2, b.Gg2) |
|
||||
|
|
||||
err := b.preparePairing() |
|
||||
if err != nil { |
|
||||
return b, err |
|
||||
} |
|
||||
|
|
||||
return b, nil |
|
||||
} |
|
||||
|
|
||||
func BigIsOdd(n *big.Int) bool { |
|
||||
one := big.NewInt(int64(1)) |
|
||||
and := new(big.Int).And(n, one) |
|
||||
return bytes.Equal(and.Bytes(), big.NewInt(int64(1)).Bytes()) |
|
||||
} |
|
||||
|
|
||||
func (bn128 *Bn128) preparePairing() error { |
|
||||
var ok bool |
|
||||
bn128.LoopCount, ok = new(big.Int).SetString("29793968203157093288", 10) |
|
||||
if !ok { |
|
||||
return errors.New("err with LoopCount from string") |
|
||||
} |
|
||||
|
|
||||
bn128.LoopCountNeg = false |
|
||||
|
|
||||
bn128.TwoInv = bn128.Fq1.Inverse(big.NewInt(int64(2))) |
|
||||
|
|
||||
bn128.CoefB = big.NewInt(int64(3)) |
|
||||
bn128.Twist = [2]*big.Int{ |
|
||||
big.NewInt(int64(9)), |
|
||||
big.NewInt(int64(1)), |
|
||||
} |
|
||||
bn128.TwistCoefB = bn128.Fq2.MulScalar(bn128.Fq2.Inverse(bn128.Twist), bn128.CoefB) |
|
||||
|
|
||||
bn128.FrobeniusCoeffsC11, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) |
|
||||
if !ok { |
|
||||
return errors.New("error parsing frobeniusCoeffsC11") |
|
||||
} |
|
||||
|
|
||||
a, ok := new(big.Int).SetString("21575463638280843010398324269430826099269044274347216827212613867836435027261", 10) |
|
||||
if !ok { |
|
||||
return errors.New("error parsing a") |
|
||||
} |
|
||||
b, ok := new(big.Int).SetString("10307601595873709700152284273816112264069230130616436755625194854815875713954", 10) |
|
||||
if !ok { |
|
||||
return errors.New("error parsing b") |
|
||||
} |
|
||||
bn128.TwistMulByQX = [2]*big.Int{ |
|
||||
a, |
|
||||
b, |
|
||||
} |
|
||||
|
|
||||
a, ok = new(big.Int).SetString("2821565182194536844548159561693502659359617185244120367078079554186484126554", 10) |
|
||||
if !ok { |
|
||||
return errors.New("error parsing a") |
|
||||
} |
|
||||
b, ok = new(big.Int).SetString("3505843767911556378687030309984248845540243509899259641013678093033130930403", 10) |
|
||||
if !ok { |
|
||||
return errors.New("error parsing b") |
|
||||
} |
|
||||
bn128.TwistMulByQY = [2]*big.Int{ |
|
||||
a, |
|
||||
b, |
|
||||
} |
|
||||
|
|
||||
bn128.FinalExp, ok = new(big.Int).SetString("552484233613224096312617126783173147097382103762957654188882734314196910839907541213974502761540629817009608548654680343627701153829446747810907373256841551006201639677726139946029199968412598804882391702273019083653272047566316584365559776493027495458238373902875937659943504873220554161550525926302303331747463515644711876653177129578303191095900909191624817826566688241804408081892785725967931714097716709526092261278071952560171111444072049229123565057483750161460024353346284167282452756217662335528813519139808291170539072125381230815729071544861602750936964829313608137325426383735122175229541155376346436093930287402089517426973178917569713384748081827255472576937471496195752727188261435633271238710131736096299798168852925540549342330775279877006784354801422249722573783561685179618816480037695005515426162362431072245638324744480", 10) |
|
||||
if !ok { |
|
||||
return errors.New("error parsing finalExp") |
|
||||
} |
|
||||
|
|
||||
return nil |
|
||||
|
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) Pairing(p1 [3]*big.Int, p2 [3][2]*big.Int) ([2][3][2]*big.Int, error) { |
|
||||
pre1 := bn128.PreComputeG1(p1) |
|
||||
pre2, err := bn128.PreComputeG2(p2) |
|
||||
if err != nil { |
|
||||
return [2][3][2]*big.Int{}, err |
|
||||
} |
|
||||
|
|
||||
r1 := bn128.MillerLoop(pre1, pre2) |
|
||||
res := bn128.FinalExponentiation(r1) |
|
||||
return res, nil |
|
||||
} |
|
||||
|
|
||||
type AteG1Precomp struct { |
|
||||
Px *big.Int |
|
||||
Py *big.Int |
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) PreComputeG1(p [3]*big.Int) AteG1Precomp { |
|
||||
pCopy := bn128.G1.Affine(p) |
|
||||
res := AteG1Precomp{ |
|
||||
Px: pCopy[0], |
|
||||
Py: pCopy[1], |
|
||||
} |
|
||||
return res |
|
||||
} |
|
||||
|
|
||||
type EllCoeffs struct { |
|
||||
Ell0 [2]*big.Int |
|
||||
EllVW [2]*big.Int |
|
||||
EllVV [2]*big.Int |
|
||||
} |
|
||||
type AteG2Precomp struct { |
|
||||
Qx [2]*big.Int |
|
||||
Qy [2]*big.Int |
|
||||
Coeffs []EllCoeffs |
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) PreComputeG2(p [3][2]*big.Int) (AteG2Precomp, error) { |
|
||||
qCopy := bn128.G2.Affine(p) |
|
||||
res := AteG2Precomp{ |
|
||||
qCopy[0], |
|
||||
qCopy[1], |
|
||||
[]EllCoeffs{}, |
|
||||
} |
|
||||
r := [3][2]*big.Int{ |
|
||||
bn128.Fq2.Copy(qCopy[0]), |
|
||||
bn128.Fq2.Copy(qCopy[1]), |
|
||||
bn128.Fq2.One(), |
|
||||
} |
|
||||
var c EllCoeffs |
|
||||
for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- { |
|
||||
bit := bn128.LoopCount.Bit(i) |
|
||||
|
|
||||
c, r = bn128.DoublingStep(r) |
|
||||
res.Coeffs = append(res.Coeffs, c) |
|
||||
if bit == 1 { |
|
||||
c, r = bn128.MixedAdditionStep(qCopy, r) |
|
||||
res.Coeffs = append(res.Coeffs, c) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
q1 := bn128.G2.Affine(bn128.G2MulByQ(qCopy)) |
|
||||
if !bn128.Fq2.Equal(q1[2], bn128.Fq2.One()) { |
|
||||
return res, errors.New("q1[2] != Fq2.One") |
|
||||
} |
|
||||
q2 := bn128.G2.Affine(bn128.G2MulByQ(q1)) |
|
||||
if !bn128.Fq2.Equal(q2[2], bn128.Fq2.One()) { |
|
||||
return res, errors.New("q2[2] != Fq2.One") |
|
||||
} |
|
||||
|
|
||||
if bn128.LoopCountNeg { |
|
||||
r[1] = bn128.Fq2.Neg(r[1]) |
|
||||
} |
|
||||
q2[1] = bn128.Fq2.Neg(q2[1]) |
|
||||
|
|
||||
c, r = bn128.MixedAdditionStep(q1, r) |
|
||||
res.Coeffs = append(res.Coeffs, c) |
|
||||
|
|
||||
c, r = bn128.MixedAdditionStep(q2, r) |
|
||||
res.Coeffs = append(res.Coeffs, c) |
|
||||
|
|
||||
return res, nil |
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) DoublingStep(current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) { |
|
||||
x := current[0] |
|
||||
y := current[1] |
|
||||
z := current[2] |
|
||||
|
|
||||
a := bn128.Fq2.MulScalar(bn128.Fq2.Mul(x, y), bn128.TwoInv) |
|
||||
b := bn128.Fq2.Square(y) |
|
||||
c := bn128.Fq2.Square(z) |
|
||||
d := bn128.Fq2.Add(c, bn128.Fq2.Add(c, c)) |
|
||||
e := bn128.Fq2.Mul(bn128.TwistCoefB, d) |
|
||||
f := bn128.Fq2.Add(e, bn128.Fq2.Add(e, e)) |
|
||||
g := bn128.Fq2.MulScalar(bn128.Fq2.Add(b, f), bn128.TwoInv) |
|
||||
h := bn128.Fq2.Sub( |
|
||||
bn128.Fq2.Square(bn128.Fq2.Add(y, z)), |
|
||||
bn128.Fq2.Add(b, c)) |
|
||||
i := bn128.Fq2.Sub(e, b) |
|
||||
j := bn128.Fq2.Square(x) |
|
||||
eSqr := bn128.Fq2.Square(e) |
|
||||
current[0] = bn128.Fq2.Mul(a, bn128.Fq2.Sub(b, f)) |
|
||||
current[1] = bn128.Fq2.Sub(bn128.Fq2.Sub(bn128.Fq2.Square(g), eSqr), |
|
||||
bn128.Fq2.Add(eSqr, eSqr)) |
|
||||
current[2] = bn128.Fq2.Mul(b, h) |
|
||||
res := EllCoeffs{ |
|
||||
Ell0: bn128.Fq2.Mul(i, bn128.Twist), |
|
||||
EllVW: bn128.Fq2.Neg(h), |
|
||||
EllVV: bn128.Fq2.Add(j, bn128.Fq2.Add(j, j)), |
|
||||
} |
|
||||
|
|
||||
return res, current |
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) MixedAdditionStep(base, current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) { |
|
||||
x1 := current[0] |
|
||||
y1 := current[1] |
|
||||
z1 := current[2] |
|
||||
x2 := base[0] |
|
||||
y2 := base[1] |
|
||||
|
|
||||
d := bn128.Fq2.Sub(x1, bn128.Fq2.Mul(x2, z1)) |
|
||||
e := bn128.Fq2.Sub(y1, bn128.Fq2.Mul(y2, z1)) |
|
||||
f := bn128.Fq2.Square(d) |
|
||||
g := bn128.Fq2.Square(e) |
|
||||
h := bn128.Fq2.Mul(d, f) |
|
||||
i := bn128.Fq2.Mul(x1, f) |
|
||||
j := bn128.Fq2.Sub( |
|
||||
bn128.Fq2.Add(h, bn128.Fq2.Mul(z1, g)), |
|
||||
bn128.Fq2.Add(i, i)) |
|
||||
|
|
||||
current[0] = bn128.Fq2.Mul(d, j) |
|
||||
current[1] = bn128.Fq2.Sub( |
|
||||
bn128.Fq2.Mul(e, bn128.Fq2.Sub(i, j)), |
|
||||
bn128.Fq2.Mul(h, y1)) |
|
||||
current[2] = bn128.Fq2.Mul(z1, h) |
|
||||
|
|
||||
coef := EllCoeffs{ |
|
||||
Ell0: bn128.Fq2.Mul( |
|
||||
bn128.Twist, |
|
||||
bn128.Fq2.Sub( |
|
||||
bn128.Fq2.Mul(e, x2), |
|
||||
bn128.Fq2.Mul(d, y2))), |
|
||||
EllVW: d, |
|
||||
EllVV: bn128.Fq2.Neg(e), |
|
||||
} |
|
||||
return coef, current |
|
||||
} |
|
||||
func (bn128 Bn128) G2MulByQ(p [3][2]*big.Int) [3][2]*big.Int { |
|
||||
fmx := [2]*big.Int{ |
|
||||
p[0][0], |
|
||||
bn128.Fq1.Mul(p[0][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)), |
|
||||
} |
|
||||
fmy := [2]*big.Int{ |
|
||||
p[1][0], |
|
||||
bn128.Fq1.Mul(p[1][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)), |
|
||||
} |
|
||||
fmz := [2]*big.Int{ |
|
||||
p[2][0], |
|
||||
bn128.Fq1.Mul(p[2][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)), |
|
||||
} |
|
||||
|
|
||||
return [3][2]*big.Int{ |
|
||||
bn128.Fq2.Mul(bn128.TwistMulByQX, fmx), |
|
||||
bn128.Fq2.Mul(bn128.TwistMulByQY, fmy), |
|
||||
fmz, |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) MillerLoop(pre1 AteG1Precomp, pre2 AteG2Precomp) [2][3][2]*big.Int { |
|
||||
// https://cryptojedi.org/papers/dclxvi-20100714.pdf
|
|
||||
// https://eprint.iacr.org/2008/096.pdf
|
|
||||
|
|
||||
idx := 0 |
|
||||
var c EllCoeffs |
|
||||
f := bn128.Fq12.One() |
|
||||
|
|
||||
for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- { |
|
||||
bit := bn128.LoopCount.Bit(i) |
|
||||
|
|
||||
c = pre2.Coeffs[idx] |
|
||||
idx++ |
|
||||
f = bn128.Fq12.Square(f) |
|
||||
|
|
||||
f = bn128.MulBy024(f, |
|
||||
c.Ell0, |
|
||||
bn128.Fq2.MulScalar(c.EllVW, pre1.Py), |
|
||||
bn128.Fq2.MulScalar(c.EllVV, pre1.Px)) |
|
||||
|
|
||||
if bit == 1 { |
|
||||
c = pre2.Coeffs[idx] |
|
||||
idx++ |
|
||||
f = bn128.MulBy024( |
|
||||
f, |
|
||||
c.Ell0, |
|
||||
bn128.Fq2.MulScalar(c.EllVW, pre1.Py), |
|
||||
bn128.Fq2.MulScalar(c.EllVV, pre1.Px)) |
|
||||
} |
|
||||
} |
|
||||
if bn128.LoopCountNeg { |
|
||||
f = bn128.Fq12.Inverse(f) |
|
||||
} |
|
||||
|
|
||||
c = pre2.Coeffs[idx] |
|
||||
idx++ |
|
||||
f = bn128.MulBy024( |
|
||||
f, |
|
||||
c.Ell0, |
|
||||
bn128.Fq2.MulScalar(c.EllVW, pre1.Py), |
|
||||
bn128.Fq2.MulScalar(c.EllVV, pre1.Px)) |
|
||||
|
|
||||
c = pre2.Coeffs[idx] |
|
||||
idx++ |
|
||||
|
|
||||
f = bn128.MulBy024( |
|
||||
f, |
|
||||
c.Ell0, |
|
||||
bn128.Fq2.MulScalar(c.EllVW, pre1.Py), |
|
||||
bn128.Fq2.MulScalar(c.EllVV, pre1.Px)) |
|
||||
|
|
||||
return f |
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) MulBy024(a [2][3][2]*big.Int, ell0, ellVW, ellVV [2]*big.Int) [2][3][2]*big.Int { |
|
||||
b := [2][3][2]*big.Int{ |
|
||||
[3][2]*big.Int{ |
|
||||
ell0, |
|
||||
bn128.Fq2.Zero(), |
|
||||
ellVV, |
|
||||
}, |
|
||||
[3][2]*big.Int{ |
|
||||
bn128.Fq2.Zero(), |
|
||||
ellVW, |
|
||||
bn128.Fq2.Zero(), |
|
||||
}, |
|
||||
} |
|
||||
return bn128.Fq12.Mul(a, b) |
|
||||
} |
|
||||
|
|
||||
func (bn128 Bn128) FinalExponentiation(r [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
res := bn128.Fq12.Exp(r, bn128.FinalExp) |
|
||||
return res |
|
||||
} |
|
@ -1,65 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"math/big" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
) |
|
||||
|
|
||||
func TestBN128(t *testing.T) { |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
big40 := big.NewInt(int64(40)) |
|
||||
big75 := big.NewInt(int64(75)) |
|
||||
|
|
||||
g1a := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(big40)) |
|
||||
g2a := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(big75)) |
|
||||
|
|
||||
g1b := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(big75)) |
|
||||
g2b := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(big40)) |
|
||||
|
|
||||
pre1a := bn128.PreComputeG1(g1a) |
|
||||
pre2a, err := bn128.PreComputeG2(g2a) |
|
||||
assert.Nil(t, err) |
|
||||
pre1b := bn128.PreComputeG1(g1b) |
|
||||
pre2b, err := bn128.PreComputeG2(g2b) |
|
||||
|
|
||||
r1 := bn128.MillerLoop(pre1a, pre2a) |
|
||||
r2 := bn128.MillerLoop(pre1b, pre2b) |
|
||||
|
|
||||
rbe := bn128.Fq12.Mul(r1, bn128.Fq12.Inverse(r2)) |
|
||||
|
|
||||
res := bn128.FinalExponentiation(rbe) |
|
||||
|
|
||||
a := bn128.Fq12.Affine(res) |
|
||||
b := bn128.Fq12.Affine(bn128.Fq12.One()) |
|
||||
|
|
||||
assert.True(t, bn128.Fq12.Equal(a, b)) |
|
||||
assert.True(t, bn128.Fq12.Equal(res, bn128.Fq12.One())) |
|
||||
} |
|
||||
|
|
||||
func TestBN128_PairingFunction(t *testing.T) { |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
big25 := big.NewInt(int64(25)) |
|
||||
big30 := big.NewInt(int64(30)) |
|
||||
|
|
||||
g1a := bn128.G1.MulScalar(bn128.G1.G, big25) |
|
||||
g2a := bn128.G2.MulScalar(bn128.G2.G, big30) |
|
||||
|
|
||||
g1b := bn128.G1.MulScalar(bn128.G1.G, big30) |
|
||||
g2b := bn128.G2.MulScalar(bn128.G2.G, big25) |
|
||||
|
|
||||
pA, err := bn128.Pairing(g1a, g2a) |
|
||||
assert.Nil(t, err) |
|
||||
pB, err := bn128.Pairing(g1b, g2b) |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
assert.True(t, bn128.Fq12.Equal(pA, pB)) |
|
||||
|
|
||||
assert.Equal(t, pA[0][0][0].String(), "73680848340331011700282047627232219336104151861349893575958589557226556635706") |
|
||||
assert.Equal(t, bn128.Fq12.Affine(pA)[0][0][0].String(), "8016119724813186033542830391460394070015218389456422587891475873290878009957") |
|
||||
} |
|
@ -1,129 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"bytes" |
|
||||
"math/big" |
|
||||
) |
|
||||
|
|
||||
// Fq is the Z field over modulus Q
|
|
||||
type Fq struct { |
|
||||
Q *big.Int // Q
|
|
||||
} |
|
||||
|
|
||||
// NewFq generates a new Fq
|
|
||||
func NewFq(q *big.Int) Fq { |
|
||||
return Fq{ |
|
||||
q, |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Zero returns a Zero value on the Fq
|
|
||||
func (fq Fq) Zero() *big.Int { |
|
||||
return big.NewInt(int64(0)) |
|
||||
} |
|
||||
|
|
||||
// One returns a One value on the Fq
|
|
||||
func (fq Fq) One() *big.Int { |
|
||||
return big.NewInt(int64(1)) |
|
||||
} |
|
||||
|
|
||||
// Add performs an addition on the Fq
|
|
||||
func (fq Fq) Add(a, b *big.Int) *big.Int { |
|
||||
r := new(big.Int).Add(a, b) |
|
||||
// return new(big.Int).Mod(r, fq.Q)
|
|
||||
return r |
|
||||
} |
|
||||
|
|
||||
// Double performs a doubling on the Fq
|
|
||||
func (fq Fq) Double(a *big.Int) *big.Int { |
|
||||
r := new(big.Int).Add(a, a) |
|
||||
// return new(big.Int).Mod(r, fq.Q)
|
|
||||
return r |
|
||||
} |
|
||||
|
|
||||
// Sub performs a substraction on the Fq
|
|
||||
func (fq Fq) Sub(a, b *big.Int) *big.Int { |
|
||||
r := new(big.Int).Sub(a, b) |
|
||||
// return new(big.Int).Mod(r, fq.Q)
|
|
||||
return r |
|
||||
} |
|
||||
|
|
||||
// Neg performs a negation on the Fq
|
|
||||
func (fq Fq) Neg(a *big.Int) *big.Int { |
|
||||
m := new(big.Int).Neg(a) |
|
||||
// return new(big.Int).Mod(m, fq.Q)
|
|
||||
return m |
|
||||
} |
|
||||
|
|
||||
// Mul performs a multiplication on the Fq
|
|
||||
func (fq Fq) Mul(a, b *big.Int) *big.Int { |
|
||||
m := new(big.Int).Mul(a, b) |
|
||||
return new(big.Int).Mod(m, fq.Q) |
|
||||
// return m
|
|
||||
} |
|
||||
|
|
||||
func (fq Fq) MulScalar(base, e *big.Int) *big.Int { |
|
||||
return fq.Mul(base, e) |
|
||||
} |
|
||||
|
|
||||
// Inverse returns the inverse on the Fq
|
|
||||
func (fq Fq) Inverse(a *big.Int) *big.Int { |
|
||||
return new(big.Int).ModInverse(a, fq.Q) |
|
||||
// q := bigCopy(fq.Q)
|
|
||||
// t := big.NewInt(int64(0))
|
|
||||
// r := fq.Q
|
|
||||
// newt := big.NewInt(int64(0))
|
|
||||
// newr := fq.Affine(a)
|
|
||||
// for !bytes.Equal(newr.Bytes(), big.NewInt(int64(0)).Bytes()) {
|
|
||||
// q := new(big.Int).Div(bigCopy(r), bigCopy(newr))
|
|
||||
//
|
|
||||
// t = bigCopy(newt)
|
|
||||
// newt = fq.Sub(t, fq.Mul(q, newt))
|
|
||||
//
|
|
||||
// r = bigCopy(newr)
|
|
||||
// newr = fq.Sub(r, fq.Mul(q, newr))
|
|
||||
// }
|
|
||||
// if t.Cmp(big.NewInt(0)) == -1 { // t< 0
|
|
||||
// t = fq.Add(t, q)
|
|
||||
// }
|
|
||||
// return t
|
|
||||
} |
|
||||
|
|
||||
// Square performs a square operation on the Fq
|
|
||||
func (fq Fq) Square(a *big.Int) *big.Int { |
|
||||
m := new(big.Int).Mul(a, a) |
|
||||
return new(big.Int).Mod(m, fq.Q) |
|
||||
} |
|
||||
|
|
||||
func (fq Fq) IsZero(a *big.Int) bool { |
|
||||
return bytes.Equal(a.Bytes(), fq.Zero().Bytes()) |
|
||||
} |
|
||||
|
|
||||
func (fq Fq) Copy(a *big.Int) *big.Int { |
|
||||
return new(big.Int).SetBytes(a.Bytes()) |
|
||||
} |
|
||||
|
|
||||
func (fq Fq) Affine(a *big.Int) *big.Int { |
|
||||
nq := fq.Neg(fq.Q) |
|
||||
|
|
||||
aux := a |
|
||||
if aux.Cmp(big.NewInt(int64(0))) == -1 { // negative value
|
|
||||
if aux.Cmp(nq) != 1 { // aux less or equal nq
|
|
||||
aux = new(big.Int).Mod(aux, fq.Q) |
|
||||
} |
|
||||
if aux.Cmp(big.NewInt(int64(0))) == -1 { // negative value
|
|
||||
aux = new(big.Int).Add(aux, fq.Q) |
|
||||
} |
|
||||
} else { |
|
||||
if aux.Cmp(fq.Q) != -1 { // aux greater or equal nq
|
|
||||
aux = new(big.Int).Mod(aux, fq.Q) |
|
||||
} |
|
||||
} |
|
||||
return aux |
|
||||
} |
|
||||
|
|
||||
func (fq Fq) Equal(a, b *big.Int) bool { |
|
||||
aAff := fq.Affine(a) |
|
||||
bAff := fq.Affine(b) |
|
||||
return bytes.Equal(aAff.Bytes(), bAff.Bytes()) |
|
||||
} |
|
@ -1,161 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"bytes" |
|
||||
"math/big" |
|
||||
) |
|
||||
|
|
||||
// Fq12 uses the same algorithms than Fq2, but with [2][3][2]*big.Int data structure
|
|
||||
|
|
||||
// Fq12 is Field 12
|
|
||||
type Fq12 struct { |
|
||||
F Fq6 |
|
||||
Fq2 Fq2 |
|
||||
NonResidue [2]*big.Int |
|
||||
} |
|
||||
|
|
||||
// NewFq12 generates a new Fq12
|
|
||||
func NewFq12(f Fq6, fq2 Fq2, nonResidue [2]*big.Int) Fq12 { |
|
||||
fq12 := Fq12{ |
|
||||
f, |
|
||||
fq2, |
|
||||
nonResidue, |
|
||||
} |
|
||||
return fq12 |
|
||||
} |
|
||||
|
|
||||
// Zero returns a Zero value on the Fq12
|
|
||||
func (fq12 Fq12) Zero() [2][3][2]*big.Int { |
|
||||
return [2][3][2]*big.Int{fq12.F.Zero(), fq12.F.Zero()} |
|
||||
} |
|
||||
|
|
||||
// One returns a One value on the Fq12
|
|
||||
func (fq12 Fq12) One() [2][3][2]*big.Int { |
|
||||
return [2][3][2]*big.Int{fq12.F.One(), fq12.F.Zero()} |
|
||||
} |
|
||||
|
|
||||
func (fq12 Fq12) mulByNonResidue(a [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{ |
|
||||
fq12.Fq2.Mul(fq12.NonResidue, a[2]), |
|
||||
a[0], |
|
||||
a[1], |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Add performs an addition on the Fq12
|
|
||||
func (fq12 Fq12) Add(a, b [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
return [2][3][2]*big.Int{ |
|
||||
fq12.F.Add(a[0], b[0]), |
|
||||
fq12.F.Add(a[1], b[1]), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Double performs a doubling on the Fq12
|
|
||||
func (fq12 Fq12) Double(a [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
return fq12.Add(a, a) |
|
||||
} |
|
||||
|
|
||||
// Sub performs a substraction on the Fq12
|
|
||||
func (fq12 Fq12) Sub(a, b [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
return [2][3][2]*big.Int{ |
|
||||
fq12.F.Sub(a[0], b[0]), |
|
||||
fq12.F.Sub(a[1], b[1]), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Neg performs a negation on the Fq12
|
|
||||
func (fq12 Fq12) Neg(a [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
return fq12.Sub(fq12.Zero(), a) |
|
||||
} |
|
||||
|
|
||||
// Mul performs a multiplication on the Fq12
|
|
||||
func (fq12 Fq12) Mul(a, b [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
// Multiplication and Squaring on Pairing-Friendly .pdf; Section 3 (Karatsuba)
|
|
||||
v0 := fq12.F.Mul(a[0], b[0]) |
|
||||
v1 := fq12.F.Mul(a[1], b[1]) |
|
||||
return [2][3][2]*big.Int{ |
|
||||
fq12.F.Add(v0, fq12.mulByNonResidue(v1)), |
|
||||
fq12.F.Sub( |
|
||||
fq12.F.Mul( |
|
||||
fq12.F.Add(a[0], a[1]), |
|
||||
fq12.F.Add(b[0], b[1])), |
|
||||
fq12.F.Add(v0, v1)), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (fq12 Fq12) MulScalar(base [2][3][2]*big.Int, e *big.Int) [2][3][2]*big.Int { |
|
||||
// for more possible implementations see g2.go file, at the function g2.MulScalar()
|
|
||||
|
|
||||
res := fq12.Zero() |
|
||||
rem := e |
|
||||
exp := base |
|
||||
|
|
||||
for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) { |
|
||||
// if rem % 2 == 1
|
|
||||
if bytes.Equal(new(big.Int).Rem(rem, big.NewInt(int64(2))).Bytes(), big.NewInt(int64(1)).Bytes()) { |
|
||||
res = fq12.Add(res, exp) |
|
||||
} |
|
||||
exp = fq12.Double(exp) |
|
||||
rem = rem.Rsh(rem, 1) // rem = rem >> 1
|
|
||||
} |
|
||||
return res |
|
||||
} |
|
||||
|
|
||||
// Inverse returns the inverse on the Fq12
|
|
||||
func (fq12 Fq12) Inverse(a [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
t0 := fq12.F.Square(a[0]) |
|
||||
t1 := fq12.F.Square(a[1]) |
|
||||
t2 := fq12.F.Sub(t0, fq12.mulByNonResidue(t1)) |
|
||||
t3 := fq12.F.Inverse(t2) |
|
||||
return [2][3][2]*big.Int{ |
|
||||
fq12.F.Mul(a[0], t3), |
|
||||
fq12.F.Neg(fq12.F.Mul(a[1], t3)), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Div performs a division on the Fq12
|
|
||||
func (fq12 Fq12) Div(a, b [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
return fq12.Mul(a, fq12.Inverse(b)) |
|
||||
} |
|
||||
|
|
||||
// Square performs a square operation on the Fq12
|
|
||||
func (fq12 Fq12) Square(a [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
ab := fq12.F.Mul(a[0], a[1]) |
|
||||
|
|
||||
return [2][3][2]*big.Int{ |
|
||||
fq12.F.Sub( |
|
||||
fq12.F.Mul( |
|
||||
fq12.F.Add(a[0], a[1]), |
|
||||
fq12.F.Add( |
|
||||
a[0], |
|
||||
fq12.mulByNonResidue(a[1]))), |
|
||||
fq12.F.Add( |
|
||||
ab, |
|
||||
fq12.mulByNonResidue(ab))), |
|
||||
fq12.F.Add(ab, ab), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (fq12 Fq12) Exp(base [2][3][2]*big.Int, e *big.Int) [2][3][2]*big.Int { |
|
||||
res := fq12.One() |
|
||||
rem := fq12.Fq2.F.Copy(e) |
|
||||
exp := base |
|
||||
|
|
||||
for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) { |
|
||||
if BigIsOdd(rem) { |
|
||||
res = fq12.Mul(res, exp) |
|
||||
} |
|
||||
exp = fq12.Square(exp) |
|
||||
rem = new(big.Int).Rsh(rem, 1) |
|
||||
} |
|
||||
return res |
|
||||
} |
|
||||
func (fq12 Fq12) Affine(a [2][3][2]*big.Int) [2][3][2]*big.Int { |
|
||||
return [2][3][2]*big.Int{ |
|
||||
fq12.F.Affine(a[0]), |
|
||||
fq12.F.Affine(a[1]), |
|
||||
} |
|
||||
} |
|
||||
func (fq12 Fq12) Equal(a, b [2][3][2]*big.Int) bool { |
|
||||
return fq12.F.Equal(a[0], b[0]) && fq12.F.Equal(a[1], b[1]) |
|
||||
} |
|
@ -1,154 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"math/big" |
|
||||
) |
|
||||
|
|
||||
// Fq2 is Field 2
|
|
||||
type Fq2 struct { |
|
||||
F Fq |
|
||||
NonResidue *big.Int |
|
||||
} |
|
||||
|
|
||||
// NewFq2 generates a new Fq2
|
|
||||
func NewFq2(f Fq, nonResidue *big.Int) Fq2 { |
|
||||
fq2 := Fq2{ |
|
||||
f, |
|
||||
nonResidue, |
|
||||
} |
|
||||
return fq2 |
|
||||
} |
|
||||
|
|
||||
// Zero returns a Zero value on the Fq2
|
|
||||
func (fq2 Fq2) Zero() [2]*big.Int { |
|
||||
return [2]*big.Int{fq2.F.Zero(), fq2.F.Zero()} |
|
||||
} |
|
||||
|
|
||||
// One returns a One value on the Fq2
|
|
||||
func (fq2 Fq2) One() [2]*big.Int { |
|
||||
return [2]*big.Int{fq2.F.One(), fq2.F.Zero()} |
|
||||
} |
|
||||
|
|
||||
func (fq2 Fq2) mulByNonResidue(a *big.Int) *big.Int { |
|
||||
return fq2.F.Mul(fq2.NonResidue, a) |
|
||||
} |
|
||||
|
|
||||
// Add performs an addition on the Fq2
|
|
||||
func (fq2 Fq2) Add(a, b [2]*big.Int) [2]*big.Int { |
|
||||
return [2]*big.Int{ |
|
||||
fq2.F.Add(a[0], b[0]), |
|
||||
fq2.F.Add(a[1], b[1]), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Double performs a doubling on the Fq2
|
|
||||
func (fq2 Fq2) Double(a [2]*big.Int) [2]*big.Int { |
|
||||
return fq2.Add(a, a) |
|
||||
} |
|
||||
|
|
||||
// Sub performs a substraction on the Fq2
|
|
||||
func (fq2 Fq2) Sub(a, b [2]*big.Int) [2]*big.Int { |
|
||||
return [2]*big.Int{ |
|
||||
fq2.F.Sub(a[0], b[0]), |
|
||||
fq2.F.Sub(a[1], b[1]), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Neg performs a negation on the Fq2
|
|
||||
func (fq2 Fq2) Neg(a [2]*big.Int) [2]*big.Int { |
|
||||
return fq2.Sub(fq2.Zero(), a) |
|
||||
} |
|
||||
|
|
||||
// Mul performs a multiplication on the Fq2
|
|
||||
func (fq2 Fq2) Mul(a, b [2]*big.Int) [2]*big.Int { |
|
||||
// Multiplication and Squaring on Pairing-Friendly.pdf; Section 3 (Karatsuba)
|
|
||||
// https://pdfs.semanticscholar.org/3e01/de88d7428076b2547b60072088507d881bf1.pdf
|
|
||||
v0 := fq2.F.Mul(a[0], b[0]) |
|
||||
v1 := fq2.F.Mul(a[1], b[1]) |
|
||||
return [2]*big.Int{ |
|
||||
fq2.F.Add(v0, fq2.mulByNonResidue(v1)), |
|
||||
fq2.F.Sub( |
|
||||
fq2.F.Mul( |
|
||||
fq2.F.Add(a[0], a[1]), |
|
||||
fq2.F.Add(b[0], b[1])), |
|
||||
fq2.F.Add(v0, v1)), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (fq2 Fq2) MulScalar(p [2]*big.Int, e *big.Int) [2]*big.Int { |
|
||||
// for more possible implementations see g2.go file, at the function g2.MulScalar()
|
|
||||
|
|
||||
q := fq2.Zero() |
|
||||
d := fq2.F.Copy(e) |
|
||||
r := p |
|
||||
|
|
||||
foundone := false |
|
||||
for i := d.BitLen(); i >= 0; i-- { |
|
||||
if foundone { |
|
||||
q = fq2.Double(q) |
|
||||
} |
|
||||
if d.Bit(i) == 1 { |
|
||||
foundone = true |
|
||||
q = fq2.Add(q, r) |
|
||||
} |
|
||||
} |
|
||||
return q |
|
||||
} |
|
||||
|
|
||||
// Inverse returns the inverse on the Fq2
|
|
||||
func (fq2 Fq2) Inverse(a [2]*big.Int) [2]*big.Int { |
|
||||
// High-Speed Software Implementation of the Optimal Ate Pairing over Barreto–Naehrig Curves .pdf
|
|
||||
// https://eprint.iacr.org/2010/354.pdf , algorithm 8
|
|
||||
t0 := fq2.F.Square(a[0]) |
|
||||
t1 := fq2.F.Square(a[1]) |
|
||||
t2 := fq2.F.Sub(t0, fq2.mulByNonResidue(t1)) |
|
||||
t3 := fq2.F.Inverse(t2) |
|
||||
return [2]*big.Int{ |
|
||||
fq2.F.Mul(a[0], t3), |
|
||||
fq2.F.Neg(fq2.F.Mul(a[1], t3)), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Div performs a division on the Fq2
|
|
||||
func (fq2 Fq2) Div(a, b [2]*big.Int) [2]*big.Int { |
|
||||
return fq2.Mul(a, fq2.Inverse(b)) |
|
||||
} |
|
||||
|
|
||||
// Square performs a square operation on the Fq2
|
|
||||
func (fq2 Fq2) Square(a [2]*big.Int) [2]*big.Int { |
|
||||
// https://pdfs.semanticscholar.org/3e01/de88d7428076b2547b60072088507d881bf1.pdf , complex squaring
|
|
||||
ab := fq2.F.Mul(a[0], a[1]) |
|
||||
return [2]*big.Int{ |
|
||||
fq2.F.Sub( |
|
||||
fq2.F.Mul( |
|
||||
fq2.F.Add(a[0], a[1]), |
|
||||
fq2.F.Add( |
|
||||
a[0], |
|
||||
fq2.mulByNonResidue(a[1]))), |
|
||||
fq2.F.Add( |
|
||||
ab, |
|
||||
fq2.mulByNonResidue(ab))), |
|
||||
fq2.F.Add(ab, ab), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (fq2 Fq2) IsZero(a [2]*big.Int) bool { |
|
||||
return fq2.F.IsZero(a[0]) && fq2.F.IsZero(a[1]) |
|
||||
} |
|
||||
|
|
||||
func (fq2 Fq2) Affine(a [2]*big.Int) [2]*big.Int { |
|
||||
return [2]*big.Int{ |
|
||||
fq2.F.Affine(a[0]), |
|
||||
fq2.F.Affine(a[1]), |
|
||||
} |
|
||||
} |
|
||||
func (fq2 Fq2) Equal(a, b [2]*big.Int) bool { |
|
||||
return fq2.F.Equal(a[0], b[0]) && fq2.F.Equal(a[1], b[1]) |
|
||||
} |
|
||||
|
|
||||
func (fq2 Fq2) Copy(a [2]*big.Int) [2]*big.Int { |
|
||||
return [2]*big.Int{ |
|
||||
fq2.F.Copy(a[0]), |
|
||||
fq2.F.Copy(a[1]), |
|
||||
} |
|
||||
} |
|
@ -1,192 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"bytes" |
|
||||
"math/big" |
|
||||
) |
|
||||
|
|
||||
// Fq6 is Field 6
|
|
||||
type Fq6 struct { |
|
||||
F Fq2 |
|
||||
NonResidue [2]*big.Int |
|
||||
} |
|
||||
|
|
||||
// NewFq6 generates a new Fq6
|
|
||||
func NewFq6(f Fq2, nonResidue [2]*big.Int) Fq6 { |
|
||||
fq6 := Fq6{ |
|
||||
f, |
|
||||
nonResidue, |
|
||||
} |
|
||||
return fq6 |
|
||||
} |
|
||||
|
|
||||
// Zero returns a Zero value on the Fq6
|
|
||||
func (fq6 Fq6) Zero() [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{fq6.F.Zero(), fq6.F.Zero(), fq6.F.Zero()} |
|
||||
} |
|
||||
|
|
||||
// One returns a One value on the Fq6
|
|
||||
func (fq6 Fq6) One() [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{fq6.F.One(), fq6.F.Zero(), fq6.F.Zero()} |
|
||||
} |
|
||||
|
|
||||
func (fq6 Fq6) mulByNonResidue(a [2]*big.Int) [2]*big.Int { |
|
||||
return fq6.F.Mul(fq6.NonResidue, a) |
|
||||
} |
|
||||
|
|
||||
// Add performs an addition on the Fq6
|
|
||||
func (fq6 Fq6) Add(a, b [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{ |
|
||||
fq6.F.Add(a[0], b[0]), |
|
||||
fq6.F.Add(a[1], b[1]), |
|
||||
fq6.F.Add(a[2], b[2]), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (fq6 Fq6) Double(a [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return fq6.Add(a, a) |
|
||||
} |
|
||||
|
|
||||
// Sub performs a substraction on the Fq6
|
|
||||
func (fq6 Fq6) Sub(a, b [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{ |
|
||||
fq6.F.Sub(a[0], b[0]), |
|
||||
fq6.F.Sub(a[1], b[1]), |
|
||||
fq6.F.Sub(a[2], b[2]), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Neg performs a negation on the Fq6
|
|
||||
func (fq6 Fq6) Neg(a [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return fq6.Sub(fq6.Zero(), a) |
|
||||
} |
|
||||
|
|
||||
// Mul performs a multiplication on the Fq6
|
|
||||
func (fq6 Fq6) Mul(a, b [3][2]*big.Int) [3][2]*big.Int { |
|
||||
v0 := fq6.F.Mul(a[0], b[0]) |
|
||||
v1 := fq6.F.Mul(a[1], b[1]) |
|
||||
v2 := fq6.F.Mul(a[2], b[2]) |
|
||||
return [3][2]*big.Int{ |
|
||||
fq6.F.Add( |
|
||||
v0, |
|
||||
fq6.mulByNonResidue( |
|
||||
fq6.F.Sub( |
|
||||
fq6.F.Mul( |
|
||||
fq6.F.Add(a[1], a[2]), |
|
||||
fq6.F.Add(b[1], b[2])), |
|
||||
fq6.F.Add(v1, v2)))), |
|
||||
|
|
||||
fq6.F.Add( |
|
||||
fq6.F.Sub( |
|
||||
fq6.F.Mul( |
|
||||
fq6.F.Add(a[0], a[1]), |
|
||||
fq6.F.Add(b[0], b[1])), |
|
||||
fq6.F.Add(v0, v1)), |
|
||||
fq6.mulByNonResidue(v2)), |
|
||||
|
|
||||
fq6.F.Add( |
|
||||
fq6.F.Sub( |
|
||||
fq6.F.Mul( |
|
||||
fq6.F.Add(a[0], a[2]), |
|
||||
fq6.F.Add(b[0], b[2])), |
|
||||
fq6.F.Add(v0, v2)), |
|
||||
v1), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (fq6 Fq6) MulScalar(base [3][2]*big.Int, e *big.Int) [3][2]*big.Int { |
|
||||
// for more possible implementations see g2.go file, at the function g2.MulScalar()
|
|
||||
|
|
||||
res := fq6.Zero() |
|
||||
rem := e |
|
||||
exp := base |
|
||||
|
|
||||
for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) { |
|
||||
// if rem % 2 == 1
|
|
||||
if bytes.Equal(new(big.Int).Rem(rem, big.NewInt(int64(2))).Bytes(), big.NewInt(int64(1)).Bytes()) { |
|
||||
res = fq6.Add(res, exp) |
|
||||
} |
|
||||
exp = fq6.Double(exp) |
|
||||
rem = rem.Rsh(rem, 1) // rem = rem >> 1
|
|
||||
} |
|
||||
return res |
|
||||
} |
|
||||
|
|
||||
// Inverse returns the inverse on the Fq6
|
|
||||
func (fq6 Fq6) Inverse(a [3][2]*big.Int) [3][2]*big.Int { |
|
||||
t0 := fq6.F.Square(a[0]) |
|
||||
t1 := fq6.F.Square(a[1]) |
|
||||
t2 := fq6.F.Square(a[2]) |
|
||||
t3 := fq6.F.Mul(a[0], a[1]) |
|
||||
t4 := fq6.F.Mul(a[0], a[2]) |
|
||||
t5 := fq6.F.Mul(a[1], a[2]) |
|
||||
|
|
||||
c0 := fq6.F.Sub(t0, fq6.mulByNonResidue(t5)) |
|
||||
c1 := fq6.F.Sub(fq6.mulByNonResidue(t2), t3) |
|
||||
c2 := fq6.F.Sub(t1, t4) |
|
||||
|
|
||||
t6 := fq6.F.Inverse( |
|
||||
fq6.F.Add( |
|
||||
fq6.F.Mul(a[0], c0), |
|
||||
fq6.mulByNonResidue( |
|
||||
fq6.F.Add( |
|
||||
fq6.F.Mul(a[2], c1), |
|
||||
fq6.F.Mul(a[1], c2))))) |
|
||||
return [3][2]*big.Int{ |
|
||||
fq6.F.Mul(t6, c0), |
|
||||
fq6.F.Mul(t6, c1), |
|
||||
fq6.F.Mul(t6, c2), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Div performs a division on the Fq6
|
|
||||
func (fq6 Fq6) Div(a, b [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return fq6.Mul(a, fq6.Inverse(b)) |
|
||||
} |
|
||||
|
|
||||
// Square performs a square operation on the Fq6
|
|
||||
func (fq6 Fq6) Square(a [3][2]*big.Int) [3][2]*big.Int { |
|
||||
s0 := fq6.F.Square(a[0]) |
|
||||
ab := fq6.F.Mul(a[0], a[1]) |
|
||||
s1 := fq6.F.Add(ab, ab) |
|
||||
s2 := fq6.F.Square( |
|
||||
fq6.F.Add( |
|
||||
fq6.F.Sub(a[0], a[1]), |
|
||||
a[2])) |
|
||||
bc := fq6.F.Mul(a[1], a[2]) |
|
||||
s3 := fq6.F.Add(bc, bc) |
|
||||
s4 := fq6.F.Square(a[2]) |
|
||||
|
|
||||
return [3][2]*big.Int{ |
|
||||
fq6.F.Add( |
|
||||
s0, |
|
||||
fq6.mulByNonResidue(s3)), |
|
||||
fq6.F.Add( |
|
||||
s1, |
|
||||
fq6.mulByNonResidue(s4)), |
|
||||
fq6.F.Sub( |
|
||||
fq6.F.Add( |
|
||||
fq6.F.Add(s1, s2), |
|
||||
s3), |
|
||||
fq6.F.Add(s0, s4)), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (fq6 Fq6) Affine(a [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{ |
|
||||
fq6.F.Affine(a[0]), |
|
||||
fq6.F.Affine(a[1]), |
|
||||
fq6.F.Affine(a[2]), |
|
||||
} |
|
||||
} |
|
||||
func (fq6 Fq6) Equal(a, b [3][2]*big.Int) bool { |
|
||||
return fq6.F.Equal(a[0], b[0]) && fq6.F.Equal(a[1], b[1]) && fq6.F.Equal(a[2], b[2]) |
|
||||
} |
|
||||
|
|
||||
func (fq6 Fq6) Copy(a [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{ |
|
||||
fq6.F.Copy(a[0]), |
|
||||
fq6.F.Copy(a[1]), |
|
||||
fq6.F.Copy(a[2]), |
|
||||
} |
|
||||
} |
|
@ -1,160 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"math/big" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
) |
|
||||
|
|
||||
func iToBig(a int) *big.Int { |
|
||||
return big.NewInt(int64(a)) |
|
||||
} |
|
||||
|
|
||||
func iiToBig(a, b int) [2]*big.Int { |
|
||||
return [2]*big.Int{iToBig(a), iToBig(b)} |
|
||||
} |
|
||||
|
|
||||
func iiiToBig(a, b int) [2]*big.Int { |
|
||||
return [2]*big.Int{iToBig(a), iToBig(b)} |
|
||||
} |
|
||||
|
|
||||
func TestFq1(t *testing.T) { |
|
||||
fq1 := NewFq(iToBig(7)) |
|
||||
|
|
||||
res := fq1.Add(iToBig(4), iToBig(4)) |
|
||||
assert.Equal(t, iToBig(1), fq1.Affine(res)) |
|
||||
|
|
||||
res = fq1.Double(iToBig(5)) |
|
||||
assert.Equal(t, iToBig(3), fq1.Affine(res)) |
|
||||
|
|
||||
res = fq1.Sub(iToBig(5), iToBig(7)) |
|
||||
assert.Equal(t, iToBig(5), fq1.Affine(res)) |
|
||||
|
|
||||
res = fq1.Neg(iToBig(5)) |
|
||||
assert.Equal(t, iToBig(2), fq1.Affine(res)) |
|
||||
|
|
||||
res = fq1.Mul(iToBig(5), iToBig(11)) |
|
||||
assert.Equal(t, iToBig(6), fq1.Affine(res)) |
|
||||
|
|
||||
res = fq1.Inverse(iToBig(4)) |
|
||||
assert.Equal(t, iToBig(2), res) |
|
||||
|
|
||||
res = fq1.Square(iToBig(5)) |
|
||||
assert.Equal(t, iToBig(4), res) |
|
||||
} |
|
||||
|
|
||||
func TestFq2(t *testing.T) { |
|
||||
fq1 := NewFq(iToBig(7)) |
|
||||
nonResidueFq2str := "-1" // i/j
|
|
||||
nonResidueFq2, ok := new(big.Int).SetString(nonResidueFq2str, 10) |
|
||||
assert.True(t, ok) |
|
||||
assert.Equal(t, nonResidueFq2.String(), nonResidueFq2str) |
|
||||
|
|
||||
fq2 := Fq2{fq1, nonResidueFq2} |
|
||||
|
|
||||
res := fq2.Add(iiToBig(4, 4), iiToBig(3, 4)) |
|
||||
assert.Equal(t, iiToBig(0, 1), fq2.Affine(res)) |
|
||||
|
|
||||
res = fq2.Double(iiToBig(5, 3)) |
|
||||
assert.Equal(t, iiToBig(3, 6), fq2.Affine(res)) |
|
||||
|
|
||||
res = fq2.Sub(iiToBig(5, 3), iiToBig(7, 2)) |
|
||||
assert.Equal(t, iiToBig(5, 1), fq2.Affine(res)) |
|
||||
|
|
||||
res = fq2.Neg(iiToBig(4, 4)) |
|
||||
assert.Equal(t, iiToBig(3, 3), fq2.Affine(res)) |
|
||||
|
|
||||
res = fq2.Mul(iiToBig(4, 4), iiToBig(3, 4)) |
|
||||
assert.Equal(t, iiToBig(3, 0), fq2.Affine(res)) |
|
||||
|
|
||||
res = fq2.Inverse(iiToBig(4, 4)) |
|
||||
assert.Equal(t, iiToBig(1, 6), fq2.Affine(res)) |
|
||||
|
|
||||
res = fq2.Square(iiToBig(4, 4)) |
|
||||
assert.Equal(t, iiToBig(0, 4), fq2.Affine(res)) |
|
||||
res2 := fq2.Mul(iiToBig(4, 4), iiToBig(4, 4)) |
|
||||
assert.Equal(t, fq2.Affine(res), fq2.Affine(res2)) |
|
||||
assert.True(t, fq2.Equal(res, res2)) |
|
||||
|
|
||||
res = fq2.Square(iiToBig(3, 5)) |
|
||||
assert.Equal(t, iiToBig(5, 2), fq2.Affine(res)) |
|
||||
res2 = fq2.Mul(iiToBig(3, 5), iiToBig(3, 5)) |
|
||||
assert.Equal(t, fq2.Affine(res), fq2.Affine(res2)) |
|
||||
} |
|
||||
|
|
||||
func TestFq6(t *testing.T) { |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
a := [3][2]*big.Int{ |
|
||||
iiToBig(1, 2), |
|
||||
iiToBig(3, 4), |
|
||||
iiToBig(5, 6)} |
|
||||
b := [3][2]*big.Int{ |
|
||||
iiToBig(12, 11), |
|
||||
iiToBig(10, 9), |
|
||||
iiToBig(8, 7)} |
|
||||
|
|
||||
mulRes := bn128.Fq6.Mul(a, b) |
|
||||
divRes := bn128.Fq6.Div(mulRes, b) |
|
||||
assert.Equal(t, bn128.Fq6.Affine(a), bn128.Fq6.Affine(divRes)) |
|
||||
} |
|
||||
|
|
||||
func TestFq12(t *testing.T) { |
|
||||
q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
|
|
||||
assert.True(t, ok) |
|
||||
fq1 := NewFq(q) |
|
||||
nonResidueFq2, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
|
|
||||
assert.True(t, ok) |
|
||||
nonResidueFq6 := iiToBig(9, 1) |
|
||||
|
|
||||
fq2 := Fq2{fq1, nonResidueFq2} |
|
||||
fq6 := Fq6{fq2, nonResidueFq6} |
|
||||
fq12 := Fq12{fq6, fq2, nonResidueFq6} |
|
||||
|
|
||||
a := [2][3][2]*big.Int{ |
|
||||
{ |
|
||||
iiToBig(1, 2), |
|
||||
iiToBig(3, 4), |
|
||||
iiToBig(5, 6), |
|
||||
}, |
|
||||
{ |
|
||||
iiToBig(7, 8), |
|
||||
iiToBig(9, 10), |
|
||||
iiToBig(11, 12), |
|
||||
}, |
|
||||
} |
|
||||
b := [2][3][2]*big.Int{ |
|
||||
{ |
|
||||
iiToBig(12, 11), |
|
||||
iiToBig(10, 9), |
|
||||
iiToBig(8, 7), |
|
||||
}, |
|
||||
{ |
|
||||
iiToBig(6, 5), |
|
||||
iiToBig(4, 3), |
|
||||
iiToBig(2, 1), |
|
||||
}, |
|
||||
} |
|
||||
|
|
||||
res := fq12.Add(a, b) |
|
||||
assert.Equal(t, |
|
||||
[2][3][2]*big.Int{ |
|
||||
{ |
|
||||
iiToBig(13, 13), |
|
||||
iiToBig(13, 13), |
|
||||
iiToBig(13, 13), |
|
||||
}, |
|
||||
{ |
|
||||
iiToBig(13, 13), |
|
||||
iiToBig(13, 13), |
|
||||
iiToBig(13, 13), |
|
||||
}, |
|
||||
}, |
|
||||
res) |
|
||||
|
|
||||
mulRes := fq12.Mul(a, b) |
|
||||
divRes := fq12.Div(mulRes, b) |
|
||||
assert.Equal(t, fq12.Affine(a), fq12.Affine(divRes)) |
|
||||
} |
|
@ -1,191 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"math/big" |
|
||||
) |
|
||||
|
|
||||
type G1 struct { |
|
||||
F Fq |
|
||||
G [3]*big.Int |
|
||||
} |
|
||||
|
|
||||
func NewG1(f Fq, g [2]*big.Int) G1 { |
|
||||
var g1 G1 |
|
||||
g1.F = f |
|
||||
g1.G = [3]*big.Int{ |
|
||||
g[0], |
|
||||
g[1], |
|
||||
g1.F.One(), |
|
||||
} |
|
||||
return g1 |
|
||||
} |
|
||||
|
|
||||
func (g1 G1) Zero() [2]*big.Int { |
|
||||
return [2]*big.Int{g1.F.Zero(), g1.F.Zero()} |
|
||||
} |
|
||||
func (g1 G1) IsZero(p [3]*big.Int) bool { |
|
||||
return g1.F.IsZero(p[2]) |
|
||||
} |
|
||||
|
|
||||
func (g1 G1) Add(p1, p2 [3]*big.Int) [3]*big.Int { |
|
||||
|
|
||||
// https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
|
|
||||
// https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp#L208
|
|
||||
// http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3
|
|
||||
|
|
||||
if g1.IsZero(p1) { |
|
||||
return p2 |
|
||||
} |
|
||||
if g1.IsZero(p2) { |
|
||||
return p1 |
|
||||
} |
|
||||
|
|
||||
x1 := p1[0] |
|
||||
y1 := p1[1] |
|
||||
z1 := p1[2] |
|
||||
x2 := p2[0] |
|
||||
y2 := p2[1] |
|
||||
z2 := p2[2] |
|
||||
|
|
||||
z1z1 := g1.F.Square(z1) |
|
||||
z2z2 := g1.F.Square(z2) |
|
||||
|
|
||||
u1 := g1.F.Mul(x1, z2z2) |
|
||||
u2 := g1.F.Mul(x2, z1z1) |
|
||||
|
|
||||
t0 := g1.F.Mul(z2, z2z2) |
|
||||
s1 := g1.F.Mul(y1, t0) |
|
||||
|
|
||||
t1 := g1.F.Mul(z1, z1z1) |
|
||||
s2 := g1.F.Mul(y2, t1) |
|
||||
|
|
||||
h := g1.F.Sub(u2, u1) |
|
||||
t2 := g1.F.Add(h, h) |
|
||||
i := g1.F.Square(t2) |
|
||||
j := g1.F.Mul(h, i) |
|
||||
t3 := g1.F.Sub(s2, s1) |
|
||||
r := g1.F.Add(t3, t3) |
|
||||
v := g1.F.Mul(u1, i) |
|
||||
t4 := g1.F.Square(r) |
|
||||
t5 := g1.F.Add(v, v) |
|
||||
t6 := g1.F.Sub(t4, j) |
|
||||
x3 := g1.F.Sub(t6, t5) |
|
||||
t7 := g1.F.Sub(v, x3) |
|
||||
t8 := g1.F.Mul(s1, j) |
|
||||
t9 := g1.F.Add(t8, t8) |
|
||||
t10 := g1.F.Mul(r, t7) |
|
||||
|
|
||||
y3 := g1.F.Sub(t10, t9) |
|
||||
|
|
||||
t11 := g1.F.Add(z1, z2) |
|
||||
t12 := g1.F.Square(t11) |
|
||||
t13 := g1.F.Sub(t12, z1z1) |
|
||||
t14 := g1.F.Sub(t13, z2z2) |
|
||||
z3 := g1.F.Mul(t14, h) |
|
||||
|
|
||||
return [3]*big.Int{x3, y3, z3} |
|
||||
} |
|
||||
|
|
||||
func (g1 G1) Neg(p [3]*big.Int) [3]*big.Int { |
|
||||
return [3]*big.Int{ |
|
||||
p[0], |
|
||||
g1.F.Neg(p[1]), |
|
||||
p[2], |
|
||||
} |
|
||||
} |
|
||||
func (g1 G1) Sub(a, b [3]*big.Int) [3]*big.Int { |
|
||||
return g1.Add(a, g1.Neg(b)) |
|
||||
} |
|
||||
func (g1 G1) Double(p [3]*big.Int) [3]*big.Int { |
|
||||
|
|
||||
// https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
|
|
||||
// http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
|
|
||||
// https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp#L325
|
|
||||
|
|
||||
if g1.IsZero(p) { |
|
||||
return p |
|
||||
} |
|
||||
|
|
||||
a := g1.F.Square(p[0]) |
|
||||
b := g1.F.Square(p[1]) |
|
||||
c := g1.F.Square(b) |
|
||||
|
|
||||
t0 := g1.F.Add(p[0], b) |
|
||||
t1 := g1.F.Square(t0) |
|
||||
t2 := g1.F.Sub(t1, a) |
|
||||
t3 := g1.F.Sub(t2, c) |
|
||||
|
|
||||
d := g1.F.Double(t3) |
|
||||
e := g1.F.Add(g1.F.Add(a, a), a) |
|
||||
f := g1.F.Square(e) |
|
||||
|
|
||||
t4 := g1.F.Double(d) |
|
||||
x3 := g1.F.Sub(f, t4) |
|
||||
|
|
||||
t5 := g1.F.Sub(d, x3) |
|
||||
twoC := g1.F.Add(c, c) |
|
||||
fourC := g1.F.Add(twoC, twoC) |
|
||||
t6 := g1.F.Add(fourC, fourC) |
|
||||
t7 := g1.F.Mul(e, t5) |
|
||||
y3 := g1.F.Sub(t7, t6) |
|
||||
|
|
||||
t8 := g1.F.Mul(p[1], p[2]) |
|
||||
z3 := g1.F.Double(t8) |
|
||||
|
|
||||
return [3]*big.Int{x3, y3, z3} |
|
||||
} |
|
||||
|
|
||||
func (g1 G1) MulScalar(p [3]*big.Int, e *big.Int) [3]*big.Int { |
|
||||
// https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add
|
|
||||
// for more possible implementations see g2.go file, at the function g2.MulScalar()
|
|
||||
|
|
||||
q := [3]*big.Int{g1.F.Zero(), g1.F.Zero(), g1.F.Zero()} |
|
||||
d := g1.F.Copy(e) |
|
||||
r := p |
|
||||
for i := d.BitLen() - 1; i >= 0; i-- { |
|
||||
q = g1.Double(q) |
|
||||
if d.Bit(i) == 1 { |
|
||||
q = g1.Add(q, r) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return q |
|
||||
} |
|
||||
|
|
||||
func (g1 G1) Affine(p [3]*big.Int) [2]*big.Int { |
|
||||
if g1.IsZero(p) { |
|
||||
return g1.Zero() |
|
||||
} |
|
||||
|
|
||||
zinv := g1.F.Inverse(p[2]) |
|
||||
zinv2 := g1.F.Square(zinv) |
|
||||
x := g1.F.Mul(p[0], zinv2) |
|
||||
|
|
||||
zinv3 := g1.F.Mul(zinv2, zinv) |
|
||||
y := g1.F.Mul(p[1], zinv3) |
|
||||
|
|
||||
return [2]*big.Int{x, y} |
|
||||
} |
|
||||
|
|
||||
func (g1 G1) Equal(p1, p2 [3]*big.Int) bool { |
|
||||
if g1.IsZero(p1) { |
|
||||
return g1.IsZero(p2) |
|
||||
} |
|
||||
if g1.IsZero(p2) { |
|
||||
return g1.IsZero(p1) |
|
||||
} |
|
||||
|
|
||||
z1z1 := g1.F.Square(p1[2]) |
|
||||
z2z2 := g1.F.Square(p2[2]) |
|
||||
|
|
||||
u1 := g1.F.Mul(p1[0], z2z2) |
|
||||
u2 := g1.F.Mul(p2[0], z1z1) |
|
||||
|
|
||||
z1cub := g1.F.Mul(p1[2], z1z1) |
|
||||
z2cub := g1.F.Mul(p2[2], z2z2) |
|
||||
|
|
||||
s1 := g1.F.Mul(p1[1], z2cub) |
|
||||
s2 := g1.F.Mul(p2[1], z1cub) |
|
||||
|
|
||||
return g1.F.Equal(u1, u2) && g1.F.Equal(s1, s2) |
|
||||
} |
|
@ -1,31 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"math/big" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/arnaucube/cryptofun/utils" |
|
||||
"github.com/stretchr/testify/assert" |
|
||||
) |
|
||||
|
|
||||
func TestG1(t *testing.T) { |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
r1 := big.NewInt(int64(33)) |
|
||||
r2 := big.NewInt(int64(44)) |
|
||||
|
|
||||
gr1 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r1)) |
|
||||
gr2 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r2)) |
|
||||
|
|
||||
grsum1 := bn128.G1.Add(gr1, gr2) // g*33 + g*44
|
|
||||
r1r2 := bn128.Fq1.Add(r1, r2) // 33 + 44
|
|
||||
grsum2 := bn128.G1.MulScalar(bn128.G1.G, r1r2) // g * (33+44)
|
|
||||
|
|
||||
assert.True(t, bn128.G1.Equal(grsum1, grsum2)) |
|
||||
a := bn128.G1.Affine(grsum1) |
|
||||
b := bn128.G1.Affine(grsum2) |
|
||||
assert.Equal(t, a, b) |
|
||||
assert.Equal(t, "0x2f978c0ab89ebaa576866706b14787f360c4d6c3869efe5a72f7c3651a72ff00", utils.BytesToHex(a[0].Bytes())) |
|
||||
assert.Equal(t, "0x12e4ba7f0edca8b4fa668fe153aebd908d322dc26ad964d4cd314795844b62b2", utils.BytesToHex(a[1].Bytes())) |
|
||||
} |
|
@ -1,221 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"math/big" |
|
||||
) |
|
||||
|
|
||||
type G2 struct { |
|
||||
F Fq2 |
|
||||
G [3][2]*big.Int |
|
||||
} |
|
||||
|
|
||||
func NewG2(f Fq2, g [2][2]*big.Int) G2 { |
|
||||
var g2 G2 |
|
||||
g2.F = f |
|
||||
g2.G = [3][2]*big.Int{ |
|
||||
g[0], |
|
||||
g[1], |
|
||||
g2.F.One(), |
|
||||
} |
|
||||
return g2 |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) Zero() [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{g2.F.Zero(), g2.F.One(), g2.F.Zero()} |
|
||||
} |
|
||||
func (g2 G2) IsZero(p [3][2]*big.Int) bool { |
|
||||
return g2.F.IsZero(p[2]) |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) Add(p1, p2 [3][2]*big.Int) [3][2]*big.Int { |
|
||||
|
|
||||
// https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
|
|
||||
// https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.cpp#L208
|
|
||||
// http://hyperelliptic.org/EFD/g2p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3
|
|
||||
|
|
||||
if g2.IsZero(p1) { |
|
||||
return p2 |
|
||||
} |
|
||||
if g2.IsZero(p2) { |
|
||||
return p1 |
|
||||
} |
|
||||
|
|
||||
x1 := p1[0] |
|
||||
y1 := p1[1] |
|
||||
z1 := p1[2] |
|
||||
x2 := p2[0] |
|
||||
y2 := p2[1] |
|
||||
z2 := p2[2] |
|
||||
|
|
||||
z1z1 := g2.F.Square(z1) |
|
||||
z2z2 := g2.F.Square(z2) |
|
||||
|
|
||||
u1 := g2.F.Mul(x1, z2z2) |
|
||||
u2 := g2.F.Mul(x2, z1z1) |
|
||||
|
|
||||
t0 := g2.F.Mul(z2, z2z2) |
|
||||
s1 := g2.F.Mul(y1, t0) |
|
||||
|
|
||||
t1 := g2.F.Mul(z1, z1z1) |
|
||||
s2 := g2.F.Mul(y2, t1) |
|
||||
|
|
||||
h := g2.F.Sub(u2, u1) |
|
||||
t2 := g2.F.Add(h, h) |
|
||||
i := g2.F.Square(t2) |
|
||||
j := g2.F.Mul(h, i) |
|
||||
t3 := g2.F.Sub(s2, s1) |
|
||||
r := g2.F.Add(t3, t3) |
|
||||
v := g2.F.Mul(u1, i) |
|
||||
t4 := g2.F.Square(r) |
|
||||
t5 := g2.F.Add(v, v) |
|
||||
t6 := g2.F.Sub(t4, j) |
|
||||
x3 := g2.F.Sub(t6, t5) |
|
||||
t7 := g2.F.Sub(v, x3) |
|
||||
t8 := g2.F.Mul(s1, j) |
|
||||
t9 := g2.F.Add(t8, t8) |
|
||||
t10 := g2.F.Mul(r, t7) |
|
||||
|
|
||||
y3 := g2.F.Sub(t10, t9) |
|
||||
|
|
||||
t11 := g2.F.Add(z1, z2) |
|
||||
t12 := g2.F.Square(t11) |
|
||||
t13 := g2.F.Sub(t12, z1z1) |
|
||||
t14 := g2.F.Sub(t13, z2z2) |
|
||||
z3 := g2.F.Mul(t14, h) |
|
||||
|
|
||||
return [3][2]*big.Int{x3, y3, z3} |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) Neg(p [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return [3][2]*big.Int{ |
|
||||
p[0], |
|
||||
g2.F.Neg(p[1]), |
|
||||
p[2], |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) Sub(a, b [3][2]*big.Int) [3][2]*big.Int { |
|
||||
return g2.Add(a, g2.Neg(b)) |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) Double(p [3][2]*big.Int) [3][2]*big.Int { |
|
||||
|
|
||||
// https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
|
|
||||
// http://hyperelliptic.org/EFD/g2p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
|
|
||||
// https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.cpp#L325
|
|
||||
|
|
||||
if g2.IsZero(p) { |
|
||||
return p |
|
||||
} |
|
||||
|
|
||||
a := g2.F.Square(p[0]) |
|
||||
b := g2.F.Square(p[1]) |
|
||||
c := g2.F.Square(b) |
|
||||
|
|
||||
t0 := g2.F.Add(p[0], b) |
|
||||
t1 := g2.F.Square(t0) |
|
||||
t2 := g2.F.Sub(t1, a) |
|
||||
t3 := g2.F.Sub(t2, c) |
|
||||
|
|
||||
d := g2.F.Double(t3) |
|
||||
e := g2.F.Add(g2.F.Add(a, a), a) |
|
||||
f := g2.F.Square(e) |
|
||||
|
|
||||
t4 := g2.F.Double(d) |
|
||||
x3 := g2.F.Sub(f, t4) |
|
||||
|
|
||||
t5 := g2.F.Sub(d, x3) |
|
||||
twoC := g2.F.Add(c, c) |
|
||||
fourC := g2.F.Add(twoC, twoC) |
|
||||
t6 := g2.F.Add(fourC, fourC) |
|
||||
t7 := g2.F.Mul(e, t5) |
|
||||
y3 := g2.F.Sub(t7, t6) |
|
||||
|
|
||||
t8 := g2.F.Mul(p[1], p[2]) |
|
||||
z3 := g2.F.Double(t8) |
|
||||
|
|
||||
return [3][2]*big.Int{x3, y3, z3} |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) MulScalar(p [3][2]*big.Int, e *big.Int) [3][2]*big.Int { |
|
||||
// https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add
|
|
||||
|
|
||||
q := [3][2]*big.Int{g2.F.Zero(), g2.F.Zero(), g2.F.Zero()} |
|
||||
d := g2.F.F.Copy(e) // d := e
|
|
||||
r := p |
|
||||
|
|
||||
/* |
|
||||
here are three possible implementations: |
|
||||
*/ |
|
||||
|
|
||||
/* index decreasing: */ |
|
||||
for i := d.BitLen() - 1; i >= 0; i-- { |
|
||||
q = g2.Double(q) |
|
||||
if d.Bit(i) == 1 { |
|
||||
q = g2.Add(q, r) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/* index increasing: */ |
|
||||
// for i := 0; i <= d.BitLen(); i++ {
|
|
||||
// if d.Bit(i) == 1 {
|
|
||||
// q = g2.Add(q, r)
|
|
||||
// }
|
|
||||
// r = g2.Double(r)
|
|
||||
// }
|
|
||||
|
|
||||
// foundone := false
|
|
||||
// for i := d.BitLen(); i >= 0; i-- {
|
|
||||
// if foundone {
|
|
||||
// q = g2.Double(q)
|
|
||||
// }
|
|
||||
// if d.Bit(i) == 1 {
|
|
||||
// foundone = true
|
|
||||
// q = g2.Add(q, r)
|
|
||||
// }
|
|
||||
// }
|
|
||||
|
|
||||
return q |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) Affine(p [3][2]*big.Int) [3][2]*big.Int { |
|
||||
if g2.IsZero(p) { |
|
||||
return g2.Zero() |
|
||||
} |
|
||||
|
|
||||
zinv := g2.F.Inverse(p[2]) |
|
||||
zinv2 := g2.F.Square(zinv) |
|
||||
x := g2.F.Mul(p[0], zinv2) |
|
||||
|
|
||||
zinv3 := g2.F.Mul(zinv2, zinv) |
|
||||
y := g2.F.Mul(p[1], zinv3) |
|
||||
|
|
||||
return [3][2]*big.Int{ |
|
||||
g2.F.Affine(x), |
|
||||
g2.F.Affine(y), |
|
||||
g2.F.One(), |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func (g2 G2) Equal(p1, p2 [3][2]*big.Int) bool { |
|
||||
if g2.IsZero(p1) { |
|
||||
return g2.IsZero(p2) |
|
||||
} |
|
||||
if g2.IsZero(p2) { |
|
||||
return g2.IsZero(p1) |
|
||||
} |
|
||||
|
|
||||
z1z1 := g2.F.Square(p1[2]) |
|
||||
z2z2 := g2.F.Square(p2[2]) |
|
||||
|
|
||||
u1 := g2.F.Mul(p1[0], z2z2) |
|
||||
u2 := g2.F.Mul(p2[0], z1z1) |
|
||||
|
|
||||
z1cub := g2.F.Mul(p1[2], z1z1) |
|
||||
z2cub := g2.F.Mul(p2[2], z2z2) |
|
||||
|
|
||||
s1 := g2.F.Mul(p1[1], z2cub) |
|
||||
s2 := g2.F.Mul(p2[1], z1cub) |
|
||||
|
|
||||
return g2.F.Equal(u1, u2) && g2.F.Equal(s1, s2) |
|
||||
} |
|
@ -1,24 +0,0 @@ |
|||||
package bn128 |
|
||||
|
|
||||
import ( |
|
||||
"math/big" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
) |
|
||||
|
|
||||
func TestG2(t *testing.T) { |
|
||||
bn128, err := NewBn128() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
r1 := big.NewInt(int64(33)) |
|
||||
r2 := big.NewInt(int64(44)) |
|
||||
|
|
||||
gr1 := bn128.G2.Affine(bn128.G2.MulScalar(bn128.G2.G, r1)) |
|
||||
gr2 := bn128.G2.Affine(bn128.G2.MulScalar(bn128.G2.G, r2)) |
|
||||
|
|
||||
grsum1 := bn128.G2.Affine(bn128.G2.Add(gr1, gr2)) |
|
||||
r1r2 := bn128.Fq1.Affine(bn128.Fq1.Add(r1, r2)) |
|
||||
grsum2 := bn128.G2.Affine(bn128.G2.MulScalar(bn128.G2.G, r1r2)) |
|
||||
assert.True(t, bn128.G2.Equal(grsum1, grsum2)) |
|
||||
} |
|