Skip to content

Commit

Permalink
🧼 Remove obsolete conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Feb 27, 2025
1 parent bceee6b commit c9d79b5
Show file tree
Hide file tree
Showing 26 changed files with 659 additions and 475 deletions.
4 changes: 2 additions & 2 deletions crypto/bench/arfCurve.bench.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEq } from "../../testing/assert";
import { compareImpls } from "../../testing/bench";
import { uint8ArrayBEtoBigInt } from "../../util/çevir";
import bigints from "../../util/bigints";
import { arfCurve, Point as IPoint } from "../arfCurve";
import { P } from "../secp256k1";

Expand Down Expand Up @@ -96,7 +96,7 @@ const G = new Point(
);

/** @const {bigint} */
const k = uint8ArrayBEtoBigInt(/** @type {!Uint8Array} */(
const k = bigints.fromBytesBE(/** @type {!Uint8Array} */(
crypto.getRandomValues(new Uint8Array(32)))) % P;
const kG = G.copy().multiply(k).project();

Expand Down
4 changes: 2 additions & 2 deletions crypto/experiment/secp256k1.exp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @see https://hackmd.io/@dJO3Nbl4RTirkR2uDM6eOA/Bk0NvC8Vo
*/
import { assertEq } from "../../testing/assert";
import { uint8ArrayBEtoBigInt } from "../../util/çevir";
import bigints from "../../util/bigints";
import { arfCurve, Point as IPoint } from "../arfCurve";
import { P, Q, sqrt } from "../secp256k1";

Expand Down Expand Up @@ -41,7 +41,7 @@ const Qoint = arfCurve(Q);
*/
Point.random = () => {
for (; ;) {
const x = uint8ArrayBEtoBigInt(/** @type {!Uint8Array} */(
const x = bigints.fromBytesBE(/** @type {!Uint8Array} */(
crypto.getRandomValues(new Uint8Array(32)))) % P;
const y2 = (x * x * x + 1n) % P;
const y = sqrt(y2);
Expand Down
12 changes: 6 additions & 6 deletions crypto/minaSchnorr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { uint8ArrayBEtoBigInt } from "../util/çevir";
import bigints from "../util/bigints";
import { arfCurve, Point as IPoint } from "./arfCurve";
import { P, poseidon } from "./minaPoseidon";
import { poseidon as poseidonLegacy } from "./minaPoseidonLegacy";
Expand Down Expand Up @@ -76,7 +76,7 @@ const hashFields = (fields, X, r) =>
const signFields = (fields, privKey, pubKey) => {
pubKey ||= G.copy().multiply(privKey).project();
/** @type {bigint} */
let k = uint8ArrayBEtoBigInt(/** @type {!Uint8Array} */(
let k = bigints.fromBytesBE(/** @type {!Uint8Array} */(
crypto.getRandomValues(new Uint8Array(32)))) % Q;
/** @const {!Point} */
const K = G.copy().multiply(k).project();
Expand Down Expand Up @@ -146,7 +146,7 @@ const hashMessage = (message, X, r) => {
const signMessage = (message, privKey, pubKey) => {
pubKey ||= G.copy().multiply(privKey).project();
/** @type {bigint} */
let k = uint8ArrayBEtoBigInt(/** @type {!Uint8Array} */(
let k = bigints.fromBytesBE(/** @type {!Uint8Array} */(
crypto.getRandomValues(new Uint8Array(32)))) % Q;
const K = G.copy().multiply(k).project();
if (K.y & 1n) k = Q - k;
Expand Down Expand Up @@ -181,12 +181,12 @@ const verifyMessage = (message, r, s, pubKey) => {

export {
G,
P,
Point,
Q,
hashFields,
hashMessage,
P,
Point,
pointFrom,
Q,
signFields,
signMessage,
verifyFields,
Expand Down
4 changes: 2 additions & 2 deletions crypto/secp256k1.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author KimlikDAO
*/

import { uint8ArrayBEtoBigInt } from "../util/çevir";
import bigints from "../util/bigints";
import { arfCurve, aX_bY, Point as IPoint } from "./arfCurve";
import { inverse } from "./modular";

Expand Down Expand Up @@ -126,7 +126,7 @@ const equal = (p, q) => {
const sign = (digest, privKey) => {
for (; ;) {
/** @const {bigint} */
const k = uint8ArrayBEtoBigInt(/** @type {!Uint8Array} */(
const k = bigints.fromBytesBE(/** @type {!Uint8Array} */(
crypto.getRandomValues(new Uint8Array(32))));
if (k <= 0 || Q <= k) continue; // probability ~2^{-128}, i.e., a near impossibility.
/** @const {!Point} */
Expand Down
4 changes: 2 additions & 2 deletions crypto/wesolowski.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const evaluate = (gArr, t) => {
y = y * y % N;
/** @const {!Uint32Array} */
const yArr = new Uint32Array(32);
hex.intoUint32Array(yArr, y.toString(16).padStart(256, "0"));
hex.intoUint32ArrayBE(yArr, 32, y.toString(16));

/**
* (2) Generate the challenge l = generateChallenge(gArr, yArr).
Expand Down Expand Up @@ -104,7 +104,7 @@ const reconstructY = (logT, gArr, π, l) => {
const y = expTimesExp(π, l, g, r, N);
/** @const {!Uint32Array} */
const yArr = new Uint32Array(32);
hex.intoUint32Array(yArr, y.toString(16).padStart(256, "0"));
hex.intoUint32ArrayBE(yArr, 32, y.toString(16));
return yArr;
}

Expand Down
8 changes: 4 additions & 4 deletions did/KPass.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Signer } from "../crosschain/signer";
import { decrypt, encrypt } from "../crosschain/unlockable";
import "../ethereum/ERC721Unlockable.d";
import { bekle } from "../util/promises";
import { wait } from "../util/promises";
import { hash } from "./section";
import { verify } from "./verifiableID";

Expand Down Expand Up @@ -129,7 +129,7 @@ const fromUnlockableNFT = async (nft, sectionNames, signer, address) => {

for (let i = 0; i < encryptedSectionsKeys.length; ++i) {
if (i > 0)
await bekle(100);
await wait(100);
/** @type {!crosschain.Unlockable} */
const encryptedSections = /** @type {!crosschain.Unlockable} */(
nft.unlockables[encryptedSectionsKeys[i]]);
Expand Down Expand Up @@ -203,7 +203,7 @@ const toUnlockableNFT = async (metadata, decryptedSections, sectionGroups, signe
const unlockables = {};
for (let i = 0; i < sectionGroups.length; ++i) {
/** @const {!Promise<void>} */
const duraklatıcı = bekle(2000);
const duraklatıcı = wait(2000);
/** @const {did.DecryptedSections} */
const sections = {};
for (const /** @type {string} */ name of sectionGroups[i].sectionNames)
Expand Down Expand Up @@ -287,10 +287,10 @@ const combineMultiple = (
}

export {
SectionGroup,
checkVerifiableIDs,
combineMultiple,
fromUnlockableNFT,
SectionGroup,
selectEncryptedSections,
toUnlockableNFT
};
26 changes: 11 additions & 15 deletions did/commitment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import { ChainGroup } from "../crosschain/chains";
import { poseidon } from "../crypto/minaPoseidon";
import { keccak256Uint32 } from "../crypto/sha3";
import { PublicKey } from "../mina/mina";
import {
uint8ArrayeBase64ten,
uint8ArrayeHexten,
uint8ArrayLEtoBigInt,
uint8ArrayLEyeSayıdan
} from "../util/çevir";
import base64 from "../util/base64";
import bigints from "../util/bigints";
import hex from "../util/hex";

/**
* @param {ChainGroup} chainGroup
Expand All @@ -22,16 +18,16 @@ const commit = (chainGroup, ownerAddress, commitmentR) => {
case ChainGroup.EVM:
/** @const {!Uint8Array} */
const buff = new Uint8Array(32 + 20);
uint8ArrayeBase64ten(buff, commitmentR);
uint8ArrayeHexten(buff.subarray(32), ownerAddress.slice(2));
base64.intoBytes(buff, commitmentR);
hex.intoBytes(buff.subarray(32), ownerAddress.slice(2));
return base64.from(new Uint8Array(
keccak256Uint32(new Uint32Array(buff.buffer)).buffer, 0, 32));
case ChainGroup.MINA:
const { x, isOdd } = PublicKey.fromBase58(ownerAddress);
const commitmentBytes = base64.toBytes(commitmentR);
const outBuff = new Uint8Array(32);
uint8ArrayLEyeSayıdan(outBuff, poseidon([
uint8ArrayLEtoBigInt(commitmentBytes), isOdd ? x + 1n : x
bigints.intoBytesLE(outBuff, poseidon([
bigints.fromBytesLE(commitmentBytes), isOdd ? x + 1n : x
]));
return base64.from(outBuff);
}
Expand All @@ -57,7 +53,7 @@ const commitDouble = (chainGroup, ownerAddress, random) => {
case ChainGroup.EVM: {
/** @const {!Uint8Array} */
const buff = new Uint8Array(32 + 20);
uint8ArrayeHexten(buff.subarray(32), ownerAddress.slice(2));
hex.intoBytes(buff.subarray(32), ownerAddress.slice(2));
buff.set(random.subarray(0, 32));
/** @const {!Uint8Array} */
const commitment = new Uint8Array(
Expand All @@ -72,12 +68,12 @@ const commitDouble = (chainGroup, ownerAddress, random) => {
const commitment = new Uint8Array(64);
const { /** bigint */ x, isOdd } = PublicKey.fromBase58(ownerAddress);
const /** bigint */ h = isOdd ? x + 1n : x;
uint8ArrayLEyeSayıdan(
bigints.intoBytesLE(
commitment,
poseidon([uint8ArrayLEtoBigInt(random.subarray(0, 32)), h]));
uint8ArrayLEyeSayıdan(
poseidon([bigints.fromBytesLE(random.subarray(0, 32)), h]));
bigints.intoBytesLE(
commitment.subarray(32),
poseidon([uint8ArrayLEtoBigInt(random.subarray(32)), h]));
poseidon([bigints.fromBytesLE(random.subarray(32)), h]));
return commitment;
}
}
Expand Down
18 changes: 8 additions & 10 deletions did/section.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { keccak256, keccak256Uint32ToHex } from "../crypto/sha3";
import {
uint8ArrayBEyeSayıdan,
uint8ArrayeBase64ten,
uint8ArrayeHexten,
} from "../util/çevir";
import base64 from "../util/base64";
import bigints from "../util/bigints";
import hex from "../util/hex";
import "./section.d";

/** @const {string} */
Expand All @@ -23,8 +21,8 @@ const hashExposureReport = (exposureReport) => {
* @const {!Uint8Array} */
const buff = new Uint8Array(64);
new TextEncoder().encodeInto(KIMLIKDAO_HASH_PREFIX, buff);
uint8ArrayBEyeSayıdan(buff, 32, exposureReport.signatureTs);
uint8ArrayeHexten(buff.subarray(32), exposureReport.id);
bigints.intoBytesBE(buff, 32, exposureReport.signatureTs);
hex.intoBytes(buff.subarray(32), exposureReport.id);
return keccak256Uint32ToHex(new Uint32Array(buff.buffer));
};

Expand All @@ -42,9 +40,9 @@ const hashHumanID = (humanID) => {
* @const {!Uint8Array} */
const buff = new Uint8Array(96);
new TextEncoder().encodeInto(KIMLIKDAO_HASH_PREFIX, buff);
uint8ArrayBEyeSayıdan(buff, 32, humanID.signatureTs);
uint8ArrayeBase64ten(buff.subarray(32), humanID.commitment);
uint8ArrayeHexten(buff.subarray(64), humanID.id);
bigints.intoBytesBE(buff, 32, humanID.signatureTs);
base64.intoBytes(buff.subarray(32), humanID.commitment);
hex.intoBytes(buff.subarray(64), humanID.id);
return keccak256Uint32ToHex(new Uint32Array(buff.buffer));
};

Expand Down
15 changes: 6 additions & 9 deletions did/test/commitment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { keccak256Uint8 } from "../../crypto/sha3";
import { addr } from "../../ethereum/mock/signer";
import { PublicKey } from "../../mina/mina";
import base64 from "../../util/base64";
import {
uint8ArrayLEtoBigInt,
uint8ArrayLEyeSayıdan
} from "../../util/çevir";
import bigints from "../../util/bigints";
import { commit, commitDouble } from "../commitment";

test("EVM commit with 0 input", () => {
Expand All @@ -18,7 +15,7 @@ test("EVM commit with 0 input", () => {

test("MINA commit with select values", () => {
const buff = new Uint8Array(32);
uint8ArrayLEyeSayıdan(buff, poseidon([0n, 32n]))
bigints.intoBytesLE(buff, poseidon([0n, 32n]))

expect(base64.toBytes(commit(
ChainGroup.MINA,
Expand All @@ -34,8 +31,8 @@ test("MINA commitDouble()", () => {
r[32] = 2;
let c = commitDouble(ChainGroup.MINA, new PublicKey(31n, false).toBase58(), r);

expect(uint8ArrayLEtoBigInt(c.subarray(0, 32))).toBe(poseidon([1n, 31n]));
expect(uint8ArrayLEtoBigInt(c.subarray(32, 64))).toBe(poseidon([2n, 31n]));
expect(bigints.fromBytesLE(c.subarray(0, 32))).toBe(poseidon([1n, 31n]));
expect(bigints.fromBytesLE(c.subarray(32, 64))).toBe(poseidon([2n, 31n]));
});

test("MINA commitDouble()", () => {
Expand All @@ -44,8 +41,8 @@ test("MINA commitDouble()", () => {
r[32] = 2;
let c = commitDouble(ChainGroup.MINA, new PublicKey(31n, true).toBase58(), r);

expect(uint8ArrayLEtoBigInt(c.subarray(0, 32))).toBe(poseidon([1n, 32n]));
expect(uint8ArrayLEtoBigInt(c.subarray(32, 64))).toBe(poseidon([2n, 32n]));
expect(bigints.fromBytesLE(c.subarray(0, 32))).toBe(poseidon([1n, 32n]));
expect(bigints.fromBytesLE(c.subarray(32, 64))).toBe(poseidon([2n, 32n]));
});

test("commit() == commitDouble()[0] on EVM", () => {
Expand Down
6 changes: 3 additions & 3 deletions ethereum/mock/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Signer } from "../../crosschain/signer";
import { inverse } from "../../crypto/modular";
import { G, Point, Q } from "../../crypto/secp256k1";
import { keccak256Uint32, keccak256Uint32ToHex } from "../../crypto/sha3";
import bigints from "../../util/bigints";
import hex from "../../util/hex";
import { uint8ArrayBEyeSayıdan } from "../../util/çevir";
import evm from "../evm";

/**
Expand Down Expand Up @@ -37,8 +37,8 @@ const addr = (privKey) => {
const sign = (digest, privKey) => {
/** @type {!Uint8Array} */
const bytes = new Uint8Array(64);
uint8ArrayBEyeSayıdan(bytes, 32, digest);
uint8ArrayBEyeSayıdan(bytes, 64, privKey);
bigints.intoBytesBE(bytes, 32, digest);
bigints.intoBytesBE(bytes, 64, privKey);
/** @const {!Uint32Array} */
const buff = new Uint32Array(bytes.buffer);

Expand Down
6 changes: 3 additions & 3 deletions ethereum/signer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Point, recoverSigner, sign } from "../crypto/secp256k1";
import { keccak256Uint32 } from "../crypto/sha3";
import hex from "../util/hex";
import { uint8ArrayBEyeSayıdan } from "../util/çevir";
import bigints from "../util/bigints";
import eth from "./eth.d";
import evm from "./evm";

Expand All @@ -12,8 +12,8 @@ import evm from "./evm";
const pointToAddress = (Q) => {
/** @const {!Uint8Array} */
const buff = new Uint8Array(64);
uint8ArrayBEyeSayıdan(buff, 32, Q.x);
uint8ArrayBEyeSayıdan(buff, 64, Q.y);
bigints.intoBytesBE(buff, 32, Q.x);
bigints.intoBytesBE(buff, 64, Q.y);
/** @const {!Uint8Array} */
const hash = new Uint8Array(
keccak256Uint32(new Uint32Array(buff.buffer)).buffer, 12, 20);
Expand Down
2 changes: 1 addition & 1 deletion kastro/cloudflare/test/workers.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import process from "node:process";
import { bekle as wait } from "../../../util/promises";
import { wait } from "../../../util/promises";
import { Auth } from "../api";
import workers from "../workers";

Expand Down
Loading

0 comments on commit c9d79b5

Please sign in to comment.