Created
September 19, 2020 08:16
-
-
Save mratsim/c7f34fa6f6fffa5bd9613848de814d38 to your computer and use it in GitHub Desktop.
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
import | |
# Internal | |
./milagro, ./common | |
proc PAIR_BLS12381_double(A: var ECP2_BLS12381, a, b, c: var FP2_BLS12381) {.importc, cdecl.} | |
proc PAIR_BLS12381_add(A, B: var ECP2_BLS12381, a, b, c: var FP2_BLS12381) {.importc, cdecl.} | |
proc ECP_BLS12381_set(P: var ECP_BLS12381, a, b: BIG_384): int {.importc, cdecl.} | |
proc PAIR_BLS12381_line(v: var FP12_BLS12381, A, B: var ECP2_BLS12381, Qx, Qy: ptr FP_BLS12381) {.importc, cdecl.} | |
proc PAIR_BLS12381_ate(r: var FP12_BLS12381, A: var ECP2_BLS12381, B: var ECP_BLS12381) {.importc, cdecl.} | |
func hexToFP(x: string): FP_BLS12381 = | |
## Convert a complex tuple x + iy to FP2 | |
# TODO: the result does not seem to need zero-initialization | |
var xBig: BIG_384 | |
discard xBig.fromHex(x) | |
FP_BLS12381_nres(result.addr, xBig) | |
func hexToFP2(x, y: string): FP2_BLS12381 = | |
## Convert a complex tuple x + iy to FP2 | |
# TODO: the result does not seem to need zero-initialization | |
var xBig, yBig: BIG_384 | |
discard xBig.fromHex(x) | |
discard yBig.fromHex(y) | |
result.fromBigs(xBig, yBig) | |
proc displayECP2Coord(name: string, point: ECP2_BLS12381) = | |
echo " --" | |
echo " ", name, ':' | |
# echo " In jacobian projective coordinates (x, y, z)" | |
# echo " ", point | |
echo " In affine coordinate (x, y)" | |
var x, y: FP2_BLS12381 | |
discard ECP2_BLS12381_get(x.addr, y.addr, point.unsafeAddr) | |
echo " (", $x, ", ", $y, ")" | |
proc displayECPCoord(name: string, point: ECP_BLS12381) = | |
echo " --" | |
echo " ", name, ':' | |
# echo " In jacobian projective coordinates (x, y, z)" | |
# echo " ", point | |
echo " In affine coordinate (x, y)" | |
var x, y: BIG_384 | |
discard ECP_BLS12381_get(x, y, point.unsafeAddr) | |
echo " (", $x, ", ", $y, ")" | |
proc toECP2(x, y: FP2_BLS12381): ECP2_BLS12381 = | |
## Create a point (x, y) on the G2 curve | |
let onCurve = bool ECP2_BLS12381_set(addr result, unsafeAddr x, unsafeAddr y) | |
doAssert onCurve, "The coordinates (x, y) are not on the G2 curve" | |
proc toECP(x, y: string): ECP_BLS12381 = | |
## Create a point (x, y) on the G2 curve | |
var bx, by: BIG_384 | |
doAssert bx.fromHex(x) | |
doAssert by.fromHex(y) | |
let onCurve = bool ECP_BLS12381_set(result, bx, by) | |
doAssert onCurve, "The coordinates (x, y) are not on the G1 curve" | |
let T0 = toECP2( | |
hexToFp2("0141ebfbdca40eb85b87142e130ab689c673cf60f1a3e98d69335266f30d9b8d4ac44c1038e9dcdd5393faf5c41fb78a", | |
"05cb8437535e20ecffaef7752baddf98034139c38452458baeefab379ba13dff5bf5dd71b72418717047f5b0f37da03d"), | |
hexToFp2("0503921d7f6a12805e72940b963c0cf3471c7b2a524950ca195d11062ee75ec076daf2d4bc358c4b190c0c98064fdd92", | |
"12424ac32561493f3fe3c260708a12b7c620e7be00099a974e259ddc7d1f6395c3c811cdd19f1e8dbf3e9ecfdcbab8d6") | |
) | |
var T = T0 | |
var a, b, c: FP2_BLS12381 | |
echo "\n-------------------------------------\n" | |
# PAIR_BLS12381_double(T, a, b, c) | |
# echo "a: ", $a | |
# echo "b: ", $b | |
# echo "c: ", $c | |
# echo "\n-------------------------------------\n" | |
var Q = toECP2( | |
hexToFp2("02c2d18e033b960562aae3cab37a27ce00d80ccd5ba4b7fe0e7a210245129dbec7780ccc7954725f4168aff2787776e6", | |
"139cddbccdc5e91b9623efd38c49f81a6f83f175e80b06fc374de9eb4b41dfe4ca3a230ed250fbe3a2acf73a41177fd8"), | |
hexToFp2("1787327b68159716a37440985269cf584bcb1e621d3a7202be6ea05c4cfe244aeb197642555a0645fb87bf7466b2ba48", | |
"00aa65dae3c8d732d10ecd2c50f8a1baf3001578f71c694e03866e9f3d49ac1e1ce70dd94a733534f106d4cec0eddd16") | |
) | |
# a.reset() | |
# b.reset() | |
# c.reset() | |
# T = T0 | |
# PAIR_BLS12381_add(T, Q, a, b, c) | |
# echo "a: ", $a | |
# echo "b: ", $b | |
# echo "c: ", $c | |
echo "\n-------------------------------------\n" | |
a.reset() | |
b.reset() | |
c.reset() | |
T = T0 | |
var v: FP12_BLS12381 | |
var P = toECP( | |
"052926add2207b76ca4fa57a8734416c8dc95e24501772c814278700eed6d1e4e8cf62d9c09db0fac349612b759e79a1", | |
"08ba738453bfed09cb546dbb0783dbb3a5f1f566ed67bb6be0e8c67e2e81a4cc68ee29813bb7994998f3eae0c9c6a265", | |
) | |
# ECP_BLS12381_affine(P.addr) | |
# PAIR_BLS12381_line(v, T, T, P.x.addr, P.y.addr) | |
# echo "v: ", v | |
echo "\n-------------------------------------\n" | |
PAIR_BLS12381_ate(v, Q, P) | |
displayECPCoord("P G1: ", P) | |
displayECP2Coord("Q G2: ", Q) | |
echo "\nv: ", v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment