renamed [lib.CyclicHash] to [lib.scha] + renamed [lib.wtime] to [lib.timeid]
This commit is contained in:
parent
6b9fdecfb1
commit
403e3f8538
|
@ -1,4 +1,4 @@
|
||||||
package CyclicHash
|
package scha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -6,9 +6,9 @@ import (
|
||||||
"git.xdrm.io/schastsp/lib/xor"
|
"git.xdrm.io/schastsp/lib/xor"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* (0) Constants
|
/* (0) Static
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* (1) Hash size */
|
/* (1) Constants */
|
||||||
const HBITSIZE uint = 512;
|
const HBITSIZE uint = 512;
|
||||||
const HSIZE uint = HBITSIZE / 8;
|
const HSIZE uint = HBITSIZE / 8;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package CyclicHash
|
package scha
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package wtime
|
package timeid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
@ -6,40 +6,33 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
/* (0) Definitions
|
|
||||||
---------------------------------------------------------*/
|
|
||||||
/* (1) Time ID struct */
|
|
||||||
type TimeID struct { ID uint; Parity uint }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* (1) Generates the current time id
|
/* (1) Generates the current time id
|
||||||
*
|
*
|
||||||
* @wsize<float64> Window Size in seconds
|
* @wsize<float64> Window Size in seconds
|
||||||
*
|
*
|
||||||
|
* @return id<uint> Current time id
|
||||||
|
* @return parity<uint> Current time parity
|
||||||
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
func GenerateID(wsize float64) *TimeID{
|
func Generate(wsize float64) (uint, uint){
|
||||||
|
|
||||||
/* (0) Initialise instance */
|
|
||||||
instance := new(TimeID);
|
|
||||||
|
|
||||||
/* (1) If wsize is 0 (div by zero possible error) */
|
/* (1) If wsize is 0 (div by zero possible error) */
|
||||||
if wsize == 0 { return instance }
|
if wsize == 0 { return 0, 0 }
|
||||||
|
|
||||||
/* (2) Get current timestamp */
|
/* (2) Get current timestamp */
|
||||||
timestamp := float64( time.Now().Unix() );
|
timestamp := float64( time.Now().Unix() );
|
||||||
|
|
||||||
/* (3) Calculate the time id */
|
/* (3) Calculate the time id */
|
||||||
instance.ID = uint( timestamp / wsize );
|
var id = uint( timestamp / wsize );
|
||||||
|
|
||||||
/* (4) Calculate parity */
|
/* (4) Calculate parity */
|
||||||
instance.Parity = instance.ID % 2;
|
var parity = id % 2;
|
||||||
|
|
||||||
return instance;
|
return id, parity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* (2) Try to guess the received ID from current time and received parity
|
/* (2) Try to guess a previous time id from its parity
|
||||||
*
|
*
|
||||||
* @wsize<float64> Window Size in seconds
|
* @wsize<float64> Window Size in seconds
|
||||||
* @parity<uint> Received parity
|
* @parity<uint> Received parity
|
||||||
|
@ -47,12 +40,12 @@ func GenerateID(wsize float64) *TimeID{
|
||||||
* @return id<uint> The guessed time id
|
* @return id<uint> The guessed time id
|
||||||
*
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
func GuessID(wsize float64, parity uint) uint{
|
func Guess(wsize float64, parity uint) uint{
|
||||||
|
|
||||||
/* (1) Get current time id */
|
/* (1) Get current time id */
|
||||||
var tidNow *TimeID = GenerateID(wsize);
|
var idNow, parityNow = Generate(wsize);
|
||||||
|
|
||||||
/* (2) Update ID with tidNow parity difference */
|
/* (2) Update ID with tidNow parity difference */
|
||||||
return tidNow.ID - uint(math.Abs( float64(tidNow.Parity) - float64(parity) ));
|
return idNow - uint(math.Abs( float64(parityNow) - float64(parity) ));
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package timeid
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func TestGuessing(t *testing.T){
|
||||||
|
|
||||||
|
var windowSize float64 = .5;
|
||||||
|
|
||||||
|
id, parity := Generate(windowSize);
|
||||||
|
|
||||||
|
time.Sleep( time.Duration(windowSize) * time.Second);
|
||||||
|
|
||||||
|
var guessedId uint = Guess(windowSize, parity);
|
||||||
|
|
||||||
|
if id != guessedId {
|
||||||
|
t.Errorf("Wrong guessed id, expected '%d' ; got '%d'", id, guessedId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,23 +0,0 @@
|
||||||
package wtime
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
func TestGuessing(t *testing.T){
|
|
||||||
|
|
||||||
var windowSize float64 = .5;
|
|
||||||
|
|
||||||
var tid *TimeID = GenerateID(windowSize);
|
|
||||||
|
|
||||||
time.Sleep( time.Duration(windowSize) * time.Second);
|
|
||||||
|
|
||||||
var guessedId uint = GuessID(windowSize, tid.Parity);
|
|
||||||
|
|
||||||
if tid.ID != guessedId {
|
|
||||||
t.Errorf("Wrong guessed id, expected '%d' ; got '%d'", tid.ID, guessedId);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue