Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge branch 'development' into 8578-improve-seed-reveal-check
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 authored Jun 21, 2023
2 parents 06f0aad + fbb7c2b commit 3b370dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
36 changes: 18 additions & 18 deletions framework/src/modules/random/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ export const getRandomSeed = (
if (height < 0 || numberOfSeeds < 0) {
throw new Error('Height or number of seeds cannot be negative.');
}
const initRandomBuffer = utils.intToBuffer(height + numberOfSeeds, 4);
let randomSeed = utils.hash(initRandomBuffer).slice(0, SEED_LENGTH);

const initRandomBuffer = utils.intToBuffer(height + numberOfSeeds, 4);
const currentSeeds = [utils.hash(initRandomBuffer).slice(0, 16)];
let isInFuture = true;
const currentSeeds = [];

for (const validatorReveal of validatorsReveal) {
if (validatorReveal.height >= height) {
isInFuture = false;
if (validatorReveal.height < height + numberOfSeeds) {
currentSeeds.push(validatorReveal);
if (validatorReveal.height < height + numberOfSeeds && validatorReveal.valid) {
currentSeeds.push(validatorReveal.seedReveal);
}
}
}
Expand All @@ -67,28 +67,28 @@ export const getRandomSeed = (
throw new Error('Height is in the future.');
}

for (const seedObject of currentSeeds) {
if (seedObject.valid) {
randomSeed = bitwiseXOR([randomSeed, seedObject.seedReveal]);
}
}

return randomSeed;
return bitwiseXOR(currentSeeds);
};

export const bitwiseXOR = (bufferArray: Buffer[]): Buffer => {
if (bufferArray.length === 0) {
throw new Error('bitwiseXOR requires at least one buffer for the input.');
}

if (bufferArray.length === 1) {
return bufferArray[0];
}

const bufferSizes = new Set(bufferArray.map(buffer => buffer.length));
if (bufferSizes.size > 1) {
throw new Error('All input for XOR should be same size');
const size = bufferArray[0].length;
for (let i = 1; i < bufferArray.length; i += 1) {
if (bufferArray[i].length !== size) {
throw new Error('All input for XOR should be same size');
}
}
const outputSize = [...bufferSizes][0];
const result = Buffer.alloc(outputSize, 0);

for (let i = 0; i < outputSize; i += 1) {
const result = Buffer.alloc(size);

for (let i = 0; i < size; i += 1) {
// eslint-disable-next-line no-bitwise
result[i] = bufferArray.map(b => b[i]).reduce((a, b) => a ^ b, 0);
}
Expand Down
12 changes: 3 additions & 9 deletions framework/test/unit/modules/random/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ describe('RandomModuleMethod', () => {
Buffer.from(genesisValidators.validators[0].hashOnion.hashes[2], 'hex'),
];
// Do XOR of randomSeed with hashes of seed reveal with height >= randomStoreValidator.height >= height + numberOfSeeds
const xorExpected = bitwiseXOR([
bitwiseXOR([randomSeed, hashesExpected[0]]),
hashesExpected[1],
]);
const xorExpected = bitwiseXOR([randomSeed, ...hashesExpected]);

expect(xorExpected).toHaveLength(16);
await expect(randomMethod.getRandomBytes(context, height, numberOfSeeds)).resolves.toEqual(
Expand All @@ -343,10 +340,7 @@ describe('RandomModuleMethod', () => {
Buffer.from(genesisValidators.validators[1].hashOnion.hashes[1], 'hex'),
];
// Do XOR of randomSeed with hashes of seed reveal with height >= randomStoreValidator.height >= height + numberOfSeeds
const xorExpected = bitwiseXOR([
bitwiseXOR([bitwiseXOR([randomSeed, hashesExpected[0]]), hashesExpected[1]]),
hashesExpected[2],
]);
const xorExpected = bitwiseXOR([randomSeed, ...hashesExpected]);

await expect(randomMethod.getRandomBytes(context, height, numberOfSeeds)).resolves.toEqual(
xorExpected,
Expand Down Expand Up @@ -385,7 +379,7 @@ describe('RandomModuleMethod', () => {
Buffer.from(genesisValidators.validators[0].hashOnion.hashes[1], 'hex'),
];
// Do XOR of randomSeed with hashes of seed reveal with height >= randomStoreValidator.height >= height + numberOfSeeds
const xorExpected = bitwiseXOR([randomSeed, hashesExpected[0]]);
const xorExpected = bitwiseXOR([randomSeed, ...hashesExpected]);

await expect(randomMethod.getRandomBytes(context, height, numberOfSeeds)).resolves.toEqual(
xorExpected,
Expand Down
6 changes: 6 additions & 0 deletions framework/test/unit/modules/random/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import { SEED_LENGTH, ADDRESS_LENGTH } from '../../../../src/modules/random/cons

describe('Random module utils', () => {
describe('bitwiseXOR', () => {
it('should throw if an empty array is provided as an argument', () => {
expect(() => bitwiseXOR([])).toThrow(
'bitwiseXOR requires at least one buffer for the input.',
);
});

it('should return the first element if there are no other elements', () => {
const buffer = Buffer.from([0, 1, 1, 1]);
const input = [buffer];
Expand Down

0 comments on commit 3b370dd

Please sign in to comment.