Skip to content

xoring until the smaller buffer ends is not semantically correct #2

@dbkaplun

Description

@dbkaplun

In theory you can xor two whole numbers of any size. Since there is a natural, one-to-one mapping between whole numbers and buffers, it makes sense that the xor operation can and should be preserved across the mapping. That means that, when applied to Buffers, it should treat the shorter array as if it was padded with 0s to the longer array's length, instead of ignoring the longer array's extra data. This is the implementation on current master:

module.exports = function xor (a, b) {
  var length = Math.min(a.length, b.length)
  var buffer = new Buffer(length)

  for (var i = 0; i < length; ++i) {
    buffer[i] = a[i] ^ b[i]
  }

  return buffer
}

It should look more like:

module.exports = function xor (a, b) {
  var length = Math.max(a.length, b.length)
  var buffer = new Buffer(length)

  for (var i = 0; i < length; ++i) {
    buffer[i] = (a[i] || 0) ^ (b[i] || 0) // unoptimized, mathematically sound xor
  }

  return buffer
}

Hope that's clear. Let me know if that doesn't make sense. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions