/// <summary> /// Generates NextAddress from a hash and sendto /// </summary> /// <param name="blockHash"></param> /// <param name="sendTo"></param> /// <returns></returns> public static string GenerateNextAddress(string blockHash, string sendTo) { Curl sponge = new Curl(); sponge.Absorb(TangleNet::TryteString.FromAsciiString(blockHash).ToTrits()); sponge.Absorb(TangleNet::TryteString.FromAsciiString(sendTo).ToTrits()); var result = new int[243]; sponge.Squeeze(result); return(Converter.TritsToTrytes(result)); }
/// <summary> /// Checks if the hash and nonce result in the dificulty /// </summary> /// <param name="hash"></param> /// <param name="nonce"></param> /// <param name="difficulty"></param> /// <returns></returns> public static bool VerifyNonce(this string hash, long nonce, int difficulty) { Curl curl = new Curl(); curl.Absorb(TangleNet::TryteString.FromAsciiString(hash).ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(nonce + "").ToTrits()); var trits = new int[120]; curl.Squeeze(trits); return(trits.VerifyDifficulty(difficulty)); }
public void testRandomTryteHash() { PowDiver powDiver = new PowDiver(); Curl curl = new Curl(); String hash; int[] hashTrits = new int[PowDiver.CURL_HASH_LENGTH], myTrits; int i = 0, testCount = 20, minWeightMagnitude = 9, numCores = -1; // use n-1 cores String trytes = getRandomTrytes(); myTrits = Converter.ToTrits(trytes); powDiver.search(myTrits, minWeightMagnitude, numCores); curl.Absorb(myTrits, 0, myTrits.Length); curl.Squeeze(hashTrits, 0, PowDiver.CURL_HASH_LENGTH); curl.Reset(); hash = Converter.ToTrytes(hashTrits); bool success = isAllNines(hash.Substring(PowDiver.CURL_HASH_LENGTH / 3 - minWeightMagnitude / 3)); Assert.IsTrue(success, "The hash should have n nines"); }
public void testRandomTryte_DoesNotCorrupt() { var powDiver = new PowDiver(); // var powDiver = new PearlDiver(); ICurl curl = new Curl(); String hash; int[] hashTrits = new int[PowDiver.CURL_HASH_LENGTH], myTrits; int i = 0, testCount = 20, minWeightMagnitude = 9, numCores = -1; // use n-1 cores String trytes = getRandomTrytes(); string orgTrytes = trytes.Clone() + ""; myTrits = Converter.ToTrits(trytes); powDiver.Search(myTrits, minWeightMagnitude, 1).Wait(); curl.Absorb(myTrits, 0, myTrits.Length); curl.Squeeze(hashTrits, 0, PowDiver.CURL_HASH_LENGTH); curl.Reset(); hash = Converter.ToTrytes(hashTrits); var hashToCheck = hash.Substring(PowDiver.CURL_HASH_LENGTH / 3 - minWeightMagnitude / 3); bool success = isAllNines(hashToCheck); var n = minWeightMagnitude / 3; Assert.IsTrue(success, $"The hash should have {n} nines but was: {hashToCheck}"); var powTrytes = Converter.ToTrytes(myTrits); Assert.AreEqual(orgTrytes, trytes); // These will differ in the last 27 chars which is the nonce. Assert.AreEqual(orgTrytes.Substring(0, 2646), powTrytes.Substring(0, 2646)); }
public async Task testRandomTryteHash() { var powDiver = new PowDiver(); ICurl curl = new Curl(); String hash; int[] hashTrits = new int[PowDiver.CURL_HASH_LENGTH], myTrits; int i = 0, testCount = 20, minWeightMagnitude = 9, numCores = -1; // use n-1 cores String trytes = getRandomTrytes(); myTrits = Converter.ToTrits(trytes); await powDiver.Search(myTrits, minWeightMagnitude, 1); curl.Absorb(myTrits, 0, myTrits.Length); curl.Squeeze(hashTrits, 0, PowDiver.CURL_HASH_LENGTH); curl.Reset(); hash = Converter.ToTrytes(hashTrits); var hashToCheck = hash.Substring(PowDiver.CURL_HASH_LENGTH / 3 - minWeightMagnitude / 3); bool success = isAllNines(hashToCheck); var n = minWeightMagnitude / 3; Assert.IsTrue(success, $"The hash should have {n} nines but was: {hashToCheck}"); }
public void TestMaskUnmask() { var mask = new CurlMask(); var curl = new Curl(CurlMode.CurlP27); curl.Absorb(this.authId.ToTrits()); var payloadTrits = this.payload.ToTrits(); mask.Mask(payloadTrits, curl); curl.Reset(); curl.Absorb(this.authId.ToTrits()); var unmasked = mask.Unmask(payloadTrits, curl); Assert.AreEqual(this.payload.Value, Converter.TritsToTrytes(unmasked)); }
/// <summary> /// /// </summary> /// <param name="trytes"></param> /// <returns></returns> public static string Hash(string trytes) { Sponge curl = new Curl(SpongeFactory.Mode.CURLP81); var trits = Converter.ToTrits(trytes); var hashTrits = new sbyte[Sponge.HASH_LENGTH]; curl.Absorb(trits, 0, trits.Length); curl.Squeeze(hashTrits, 0, Sponge.HASH_LENGTH); curl.Reset(); return(Converter.ToTrytes(hashTrits)); }
private string GetHashFor(string trytes) { Sponge curl = new Curl(CurlMode.CurlP81); int[] myTrits = Converter.ToTrits(trytes); bool result = _pearlDiver.Search(myTrits, MinWeightMagnitude, NumCores); Assert.IsTrue(result, "Search Failed"); curl.Absorb(myTrits, 0, myTrits.Length); curl.Squeeze(_hashTrits, 0, Sponge.HashLength); curl.Reset(); return(Converter.ToTrytes(_hashTrits)); }
/// <summary> /// Generates the hash of the transaction /// </summary> /// <returns></returns> public IDownloadable GenerateHash() { Curl curl = new Curl(); curl.Absorb(TangleNet::TryteString.FromAsciiString(SendTo).ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(From).ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(Mode + "").ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(Data.GetHashCode() + "").ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(Time + "").ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(OutputValue.GetHashCode() + "").ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(OutputReceiver.GetHashCode() + "").ToTrits()); curl.Absorb(TangleNet::TryteString.FromAsciiString(Data.GetHashCode() + "").ToTrits()); var hash = new int[60]; curl.Squeeze(hash); Hash = Converter.TritsToTrytes(hash); return(this); }
public static string Hash(int length, params object[] objects) { StringBuilder builder = new StringBuilder(); foreach (var obj in objects) { builder.Append(obj.ToString()); } Curl sponge = new Curl(); sponge.Absorb(TryteString.FromAsciiString(builder.ToString()).ToTrits()); var hash = new int[length * 3]; sponge.Squeeze(hash); return(Converter.TritsToTrytes(hash)); }
/// <summary> /// The from trytes. /// </summary> /// <param name="trytes"> /// The trytes. /// </param> /// <param name="hash"> /// The hash. /// </param> /// <returns> /// The <see cref="Transaction"/>. /// </returns> public static Transaction FromTrytes(TransactionTrytes trytes, Hash hash = null) { if (hash == null) { var hashTrits = new int[Constants.TritHashLength]; var kerl = new Curl(); kerl.Absorb(trytes.ToTrits()); kerl.Squeeze(hashTrits); hash = new Hash(Converter.TritsToTrytes(hashTrits)); } var valueTrits = trytes.GetChunk(2268, 27).ToTrits(); var attachementTimestamp = trytes.GetChunk(2619, 9).ToTrits(); var attachementTimestampLower = trytes.GetChunk(2628, 9).ToTrits(); var attachementTimestampUpper = trytes.GetChunk(2637, 9).ToTrits(); var timestamp = trytes.GetChunk(2322, 9).ToTrits(); return(new Transaction { Address = trytes.GetChunk <Address>(2187, Address.Length), Hash = hash, Fragment = trytes.GetChunk <Fragment>(0, 2187), Value = (long)Converter.ConvertTritsToBigInt(valueTrits, 0, valueTrits.Length), ObsoleteTag = trytes.GetChunk <Tag>(2295, Tag.Length), Timestamp = (long)Converter.ConvertTritsToBigInt(timestamp, 0, 27), CurrentIndex = Converter.TritsToInt(trytes.GetChunk(2331, 9).ToTrits()), LastIndex = Converter.TritsToInt(trytes.GetChunk(2340, 9).ToTrits()), BundleHash = trytes.GetChunk <Hash>(2349, Hash.Length), TrunkTransaction = trytes.GetChunk <Hash>(2430, Hash.Length), BranchTransaction = trytes.GetChunk <Hash>(2511, Hash.Length), Tag = trytes.GetChunk <Tag>(2592, Tag.Length), Nonce = trytes.GetChunk <Tag>(2646, Tag.Length), AttachmentTimestamp = (long)Converter.ConvertTritsToBigInt(attachementTimestamp, 0, attachementTimestamp.Length), AttachmentTimestampLowerBound = (long)Converter.ConvertTritsToBigInt(attachementTimestampLower, 0, attachementTimestampLower.Length), AttachmentTimestampUpperBound = (long)Converter.ConvertTritsToBigInt(attachementTimestampUpper, 0, attachementTimestampUpper.Length), }); }
/// <summary> /// Generates the hash of the block /// </summary> /// <param name="block"></param> /// <returns></returns> public IDownloadable GenerateHash() { Curl curl = new Curl(); curl.Absorb(TryteString.FromAsciiString(Height + "").ToTrits()); curl.Absorb(TryteString.FromAsciiString(Time + "").ToTrits()); curl.Absorb(TryteString.FromAsciiString(Owner).ToTrits()); curl.Absorb(TryteString.FromAsciiString(SendTo).ToTrits()); curl.Absorb(TryteString.FromAsciiString(CoinName).ToTrits()); curl.Absorb(TryteString.FromAsciiString(TransactionHashes.HashList(20) + "").ToTrits()); curl.Absorb(TryteString.FromAsciiString(SmartcontractHashes.HashList(20) + "").ToTrits()); var hash = new int[243]; curl.Squeeze(hash); Hash = Converter.TritsToTrytes(hash); return(this); }
/// <summary> /// </summary> /// <param name="payload"></param> /// <param name="sideKey"></param> /// <param name="root"></param> /// <returns>(message,nextRoot)</returns> public Tuple <string, string> DecodeMessage(string payload, string sideKey, string root) { if (string.IsNullOrEmpty(sideKey)) { sideKey = "999999999999999999999999999999999999999999999999999999999999999999999999999999999"; } var payloadTrits = Converter.ToTrits(payload); var sideKeyTrits = Converter.ToTrits(sideKey); var rootTrits = Converter.ToTrits(root); ICurl curl = new Curl(SpongeFactory.Mode.CURLP27); // parse var result = Pascal.Decode(payloadTrits); var index = (int)result.Item1; var indexEnd = result.Item2; var tempTrits = new sbyte[payloadTrits.Length - indexEnd]; Array.Copy(payloadTrits, indexEnd, tempTrits, 0, tempTrits.Length); result = Pascal.Decode(tempTrits); var messageLength = (int)result.Item1; var messageLengthEnd = result.Item2; var nextRootStart = indexEnd + messageLengthEnd; var messageStart = nextRootStart + Constants.HashLength; var messageEnd = messageStart + messageLength; curl.Absorb(sideKeyTrits); curl.Absorb(rootTrits); if (messageLength > payloadTrits.Length) { throw new ArgumentOutOfRangeException(); } curl.Absorb(payloadTrits, 0, nextRootStart); MaskHelper.UnMaskSlice(payloadTrits, nextRootStart, messageStart - nextRootStart, curl); MaskHelper.UnMaskSlice(payloadTrits, messageStart, messageEnd - messageStart, curl); var pos = messageEnd; MaskHelper.UnMaskSlice(payloadTrits, pos, Constants.MessageNonceLength, curl); pos += Constants.HashLength / 3; var hmac = new sbyte[Constants.HashLength]; Array.Copy(curl.Rate, hmac, Constants.HashLength); var security = HashHelper.CheckSumSecurity(hmac); MaskHelper.UnMaskSlice(payloadTrits, pos, payloadTrits.Length - pos, curl); if (security == 0) { // InvalidHash curl.Reset(); throw new ApplicationException(); } var sigEnd = pos + security * Constants.KeyLength; HashHelper.DigestBundleSignature(hmac, payloadTrits, pos, sigEnd - pos, curl); Array.Copy(curl.Rate, hmac, Constants.HashLength); curl.Reset(); pos = sigEnd; tempTrits = new sbyte[payloadTrits.Length - pos]; Array.Copy(payloadTrits, pos, tempTrits, 0, tempTrits.Length); result = Pascal.Decode(tempTrits); curl.Absorb(hmac); if (result.Item1 != 0) { // get address lite Array.Copy(curl.Rate, hmac, Constants.HashLength); pos += result.Item2; var sibEnd = pos + (int)result.Item1; var siblings = new sbyte[sibEnd - pos]; Array.Copy(payloadTrits, pos, siblings, 0, siblings.Length); curl.Reset(); MerkleTree.Root(hmac, siblings, index, curl); } if (!curl.Rate.SequenceEqual(rootTrits)) { // InvalidSignature curl.Reset(); throw new ApplicationException(); } var message = Converter.ToTrytes(payloadTrits, messageStart, messageLength); var nextRoot = Converter.ToTrytes(payloadTrits, nextRootStart, Constants.HashLength); return(new Tuple <string, string>(message, nextRoot)); }
private sbyte[] CreateMaskedPayload( sbyte[] seedTrits, sbyte[] messageTrits, sbyte[] keyTrits, sbyte[] rootTrits, sbyte[] siblingsTrits, sbyte[] nextRootTrits, int start, int index, int security) { ICurl curl = new Curl(SpongeFactory.Mode.CURLP27); ICurl encrCurl = new Curl(SpongeFactory.Mode.CURLP27); var hammingNonce = new HammingNonce(SpongeFactory.Mode.CURLP27); var minLength = GetPayloadMinLength(messageTrits.Length, siblingsTrits.Length, index, security); var payloadLength = (int)TritsHelper.RoundThird(minLength); var payload = new sbyte[payloadLength]; // Creates a signed, encrypted payload from a message // generate the key and the get the merkle tree hashes var messageLength = messageTrits.Length; var indexP = Pascal.EncodedLength(index); var messageP = Pascal.EncodedLength(messageLength); var siblingsLength = siblingsTrits.Length; var siblingsCount = siblingsTrits.Length / Constants.HashLength; var siblingsPascalLength = Pascal.EncodedLength(siblingsCount); var signatureLength = security * Constants.KeyLength; var nextRootStart = indexP + messageP; var nextEnd = nextRootStart + nextRootTrits.Length; var messageEnd = nextRootStart + Constants.HashLength + messageLength; var nonceEnd = messageEnd + Constants.MessageNonceLength; var signatureEnd = nonceEnd + signatureLength; var siblingsPascalEnd = signatureEnd + siblingsPascalLength; var siblingsEnd = siblingsPascalEnd + siblingsLength; encrCurl.Absorb(keyTrits); encrCurl.Absorb(rootTrits); var trits = new sbyte[indexP]; Pascal.Encode(index, trits); Array.Copy(trits, 0, payload, 0, indexP); trits = new sbyte[messageP]; Pascal.Encode(messageLength, trits); Array.Copy(trits, 0, payload, indexP, messageP); encrCurl.Absorb(payload, 0, nextRootStart); Array.Copy(nextRootTrits, 0, payload, nextRootStart, nextRootTrits.Length); Array.Copy(messageTrits, 0, payload, nextEnd, messageTrits.Length); MaskHelper.MaskSlice(payload, nextRootStart, messageEnd - nextRootStart, encrCurl); Array.Copy(encrCurl.State, curl.State, encrCurl.State.Length); hammingNonce.Search(security, 0, Constants.HashLength / 3, curl); Array.Copy(curl.State, 0, payload, messageEnd, Constants.MessageNonceLength); MaskHelper.MaskSlice(payload, messageEnd, nonceEnd - messageEnd, encrCurl); curl.Reset(); var subseed = HashHelper.Subseed(seedTrits, start + index, curl); Array.Copy(subseed, 0, payload, nonceEnd, subseed.Length); curl.Reset(); HashHelper.Key(payload, nonceEnd, signatureEnd - nonceEnd, security, curl); curl.Reset(); HashHelper.Signature(encrCurl.Rate, payload, nonceEnd, signatureEnd - nonceEnd, curl); curl.Reset(); trits = new sbyte[siblingsPascalLength]; Pascal.Encode(siblingsCount, trits); Array.Copy(trits, 0, payload, signatureEnd, siblingsPascalLength); Array.Copy(siblingsTrits, 0, payload, siblingsPascalEnd, siblingsLength); MaskHelper.MaskSlice(payload, nonceEnd, siblingsEnd - nonceEnd, encrCurl); encrCurl.Reset(); return(payload); }