diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset.go b/src/git.xdrm.io/schastsp/client/keyset/keyset.go index 5d7f3ba..0a44ac7 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset.go @@ -28,17 +28,14 @@ type Set struct { /* (1) Creates a new KeySet * -* @min Minimum depth value -* @max Maximum depth value -* -* @return outName outDesc +* @ctx Context constants * ---------------------------------------------------------*/ -func Create(ctx *context.Context, max uint16) (*Set, error) { +func Create(ctx *context.Context) (*Set, error) { /* (1) Fail if min+thre >= max */ - if max < ctx.MinDepth()+ctx.DepthThreshold() { - return nil, errors.New("Maximum depth must be greater than Min+threshold (from given Context)") + if ctx == nil { + return nil, errors.New("Context must not be null") } /* (2) Instanciate */ @@ -46,7 +43,6 @@ func Create(ctx *context.Context, max uint16) (*Set, error) { /* (3) Set attributes */ instance.ctx = ctx - instance.max = max /* (4) Generate values if secret */ if instance.sec == nil { @@ -77,7 +73,7 @@ func (s *Set) generate() { /* (2) Manage other attributes ---------------------------------------------------------*/ /* (1) Random depth pick init */ - var randMin, randMax uint16 = s.ctx.MinDepth() + (s.max-s.ctx.MinDepth())/2, s.max + var randMin, randMax = s.ctx.MinDepth() + (s.ctx.MaxDepth()-s.ctx.MinDepth()) / 2, s.ctx.MaxDepth() /* (2) Select "random" depth */ s.depth = randMin + uint16(rand.Intn(int(randMax-randMin))) diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go b/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go index cd26580..dde68a0 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go @@ -1,27 +1,21 @@ package keyset import ( + "git.xdrm.io/schastsp/lib/context" "git.xdrm.io/schastsp/lib/scha" "testing" ) - -func TestCreateNotMaxGreaterThanMin(t *testing.T) { - - var _, err = Create(2, 1) - - if err == nil { - t.Errorf("Expected an error") - } - -} - func TestGenerationDepthBoundaries(t *testing.T) { var min, max uint16 = 0x0f0, 0xfff var rangeMin = min + (max-min)/2 var rangeMax = max + var created *Set; - var created, err = Create(min, max) + ctx, err := context.Create(2.5, min, 0, max); + if err != nil { t.Errorf("Do not expected an error: %s", err); return } + + created, err = Create(ctx) if err != nil { t.Errorf("Do not expected an error, got: %s", err) @@ -34,12 +28,13 @@ func TestGenerationDepthBoundaries(t *testing.T) { } func TestSchaDecrementingProperty(t *testing.T) { + ctx, err := context.Create(2.5); + if err != nil { t.Errorf("Do not expected an error"); return } var h1, h2, hcheck []byte - var err error var created *Set - created, err = Create(0x0f0, 0xfff) + created, err = Create(ctx) if err != nil { t.Errorf("Do not expected an error, got: %s", err) @@ -70,7 +65,7 @@ func TestSchaDecrementingProperty(t *testing.T) { for k, v := range h1 { if v != hcheck[k] { - t.Errorf("Expected h(h[x-1]) to equel h[x]; expected '%x' ; got '%x'", h1, hcheck) + t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck) return } @@ -79,12 +74,13 @@ func TestSchaDecrementingProperty(t *testing.T) { } func TestDecrementMinimum(t *testing.T) { + ctx, err := context.Create(2.5); + if err != nil { t.Errorf("Do not expected an error"); return } var h1, h2, hcheck []byte - var err error var created *Set - created, err = Create(0x0f0, 0xfff) + created, err = Create(ctx) if err != nil { t.Errorf("Do not expected an error, got: %s", err) @@ -115,7 +111,7 @@ func TestDecrementMinimum(t *testing.T) { for k, v := range h1 { if v != hcheck[k] { - t.Errorf("Expected h(h[x-1]) to equel h[x]; expected '%x' ; got '%x'", h1, hcheck) + t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck) return } diff --git a/src/git.xdrm.io/schastsp/lib/context/context.go b/src/git.xdrm.io/schastsp/lib/context/context.go index 5cade7d..cf7d62f 100644 --- a/src/git.xdrm.io/schastsp/lib/context/context.go +++ b/src/git.xdrm.io/schastsp/lib/context/context.go @@ -4,34 +4,77 @@ import ( "errors" ) -/* (1) Context class +/* (1) Definitions ---------------------------------------------------------*/ -/* (1) Attributes */ +/* (1) Default values */ +const DefaultMin = 0x00f0 +const DefaultMax = 0x0fff +const DefaultThr = 0x000a + + +/* (2) Struct attributes */ type Context struct { - win float64; // 'timeid' window size - dMin uint16; // minimum scha depth - dThreshold uint16; // scha depth threshold + win float64; // 'timeid' window size + min uint16; // minimum scha depth + max uint16; // maximum scha depth + thr uint16; // scha depth threshold } -/* (2) Constructor */ -func Create(win float64, dMin uint16, dThreshold uint16) (*Context, error) { +/* (3) Constructor +* +* @win Window size for 'timeid' package +* @min [OPT] Minimum scha depth +* @thr [OPT] scha depth threshold before renewal +* @max [OPT] Maximum scha depth +* +* @return outName outDesc +* +---------------------------------------------------------*/ + +func Create(win float64, optional... uint16) (*Context, error) { + var instance = new(Context); /* (1) Window size error */ if win < 0 { return nil, errors.New("Window size must be positive and is negative") } instance.win = win; - /* (2) Minimum Depth error */ - if dMin < 0x0f { return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues") } - instance.dMin = dMin; + /* (2) Default values */ + instance.min = DefaultMin + instance.thr = DefaultThr + instance.max = DefaultMax - /* (3) Depth Threshold error */ - instance.dThreshold = dThreshold; + /* (3) Optional 'min' */ + if len(optional) > 0 { + + if optional[0] < 0x0f { return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues") } + + instance.min = optional[0]; + + } + + /* (4) Optional 'thr' */ + if len(optional) > 1 { + instance.thr = optional[1]; + } + + + /* (5) Optional 'max' */ + if len(optional) > 2 { + + if optional[2] <= instance.min+instance.thr { + return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues") + } + + instance.max = optional[2]; + + } return instance, nil } -/* (3) Getters */ +/* (4) Getters */ func (c Context) Window() float64 { return c.win } -func (c Context) MinDepth() uint16 { return c.dMin } -func (c Context) DepthThreshold() uint16 { return c.dThreshold } \ No newline at end of file +func (c Context) MinDepth() uint16 { return c.min } +func (c Context) MaxDepth() uint16 { return c.max } +func (c Context) DepthThreshold() uint16 { return c.thr } \ No newline at end of file