Skip to content

Commit

Permalink
fix encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Nov 3, 2023
1 parent 49c3f0f commit af48d4e
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions lib/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const Bijective = {
};

function toBytesBijective(fields: Field[], p: bigint) {
let fieldsBigInts = fields.map(fieldToBigInt);
let fieldsBigInts = fields.map((x) => x.toBigInt());
let bytesBig = changeBase(fieldsBigInts, p, bytesBase);
let bytes = bigIntArrayToBytes(bytesBig, bytesPerBigInt);
return bytes;
Expand All @@ -132,26 +132,12 @@ function toBytesBijective(fields: Field[], p: bigint) {
function toFieldsBijective(bytes: Uint8Array, p: bigint) {
let bytesBig = bytesToBigIntArray(bytes, bytesPerBigInt);
let fieldsBigInts = changeBase(bytesBig, bytesBase, p);
let fields = fieldsBigInts.map(bigIntToField);
let fields = fieldsBigInts.map(Field);
return fields;
}

// a constant field is internally represented as {value: [0, Uint8Array(32)]}
function bytesOfConstantField(field: Field): Uint8Array {
let value = (field as any).value;
if (value[0] !== 0) throw Error('Field is not constant');
return value[1];
}

function fieldToBigInt(field: Field) {
let bytes = bytesOfConstantField(field);
return bytesToBigInt(bytes);
}

function bigIntToField(x: bigint) {
let field = Field(1);
(field as any).value = [0, bigIntToBytes(x, 32)];
return field;
return Uint8Array.from(Field.toBytes(field));
}

function bigIntToBytes(x: bigint, length: number) {
Expand Down

0 comments on commit af48d4e

Please sign in to comment.