function checkRoot(commitment, commitmentIndex, siblingPath, root) {
  // define Merkle Constants:
  const { TREE_HEIGHT, NODE_HASHLENGTH } = config;

  const truncatedCommitment = commitment.slice(-NODE_HASHLENGTH * 2); // truncate to the desired 216 bits for Merkle Path computations

  const binaryCommitmentIndex = commitmentIndex
    .toString(2) // to binary
    .padStart(TREE_HEIGHT, '0') // pad to correct length
    .split(''); // convert to array for easier iterability

  // console.log(`commitment:`, commitment);
  // console.log(`truncatedCommitment:`, truncatedCommitment);
  // console.log(`commitmentIndex:`, commitmentIndex);
  // console.log(`binaryCommitmentIndex:`, binaryCommitmentIndex);
  // console.log(`siblingPath:`, siblingPath);
  // console.log(`root:`, root);

  const siblingPathTruncated = siblingPath.map(node => `0x${node.slice(-NODE_HASHLENGTH * 2)}`);

  let hash216 = truncatedCommitment;
  let hash256;

  for (let r = TREE_HEIGHT; r > 0; r -= 1) {
    const pair = [hash216, siblingPathTruncated[r]];
    // console.log('leftInput pre ordering:', pair[0]);
    // console.log('rightInput pre ordering:', pair[1]);
    // console.log('left or right?:', binaryCommitmentIndex[r - 1]);
    const orderedPair = orderBeforeConcatenation(binaryCommitmentIndex[r - 1], pair);
    // console.log('leftInput:', orderedPair[0]);
    // console.log('rightInput:', orderedPair[1]);
    hash256 = utils.concatenateThenHash(...orderedPair);
    // keep the below comments for future debugging:
    // console.log(`output pre-slice at row ${r - 1}:`, hash256);
    hash216 = `0x${hash256.slice(-NODE_HASHLENGTH * 2)}`;
    // console.log(`output at row ${r - 1}:`, hash216);
  }

  const rootCheck = hash256;

  if (root !== rootCheck) {
    throw new Error(
      `Root ${root} cannot be recalculated from the path and commitment ${commitment}. An attempt to recalculate gives ${rootCheck} as the root.`,
    );
  } else {
    console.log(
      `\nRoot ${root} successfully reconciled from first principles using the commitment and its sister-path.`,
    );
  }
}
/**
checkRoot - for DEBUGGING only. Helps give detailed logging for each hash up the merkle-tree, so as to better debug zokrates code.
*/
for DEBUGGING only. Helps give detailed logging for each hash up the merkle-tree, so as to better debug zokrates code.
for DEBUGGING only . Helps give detailed logging for each hash up the merkle - tree so as to better debug zokrates code .
function checkRoot(commitment, commitmentIndex, siblingPath, root) {
  const { TREE_HEIGHT, NODE_HASHLENGTH } = config;
  const truncatedCommitment = commitment.slice(-NODE_HASHLENGTH * 2); 
  const binaryCommitmentIndex = commitmentIndex
    .toString(2) 
    .padStart(TREE_HEIGHT, '0') 
    .split(''); 
  const siblingPathTruncated = siblingPath.map(node => `0x${node.slice(-NODE_HASHLENGTH * 2)}`);
  let hash216 = truncatedCommitment;
  let hash256;
  for (let r = TREE_HEIGHT; r > 0; r -= 1) {
    const pair = [hash216, siblingPathTruncated[r]];
    const orderedPair = orderBeforeConcatenation(binaryCommitmentIndex[r - 1], pair);
    hash256 = utils.concatenateThenHash(...orderedPair);
    hash216 = `0x${hash256.slice(-NODE_HASHLENGTH * 2)}`;
  }
  const rootCheck = hash256;
  if (root !== rootCheck) {
    throw new Error(
      `Root ${root} cannot be recalculated from the path and commitment ${commitment}. An attempt to recalculate gives ${rootCheck} as the root.`,
    );
  } else {
    console.log(
      `\nRoot ${root} successfully reconciled from first principles using the commitment and its sister-path.`,
    );
  }
}
function checkRoot ( commitment , commitmentIndex , siblingPath , root ) { const { TREE_HEIGHT , NODE_HASHLENGTH } = config ; const truncatedCommitment = commitment . slice ( - NODE_HASHLENGTH * 2 ) ; const binaryCommitmentIndex = commitmentIndex . toString ( 2 ) . padStart ( TREE_HEIGHT , '0' ) . split ( '' ) ; const siblingPathTruncated = siblingPath . map ( node => ` ${ node . slice ( - NODE_HASHLENGTH * 2 ) } ` ) ; let hash216 = truncatedCommitment ; let hash256 ; for ( let r = TREE_HEIGHT ; r > 0 ; r -= 1 ) { const pair = [ hash216 , siblingPathTruncated [ r ] ] ; const orderedPair = orderBeforeConcatenation ( binaryCommitmentIndex [ r - 1 ] , pair ) ; hash256 = utils . concatenateThenHash ( ... orderedPair ) ; hash216 = ` ${ hash256 . slice ( - NODE_HASHLENGTH * 2 ) } ` ; } const rootCheck = hash256 ; if ( root !== rootCheck ) { throw new Error ( ` ${ root } ${ commitment } ${ rootCheck } ` , ) ; } else { console . log ( ` \n ${ root } ` , ) ; } }
checkRoot - for DEBUGGING only.
checkRoot - for DEBUGGING only .
// define Merkle Constants: // truncate to the desired 216 bits for Merkle Path computations // to binary // pad to correct length // convert to array for easier iterability // console.log(`commitment:`, commitment); // console.log(`truncatedCommitment:`, truncatedCommitment); // console.log(`commitmentIndex:`, commitmentIndex); // console.log(`binaryCommitmentIndex:`, binaryCommitmentIndex); // console.log(`siblingPath:`, siblingPath); // console.log(`root:`, root); // console.log('leftInput pre ordering:', pair[0]); // console.log('rightInput pre ordering:', pair[1]); // console.log('left or right?:', binaryCommitmentIndex[r - 1]); // console.log('leftInput:', orderedPair[0]); // console.log('rightInput:', orderedPair[1]); // keep the below comments for future debugging: // console.log(`output pre-slice at row ${r - 1}:`, hash256); // console.log(`output at row ${r - 1}:`, hash216);