From af48d4e80f51c2dff284bb7da23d91fb3f8fd3e9 Mon Sep 17 00:00:00 2001 From: Gregor Date: Fri, 3 Nov 2023 17:12:22 +0100 Subject: [PATCH] fix encoding --- lib/encoding.ts | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/lib/encoding.ts b/lib/encoding.ts index 4ecc5861..9d647c24 100644 --- a/lib/encoding.ts +++ b/lib/encoding.ts @@ -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; @@ -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) {