insurge_wire.go raw

   1  package gnarlring
   2  
   3  import (
   4  	"errors"
   5  
   6  	"git.smesh.lol/gnarl-hamadryad/crypto"
   7  )
   8  
   9  // Wire message types for gnarlring frames transported over GnarlWire.
  10  const (
  11  	MsgTypeCommitment  = 0xE0 // child commitment frame (63 bytes)
  12  	MsgTypeEpochFull   = 0xE1 // full epoch broadcast (1787 bytes)
  13  	MsgTypeEpochCheck  = 0xE2 // compact epoch check (113 bytes)
  14  	MsgTypeVote        = 0xE3 // encrypted vote
  15  )
  16  
  17  // SealCommitment encrypts and authenticates a child commitment frame.
  18  func SealCommitment(secret crypto.Hamadryad, identity crypto.GnarlMid,
  19  	nonce [crypto.GnarlNonceLen]byte, cc *ChildCommitment) *crypto.GnarlPacket {
  20  	payload := append([]byte{MsgTypeCommitment}, MarshalCommitmentFrame(cc)...)
  21  	return crypto.GnarlSeal(secret, identity, nonce, payload)
  22  }
  23  
  24  // OpenCommitment verifies and decrypts a commitment frame.
  25  func OpenCommitment(secret crypto.Hamadryad, pkt *crypto.GnarlPacket) (*ChildCommitment, error) {
  26  	plain, err := crypto.GnarlOpen(secret, pkt)
  27  	if err != nil {
  28  		return nil, err
  29  	}
  30  	if len(plain) < 1 || plain[0] != MsgTypeCommitment {
  31  		return nil, errors.New("gnarlring: wrong message type for commitment")
  32  	}
  33  	return UnmarshalCommitmentFrame(plain[1:]), nil
  34  }
  35  
  36  // SealEpoch encrypts and authenticates a full epoch frame.
  37  func SealEpoch(secret crypto.Hamadryad, identity crypto.GnarlMid,
  38  	nonce [crypto.GnarlNonceLen]byte, es *EpochState) *crypto.GnarlPacket {
  39  	payload := append([]byte{MsgTypeEpochFull}, MarshalEpochFrame(es)...)
  40  	return crypto.GnarlSeal(secret, identity, nonce, payload)
  41  }
  42  
  43  // OpenEpoch verifies and decrypts a full epoch frame.
  44  func OpenEpoch(secret crypto.Hamadryad, pkt *crypto.GnarlPacket) (*EpochState, error) {
  45  	plain, err := crypto.GnarlOpen(secret, pkt)
  46  	if err != nil {
  47  		return nil, err
  48  	}
  49  	if len(plain) < 1 || plain[0] != MsgTypeEpochFull {
  50  		return nil, errors.New("gnarlring: wrong message type for epoch")
  51  	}
  52  	return UnmarshalEpochFrame(plain[1:]), nil
  53  }
  54  
  55  // SealEpochCheck encrypts and authenticates a compact epoch check frame.
  56  func SealEpochCheck(secret crypto.Hamadryad, identity crypto.GnarlMid,
  57  	nonce [crypto.GnarlNonceLen]byte, es *EpochState) *crypto.GnarlPacket {
  58  	payload := append([]byte{MsgTypeEpochCheck}, MarshalEpochCheckFrame(es)...)
  59  	return crypto.GnarlSeal(secret, identity, nonce, payload)
  60  }
  61  
  62  // OpenEpochCheck verifies and decrypts a compact epoch check frame.
  63  func OpenEpochCheck(secret crypto.Hamadryad, pkt *crypto.GnarlPacket) (*EpochCheckFrame, error) {
  64  	plain, err := crypto.GnarlOpen(secret, pkt)
  65  	if err != nil {
  66  		return nil, err
  67  	}
  68  	if len(plain) < 1 || plain[0] != MsgTypeEpochCheck {
  69  		return nil, errors.New("gnarlring: wrong message type for epoch check")
  70  	}
  71  	return UnmarshalEpochCheckFrame(plain[1:])
  72  }
  73