Skip to content

Commit

Permalink
⛺️ Reorg base conversion utils
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Feb 26, 2025
1 parent 4de4969 commit bceee6b
Show file tree
Hide file tree
Showing 37 changed files with 430 additions and 323 deletions.
10 changes: 5 additions & 5 deletions crosschain/unlockable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author KimlikDAO
*/

import { base64, base64ten } from "../util/çevir";
import base64 from "../util/base64";
import { Signer } from "./signer";
import "./unlockable.d";

Expand All @@ -23,11 +23,11 @@ const decrypt = (unlockable, signer, address) => {
.then((/** @type {!webCrypto.CryptoKey}*/ key) =>
crypto.subtle.decrypt({
name: "AES-CTR",
counter: base64ten(unlockable.nonce),
counter: base64.toBytes(unlockable.nonce),
length: 64
},
key,
base64ten(unlockable.ciphertext)
base64.toBytes(unlockable.ciphertext)
))
.then((/** @type {!ArrayBuffer} */ decrypted) => {
const decoded = new TextDecoder().decode(decrypted);
Expand Down Expand Up @@ -76,8 +76,8 @@ const encrypt = (text, userPrompt, version, signer, address) => {
))
.then((/** @type {!ArrayBuffer} */ encrypted) => /** @type {!crosschain.Unlockable} */({
version: "promptsign-sha256-aes-ctr",
nonce: base64(counter),
ciphertext: base64(new Uint8Array(encrypted)),
nonce: base64.from(counter),
ciphertext: base64.from(new Uint8Array(encrypted)),
userPrompt
}));
/** @return {!Promise<!ArrayBuffer>} */
Expand Down
4 changes: 2 additions & 2 deletions crypto/bench/primes.bench.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from "../../testing/assert";
import { hex } from "../../util/çevir";
import hex from "../../util/hex";
import { getNonsmooth, millerRabinBase2 } from "../primes";
import { keccak256Uint8 } from "../sha3";

Expand All @@ -10,7 +10,7 @@ const benchGetNonsmooth = () => {

for (let i = 0; i < 1000; ++i) {
/** @const {bigint} */
const p = getNonsmooth(hex(seed).slice(3));
const p = getNonsmooth(hex.from(seed).slice(3));
assert(millerRabinBase2(p, (p - 1n) >> 1n, 1));
seed = keccak256Uint8(seed);
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/test/sha3/sha3_prev.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hex } from '../../../util/çevir';
import hex from '../../../util/hex';

/** @const {Array<number>} */
const KECCAK_PADDING = [1, 256, 65536, 16777216];
Expand Down Expand Up @@ -73,7 +73,7 @@ export const keccak256 = (veri) => {
}
f(s);

return hex(new Uint8Array(s.buffer).subarray(0, 32));
return hex.from(new Uint8Array(s.buffer).subarray(0, 32));
}

const f = (s) => {
Expand Down
6 changes: 3 additions & 3 deletions crypto/test/sha3/unit.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, test } from "bun:test";
import { hex, hexten } from "../../../util/çevir";
import hex from "../../../util/hex";
import {
keccak256,
keccak256Uint32,
Expand Down Expand Up @@ -57,14 +57,14 @@ describe("keccak256Uint32() tests", () => {
1877774715, 2536810181, 353523037, 1382876301, 1489394885, 2273392375, 1724133069, 2263322640
]));

expect(hex(new Uint8Array(keccak256Uint32(new Uint32Array([0, 0, 0, 0xFF000000])).buffer, 0, 32)))
expect(hex.from(new Uint8Array(keccak256Uint32(new Uint32Array([0, 0, 0, 0xFF000000])).buffer, 0, 32)))
.toBe("83c1ba322bb919d20c2e09ca70fd27bc245617a9e9abd5315b8afaebc4136044");
});
});

test("keccak256Uint32ToHex() and keccak256Uint32() consistency", () => {
const input = Uint32Array.from("123123123123");
expect(hexten(keccak256Uint32ToHex(input)))
expect(hex.toUint8Array(keccak256Uint32ToHex(input)))
.toEqual(new Uint8Array(keccak256Uint32(input).buffer, 0, 32));
});

