/// <summary> /// Creates a new correlation vector by applying the Spin operator to an existing value. /// This should be done at the entry point of an operation. /// </summary> /// <param name="correlationVector"> /// Taken from the message header indicated by <see cref="HeaderName"/>. /// </param> /// <param name="parameters"> /// The parameters to use when applying the Spin operator. /// </param> /// <returns>A new correlation vector extended from the current vector.</returns> public static CorrelationVector Spin(string correlationVector, SpinParameters parameters) { if (CorrelationVector.IsImmutable(correlationVector)) { return(CorrelationVector.Parse(correlationVector)); } CorrelationVectorVersion version = CorrelationVector.InferVersion( correlationVector, CorrelationVector.ValidateCorrelationVectorDuringCreation); if (CorrelationVector.ValidateCorrelationVectorDuringCreation) { CorrelationVector.Validate(correlationVector, version); } byte[] entropy = new byte[parameters.EntropyBytes]; rng.NextBytes(entropy); ulong value = (ulong)(DateTime.UtcNow.Ticks >> parameters.TicksBitsToDrop); for (int i = 0; i < parameters.EntropyBytes; i++) { value = (value << 8) | Convert.ToUInt64(entropy[i]); } // Generate a bitmask and mask the lower TotalBits in the value. // The mask is generated by (1 << TotalBits) - 1. We need to handle the edge case // when shifting 64 bits, as it wraps around. value &= (parameters.TotalBits == 64 ? 0 : (ulong)1 << parameters.TotalBits) - 1; string s = unchecked ((uint)value).ToString(); if (parameters.TotalBits > 32) { s = string.Concat((value >> 32).ToString(), ".", s); } string baseVector = string.Concat(correlationVector, ".", s); if (CorrelationVector.IsOversized(baseVector, 0, version)) { return(CorrelationVector.Parse(correlationVector + CorrelationVector.TerminationSign)); } return(new CorrelationVector(baseVector, 0, version, false)); }
/// <summary> /// Creates a new correlation vector by parsing its string representation /// </summary> /// <param name="correlationVector">correlationVector</param> /// <returns>CorrelationVector</returns> public static CorrelationVector Parse(string correlationVector) { if (!string.IsNullOrEmpty(correlationVector)) { int p = correlationVector.LastIndexOf('.'); bool immutable = CorrelationVector.IsImmutable(correlationVector); if (p > 0) { string extensionValue = immutable ? correlationVector.Substring(p + 1, correlationVector.Length - p - 1 - CorrelationVector.TerminationSign.Length) : correlationVector.Substring(p + 1); int extension; if (int.TryParse(extensionValue, out extension) && extension >= 0) { return(new CorrelationVector(correlationVector.Substring(0, p), extension, CorrelationVector.InferVersion(correlationVector, false), immutable)); } } } return(new CorrelationVector()); }
/// <summary> /// Creates a new correlation vector by extending an existing value. This should be /// done at the entry point of an operation. /// </summary> /// <param name="correlationVector"> /// Taken from the message header indicated by <see cref="HeaderName"/>. /// </param> /// <returns>A new correlation vector extended from the current vector.</returns> public static CorrelationVector Extend(string correlationVector) { if (CorrelationVector.IsImmutable(correlationVector)) { return(CorrelationVector.Parse(correlationVector)); } CorrelationVectorVersion version = CorrelationVector.InferVersion( correlationVector, CorrelationVector.ValidateCorrelationVectorDuringCreation); if (CorrelationVector.ValidateCorrelationVectorDuringCreation) { CorrelationVector.Validate(correlationVector, version); } if (CorrelationVector.IsOversized(correlationVector, 0, version)) { return(CorrelationVector.Parse(correlationVector + CorrelationVector.TerminationSign)); } return(new CorrelationVector(correlationVector, 0, version, false)); }