Skip to content

Commit

Permalink
Merge pull request #5 from Modulo-P/feature/point-conversions
Browse files Browse the repository at this point in the history
Prototype of the Groth16 function
  • Loading branch information
AgustinBadi authored Feb 29, 2024
2 parents 5fcb2a3 + c74aecd commit b22fbe1
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ artifacts/
build/
# Aiken's default documentation export
docs/


node_modules
conversion/package-lock.json
16 changes: 8 additions & 8 deletions 3_fac/proof.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"pi_a": [
"2855044796369946382827590498028074399954178188404052044460392173044055949963055520226458797341961615378727946048206",
"3607648035569852498712601197821672930980634267835166890422819022801738069852659753344138199712325573228301683310722",
"1772906745093932579836240209170795378753849961020179699871382829952351871832226492308486069361021314982009562735843",
"1060554534780163267724558467040990415559388672742345068275893102509213372714145003450106197214490777822228922952656",
"1"
],
"pi_b": [
[
"2216529660448908459446533763529433361366105288599360068340479797481300540437545794646354474692017045121551582868515",
"3867914260234198041425601044308116127196057132281581166005254303219906875747339103782814281378360746808337469178926"
"1358486866497956789862128624707494045021569151043861378376291931980647848946728667100167175682451312814072710519566",
"2892568549106560709617872008499143468746473249719383613874674515863767425449396997771169971147197463981607825748431"
],
[
"1959291290564527252319212910819685638672433971337410545600749187884388402471099470819029490531580085761840095948690",
"2011071151816425671679220330755640211651471450664831295838757345432821939838685402356499133954205160730680575168228"
"3774155091396786738197845866416007916901023392079351182692381863535299038480178339300724209059839391036807743888327",
"347983957076885655346478330294523987890867898739967665064964046997306980380350203447680726102390180081745831115215"
],
[
"1",
"0"
]
],
"pi_c": [
"1295247664883995735684917166146384745983218062371398982813013851011964236053567050995264999819727337215745868836854",
"425196572825763797251425507115701065909002365213946325141666886779730764869850382851002816252507146444245309603107",
"759582637143989971983231319490726934998091227338384192733966680457083122987151635351010473067481154882106265771902",
"3759548828452159746415829615402022741023694117893369552443984303026454864934040458775681494473824000684596598390571",
"1"
],
"protocol": "groth16",
Expand Down
2 changes: 1 addition & 1 deletion 3_fac/public.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
"562",
"561",
"3"
]
170 changes: 170 additions & 0 deletions conversion/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const fs = require("fs");
const bb = require("bigint-buffer");
const ff = require("ffjavascript");

const proof = JSON.parse(fs.readFileSync("proof.json", "utf-8"));
const verificationKey = JSON.parse(fs.readFileSync("verification_key.json", "utf-8"));

async function compressedG1(point) {
const curve = await ff.getCurveFromName("bls12381");

const result = bb.toBufferBE(BigInt(point[0]), 48);
const COMPRESSED = 0b10000000;
const INFINITY = 0b01000000;
const YBIT = 0b00100000;

result[0] = result[0] | COMPRESSED;

if (BigInt(point[2]) !== 1n) {
result[0] = result[0] | INFINITY;
} else {
const F = curve.G1.F;

const x = F.fromObject(BigInt(point[0]));

const x3b = F.add(F.mul(F.square(x), x), curve.G1.b);
const y1 = F.toObject(F.sqrt(x3b));
const y2 = F.toObject(F.neg(F.sqrt(x3b)));

const y = BigInt(point[1]);

if (y1 > y2 && y > y2) {
result[0] = result[0] | YBIT;
} else if (y1 < y2 && y > y1) {
result[0] = result[0] | YBIT;
}
}


return result.toString("hex");
}

async function compressedG2(point) {
const curve = await ff.getCurveFromName("bls12381");

const result = Buffer.concat([bb.toBufferBE(BigInt(point[0][1]), 48), bb.toBufferBE(BigInt(point[0][0]), 48)]);
const COMPRESSED = 0b10000000;
const INFINITY = 0b01000000;
const YBIT = 0b00100000;

result[0] = result[0] | COMPRESSED;

if (BigInt(point[2][0]) !== 1n) {
result[0] = result[0] | INFINITY;
} else {
const F = curve.G2.F;

const x = F.fromObject(point[0].map(item => BigInt(item)));

// console.log("x", x);

const x3b = F.add(F.mul(F.square(x), x), curve.G2.b);
const y1 = F.toObject(F.sqrt(x3b));
const y2 = F.toObject(F.neg(F.sqrt(x3b)));
// console.log("y1", y1);
// console.log("y2", y2);
// console.log("point", point[1]);

function greaterThan(a, b) {
if (a[1] > b[1]) {
return true
} else if (a[1] === b[1] && a[0] > b[0]) {
return true;
}
return false;
}

const y = point[1].map(item => BigInt(item));

if (greaterThan(y1, y2) && greaterThan(y, y2)) {
result[0] = result[0] | YBIT;
} else if (greaterThan(y2, y1) && greaterThan(y, y1)) {
result[0] = result[0] | YBIT;
}
}
return result.toString("hex");
}


async function convertProofToUncompressed(proof) {

const uncompressedProof = {
"pi_a": await compressedG1(proof.pi_a),
"pi_b": await compressedG2(proof.pi_b),
"pi_c": await compressedG1(proof.pi_c),
}

return uncompressedProof;
}


async function convertVerificationKeyToUncompressed(verificationKey) {
const uncompressedVerificationKey = {
"vk_alpha_1": await compressedG1(verificationKey.vk_alpha_1),
"vk_beta_2": await compressedG2(verificationKey.vk_beta_2),
"vk_gamma_2": await compressedG2(verificationKey.vk_gamma_2),
"vk_delta_2": await compressedG2(verificationKey.vk_delta_2),
"IC": await Promise.all(verificationKey.IC.map(async (item) => {
try {
return await compressedG1(item);
} catch (error) {
console.error('Error processing item:', item, error);
return null;
}
})),
}

return uncompressedVerificationKey;
}

async function printCompressedProof() {
console.log("Compressed proof", JSON.stringify(await convertProofToUncompressed(proof)));
}

printCompressedProof();

async function printCompressedVerificationKey() {
console.log("\n\nUncompressed verification key", JSON.stringify(await convertVerificationKeyToUncompressed(verificationKey)));
}

printCompressedVerificationKey();


async function ffTest() {
const curve = await ff.getCurveFromName("bls12381");

const point = proof.pi_c;

const xBufferBE = bb.toBufferBE(BigInt(point[0]), 48);

console.log("Point x buffer BE", new Uint8Array(xBufferBE));

const xBufferLE = bb.toBufferLE(BigInt(point[0]), 48);

console.log("Point x buffer LE", new Uint8Array(xBufferLE));


const g1Element = curve.G1.fromObject(point.map((item) => BigInt(item).toString(16)));

console.log("G1 element", g1Element);

console.log("G1 Element is valid", curve.G1.isValid(g1Element));

const buff = new Uint8Array(48);

curve.G1.toRprCompressed(buff, 0, g1Element);

console.log("G1 element Compressed", buff);

console.log("MSB", buff[0].toString(2));

const g1ElementFromCompressed = curve.G1.fromRprCompressed(buff, 0);

console.log("G1 from compressed", g1ElementFromCompressed);

console.log("G1 from compressed is valid", curve.G1.isValid(g1ElementFromCompressed));
}

// ffTest();


17 changes: 17 additions & 0 deletions conversion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "conversion",
"version": "1.0.0",
"description": "",
"type": "commonjs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bigint-buffer": "^1.1.5",
"ffjavascript": "^0.2.63"
}
}
28 changes: 28 additions & 0 deletions conversion/proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"pi_a": [
"1772906745093932579836240209170795378753849961020179699871382829952351871832226492308486069361021314982009562735843",
"1060554534780163267724558467040990415559388672742345068275893102509213372714145003450106197214490777822228922952656",
"1"
],
"pi_b": [
[
"1358486866497956789862128624707494045021569151043861378376291931980647848946728667100167175682451312814072710519566",
"2892568549106560709617872008499143468746473249719383613874674515863767425449396997771169971147197463981607825748431"
],
[
"3774155091396786738197845866416007916901023392079351182692381863535299038480178339300724209059839391036807743888327",
"347983957076885655346478330294523987890867898739967665064964046997306980380350203447680726102390180081745831115215"
],
[
"1",
"0"
]
],
"pi_c": [
"759582637143989971983231319490726934998091227338384192733966680457083122987151635351010473067481154882106265771902",
"3759548828452159746415829615402022741023694117893369552443984303026454864934040458775681494473824000684596598390571",
"1"
],
"protocol": "groth16",
"curve": "bls12381"
}
99 changes: 99 additions & 0 deletions conversion/verification_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"protocol": "groth16",
"curve": "bls12381",
"nPublic": 2,
"vk_alpha_1": [
"2191843459987598773590024133079527616758118088671934301945762281620908967162581012712567405019815572665712872623796",
"1664035774849863503646003048494213577015151537859184157765549528966387776772789207030433777101994828854739585442222",
"1"
],
"vk_beta_2": [
[
"3764231095688882375546394077913791289729500066732987589069835200735210168251677834789785070698671518741236262863583",
"1975956737674144585508789155185883489602647165720568470078042365709562638839124474451400468748578229290756203131924"
],
[
"2664149391785363356913750779411127000638406920365075800895742802208799038187313640182620725503804085435506720148070",
"491716795312779218659130638886776306404649091170844785976521584811326619381572211056932234374432526609285865390398"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160",
"3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758"
],
[
"1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905",
"927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"413154724199753058200524769130480539747847790863923642234877877680592088694363992783042398840429407626050743066789",
"3002644543443356676160163060928218871865410081831733983743342774940778555956806189822098470475670237779542851415944"
],
[
"2438985653642956949171527495262358641872663641541030192914572463146908921129516216779501221673275997233817960176712",
"1153945107219418639093484597929798696693867386162471902015435074259666973369548699571341876110098845066777294832589"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"1119614312890782083309600999057785795397603170170631015257396745570323316457963535074630855686990494673778448196147",
"742038601496863801453526417229271542806078110469177730792585776439827530482489842329316749055341641464553626528152"
],
[
"1088390838327808008035210883448228493907177201542653184182090904196651559543951664333111649677783977078506847884041",
"2837214357972618712140457906768388362286637294555761672630024210522781142923797999255029248557137948544249112648333"
],
[
"3877433249608326965639214147645932794884649572211492101458714681451992481281490963848814775798303784975033778565476",
"597955767106016876429201374986914550795954983229979662231585633284625712368958497140704039770602231110571127993019"
]
],
[
[
"2662556007646867761078073839158787132584127608421740379641502416597911492081507983862867550569692646879338719901980",
"1728784185468663874776440121711147809386195240021570349495245715254841355674231060337884553346596272099905654906277"
],
[
"3783046564574746437870408327888798832808904026618842104076700319916198573571995197610592169664700148018523815708027",
"1650953322183301436255680254214000225128094685097878264284712810475465218613191405608582106951277280117676043243386"
],
[
"661282582370783830377759997030246421465048985306548778665276485713752568765509911394270899905744300097503830686188",
"554462205254579485033981033913994147758083815475117093767616549931962491805668145901814272592450333224914413991514"
]
]
],
"IC": [
[
"3309896332232763728332884765232343789621967757514439814841772750001004110163769133520530042440224977308143690966538",
"2729872703560414785705863386228322151240301147292791955557293720514020028816188672981305414934902446634648161893597",
"1"
],
[
"631322038111762990511918330017151125033987672955885718113870795938008485779609080354246784064816111921692130342156",
"2237991347874379964013843270729116257803284877394816325928044453833709035829235921538541619664979249135195864580089",
"1"
],
[
"2156395363321496459977006673790312524899213752348596549626151845365363536870686406447459761442071948022650812037505",
"859509658696984707388484840799535446245965210460962278516264652307712352123089491042788915533211361883018851645942",
"1"
]
]
}
Loading

0 comments on commit b22fbe1

Please sign in to comment.