Skip to content

Commit

Permalink
Merge branch 'main' into feature/ffadd
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Nov 13, 2023
2 parents f14ffa2 + 9a90a9d commit f9f54a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
7 changes: 7 additions & 0 deletions crypto/finite_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ function createField(
}
return BigInt('0b' + binary.reverse().join(''));
},
leftShift(x: bigint, bits: number, maxBitSize: number = 64) {
let shifted = x << BigInt(bits);
return shifted & ((1n << BigInt(maxBitSize)) - 1n);
},
rightShift(x: bigint, bits: number) {
return x >> BigInt(bits);
},
};
}

Expand Down
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 f9f54a0

Please sign in to comment.