Skip to content

Commit

Permalink
Create strings.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pa-long authored Oct 14, 2024
1 parent 2b2e7e2 commit a8f22b3
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions arc721/utils/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const FIELD_MODULUS = 8444461749428370424248824938781546531375899335154063827935233455917409239040n;

function stringToBigInt(input) {
const encoder = new TextEncoder();
const encodedBytes = encoder.encode(input);
encodedBytes.reverse();

let bigIntValue = BigInt(0);
for (let i = 0; i < encodedBytes.length; i++) {
const byteValue = BigInt(encodedBytes[i]);
const shiftedValue = byteValue << BigInt(8 * i);
bigIntValue = bigIntValue | shiftedValue;
}

return bigIntValue;
}

function bigIntToString(bigIntValue) {
const bytes = [];
let tempBigInt = bigIntValue;
while (tempBigInt > BigInt(0)) {
const byteValue = Number(tempBigInt & BigInt(255));
bytes.push(byteValue);
tempBigInt = tempBigInt >> BigInt(8);
}
bytes.reverse();
const decoder = new TextDecoder();
const asciiString = decoder.decode(Uint8Array.from(bytes));
return asciiString;
}

function stringToFields(input, numFieldElements = 4) {
const bigIntValue = stringToBigInt(input);
const fieldElements = [];
let remainingValue = bigIntValue;
for (let i = 0; i < numFieldElements; i++) {
const fieldElement = remainingValue % FIELD_MODULUS;
fieldElements.push(fieldElement);
remainingValue = remainingValue / FIELD_MODULUS;
}
if (remainingValue !== 0n) {
throw new Error("String is too big to be encoded.");
}
return fieldElements;
}

function fieldsToString(fields) {
let bigIntValue = BigInt(0);
let multiplier = BigInt(1);
for (const fieldElement of fields) {
bigIntValue += fieldElement * multiplier;
multiplier *= FIELD_MODULUS;
}
return bigIntToString(bigIntValue);
}

console.log(stringToFields("ANS"));

0 comments on commit a8f22b3

Please sign in to comment.