Skip to content

Commit e392d7c

Browse files
authored
fix: only clear trailing zeros on logs (#13727)
We used to assume we'd only ever see zeros in a log due to padding, but we now have scenarios were users put structs in a log that have some zero fields, e.g. a bignum value with multiple limbs for which some of them are zero when completing a partial note. If we knew the log length as per #11636 this would be a non-issue, but for now we need to only remove zeros _at the end_ of the array so that we can tell users that their logs will be fine as long as it has a non-zero field at the end.
1 parent e459b2a commit e392d7c

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

yarn-project/pxe/src/pxe_oracle_interface/pxe_oracle_interface.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { NoteDataProvider } from '../storage/note_data_provider/note_data_provid
2727
import { PrivateEventDataProvider } from '../storage/private_event_data_provider/private_event_data_provider.js';
2828
import { SyncDataProvider } from '../storage/sync_data_provider/sync_data_provider.js';
2929
import { TaggingDataProvider } from '../storage/tagging_data_provider/tagging_data_provider.js';
30-
import { PXEOracleInterface } from './pxe_oracle_interface.js';
30+
import { PXEOracleInterface, trimTrailingZeros } from './pxe_oracle_interface.js';
3131
import { WINDOW_HALF_SIZE } from './tagging_utils.js';
3232

3333
jest.setTimeout(30_000);
@@ -830,4 +830,34 @@ describe('PXEOracleInterface', () => {
830830
}),
831831
);
832832
};
833+
834+
describe('trimTrailingZeros', () => {
835+
function toFr(arr: number[]): Fr[] {
836+
return arr.map(x => new Fr(x));
837+
}
838+
839+
it('does not modify arrays with no zeros', () => {
840+
expect(trimTrailingZeros(toFr([1, 2, 3]))).toEqual(toFr([1, 2, 3]));
841+
});
842+
843+
it('removes zeros at the end', () => {
844+
expect(trimTrailingZeros(toFr([1, 2, 3, 0, 0]))).toEqual(toFr([1, 2, 3]));
845+
});
846+
847+
it('does not remove zeros not at the end', () => {
848+
expect(trimTrailingZeros(toFr([1, 0, 2, 3, 0, 0]))).toEqual(toFr([1, 0, 2, 3]));
849+
});
850+
851+
it('does not modify the original array', () => {
852+
const original = toFr([1, 0, 2, 3, 0, 0]);
853+
const result = trimTrailingZeros(original);
854+
855+
expect(result.length != original.length);
856+
expect(original).toEqual(toFr([1, 0, 2, 3, 0, 0]));
857+
});
858+
859+
it('works on an empty array', () => {
860+
expect(trimTrailingZeros(toFr([]))).toEqual(toFr([]));
861+
});
862+
});
833863
});

yarn-project/pxe/src/pxe_oracle_interface/pxe_oracle_interface.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,10 +725,9 @@ export class PXEOracleInterface implements ExecutionDataProvider {
725725
}
726726

727727
// Public logs always take up all available fields by padding with zeroes, and the length of the originally emitted
728-
// log is lost. Until this is improved, we simply remove all of the zero elements (which are expected to be at the
729-
// end).
728+
// log is lost. Until this is improved, we simply remove all of the zero elements we find at the end.
730729
// TODO(#11636): use the actual log length.
731-
const trimmedLog = scopedLog.log.toFields().filter(x => !x.isZero());
730+
const trimmedLog = trimTrailingZeros(scopedLog.log.toFields());
732731

733732
return new LogWithTxData(trimmedLog, scopedLog.txHash, txEffect.data.noteHashes, txEffect.data.nullifiers[0]);
734733
}
@@ -834,3 +833,15 @@ export class PXEOracleInterface implements ExecutionDataProvider {
834833
);
835834
}
836835
}
836+
837+
// TODO(#11636): remove once we have the actual log length and we don't need to trim it anymore
838+
export function trimTrailingZeros(array: Fr[]): Fr[] {
839+
// Make a copy to avoid modifying the original one
840+
const toReturn = [...array];
841+
842+
while (toReturn.length > 0 && toReturn[toReturn.length - 1].isZero()) {
843+
toReturn.pop();
844+
}
845+
846+
return toReturn;
847+
}

0 commit comments

Comments
 (0)