public DivideAndRemainder ( |
||
val | ||
return | Org.BouncyCastle.Math.BigInteger[] |
public static BigInteger H1hash(string ID, BigInteger p) { // H1 je sha256(ID) mod p SHA256Managed crypt = new SHA256Managed(); StringBuilder hash = new StringBuilder(); byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(ID), 0, Encoding.UTF8.GetByteCount(ID)); foreach (byte theByte in crypto) { hash.Append(theByte.ToString("x2")); } BigInteger hsh = new BigInteger(hash.ToString(), 16); // hash mod p BigInteger x = hsh.DivideAndRemainder(p)[1]; return x; }
public static string H2hash(BigInteger qid, BigInteger p) { // H2 je RiPEMD-120: točka iz polja -> niz bita mod p string tocka = qid.ToString(); RIPEMD160Managed crypt = new RIPEMD160Managed(); StringBuilder hash = new StringBuilder(); byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(tocka), 0, Encoding.UTF8.GetByteCount(tocka)); foreach (byte theByte in crypto) { hash.Append(theByte.ToString("x2")); } BigInteger hsh = new BigInteger(hash.ToString(), 16); // hash mod n BigInteger c = hsh.DivideAndRemainder(p)[1]; return c.ToString(); }
public string Encode(byte[] input) { // Decode byte[] to BigInteger BigInteger intData=new BigInteger(1,input); // Encode BigInteger to Base string string result=string.Empty; while(intData.CompareTo(0)>0) { BigInteger[] divide=intData.DivideAndRemainder(Base); intData=divide[0]; result=Alphabet[divide[1].IntValue]+result; } // Append `1` for each leading 0 byte for(int i=0;i<input.Length && input[i]==0;i++) { result=Alphabet[0]+result; } return result; }
public void TestDivideAndRemainder() { // TODO More basic tests BigInteger n = new BigInteger(48, random); BigInteger[] qr = n.DivideAndRemainder(one); Assert.AreEqual(n, qr[0]); Assert.AreEqual(zero, qr[1]); for (int rep = 0; rep < 10; ++rep) { BigInteger a = new BigInteger(100 - rep, 0, random); BigInteger b = new BigInteger(100 + rep, 0, random); BigInteger c = new BigInteger(10 + rep, 0, random); BigInteger d = a.Multiply(b).Add(c); BigInteger[] es = d.DivideAndRemainder(a); Assert.AreEqual(b, es[0]); Assert.AreEqual(c, es[1]); } // Special tests for power of two since uses different code path internally for (int i = 0; i < 100; ++i) { int shift = random.Next(64); BigInteger a = one.ShiftLeft(shift); BigInteger b = new BigInteger(64 + random.Next(64), random); BigInteger bShift = b.ShiftRight(shift); BigInteger bMod = b.And(a.Subtract(one)); string data = "shift=" + shift +", b=" + b.ToString(16); qr = b.DivideAndRemainder(a); Assert.AreEqual(bShift, qr[0], data); Assert.AreEqual(bMod, qr[1], data); qr = b.DivideAndRemainder(a.Negate()); Assert.AreEqual(bShift.Negate(), qr[0], data); Assert.AreEqual(bMod, qr[1], data); qr = b.Negate().DivideAndRemainder(a); Assert.AreEqual(bShift.Negate(), qr[0], data); Assert.AreEqual(bMod.Negate(), qr[1], data); qr = b.Negate().DivideAndRemainder(a.Negate()); Assert.AreEqual(bShift, qr[0], data); Assert.AreEqual(bMod.Negate(), qr[1], data); } }