Created
December 20, 2016 23:01
-
-
Save RFV/94fb2ad97f6a838e30e3c91ea154a346 to your computer and use it in GitHub Desktop.
Pairing Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
library Pairing { | |
struct G1Point { | |
uint X; | |
uint Y; | |
uint Z; | |
} | |
struct G2Point { | |
uint[2] X; | |
uint[2] Y; | |
uint[2] Z; | |
} | |
function add(G1Point p1, G1Point p2) internal returns (G1Point r) { | |
uint[6] memory input; | |
input[0] = p1.X; | |
input[1] = p1.Y; | |
input[2] = p1.Z; | |
input[3] = p2.X; | |
input[4] = p2.Y; | |
input[5] = p2.Z; | |
bool success; | |
assembly { | |
success := call(gas, 0x20, 0, input, 0xc0, r, 0x60) | |
} | |
if (!success) throw; | |
} | |
function mul(G1Point p, uint s) internal returns (G1Point r) { | |
uint[4] memory input; | |
input[0] = s; | |
input[1] = p.X; | |
input[2] = p.Y; | |
input[3] = p.Z; | |
bool success; | |
assembly { | |
success := call(gas, 0x21, 0, input, 0x80, r, 0x60) | |
} | |
if (!success) throw; | |
} | |
function pairing(G1Point[] p1, G2Point[] p2) internal returns (bool) { | |
if (p1.length != p2.length) throw; | |
uint inputSize = p1.length * 9; | |
uint[] memory input = new uint[](inputSize); | |
for (uint i = 0; i < p1.length; i++) | |
{ | |
input[i * 9 + 0] = p1[i].X; | |
input[i * 9 + 1] = p1[i].Y; | |
input[i * 9 + 2] = p1[i].Z; | |
input[i * 9 + 3] = p2[i].X[0]; | |
input[i * 9 + 4] = p2[i].X[1]; | |
input[i * 9 + 5] = p2[i].Y[0]; | |
input[i * 9 + 6] = p2[i].Y[1]; | |
input[i * 9 + 7] = p2[i].Z[0]; | |
input[i * 9 + 8] = p2[i].Z[1]; | |
} | |
uint[1] memory out; | |
bool success; | |
assembly { | |
success := call(gas, 0x22, 0, input, mul(inputSize, 0x20), out, 0x20) | |
} | |
if (!success) throw; | |
return out[0] != 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment