summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/sphincsplus/Adrs.cs
blob: a6ec753fdf4255504b3aa26ae214d152b9ac6021 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus
{

    internal class Adrs
    {
        public static uint WOTS_HASH = 0;
        public static uint WOTS_PK = 1;
        public static uint TREE = 2;
        public static uint FORS_TREE = 3;
        public static uint FORS_PK = 4;
        public static uint WOTS_PRF = 5;
        public static uint FORS_PRF = 6;

        internal static int OFFSET_LAYER = 0;
        internal static int OFFSET_TREE = 4;
        static int OFFSET_TREE_HGT = 24;
        static int OFFSET_TREE_INDEX = 28;
        internal static int OFFSET_TYPE = 16;
        static int OFFSET_KP_ADDR = 20;
        static int OFFSET_CHAIN_ADDR = 24;
        static int OFFSET_HASH_ADDR = 28;

        internal byte[] value = new byte[32];

        internal Adrs()
        {
        }

        internal Adrs(Adrs adrs)
        {
            Array.Copy(adrs.value, 0, this.value, 0, adrs.value.Length);
        }

        public void SetLayerAddress(uint layer)
        {
            Pack.UInt32_To_BE(layer, value, OFFSET_LAYER);
        }

        public uint GetLayerAddress()
        {
            return Pack.BE_To_UInt32(value, OFFSET_LAYER);
        }

        public void SetTreeAddress(ulong tree)
        {
            // tree address is 12 bytes
            Pack.UInt64_To_BE(tree, value, OFFSET_TREE + 4);
        }

        public ulong GetTreeAddress()
        {
            return Pack.BE_To_UInt64(value, OFFSET_TREE + 4);
        }

        public void SetTreeHeight(uint height)
        {
            Pack.UInt32_To_BE(height, value, OFFSET_TREE_HGT);
        }

        public uint GetTreeHeight()
        {
            return Pack.BE_To_UInt32(value, OFFSET_TREE_HGT);
        }

        public void SetTreeIndex(uint index)
        {
            Pack.UInt32_To_BE(index, value, OFFSET_TREE_INDEX);
        }

        public uint GetTreeIndex()
        {
            return Pack.BE_To_UInt32(value, OFFSET_TREE_INDEX);
        }

        // resets part of value to zero in line with 2.7.3
        public void SetType(uint type)
        {
            Pack.UInt32_To_BE(type, value, OFFSET_TYPE);

            Arrays.Fill(value, 20, value.Length, (byte) 0);
        }
        
        public void ChangeType(uint type)
        {
            Pack.UInt32_To_BE(type, value, OFFSET_TYPE);
        }

        public new uint GetType()
        {
            return Pack.BE_To_UInt32(value, OFFSET_TYPE);
        }

        public void SetKeyPairAddress(uint keyPairAddr)
        {
            Pack.UInt32_To_BE(keyPairAddr, value, OFFSET_KP_ADDR);
        }

        public uint GetKeyPairAddress()
        {
            return Pack.BE_To_UInt32(value, OFFSET_KP_ADDR);
        }

        public void SetHashAddress(uint hashAddr)
        {
            Pack.UInt32_To_BE(hashAddr, value, OFFSET_HASH_ADDR);
        }

        public void SetChainAddress(uint chainAddr)
        {
            Pack.UInt32_To_BE(chainAddr, value, OFFSET_CHAIN_ADDR);
        }
    }
}