-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring_pedersen_parameters.c
executable file
·201 lines (163 loc) · 5.82 KB
/
ring_pedersen_parameters.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "ring_pedersen_parameters.h"
#include <assert.h>
ring_pedersen_private_t *ring_pedersen_private_new ()
{
ring_pedersen_private_t *priv = malloc(sizeof(ring_pedersen_private_t));
priv->phi_N = scalar_new();
priv->N = scalar_new();
priv->t = scalar_new();
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
priv->lam[i] = scalar_new();
priv->s[i] = scalar_new();
}
return priv;
}
void ring_pedersen_private_from_primes (ring_pedersen_private_t *priv, const scalar_t p, const scalar_t q)
{
BN_CTX *bn_ctx = BN_CTX_secure_new();
BN_mul(priv->N, p, q, bn_ctx);
BN_sub(priv->phi_N, priv->N, p);
BN_sub(priv->phi_N, priv->phi_N, q);
BN_add_word(priv->phi_N, 1);
scalar_t r = scalar_new();
scalar_sample_in_range(r, priv->N, 1, bn_ctx);
BN_mod_mul(priv->t, r, r, priv->N, bn_ctx);
scalar_free(r);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
scalar_sample_in_range(priv->lam[i], priv->phi_N, 0, bn_ctx);
BN_mod_exp(priv->s[i], priv->t, priv->lam[i], priv->N, bn_ctx);
}
BN_CTX_free(bn_ctx);
}
void ring_pedersen_generate_private (ring_pedersen_private_t *priv, uint64_t prime_bits)
{
scalar_t p = scalar_new();
scalar_t q = scalar_new();
BN_generate_prime_ex(p, prime_bits, 1, NULL, NULL, NULL);
BN_generate_prime_ex(q, prime_bits, 1, NULL, NULL, NULL);
ring_pedersen_private_from_primes(priv, p, q);
scalar_free(p);
scalar_free(q);
}
ring_pedersen_public_t *ring_pedersen_public_new()
{
ring_pedersen_public_t *pub = malloc(sizeof(ring_pedersen_public_t));
pub->N = scalar_new();
pub->t = scalar_new();
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
pub->s[i] = scalar_new();
}
return pub;
}
void ring_pedersen_copy_param (ring_pedersen_private_t *copy_priv, ring_pedersen_public_t *copy_pub, const ring_pedersen_private_t *priv, const ring_pedersen_public_t *pub)
{
if (pub && copy_pub)
{
BN_copy(copy_pub->N, pub->N);
BN_copy(copy_pub->t, pub->t);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
BN_copy(copy_pub->s[i], pub->s[i]);
}
}
if (priv)
{
if (copy_priv)
{
BN_copy(copy_priv->N, priv->N);
BN_copy(copy_priv->t, priv->t);
BN_copy(copy_priv->phi_N, priv->phi_N);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i){
BN_copy(copy_priv->lam[i], priv->lam[i]);
BN_copy(copy_priv->s[i], priv->s[i]);
}
}
if (!pub && copy_pub)
{
BN_copy(copy_pub->N, priv->N);
BN_copy(copy_pub->t, priv->t);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
BN_copy(copy_pub->s[i], priv->s[i]);
}
}
}
}
void ring_pedersen_free_param(ring_pedersen_private_t *priv, ring_pedersen_public_t *pub)
{
if (priv)
{
scalar_free(priv->phi_N);
scalar_free(priv->N);
scalar_free(priv->t);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
scalar_free(priv->s[i]);
scalar_free(priv->lam[i]);
}
free(priv);
}
if (pub)
{
scalar_free(pub->N);
scalar_free(pub->t);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
scalar_free(pub->s[i]);
}
free(pub);
}
}
void ring_pedersen_commit(scalar_t rped_commitment, const scalar_t *s_exp, uint64_t num_s_exp, const scalar_t t_exp, const ring_pedersen_public_t *rped_pub)
{
assert(num_s_exp <= RING_PEDERSEN_MULTIPLICITY);
BN_CTX *bn_ctx = BN_CTX_secure_new();
scalar_t res_rped_commitment = scalar_new();
scalar_t curr_factor = scalar_new();
BN_mod_exp(res_rped_commitment, rped_pub->t, t_exp, rped_pub->N, bn_ctx);
if (BN_is_negative(t_exp)) BN_mod_inverse(res_rped_commitment, res_rped_commitment, rped_pub->N, bn_ctx);
for (uint64_t i = 0; i < num_s_exp; ++i) {
BN_mod_exp(curr_factor, rped_pub->s[i], s_exp[i], rped_pub->N, bn_ctx);
if (BN_is_negative(s_exp[i])) BN_mod_inverse(curr_factor, curr_factor, rped_pub->N, bn_ctx);
BN_mod_mul(res_rped_commitment, curr_factor, res_rped_commitment, rped_pub->N, bn_ctx);
}
BN_copy(rped_commitment, res_rped_commitment);
scalar_free(res_rped_commitment);
scalar_free(curr_factor);
BN_CTX_free(bn_ctx);
}
uint64_t ring_pedersen_public_bytelen (uint64_t rped_modulus_bytes){
return (2+RING_PEDERSEN_MULTIPLICITY)*rped_modulus_bytes;
}
void ring_pedersen_public_to_bytes (uint8_t **bytes, uint64_t *byte_len, const ring_pedersen_public_t *rped_pub, uint64_t rped_modulus_bytes, int move_to_end)
{
uint64_t needed_byte_len = ring_pedersen_public_bytelen(rped_modulus_bytes);
if ((!bytes) || (!*bytes) || (!rped_pub) || (needed_byte_len > *byte_len))
{
*byte_len = needed_byte_len;
return ;
}
uint8_t *set_bytes = *bytes;
scalar_to_bytes(&set_bytes, rped_modulus_bytes, rped_pub->N, 1);
scalar_to_bytes(&set_bytes, rped_modulus_bytes, rped_pub->t, 1);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
scalar_to_bytes(&set_bytes, rped_modulus_bytes, rped_pub->s[i], 1);
}
assert(set_bytes == *bytes + needed_byte_len);
*byte_len = needed_byte_len;
if (move_to_end) *bytes = set_bytes;
}
void ring_pedersen_public_from_bytes (ring_pedersen_public_t *rped_pub, uint8_t **bytes, uint64_t *byte_len, uint64_t rped_modulus_bytes, int move_to_end)
{
uint64_t needed_byte_len = (2+RING_PEDERSEN_MULTIPLICITY)*rped_modulus_bytes;
if ((!bytes) || (!*bytes) || (!rped_pub) || (needed_byte_len > *byte_len))
{
*byte_len = needed_byte_len;
return ;
}
uint8_t *read_bytes = *bytes;
scalar_from_bytes(rped_pub->N, &read_bytes, rped_modulus_bytes, 1);
scalar_from_bytes(rped_pub->t, &read_bytes, rped_modulus_bytes, 1);
for (uint64_t i = 0; i < RING_PEDERSEN_MULTIPLICITY; ++i) {
scalar_from_bytes(rped_pub->s[i], &read_bytes, rped_modulus_bytes, 1);
}
assert(read_bytes == *bytes + needed_byte_len);
*byte_len = needed_byte_len;
if (move_to_end) *bytes = read_bytes;
}