Skip to content

Commit

Permalink
refactor(bitwise): extract bit length validation logic into a separat…
Browse files Browse the repository at this point in the history
…e function to reduce code duplication

refactor(bitwise): reduce maximum bit length for xor operation from 254 to 240 bits to improve performance and simplify implementation
docs(bitwise): improve error messages for bit length validation to provide more context and clarity on the cause of the error
  • Loading branch information
MartinMinkov committed Jul 11, 2024
1 parent 80f3937 commit 2430572
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/lib/provable/gadgets/bitwise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ export {
};

function not(a: Field, length: number, checked: boolean = false) {
// check that input length is positive
assert(length > 0, `Input length needs to be positive values.`);

// Check that length does not exceed maximum field size in bits
assert(
length < Field.sizeInBits,
`Length ${length} exceeds maximum of ${Field.sizeInBits} bits.`
);
validateBitLength(length, Field.sizeInBits, 'not');

// obtain pad length until the length is a multiple of 16 for n-bit length lookup table
let padLength = Math.ceil(length / 16) * 16;
Expand All @@ -52,11 +45,7 @@ function not(a: Field, length: number, checked: boolean = false) {
}

function xor(a: Field, b: Field, length: number) {
// check that both input lengths are positive
assert(length > 0, `Input lengths need to be positive values.`);

// check that length does not exceed maximum 254 size in bits
assert(length <= 254, `Length ${length} exceeds maximum of 254 bits.`);
validateBitLength(length, 240, 'xor');

// obtain pad length until the length is a multiple of 16 for n-bit length lookup table
let padLength = Math.ceil(length / 16) * 16;
Expand Down Expand Up @@ -150,14 +139,7 @@ function buildXor(a: Field, b: Field, out: Field, padLength: number) {
}

function and(a: Field, b: Field, length: number) {
// check that both input lengths are positive
assert(length > 0, `Input lengths need to be positive values.`);

// check that length does not exceed maximum field size in bits
assert(
length <= Field.sizeInBits,
`Length ${length} exceeds maximum of ${Field.sizeInBits} bits.`
);
validateBitLength(length, Field.sizeInBits, 'and');

// obtain pad length until the length is a multiple of 16 for n-bit length lookup table
let padLength = Math.ceil(length / 16) * 16;
Expand Down Expand Up @@ -343,3 +325,17 @@ function leftShift32(field: Field, bits: number) {
let { remainder: shifted } = divMod32(field.mul(1n << BigInt(bits)));
return shifted;
}

function validateBitLength(
length: number,
maxLength: number,
functionName: string
) {
// check that both input lengths are positive
assert(length > 0, `${functionName}: Input length must be a positive value.`);
// check that length does not exceed maximum 240 size in bits
assert(
length <= maxLength,
`${functionName}: Length ${length} exceeds maximum of ${maxLength} bits.`
);
}

0 comments on commit 2430572

Please sign in to comment.