Skip to content

Instantly share code, notes, and snippets.

@asanso
Created January 25, 2023 14:50
Show Gist options
  • Save asanso/415c01b10cca5876ab0eec92eb3ae25e to your computer and use it in GitHub Desktop.
Save asanso/415c01b10cca5876ab0eec92eb3ae25e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# BLS12-381 sage implementation"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"p = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 59,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"p in Primes()"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# G1"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Elliptic Curve defined by y^2 = x^3 + 4 over Finite Field of size 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"
]
},
"execution_count": 61,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"E1 = EllipticCurve(GF(p), [0, 4])\n",
"E1"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3 * 11^2 * 10177^2 * 859267^2 * 52437899^2 * 52435875175126190479447740508185965837690552500527637822603658699938581184513"
]
},
"execution_count": 62,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"factor(E1.order())"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"E1cofactor = 0x396c8c005555e1568c00aaab0000aaab"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"3 * 11^2 * 10177^2 * 859267^2 * 52437899^2"
]
},
"execution_count": 64,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"factor(E1cofactor)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"order = 52435875175126190479447740508185965837690552500527637822603658699938581184513"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3006311963586652835656762133460128766163260646573370711678967253222264983490593288693517955057088185894226975257034 : 190532816206323054320525886194094175795290992649002582138245867770654111424551066147978049852962252452029105685383 : 1)"
]
},
"execution_count": 77,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"Pa = E1cofactor * E1.random_point()\n",
"Pa"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"assert Pa.order() == order"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# G2"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Finite Field in i of size 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787^2"
]
},
"execution_count": 69,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"_.<I> = GF(p)[]\n",
"K.<i> = GF(p^2, modulus=I^2+1)\n",
"K"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Elliptic Curve defined by y^2 = x^3 + (4*i+4) over Finite Field in i of size 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787^2"
]
},
"execution_count": 70,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"E2 = EllipticCurve(K, [0, 4*(i+1)])\n",
"E2"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"E2order = E2.order()"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"E2cofactor = 0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"13^2 * 23^2 * 2713 * 11953 * 262069 * 402096035359507321594726366720466575392706800671181159425656785868777272553337714697862511267018014931937703598282857976535744623203249"
]
},
"execution_count": 73,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"factor(E2cofactor)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"assert order*E2cofactor == E2order"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3556974607485379506450942071186834848318733278412474767063158158341978728914539372133478039285472233842208924967491*i + 480020083071892448841766069008778524550836399842721415032934076185502765160008572267669855479828669097337168440585 : 2311949564102525834818129998052757219383414673614672803112797707945036274848515620111320569650610401297048611324209*i + 3321138396901700800616131492234377510325735277629746819727785681965062147862156539714231834693308501169920600662845 : 1)"
]
},
"execution_count": 78,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"Pb = E2cofactor * E2.random_point()\n",
"Pb"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.3",
"language": "sagemath",
"metadata": {
"cocalc": {
"description": "Open-source mathematical software system",
"priority": 10,
"url": "https://www.sagemath.org/"
}
},
"name": "sage-9.3",
"resource_dir": "/ext/jupyter/kernels/sage-9.3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment