Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding #204

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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