English 中文(简体)
过度流入的重复作业
原标题:Reversing multiplication operation that has overflowed

Given the code:

uint Function(uint value)
{
  return value * 0x123456D;
}

Inputting the value 0x300 yields the result 0x69D04700. This is only the lower 32 bits of the result. Given the result 0x69D04700 and the factor 0x123456D, is it possible to retrieve all numbers such that (value * 0x123456D) & 0xFFFFFFFF = 0x69D04700 in a fast way?

Edit: The Code I show is pseudocode - I can t Expansion the Return category.

最佳回答

What you need is modular division, which can be computed with a version of Euclid s algorithm. In this case the result is 768.

时间(log n<>>>>><>2,即使执行得天。 (如果你需要大量的工作,我可以提及更好的算法。)

See , 用于概述如何执行这一方法。

问题回答

如果你需要0x100000,将其隔开0x123456D,你就会收到224.9999(dec)。 这告诉你,大约每225人中就有1人丧生。 当然,由于只有225吨,你没有用注射器打其余的。 正如@Jacob所指出的,在32个轨道世界中,你只打了一个价值(768或0x300)。 因此,对于这一特殊检验,答案为2^32 *X+768,所有分类账X >=0。

当然,你可以形成长期价值,低半与你的结果相等,高半=1,2,3,4,5......,按你的固定乘数区分,看你是否取得了结果,而没有其他结果。 可以通过理解数字的格局来略微改变这种可能性,这样你甚至可以放弃价值观或某些价值观。

但我想到的是,没有多少详尽无遗的方法(而且模糊的怀疑是,很难做到这一点与一些加密技术有关)。

Well ...

Take that all back

import java.io.*;
public class multiply {
    public static void main(String[] argv) {
        long multiplier = 0x123456DL;
        // long result = 0x69D04700L;
        long result = (multiplier * 300L) & 0xFFFFFFFFL;
        System.out.println("New result = " + Long.toHexString(result));
        long offset = (multiplier * 300L) >> 32;
        System.out.println("New offset = " + offset);
        for (int i = 0; i < 30; i++) {
            long test = result + (((i * multiplier) + offset) << 32);
            long quotient = test / multiplier;
            long remainder = test % multiplier;
            System.out.println("Test: " + Long.toHexString(test) + " quotient: " + Long.toHexString(quotient) + " remainder: " + Long.toHexString(remainder));
        }
    }
}

成果(更正):

C:JavaTools>java multiply
New result = 55555bbc
New offset = 1
Test: 155555bbc quotient: 12c remainder: 0
Test: 123456e55555bbc quotient: 10000012c remainder: 0
Test: 2468adb55555bbc quotient: 20000012c remainder: 0
Test: 369d04855555bbc quotient: 30000012c remainder: 0
Test: 48d15b555555bbc quotient: 40000012c remainder: 0
Test: 5b05b2255555bbc quotient: 50000012c remainder: 0
Test: 6d3a08f55555bbc quotient: 60000012c remainder: 0
Test: 7f6e5fc55555bbc quotient: 70000012c remainder: 0
Test: 91a2b6955555bbc quotient: 80000012c remainder: 0
Test: a3d70d655555bbc quotient: 90000012c remainder: 0
Test: b60b64355555bbc quotient: a0000012c remainder: 0
Test: c83fbb055555bbc quotient: b0000012c remainder: 0
Test: da7411d55555bbc quotient: c0000012c remainder: 0
Test: eca868a55555bbc quotient: d0000012c remainder: 0
Test: fedcbf755555bbc quotient: e0000012c remainder: 0
Test: 1111116455555bbc quotient: f0000012c remainder: 0
Test: 123456d155555bbc quotient: 100000012c remainder: 0
Test: 13579c3e55555bbc quotient: 110000012c remainder: 0
Test: 147ae1ab55555bbc quotient: 120000012c remainder: 0
Test: 159e271855555bbc quotient: 130000012c remainder: 0
Test: 16c16c8555555bbc quotient: 140000012c remainder: 0
Test: 17e4b1f255555bbc quotient: 150000012c remainder: 0
Test: 1907f75f55555bbc quotient: 160000012c remainder: 0
Test: 1a2b3ccc55555bbc quotient: 170000012c remainder: 0
Test: 1b4e823955555bbc quotient: 180000012c remainder: 0
Test: 1c71c7a655555bbc quotient: 190000012c remainder: 0
Test: 1d950d1355555bbc quotient: 1a0000012c remainder: 0
Test: 1eb8528055555bbc quotient: 1b0000012c remainder: 0
Test: 1fdb97ed55555bbc quotient: 1c0000012c remainder: 0
Test: 20fedd5a55555bbc quotient: 1d0000012c remainder: 0

OK, I see the quotients (other than the first) are > 32 bits.

是的,这是可能的。

http://secure.wikimedia.org/wikipedia/en/wiki/Chinese_remainder_theorem”rel=“nofollow” 中国 Theorem

你们知道:

n = 0 (mod 0x123456D)

以及

n = 0x69D04700 (mod 0x100000000)

These are relatively prime, since 0x123456D is odd 以及 0x100000000 is a power of two. So the Chinese remainder theorem applies, 以及it gives you

n = 0x369D04700 (mod 0x123456D00000000)

This tells you that the results without truncation are 0x369D04700 + k * 0x123456D00000000. Dividing that by 0x123456D gives you 0x300 + k * 0x100000000.

你的职责:

uint Function(uint value)
{
  return value * 0x123456D;
}

<代码>uint中的多种版本 (如在所谓的<代码>unchecked上使用的正文/代码>)中,以odd号为标题。 这种奇数具有独特的反向、组合式<代码>2**64。 此处为<代码>0xE2D68C65u,因为您可以核对(C# syntax):

unchecked(0x123456Du * 0xE2D68C65u) == 1u

这种多重性是一种关联性和对应性。 因此,你的“反向”方法是:

uint UndoFunction(uint value)
{
  return value * 0xE2D68C65u;
}

(unckecked context 预期)。

关于任何投入的<代码>x, 两项均<编码> UndoFunction(x) 和Function(undoFunction(x) > , 均请上x


PS! 为了找到逆形<代码>0xE2D68C65u中的模块,我使用了“NET”以外的东西。 实际上,GGP/PARI与Charles一样在其答复中。 在GP中,你可以做1/Mod(19088749, 2^32)Mod(19088749,2^32)^-1<>。 它采用默认的折旧。

Not with the return type you specify. If you want to be able to handle 64 bits, you have to specify the return type as a ulong (or UInt64). C# uses strict static typing in cases like this, and will not automatically "upconvert" values if the result cannot be stored in the defined type. Even if it did upconvert, it would have to cast back down to provide a legal return type, because in .NET s inheritance hierarchy, UInt64 is not derived from UInt32.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...