hashASCIIStrToField function

BigInt hashASCIIStrToField(
  1. String str,
  2. int maxSize
)

hashes an ASCII string to a field element

Implementation

BigInt hashASCIIStrToField(String str, int maxSize) {
  if (str.length > maxSize) {
    throw ArgumentError('String $str is longer than $maxSize chars');
  }

  // Padding with zeroes is safe because we are only using this function to map human-readable sequence of bytes.
  // So the ASCII values of those characters will never be zero (null character).
  //final strPadded = str.padRight(maxSize, String.fromCharCode(0));
  List<int> strPadded = str.padRight(maxSize, String.fromCharCode(0)).codeUnits;

  const chunkSize = PACK_WIDTH ~/ 8;
  final packed = chunkArray(strPadded, chunkSize)
      .map((chunk) => bytesBEToBigInt(chunk.map((e) => e as int).toList()))
      .toList();
  return poseidonHash(packed);
}