[lib.context] added 'minDepth' and 'depthThreshold' + [client.keyset] added context management
This commit is contained in:
parent
6d0a99b0d6
commit
b5c8477582
|
@ -1,6 +1,7 @@
|
||||||
package keyset
|
package keyset
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.xdrm.io/schastsp/lib/context"
|
||||||
"io"
|
"io"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -13,12 +14,12 @@ const a = 12
|
||||||
|
|
||||||
/* Attributes */
|
/* Attributes */
|
||||||
type Set struct {
|
type Set struct {
|
||||||
min uint16 // min depth
|
ctx *context.Context // current context
|
||||||
depth uint16 // cur depth
|
depth uint16 // cur depth
|
||||||
max uint16 // max depth
|
max uint16 // max depth
|
||||||
|
|
||||||
sec []byte // secret
|
sec []byte // secret
|
||||||
consumption uint // consumption level
|
consumption uint // consumption level
|
||||||
// 0: none
|
// 0: none
|
||||||
// 1: need to migrate
|
// 1: need to migrate
|
||||||
// 2: waiting migration
|
// 2: waiting migration
|
||||||
|
@ -33,18 +34,18 @@ type Set struct {
|
||||||
* @return outName<outType> outDesc
|
* @return outName<outType> outDesc
|
||||||
*
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
func Create(min uint16, max uint16) (*Set, error) {
|
func Create(ctx *context.Context, max uint16) (*Set, error) {
|
||||||
|
|
||||||
/* (1) Fail if min >= max */
|
/* (1) Fail if min+thre >= max */
|
||||||
if min >= max {
|
if ctx.MinDepth()+ctx.DepthThreshold() >= max {
|
||||||
return nil, errors.New("Max depth is not greater than Min depth")
|
return nil, errors.New("Maximum depth must be greater than Min+threshold (from given Context)")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Instanciate */
|
/* (2) Instanciate */
|
||||||
var instance = new(Set)
|
var instance = new(Set)
|
||||||
|
|
||||||
/* (3) Set attributes */
|
/* (3) Set attributes */
|
||||||
instance.min = min
|
instance.ctx = ctx
|
||||||
instance.max = max
|
instance.max = max
|
||||||
|
|
||||||
/* (4) Generate values if <nil> secret */
|
/* (4) Generate values if <nil> secret */
|
||||||
|
@ -76,7 +77,7 @@ func (s *Set) generate() {
|
||||||
/* (2) Manage other attributes
|
/* (2) Manage other attributes
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* (1) Random depth pick init */
|
/* (1) Random depth pick init */
|
||||||
var randMin, randMax uint16 = s.min + (s.max-s.min)/2, s.max
|
var randMin, randMax uint16 = s.ctx.MinDepth() + (s.max-s.ctx.MinDepth())/2, s.max
|
||||||
|
|
||||||
/* (2) Select "random" depth */
|
/* (2) Select "random" depth */
|
||||||
s.depth = randMin + uint16(rand.Intn(int(randMax-randMin)))
|
s.depth = randMin + uint16(rand.Intn(int(randMax-randMin)))
|
||||||
|
@ -117,7 +118,7 @@ func (s *Set) Decrement() uint16 {
|
||||||
s.depth--
|
s.depth--
|
||||||
|
|
||||||
/* (2) If near minDepth (10 far): set consumption to 1 */
|
/* (2) If near minDepth (10 far): set consumption to 1 */
|
||||||
if s.depth <= s.min+10 {
|
if s.depth <= s.ctx.MinDepth()+s.ctx.DepthThreshold() {
|
||||||
s.consumption = 1
|
s.consumption = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +147,7 @@ func (s *Set) Write(writer io.Writer) error {
|
||||||
|
|
||||||
var err error;
|
var err error;
|
||||||
|
|
||||||
/* (1) Copy hash size */
|
/* (1) Copy secret size */
|
||||||
err = binary.Write(writer, binary.BigEndian, scha.HSIZE)
|
err = binary.Write(writer, binary.BigEndian, scha.HSIZE)
|
||||||
if err != nil { return err }
|
if err != nil { return err }
|
||||||
|
|
||||||
|
@ -178,16 +179,12 @@ func (s *Set) Write(writer io.Writer) error {
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
func (s *Set) Read(reader io.Reader) error {
|
func (s *Set) Read(reader io.Reader) error {
|
||||||
|
|
||||||
var (
|
var err error
|
||||||
err error
|
var secretLength uint16
|
||||||
secretLength uint16
|
|
||||||
)
|
|
||||||
|
|
||||||
/* (1) Read the secret size */
|
/* (1) Read the secret size */
|
||||||
err = binary.Read(reader, binary.BigEndian, &secretLength)
|
err = binary.Read(reader, binary.BigEndian, &secretLength)
|
||||||
if err != nil {
|
if err != nil { return err }
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (2) Fail if secretLength different than digest size */
|
/* (2) Fail if secretLength different than digest size */
|
||||||
if secretLength != scha.HSIZE {
|
if secretLength != scha.HSIZE {
|
||||||
|
|
|
@ -1,20 +1,37 @@
|
||||||
package context
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
/* (1) Context class
|
/* (1) Context class
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* (1) Attributes */
|
/* (1) Attributes */
|
||||||
type Context struct {
|
type Context struct {
|
||||||
windowSize float64; // 'timeid' window size
|
win float64; // 'timeid' window size
|
||||||
|
dMin uint16; // minimum scha depth
|
||||||
|
dThreshold uint16; // scha depth threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Constructor */
|
/* (2) Constructor */
|
||||||
func Create(winSize float64) *Context{
|
func Create(win float64, dMin uint16, dThreshold uint16) (*Context, error) {
|
||||||
var instance = new(Context);
|
var instance = new(Context);
|
||||||
|
|
||||||
instance.windowSize = winSize;
|
/* (1) Window size error */
|
||||||
|
if win < 0 { return nil, errors.New("Window size must be positive and is negative") }
|
||||||
|
instance.win = win;
|
||||||
|
|
||||||
return instance
|
/* (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;
|
||||||
|
|
||||||
|
/* (3) Depth Threshold error */
|
||||||
|
instance.dThreshold = dThreshold;
|
||||||
|
|
||||||
|
return instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (3) Getters */
|
/* (3) Getters */
|
||||||
func (c Context) GetWindowSize() float64 { return c.windowSize }
|
func (c Context) Window() float64 { return c.win }
|
||||||
|
func (c Context) MinDepth() uint16 { return c.dMin }
|
||||||
|
func (c Context) DepthThreshold() uint16 { return c.dThreshold }
|
Loading…
Reference in New Issue