public Vector(string fileName, MathDomain mathDomain) { _buffer = new MemoryBuffer(0, 0, memorySpace, mathDomain); switch (mathDomain) { case MathDomain.Null: throw new ArgumentNullException(); case MathDomain.Int: { ReadFromBinaryFile <int>(fileName); break; } case MathDomain.Float: { ReadFromBinaryFile <float>(fileName); break; } case MathDomain.Double: { ReadFromBinaryFile <double>(fileName); break; } default: throw new NotImplementedException(); } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { MathDomain md = (MathDomain)value; writer.WriteStartArray(); foreach (var segment in md.DomainSegments) { var valueSegment = segment as ValueDomainSegment; if (valueSegment != null) { writer.WriteValue(valueSegment.RangeMinMax.Minimum); } var rangeSegment = segment as RangeDomainSegment; if (rangeSegment != null) { var minMax = rangeSegment.RangeMinMax; writer.WriteStartObject(); writer.WritePropertyName("min"); writer.WriteValue(minMax.Minimum); writer.WritePropertyName("max"); writer.WriteValue(minMax.Maximum); writer.WritePropertyName("increment"); writer.WriteValue(minMax.Increment); writer.WriteEndObject(); } } writer.WriteEndArray(); }
private MathDomain Create(JArray jArray) { MathDomain domain = new MathDomain(); foreach (var item in jArray) { if (int.TryParse(item.ToString(), out var parsedValue)) { domain.AddSegment(new ValueDomainSegment(parsedValue)); } else { var model = JsonConvert.DeserializeObject <RangeDomainModel>( item.ToString(), new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore } ); domain.AddSegment(new RangeDomainSegment(_random, model.Min, model.Max, model.Increment)); } } return(domain); }
public void ShouldWriteJsonAndDeserializeIntoSameObject() { MathDomain original = new MathDomain(); ValueDomainSegment vds = new ValueDomainSegment(1024); RangeDomainSegment rds = new RangeDomainSegment(new Random800_90(), 0, 1024, 128); original.AddSegment(vds); original.AddSegment(rds); var serializedObject = JsonConvert.SerializeObject( original, new JsonSerializerSettings { Converters = new List <JsonConverter> { new DomainConverter() }, NullValueHandling = NullValueHandling.Ignore } ); var deserializedObject = JsonConvert.DeserializeObject <MathDomain>( serializedObject, new DomainConverter() ); var vdsReObjectified = deserializedObject.DomainSegments.OfType <ValueDomainSegment>().First(); var rdsReObjectified = deserializedObject.DomainSegments.OfType <RangeDomainSegment>().First(); Assert.AreEqual(original.DomainSegments.Count(), deserializedObject.DomainSegments.Count(), "count"); Assert.AreEqual(vds.ToString(), vdsReObjectified.ToString(), nameof(vds)); Assert.AreEqual(rds.ToString(), rdsReObjectified.ToString(), nameof(rds)); }
public SparseMemoryBuffer(PtrT pointer = 0, uint nNonZeros = 0, PtrT indices = 0, MemorySpace memorySpace = MemorySpace.Null, MathDomain mathDomain = MathDomain.Null) : base(pointer, nNonZeros, memorySpace, mathDomain) { this.indices = indices; }
public MemoryTile(PtrT pointer = 0, uint nRows = 0, uint nCols = 0, MemorySpace memorySpace = MemorySpace.Null, MathDomain mathDomain = MathDomain.Null) : base(pointer, nRows * nCols, memorySpace, mathDomain) { this.nRows = nRows; this.nCols = nCols; }
public MemoryBuffer(PtrT pointer = 0, uint size = 0, MemorySpace memorySpace = MemorySpace.Null, MathDomain mathDomain = MathDomain.Null) { this.pointer = pointer; this.memorySpace = memorySpace; this.mathDomain = mathDomain; this.size = size; }
public void ShouldOnlyReturnValidValuesFromGetValues() { var domain = new MathDomain().AddSegment(new RangeDomainSegment(new Random800_90(), 64, 128, 64)); domain.SetRangeOptions(RangeDomainSegmentOptions.Random); var values = domain.GetValues(128).ToList(); Assert.AreEqual(2, values.Count()); Assert.IsTrue(values.Contains(64)); Assert.IsTrue(values.Contains(128)); }
public SparseVector(int denseSize, Vector nonZeroIndices, MathDomain mathDomain) : base(false, // SparseVector doesn't allocate its memory in its buffer, but it uses the convenience vector this.values nonZeroIndices.memorySpace, mathDomain) { Debug.Assert(denseSize > nonZeroIndices.Size); this.denseSize = denseSize; values = new Vector(nonZeroIndices.Size, nonZeroIndices.memorySpace, mathDomain); this.nonZeroIndices = nonZeroIndices; _buffer = new SparseMemoryBuffer(0, (uint)nonZeroIndices.Size, 0, memorySpace, mathDomain); SyncPointers(); }
public SparseMemoryTile(PtrT pointer = 0, uint nNonZeros = 0, PtrT nonZeroColumnIndices = 0, PtrT nNonZeroRows = 0, uint nRows = 0, uint nCols = 0, MemorySpace memorySpace = MemorySpace.Null, MathDomain mathDomain = MathDomain.Null) : base(pointer, nNonZeros, memorySpace, mathDomain) { this.nonZeroColumnIndices = nonZeroColumnIndices; this.nNonZeroRows = nNonZeroRows; this.nRows = nRows; this.nCols = nCols; }
/* * Seed = random n bits, where n is digest size. * * For 100 iterations (j = 0) * MD[0] = MD[1] = MD[2] = Seed * * For 1000 iterations (i = 3) * M[i] = MD[i-3] || MD[i-2] || MD[i-1] * MD[i] = SHA(M[i]) * * MD[j] = Seed = MD[1002] (last MD from inner loop) */ #endregion MonteCarloAlgorithm Pseudocode public MctResult <AlgoArrayResponse> MctHash(BitString message, MathDomain domain = null, bool isSample = false) { if (isSample) { NUM_OF_RESPONSES = 3; } var responses = new List <AlgoArrayResponse>(); var i = 0; var j = 0; try { for (i = 0; i < NUM_OF_RESPONSES; i++) { BitString innerMessage = ResetDigestList(message); BitString innerDigest = null; var iterationResponse = new AlgoArrayResponse { Message = innerMessage }; for (j = 0; j < 1000; j++) { var innerResult = _sha.HashMessage(innerMessage); innerDigest = innerResult.Digest; AddDigestToList(innerDigest); innerMessage = GetNextMessage(); } iterationResponse.Digest = innerDigest; responses.Add(iterationResponse); message = innerDigest; } } catch (Exception ex) { ThisLogger.Debug($"i count {i}, j count {j}"); ThisLogger.Error(ex); return(new MctResult <AlgoArrayResponse>(ex.Message)); } return(new MctResult <AlgoArrayResponse>(responses)); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JArray jArray; try { jArray = JArray.Load(reader); } catch (Exception) { return(null); } // Create target object based on JObject MathDomain target = Create(jArray); return(target); }
public void ShouldWriteJsonAndDeserializeIntoSameObject() { MathDomain original = new MathDomain(); ValueDomainSegment vds = new ValueDomainSegment(1024); RangeDomainSegment rds = new RangeDomainSegment(new Random800_90(), 0, 1024, 128); original.AddSegment(vds); original.AddSegment(rds); var serializedObject = JsonSerializer.Serialize(original, _jsonSerializerOptions); var deserializedObject = JsonSerializer.Deserialize <MathDomain>(serializedObject, _jsonSerializerOptions); Assert.IsNotNull(deserializedObject, $"{nameof(deserializedObject)} was null."); var vdsReObjectified = deserializedObject.DomainSegments.OfType <ValueDomainSegment>().First(); var rdsReObjectified = deserializedObject.DomainSegments.OfType <RangeDomainSegment>().First(); Assert.AreEqual(original.DomainSegments.Count(), deserializedObject.DomainSegments.Count(), "count"); Assert.AreEqual(vds.ToString(), vdsReObjectified.ToString(), nameof(vds)); Assert.AreEqual(rds.ToString(), rdsReObjectified.ToString(), nameof(rds)); }
public SparseVector(int denseSize, Vector nonZeroIndices, double value, MathDomain mathDomain) : this(denseSize, nonZeroIndices, mathDomain) { values.Set(value); }
/* * INPUT: The initial Msg of 128 bits long * * Initial Outputlen = (floor(maxoutlen/8) )*8 * //makes maxoutlen a multiple of 8 and remains within the range specified. * * { * Output0 = Msg; * for (j=0; j<100; j++) { * for (i=1; i<1001; i++) { * M[i] = 128 leftmost bits of Output[i-1]; * Output[i] = SHAKE(M[i],Outputlen); * If (i == 1000){ * Outputlen[j] = Outputlen; * } * Rightmost_Output_bits = rightmost 16 bits of Output[i]; * Range = (maxoutbytes – minoutbytes + 1); * Outputlen = minoutbytes + (Rightmost_Output_bits mod Range); * } * Output[j] = Output[1000]; * OUTPUT: Outputlen[j], Output[j] * } * } */ #endregion MonteCarloAlgorithm Pseudocode public MctResult <AlgoArrayResponse> MctHash(BitString message, MathDomain domain, bool isSample = false) { if (isSample) { NUM_OF_RESPONSES = 3; } var responses = new List <AlgoArrayResponse>(); var i = 0; var j = 0; var min = domain.GetDomainMinMax().Minimum; var max = domain.GetDomainMinMax().Maximum; var minBytes = min / 8; var maxBytes = max / 8; var outputLen = (int)System.Math.Floor((double)max / 8) * 8; var range = (max - min) + 8; //var range = (max - min) + min; var innerMessage = message.GetDeepCopy(); // Might not have 128 bits to pull from so we pad with 0 innerMessage = BitString.ConcatenateBits(innerMessage, BitString.Zeroes(128)); innerMessage = BitString.MSBSubstring(innerMessage, 0, 128); try { for (i = 0; i < NUM_OF_RESPONSES; i++) { var innerDigest = new BitString(0); var iterationResponse = new AlgoArrayResponse() { }; iterationResponse.Message = innerMessage; for (j = 0; j < 1000; j++) { var innerResult = _sha.HashMessage(innerMessage, outputLen); innerDigest = innerResult.Digest.GetDeepCopy(); // Will always have 16 bits to pull from var rightmostBits = BitString.Substring(innerDigest, 0, 16).Bits; outputLen = min + (8 * GetIntFromBits(rightmostBits)) % range; innerMessage = innerDigest.GetDeepCopy(); // Might not have 128 bits to pull from so we pad with 0 innerMessage = BitString.ConcatenateBits(innerMessage, BitString.Zeroes(128)); innerMessage = BitString.MSBSubstring(innerMessage, 0, 128); } iterationResponse.Digest = innerDigest.GetDeepCopy(); responses.Add(iterationResponse); } } catch (Exception ex) { ThisLogger.Debug($"i count {i}, j count {j}"); ThisLogger.Error(ex); return(new MctResult <AlgoArrayResponse>($"{ex.Message}; {outputLen}")); } return(new MctResult <AlgoArrayResponse>(responses)); }
private static extern unsafe int _AbsMaxRaw(ref double min, PtrT v, uint size, MemorySpace memorySpace, MathDomain mathDomain);
private static extern unsafe int _SumRaw(ref double sum, PtrT v, uint size, MemorySpace memorySpace, MathDomain mathDomain);
public CompressedSparseRowMatrix(int nRows, int nCols, Vector nonZeroColumnIndices, Vector nNonZeroRows, MathDomain mathDomain) : base(false, // SparseVector doesn't allocate its memory in its buffer, but it uses the convenience vector this.values nonZeroColumnIndices.memorySpace, mathDomain) { _buffer = new SparseMemoryTile(0, (uint)nonZeroColumnIndices.Size, 0, 0, (uint)nRows, (uint)nCols, nonZeroColumnIndices.memorySpace, mathDomain); Debug.Assert(denseSize > nonZeroColumnIndices.Size); values = new Vector(nonZeroColumnIndices.Size, nonZeroColumnIndices.memorySpace, mathDomain); this.nonZeroColumnIndices = nonZeroColumnIndices; this.nNonZeroRows = nNonZeroRows; SyncPointers(); }
/* * INPUT: The initial Single-Tuple of a random length between 0 and 65536 bits. * * MCT(Tuple, MaxOutLen, MinOutLen, OutLenIncrement) * { * Range = (MaxOutLen – MinOutLen + 1); * OutputLen = MaxOutLen; * Customization = ""; * * T[0][0] = Tuple; * * for (j = 0; j < 100; j++) * { * for (i = 1; i < 1001; i++) * { * workingBits = Left(T[i-1][0] || ZeroBits(288), 288); * tupleSize = Left(workingBits, 3) % 4 + 1; // never more than 4 tuples to a round * for (k = 0; k < tupleSize; k++) * { * T[i][k] = Substring of workingBits from (k * 288 / tupleSize) to ((k+1) * 288 / tupleSize - 1); * } * Output[i] = TupleHash(T[i], OutputLen, Customization); * Rightmost_Output_bits = Right(Output[i], 16); * OutputLen = MinOutLen + (floor((Rightmost_Output_bits % Range) / OutLenIncrement) * OutLenIncrement); * Customization = BitsToString(T[i][0] || Rightmost_Output_bits); * } * * OutputJ[j] = Output[1000]; * } * * return OutputJ; * } */ #endregion MonteCarloAlgorithm Pseudocode public MCTResultTuple <AlgoArrayResponse> MCTHash(HashFunction function, IEnumerable <BitString> tuple, MathDomain domain, bool hexCustomization, bool isSample) { _hexCustomization = hexCustomization; if (isSample) { NUM_OF_RESPONSES = 3; } var responses = new List <AlgoArrayResponse>(); var i = 0; var j = 0; var min = domain.GetDomainMinMax().Minimum; var max = domain.GetDomainMinMax().Maximum; var increment = domain.GetDomainMinMax().Increment; var minBytes = min / 8; var maxBytes = max / 8; var outputLen = max; var customization = ""; var range = (max - min) + 1; var innerTuple = GetDeepCopy(tuple); try { for (i = 0; i < NUM_OF_RESPONSES; i++) { var innerDigest = new BitString(0); var iterationResponse = new AlgoArrayResponse() { }; iterationResponse.Tuple = innerTuple; iterationResponse.Customization = customization; for (j = 0; j < 1000; j++) { // Might not have 144 bits to pull from so we pad with 0 var innerBitString = BitString.ConcatenateBits(innerTuple.ElementAt(0), BitString.Zeroes(288)) .GetMostSignificantBits(288); var innerTupleSize = innerBitString.GetMostSignificantBits(3).Bits.ToInt() % 4 + 1; innerTuple = new List <BitString>(); for (int k = 0; k < innerTupleSize; k++) { innerTuple.Add(BitString.MSBSubstring(innerBitString, k * 288 / innerTupleSize, 288 / innerTupleSize)); } function.DigestLength = outputLen; var innerResult = _iTupleHash.HashMessage(function, innerTuple, customization); innerDigest = innerResult.Digest.GetDeepCopy(); // Will always have 16 bits to pull from var rightmostBitString = BitString.Substring(innerDigest, 0, 16); var rightmostBits = rightmostBitString.Bits; outputLen = min + (int)System.Math.Floor((double)(rightmostBits.ToInt() % range) / increment) * increment; customization = GetStringFromBytes(BitString.ConcatenateBits(innerTuple.ElementAt(0), rightmostBitString).ToBytes()); innerTuple = new List <BitString>(); innerTuple.Add(innerDigest.GetDeepCopy()); } iterationResponse.Digest = innerDigest.GetDeepCopy(); responses.Add(iterationResponse); } } catch (Exception ex) { ThisLogger.Debug($"i count {i}, j count {j}"); ThisLogger.Error(ex); return(new MCTResultTuple <AlgoArrayResponse>($"{ex.Message}; {outputLen}")); } return(new MCTResultTuple <AlgoArrayResponse>(responses)); }
private static extern unsafe int _SparseMultiplyRaw(PtrT A, PtrT B, PtrT C, uint nNonZeros, PtrT nonZeroColumnIndices, PtrT nNonZeroRows, uint nRowsB, uint nRowsC, uint nColsC, MemorySpace memorySpace, MathDomain mathDomain, uint leadingDimensionB, uint leadingDimensionC, MatrixOperation bOperation, double alpha);
public CompressedSparseRowMatrix(int nRows, int nCols, Vector nonZeroColumnIndices, Vector nNonZeroRows, double value, MathDomain mathDomain) : this(nRows, nCols, nonZeroColumnIndices, nNonZeroRows, mathDomain) { values.Set(value); }
public Vector(int size, MemorySpace memorySpace = MemorySpace.Device, MathDomain mathDomain = MathDomain.Float) : base(true, memorySpace, mathDomain) { _buffer = new MemoryBuffer(0, (uint)size, memorySpace, mathDomain); ctor(_buffer); }
public static Vector LinSpace(int size, double x0, double x1, MemorySpace memorySpace = MemorySpace.Device, MathDomain mathDomain = MathDomain.Float) { var vec = new Vector(size, memorySpace, mathDomain); vec.LinSpace(x0, x1); return(vec); }
public SparseVector(int denseSize, int nNonZeroIndices, MemorySpace memorySpace, MathDomain mathDomain) : this(denseSize, new Vector(nNonZeroIndices, memorySpace, MathDomain.Int), mathDomain) { }
public static Vector RandomGaussian(int size, int seed, MemorySpace memorySpace = MemorySpace.Device, MathDomain mathDomain = MathDomain.Float) { var vec = new Vector(size, memorySpace, mathDomain); vec.RandomGaussian(seed); return(vec); }
private static extern unsafe int _SparseAddRaw(PtrT z, PtrT x, PtrT y, uint nNonZeros, PtrT nonZeroIndices, MemorySpace memorySpace, MathDomain mathDomain, uint size, double alpha);
public Vector(int size, double value, MemorySpace memorySpace = MemorySpace.Device, MathDomain mathDomain = MathDomain.Float) : this(size, memorySpace, mathDomain) { Set(value); }
private static extern unsafe int _SparseSubtractRaw(PtrT z, PtrT x, PtrT y, uint nNonZeros, PtrT nonZeroIndices, MemorySpace memorySpace, MathDomain mathDomain, uint size);
/* * INPUT: The initial Msg is the length of the digest size * * MCT(Msg, MaxOutLen, MinOutLen, OutLenIncrement, MaxBlockSize, MinBlockSize) * { * Range = (MaxOutLen – MinOutLen + 1); * OutputLen = MaxOutLen; * BlockRange = (MaxBlockSize – MinBlockSize + 1); * BlockSize = MinBlockSize; * Customization = ""; * * Output[0] = Msg; * for (j = 0; j < 100; j++) * { * for (i = 1; i < 1001; i++) * { * InnerMsg = Left(Output[i-1] || ZeroBits(128), 128); * Output[i] = ParallelHash(InnerMsg, OutputLen, BlockSize, FunctionName, Customization); * Rightmost_Output_bits = Right(Output[i], 16); * OutputLen = MinOutLen + (floor((Rightmost_Output_bits % Range) / OutLenIncrement) * OutLenIncrement); * BlockSize = MinBlockSize + Right(Rightmost_Output_bits, 8) % BlockRange; * Customization = BitsToString(InnerMsg || Rightmost_Output_bits); * } * * OutputJ[j] = Output[1000]; * } * * return OutputJ; * } */ #endregion MonteCarloAlgorithm Pseudocode public MctResult <AlgoArrayResponseWithCustomization> MCTHash(HashFunction function, BitString message, MathDomain outputLength, MathDomain blockSizeDomain, bool hexCustomization, bool isSample) { _hexCustomization = hexCustomization; if (isSample) { NUM_OF_RESPONSES = 3; } var responses = new List <AlgoArrayResponseWithCustomization>(); var i = 0; var j = 0; var min = outputLength.GetDomainMinMax().Minimum; var max = outputLength.GetDomainMinMax().Maximum; var increment = outputLength.GetDomainMinMax().Increment; var minBlockSize = blockSizeDomain.GetDomainMinMax().Minimum; var maxBlockSize = blockSizeDomain.GetDomainMinMax().Maximum; var outputLen = max; var blockSize = minBlockSize; var blockSizeRange = (maxBlockSize - minBlockSize) + 1; var customization = ""; var range = (max - min) + 1; var innerMessage = message.GetDeepCopy(); try { for (i = 0; i < NUM_OF_RESPONSES; i++) { var innerDigest = new BitString(0); var iterationResponse = new AlgoArrayResponseWithCustomization() { }; iterationResponse.Message = innerMessage; iterationResponse.Customization = customization; iterationResponse.BlockSize = blockSize; for (j = 0; j < 1000; j++) { // Might not have 128 bits to pull from so we pad with 0 innerMessage = BitString.ConcatenateBits(innerMessage, BitString.Zeroes(128)) .GetMostSignificantBits(128); function.DigestLength = outputLen; var innerResult = _iParallelHash.HashMessage(function, innerMessage, blockSize, customization); innerDigest = innerResult.Digest.GetDeepCopy(); // Will always have 16 bits to pull from var rightmostBitString = innerDigest.GetLeastSignificantBits(16); var rightmostBits = rightmostBitString.Bits; outputLen = min + (int)System.Math.Floor((double)(rightmostBits.ToInt() % range) / increment) * increment; blockSize = minBlockSize + rightmostBitString.GetLeastSignificantBits(8).Bits.ToInt() % blockSizeRange; customization = GetStringFromBytes(BitString.ConcatenateBits(innerMessage, rightmostBitString).ToBytes()); innerMessage = innerDigest.GetDeepCopy(); } iterationResponse.Digest = innerDigest.GetDeepCopy(); responses.Add(iterationResponse); } } catch (Exception ex) { ThisLogger.Debug($"i count {i}, j count {j}"); ThisLogger.Error(ex); return(new MctResult <AlgoArrayResponseWithCustomization>($"{ex.Message}; {outputLen}")); } return(new MctResult <AlgoArrayResponseWithCustomization>(responses)); }
private static extern unsafe int _SparseDotRaw(PtrT y, PtrT A, PtrT x, uint nNonZeros, PtrT nonZeroColumnIndices, PtrT nNonZeroRows, uint nRows, uint nCols, MemorySpace memorySpace, MathDomain mathDomain, MatrixOperation aOperation, double alpha);