package gnarlring import ( "errors" "git.smesh.lol/gnarl-hamadryad/crypto" ) // Wire message types for gnarlring frames transported over GnarlWire. const ( MsgTypeCommitment = 0xE0 // child commitment frame (63 bytes) MsgTypeEpochFull = 0xE1 // full epoch broadcast (1787 bytes) MsgTypeEpochCheck = 0xE2 // compact epoch check (113 bytes) MsgTypeVote = 0xE3 // encrypted vote ) // SealCommitment encrypts and authenticates a child commitment frame. func SealCommitment(secret crypto.Hamadryad, identity crypto.GnarlMid, nonce [crypto.GnarlNonceLen]byte, cc *ChildCommitment) *crypto.GnarlPacket { payload := append([]byte{MsgTypeCommitment}, MarshalCommitmentFrame(cc)...) return crypto.GnarlSeal(secret, identity, nonce, payload) } // OpenCommitment verifies and decrypts a commitment frame. func OpenCommitment(secret crypto.Hamadryad, pkt *crypto.GnarlPacket) (*ChildCommitment, error) { plain, err := crypto.GnarlOpen(secret, pkt) if err != nil { return nil, err } if len(plain) < 1 || plain[0] != MsgTypeCommitment { return nil, errors.New("gnarlring: wrong message type for commitment") } return UnmarshalCommitmentFrame(plain[1:]), nil } // SealEpoch encrypts and authenticates a full epoch frame. func SealEpoch(secret crypto.Hamadryad, identity crypto.GnarlMid, nonce [crypto.GnarlNonceLen]byte, es *EpochState) *crypto.GnarlPacket { payload := append([]byte{MsgTypeEpochFull}, MarshalEpochFrame(es)...) return crypto.GnarlSeal(secret, identity, nonce, payload) } // OpenEpoch verifies and decrypts a full epoch frame. func OpenEpoch(secret crypto.Hamadryad, pkt *crypto.GnarlPacket) (*EpochState, error) { plain, err := crypto.GnarlOpen(secret, pkt) if err != nil { return nil, err } if len(plain) < 1 || plain[0] != MsgTypeEpochFull { return nil, errors.New("gnarlring: wrong message type for epoch") } return UnmarshalEpochFrame(plain[1:]), nil } // SealEpochCheck encrypts and authenticates a compact epoch check frame. func SealEpochCheck(secret crypto.Hamadryad, identity crypto.GnarlMid, nonce [crypto.GnarlNonceLen]byte, es *EpochState) *crypto.GnarlPacket { payload := append([]byte{MsgTypeEpochCheck}, MarshalEpochCheckFrame(es)...) return crypto.GnarlSeal(secret, identity, nonce, payload) } // OpenEpochCheck verifies and decrypts a compact epoch check frame. func OpenEpochCheck(secret crypto.Hamadryad, pkt *crypto.GnarlPacket) (*EpochCheckFrame, error) { plain, err := crypto.GnarlOpen(secret, pkt) if err != nil { return nil, err } if len(plain) < 1 || plain[0] != MsgTypeEpochCheck { return nil, errors.New("gnarlring: wrong message type for epoch check") } return UnmarshalEpochCheckFrame(plain[1:]) }