import { sha256 } from 'hash.js' import { Fp, Fp2, PointG1, PointG2 } from 'noble-bls12-381' export function privateBlsToPublic(privateKey: string) { const pointG2 = PointG2.fromPrivateKey(privateKey) const [x, y]: [Fp2, Fp2] = pointG2.toAffine() const [x0, x1] = x.values const s = x0.toString(16).padStart(96, '0') + x1.toString(16).padStart(96, '0') const negatedY = y.negate() const greatest = isGreater(negatedY, y) ? 0 : 1 const asBuffer = Buffer.from(s, 'hex') asBuffer[0] = asBuffer[0] | 0x80 | (greatest << 5) return asBuffer } function isGreater(negatedY: Fp2, y: Fp2) { if (negatedY.c[0] > y.c[0]) { return true } else if (negatedY.c[0] < y.c[0]) { return false } else { return negatedY.c[1] > y.c[1] } } const xor = (left: Buffer, right: Buffer) => { const result = Buffer.from(left) xorInPlace(result, right) return result } const xorInPlace = (target: Buffer, other: Buffer) => { for (let i = 0; i < target.length; i++) { target[i] ^= other[i] } } function bytesToNumberBE(bytes: Buffer) { let value = BigInt(0) for (let i = bytes.length - 1, j = 0; i >= 0; i--, j++) { // tslint:disable-next-line:no-bitwise value += (BigInt(bytes[i]) & BigInt(255)) << (BigInt(8) * BigInt(j)) } return value } function serializeG1Point(point: PointG1): Buffer { // this gives the same output as the Java version. return Buffer.from(point.toRawBytes(true)) } // Assumes that the message is a sha256 hash export function sign(privateKey: bigint, message: Buffer) { const hashedMessage: PointG1 = hash(message) const signature = hashedMessage.multiply(privateKey) return serializeG1Point(signature) } const G1_X_DEN: Fp[] = [ new Fp(BigInt('1353092447850172218905095041059784486169131709710991428415161466575141675351394082965234118340787683181925558786844')), new Fp(BigInt('2822220997908397120956501031591772354860004534930174057793539372552395729721474912921980407622851861692773516917759')), new Fp(BigInt('1717937747208385987946072944131378949849282930538642983149296304709633281382731764122371874602115081850953846504985')), new Fp(BigInt('501624051089734157816582944025690868317536915684467868346388760435016044027032505306995281054569109955275640941784')), new Fp(BigInt('3025903087998593826923738290305187197829899948335370692927241015584233559365859980023579293766193297662657497834014')), new Fp(BigInt('2224140216975189437834161136818943039444741035168992629437640302964164227138031844090123490881551522278632040105125')), new Fp(BigInt('1146414465848284837484508420047674663876992808692209238763293935905506532411661921697047880549716175045414621825594')), new Fp(BigInt('3179090966864399634396993677377903383656908036827452986467581478509513058347781039562481806409014718357094150199902')), new Fp(BigInt('1549317016540628014674302140786462938410429359529923207442151939696344988707002602944342203885692366490121021806145')), new Fp(BigInt('1442797143427491432630626390066422021593505165588630398337491100088557278058060064930663878153124164818522816175370')), new Fp(BigInt('1')), ] const G1_X_NUM: Fp[] = [ new Fp(BigInt('2712959285290305970661081772124144179193819192423276218370281158706191519995889425075952244140278856085036081760695')), new Fp(BigInt('3564859427549639835253027846704205725951033235539816243131874237388832081954622352624080767121604606753339903542203')), new Fp(BigInt('2051387046688339481714726479723076305756384619135044672831882917686431912682625619320120082313093891743187631791280')), new Fp(BigInt('3612713941521031012780325893181011392520079402153354595775735142359240110423346445050803899623018402874731133626465')), new Fp(BigInt('2247053637822768981792833880270996398470828564809439728372634811976089874056583714987807553397615562273407692740057')), new Fp(BigInt('3415427104483187489859740871640064348492611444552862448295571438270821994900526625562705192993481400731539293415811')), new Fp(BigInt('2067521456483432583860405634125513059912765526223015704616050604591207046392807563217109432457129564962571408764292')), new Fp(BigInt('3650721292069012982822225637849018828271936405382082649291891245623305084633066170122780668657208923883092359301262')), new Fp(BigInt('1239271775787030039269460763652455868148971086016832054354147730155061349388626624328773377658494412538595239256855')), new Fp(BigInt('3479374185711034293956731583912244564891370843071137483962415222733470401948838363051960066766720884717833231600798')), new Fp(BigInt('2492756312273161536685660027440158956721981129429869601638362407515627529461742974364729223659746272460004902959995')), new Fp(BigInt('1058488477413994682556770863004536636444795456512795473806825292198091015005841418695586811009326456605062948114985')), ] const G1_Y_DEN: Fp[] = [ new Fp(BigInt('3396434800020507717552209507749485772788165484415495716688989613875369612529138640646200921379825018840894888371137')), new Fp(BigInt('3907278185868397906991868466757978732688957419873771881240086730384895060595583602347317992689443299391009456758845')), new Fp(BigInt('854914566454823955479427412036002165304466268547334760894270240966182605542146252771872707010378658178126128834546')), new Fp(BigInt('3496628876382137961119423566187258795236027183112131017519536056628828830323846696121917502443333849318934945158166')), new Fp(BigInt('1828256966233331991927609917644344011503610008134915752990581590799656305331275863706710232159635159092657073225757')), new Fp(BigInt('1362317127649143894542621413133849052553333099883364300946623208643344298804722863920546222860227051989127113848748')), new Fp(BigInt('3443845896188810583748698342858554856823966611538932245284665132724280883115455093457486044009395063504744802318172')), new Fp(BigInt('3484671274283470572728732863557945897902920439975203610275006103818288159899345245633896492713412187296754791689945')), new Fp(BigInt('3755735109429418587065437067067640634211015783636675372165599470771975919172394156249639331555277748466603540045130')), new Fp(BigInt('3459661102222301807083870307127272890283709299202626530836335779816726101522661683404130556379097384249447658110805')), new Fp(BigInt('742483168411032072323733249644347333168432665415341249073150659015707795549260947228694495111018381111866512337576')), new Fp(BigInt('1662231279858095762833829698537304807741442669992646287950513237989158777254081548205552083108208170765474149568658')), new Fp(BigInt('1668238650112823419388205992952852912407572045257706138925379268508860023191233729074751042562151098884528280913356')), new Fp(BigInt('369162719928976119195087327055926326601627748362769544198813069133429557026740823593067700396825489145575282378487')), new Fp(BigInt('2164195715141237148945939585099633032390257748382945597506236650132835917087090097395995817229686247227784224263055')), new Fp(BigInt('1')), ] const G1_Y_NUM: Fp[] = [ new Fp(BigInt('1393399195776646641963150658816615410692049723305861307490980409834842911816308830479576739332720113414154429643571')), new Fp(BigInt('2968610969752762946134106091152102846225411740689724909058016729455736597929366401532929068084731548131227395540630')), new Fp(BigInt('122933100683284845219599644396874530871261396084070222155796123161881094323788483360414289333111221370374027338230')), new Fp(BigInt('303251954782077855462083823228569901064301365507057490567314302006681283228886645653148231378803311079384246777035')), new Fp(BigInt('1353972356724735644398279028378555627591260676383150667237975415318226973994509601413730187583692624416197017403099')), new Fp(BigInt('3443977503653895028417260979421240655844034880950251104724609885224259484262346958661845148165419691583810082940400')), new Fp(BigInt('718493410301850496156792713845282235942975872282052335612908458061560958159410402177452633054233549648465863759602')), new Fp(BigInt('1466864076415884313141727877156167508644960317046160398342634861648153052436926062434809922037623519108138661903145')), new Fp(BigInt('1536886493137106337339531461344158973554574987550750910027365237255347020572858445054025958480906372033954157667719')), new Fp(BigInt('2171468288973248519912068884667133903101171670397991979582205855298465414047741472281361964966463442016062407908400')), new Fp(BigInt('3915937073730221072189646057898966011292434045388986394373682715266664498392389619761133407846638689998746172899634')), new Fp(BigInt('3802409194827407598156407709510350851173404795262202653149767739163117554648574333789388883640862266596657730112910')), new Fp(BigInt('1707589313757812493102695021134258021969283151093981498394095062397393499601961942449581422761005023512037430861560')), new Fp(BigInt('349697005987545415860583335313370109325490073856352967581197273584891698473628451945217286148025358795756956811571')), new Fp(BigInt('885704436476567581377743161796735879083481447641210566405057346859953524538988296201011389016649354976986251207243')), new Fp(BigInt('3370924952219000111210625390420697640496067348723987858345031683392215988129398381698161406651860675722373763741188')), ] function hash(message: Buffer): PointG1 { const [v0, v1] = hashToField(message) const [x0, y0] = mapToCurve(v0) const [x1, y1] = mapToCurve(v1) const p0: PointG1 | undefined = applyIsogenyMap(x0, y0) const p1: PointG1 | undefined = applyIsogenyMap(x1, y1) if (p0 === undefined || p1 === undefined) { throw new Error('Could not hash to point') } // unlike the java code, we apply the isogeny map before adding the points. It's only after the // isogeny map has been applied that the points from mapToCurve are on the right curve, so // noble-bls will complain if we try to add (x0, y0), (x1, y1) before applying the map. const p = p0.add(p1) // Our java code uses a faster way of clearing the cofactor, which is not compatible with the // PointG1#clearCofactor method in noble-bls. See the "Clearing cofactors" paragraph in section 5 // of https://eprint.iacr.org/2019/403.pdf. return clearCofactor(p) } function clearCofactor(p: PointG1): PointG1 { return p.multiply(BigInt('15132376222941642753')) } function applyIsogenyMap(x: Fp, y: Fp): PointG1 | undefined { const xxNum = evaluateIsogeny(x, G1_X_NUM) const xxDen = evaluateIsogeny(x, G1_X_DEN) const yyNum = evaluateIsogeny(x, G1_Y_NUM) const yyDen = evaluateIsogeny(x, G1_Y_DEN) if (yyDen.isZero() || xxDen.isZero()) { return undefined } return new PointG1(xxNum.multiply(xxDen.invert()), y.multiply(yyNum).multiply(yyDen.invert())) } function evaluateIsogeny(base: Fp, constants: Fp[]): Fp { const length = constants.length let result: Fp = constants[length - 1] for (let i = length - 2; i >= 0; i--) { result = result.multiply(base).add(constants[i]) } return result } const swuA: Fp = new Fp( BigInt('12190336318893619529228877361869031420615612348429' + '846051986726275283378313155663745811710833465465981901188123677') ) const swuB: Fp = new Fp( BigInt('290667032464192757049125815802629388157708612141662' + '8140204402091718288198173574630967936031029026176254968826637280') ) const swuZ: Fp = new Fp(BigInt('11')) function mapToCurve(element: Fp): Fp[] { const uu = element.square() const tv1 = inv0( swuZ .square() .multiply(uu.square()) .add(swuZ.multiply(uu)) ) let x1 = swuB .negate() .multiply(swuA.invert()) .multiply(new Fp(BigInt(1)).add(tv1)) if (tv1.isZero()) { x1 = swuB.multiply(swuZ.multiply(swuA).invert()) } const gx1 = x1 .multiply(x1.square()) .add(swuA.multiply(x1)) .add(swuB) const x2 = swuZ.multiply(uu).multiply(x1) const gx2 = x2 .multiply(x2.square()) .add(swuA.multiply(x2)) .add(swuB) let xx: Fp let yy: Fp if (isSquare(gx1)) { xx = x1 // TODO: Is the right way to deal with Fp | undefined return types? yy = gx1.sqrt() as Fp } else { xx = x2 yy = gx2.sqrt() as Fp } if (signBit(element) !== signBit(yy)) { return [xx, yy.negate()] } else { return [xx, yy] } } function isSquare(element: Fp): boolean { return element .pow(BigInt('2001204777610833696708894912867952078278441409969503942666029068062015825245418932221343814564507832018947136279893')) .equals(Fp.ONE) } function signBit(element: Fp): boolean { const x: string = element.value.toString(2) return x[x.length - 1] === '1' } function inv0(element: Fp): Fp { if (element.isZero()) { return Fp.ZERO } else { return element.invert() } } function hashToField(message: Buffer): [Fp, Fp] { const lenInBytes = 128 const uniformBytes = expandMessage(message, lenInBytes) return [new Fp(bytesToNumberBE(uniformBytes.slice(0, 64))), new Fp(bytesToNumberBE(uniformBytes.slice(64, 128)))] } function sha(block: Buffer) { return Buffer.from( sha256() .update(block) .digest() ) } function expandMessage(message: Buffer, outputSize: number) { const first = computeFirstHash(message, outputSize) const outputBytes: Buffer[] = [] outputBytes.push(first) outputBytes.push(sha(Buffer.concat([first, Buffer.of(1), DST]))) // Assumes outputSize is multiple of 32 const ell = outputSize / 32 for (let i = 2; i < ell; i++) { // sha256(xor(first, b(i-1)) || i || DOMAIN_SEPARATOR) outputBytes.push(sha(Buffer.concat([xor(first, outputBytes[i - 1]), Buffer.of(i), DST]))) } return Buffer.concat(outputBytes) } const DST = Buffer.from('5061727469736961626c6f636b636861696e', 'hex') function computeFirstHash(messageBytes: Buffer, outputSize: number) { const outputSizeBytes: Buffer = encodeSize(outputSize) const msg = Buffer.concat([Buffer.alloc(64), messageBytes, outputSizeBytes, Buffer.of(0), DST]) return sha(msg) } function encodeSize(outputSize: number) { const buffer = Buffer.alloc(2) buffer.writeUInt16BE(outputSize) return buffer }