Expand Down
10 changes: 5 additions & 5 deletions crypto/wesolowski.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hex, uint32ArrayeHexten } from "../util/çevir";
import hex from "../util/hex";
import { expTimesExp } from "./modular";
import { getNonsmooth } from "./primes";
import { keccak256Uint32ToHex } from "./sha3";
Expand Down Expand Up @@ -47,7 +47,7 @@ const evaluate = (gArr, t) => {
*
* @const {bigint}
*/
const g = BigInt("0x" + hex(new Uint8Array(gArr.buffer, 0, 32)));
const g = BigInt("0x" + hex.from(new Uint8Array(gArr.buffer, 0, 32)));

/**
* (1) Calculate y = g^{2^t} (mod N)
Expand All @@ -59,7 +59,7 @@ const evaluate = (gArr, t) => {
y = y * y % N;
/** @const {!Uint32Array} */
const yArr = new Uint32Array(32);
uint32ArrayeHexten(yArr, y.toString(16).padStart(256, "0"));
hex.intoUint32Array(yArr, y.toString(16).padStart(256, "0"));

/**
* (2) Generate the challenge l = generateChallenge(gArr, yArr).
Expand Down Expand Up @@ -95,7 +95,7 @@ const evaluate = (gArr, t) => {
*/
const reconstructY = (logT, gArr, π, l) => {
/** @const {bigint} */
const g = BigInt("0x" + hex(new Uint8Array(gArr.buffer, 0, 32)));
const g = BigInt("0x" + hex.from(new Uint8Array(gArr.buffer, 0, 32)));
/** @type {bigint} */
let r = 2n;
for (let i = 0; i < logT; ++i)
Expand All @@ -104,7 +104,7 @@ const reconstructY = (logT, gArr, π, l) => {
const y = expTimesExp(π, l, g, r, N);
/** @const {!Uint32Array} */
const yArr = new Uint32Array(32);
uint32ArrayeHexten(yArr, y.toString(16).padStart(256, "0"));
hex.intoUint32Array(yArr, y.toString(16).padStart(256, "0"));
return yArr;
}

Expand Down
4 changes: 2 additions & 2 deletions did/KPassMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ChainId } from "../crosschain/chains";
import "../ethereum/ERC721Unlockable.d";
import KPass from "../ethereum/KPassLite";
import dom from "../util/dom";
import hex from "../util/hex";
import { I18nString } from "../util/i18n";
import { hex } from "../util/çevir";
import { SectionGroup } from "./KPass";

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ section${sections.length == 1 ? "" : "s"} of your KPass. Only sign this message
const sectionGroup = (sections, chainId) => /** @type {!SectionGroup} */({
sectionNames: sections,
userPrompt: userPrompt(sections)
+ "Nonce: " + hex(/** @type {!Uint8Array} */(crypto.getRandomValues(new Uint8Array(8))))
+ "Nonce: " + hex.from(/** @type {!Uint8Array} */(crypto.getRandomValues(new Uint8Array(8))))
+ "\nChainId: " + chainId
+ "\nNFT: " + KPass.getAddress(chainId)
});
Expand Down
9 changes: 4 additions & 5 deletions did/commitment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { poseidon } from "../crypto/minaPoseidon";
import { keccak256Uint32 } from "../crypto/sha3";
import { PublicKey } from "../mina/mina";
import {
base64,
base64ten,
uint8ArrayeBase64ten,
uint8ArrayeHexten,
uint8ArrayLEtoBigInt,
uint8ArrayLEyeSayıdan
} from "../util/çevir";
import base64 from "../util/base64";

/**
* @param {ChainGroup} chainGroup
Expand All @@ -25,16 +24,16 @@ const commit = (chainGroup, ownerAddress, commitmentR) => {
const buff = new Uint8Array(32 + 20);
uint8ArrayeBase64ten(buff, commitmentR);
uint8ArrayeHexten(buff.subarray(32), ownerAddress.slice(2));
return base64(new Uint8Array(
return base64.from(new Uint8Array(
keccak256Uint32(new Uint32Array(buff.buffer)).buffer, 0, 32));
case ChainGroup.MINA:
const { x, isOdd } = PublicKey.fromBase58(ownerAddress);
const commitmentBytes = base64ten(commitmentR);
const commitmentBytes = base64.toBytes(commitmentR);
const outBuff = new Uint8Array(32);
uint8ArrayLEyeSayıdan(outBuff, poseidon([
uint8ArrayLEtoBigInt(commitmentBytes), isOdd ? x + 1n : x
]));
return base64(outBuff);
return base64.from(outBuff);
}
}

Expand Down
6 changes: 3 additions & 3 deletions did/sectionSigner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChainGroup } from "../crosschain/chains";
import { signCompact, signerAddress } from "../ethereum/signer";
import { signFields, verifyFields } from "../mina/signer";
import { base64tenSayıya } from "../util/çevir";
import base64 from "../util/base64";
import { commit } from "./commitment";
import { hash } from "./section";

Expand All @@ -22,7 +22,7 @@ const SignParams = {};
const signHumanID = (humanID, privateKey) => {
humanID.minaSchnorr = [signFields([
BigInt(humanID.signatureTs),
base64tenSayıya(humanID.commitment),
base64.toBigInt(humanID.commitment),
BigInt("0x" + humanID.id)
], privateKey)];
}
Expand All @@ -39,7 +39,7 @@ const recoverHumanIDSigners = (humanID, ownerAddress) => {
/** @const {!Array<bigint>} */
const fields = [
BigInt(humanID.signatureTs),
base64tenSayıya(humanID.commitment),
base64.toBigInt(humanID.commitment),
BigInt("0x" + humanID.id)
];
if (!humanID.minaSchnorr) return [];
Expand Down
14 changes: 7 additions & 7 deletions did/test/KPass/combine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, test } from "bun:test";
import { ChainGroup } from "../../../crosschain/chains";
import { keccak256Uint8 } from "../../../crypto/sha3";
import { addr as evmAddr } from "../../../ethereum/mock/signer";
import { base64 } from "../../../util/çevir";
import base64 from "../../../util/base64";
import { commit } from "../../commitment";
import { combineMultiple } from "../../KPass";
import { recoverSectionSigners, signDecryptedSections } from "../../sectionSigner";
Expand All @@ -14,9 +14,9 @@ describe("Combinig multiple KPass'es", () => {
/** @const {string} */
const ownerAddress = evmAddr(1n);
/** @const {string} */
const commitmentR = base64(keccak256Uint8(encoder.encode("commitmentR")));
const commitmentR = base64.from(keccak256Uint8(encoder.encode("commitmentR")));
/** @const {string} */
const commitmentAnonR = base64(keccak256Uint8(encoder.encode("commitmentAnonR")));
const commitmentAnonR = base64.from(keccak256Uint8(encoder.encode("commitmentAnonR")));
/** @const {string} */
const commitment = commit(ChainGroup.EVM, ownerAddress, commitmentR);
/** @const {string} */
Expand Down Expand Up @@ -79,9 +79,9 @@ describe("Combinig multiple KPass'es", () => {
const ownerAddress = evmAddr(1n);

/** @const {string} */
const commitmentR = base64(keccak256Uint8(encoder.encode("commitmentR")));
const commitmentR = base64.from(keccak256Uint8(encoder.encode("commitmentR")));
/** @const {string} */
const commitmentAnonR = base64(keccak256Uint8(encoder.encode("commitmentAnonR")));
const commitmentAnonR = base64.from(keccak256Uint8(encoder.encode("commitmentAnonR")));

/** @const {string} */
const commitment = commit(ChainGroup.EVM, ownerAddress, commitmentR);
Expand Down Expand Up @@ -155,9 +155,9 @@ describe("Combinig multiple KPass'es", () => {
/** @const {string} */
const ownerAddress = evmAddr(1n);
/** @const {string} */
const commitmentR = base64(keccak256Uint8(encoder.encode("commitmentR")));
const commitmentR = base64.from(keccak256Uint8(encoder.encode("commitmentR")));
/** @const {string} */
const commitmentAnonR = base64(keccak256Uint8(encoder.encode("commitmentAnonR")));
const commitmentAnonR = base64.from(keccak256Uint8(encoder.encode("commitmentAnonR")));
/** @const {string} */
const commitment = commit(ChainGroup.EVM, ownerAddress, commitmentR);
/** @const {string} */
Expand Down
27 changes: 13 additions & 14 deletions did/test/commitment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ import { poseidon } from "../../crypto/minaPoseidon";
import { keccak256Uint8 } from "../../crypto/sha3";
import { addr } from "../../ethereum/mock/signer";
import { PublicKey } from "../../mina/mina";
import base64 from "../../util/base64";
import {
base64,
base64ten,
uint8ArrayLEtoBigInt,
uint8ArrayLEyeSayıdan
} from "../../util/çevir";
import { commit, commitDouble } from "../commitment";

test("EVM commit with 0 input", () => {
expect(commit(ChainGroup.EVM, "0x" + "0".repeat(40), base64(new Uint8Array(32))))
.toBe(base64(keccak256Uint8(new Uint8Array(52))));
expect(commit(ChainGroup.EVM, "0x" + "0".repeat(40), base64.from(new Uint8Array(32))))
.toBe(base64.from(keccak256Uint8(new Uint8Array(52))));
});

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

expect(base64ten(commit(
expect(base64.toBytes(commit(
ChainGroup.MINA,
new PublicKey(31n, true).toBase58(),
base64(new Uint8Array(32))
base64.from(new Uint8Array(32))
)))
.toEqual(buff);
});
Expand Down Expand Up @@ -55,10 +54,10 @@ test("commit() == commitDouble()[0] on EVM", () => {

let commitment = commitDouble(ChainGroup.EVM, evmAddr, random);

expect(base64(commitment.subarray(0, 32)))
.toBe(commit(ChainGroup.EVM, evmAddr, base64(random.subarray(0, 32))));
expect(base64(commitment.subarray(32)))
.toBe(commit(ChainGroup.EVM, evmAddr, base64(random.subarray(32))));
expect(base64.from(commitment.subarray(0, 32)))
.toBe(commit(ChainGroup.EVM, evmAddr, base64.from(random.subarray(0, 32))));
expect(base64.from(commitment.subarray(32)))
.toBe(commit(ChainGroup.EVM, evmAddr, base64.from(random.subarray(32))));
});

test("commit() == commitDouble()[0] on MINA", () => {
Expand All @@ -67,8 +66,8 @@ test("commit() == commitDouble()[0] on MINA", () => {

let commitment = commitDouble(ChainGroup.MINA, minaAddr, random);

expect(base64(commitment.subarray(0, 32)))
.toBe(commit(ChainGroup.MINA, minaAddr, base64(random.subarray(0, 32))));
expect(base64(commitment.subarray(32)))
.toBe(commit(ChainGroup.MINA, minaAddr, base64(random.subarray(32))));
expect(base64.from(commitment.subarray(0, 32)))
.toBe(commit(ChainGroup.MINA, minaAddr, base64.from(random.subarray(0, 32))));
expect(base64.from(commitment.subarray(32)))
.toBe(commit(ChainGroup.MINA, minaAddr, base64.from(random.subarray(32))));
});
6 changes: 3 additions & 3 deletions did/test/sectionSigner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChainGroup } from "../../crosschain/chains";
import { addr as evmAddr } from "../../ethereum/mock/signer";
import { PublicKey } from "../../mina/mina";
import { addr as minaAddr } from "../../mina/mock/signer";
import { base64 } from "../../util/çevir";
import base64 from "../../util/base64";
import { commit } from "../commitment";
import {
recoverHumanIDSigners,
Expand All @@ -23,7 +23,7 @@ test("sign section", () => {
secp256k1: ["incorrect_sign"]
})
/** @const {string} */
const commitmentR = base64([1, 2, 3]);
const commitmentR = base64.from([1, 2, 3]);
/** @const {number} */
const signatureTs = Date.now() / 1000 | 0;
/** @const {string} */
Expand Down Expand Up @@ -81,7 +81,7 @@ test("humanID minaSchnorr signature", () => {
secp256k1: ["incorrect_sign"]
})
/** @const {string} */
const commitmentR = base64([1, 2, 3]);
const commitmentR = base64.from([1, 2, 3]);
/** @const {number} */
const signatureTs = Date.now() / 1000 | 0;
/** @const {string} */
Expand Down
6 changes: 3 additions & 3 deletions did/tools/verifiableIDKeyGenerator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base64 } from "../../util/çevir";
import base64 from "../../util/base64";

/**
* @return {!Promise<void>}
Expand All @@ -12,8 +12,8 @@ const generateKeyPair = () => crypto.subtle.generateKey({
crypto.subtle.exportKey("pkcs8", /** @type {!webCrypto.CryptoKeyPair} */(res).privateKey),
crypto.subtle.exportKey("spki", /** @type {!webCrypto.CryptoKeyPair} */(res).publicKey),
])).then((/** @type {!Array<!ArrayBuffer>} */[privateKey, publicKey]) => {
console.log("private key:", base64(new Uint8Array(privateKey)));
console.log("public key:", base64(new Uint8Array(publicKey)));
console.log("private key:", base64.from(new Uint8Array(privateKey)));
console.log("public key:", base64.from(new Uint8Array(publicKey)));
});

generateKeyPair();
Loading

0 comments on commit bceee6b

Please sign in to comment.