forked from guanzhi/GmSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsm2_commit.c
More file actions
172 lines (140 loc) · 3.36 KB
/
Copy pathsm2_commit.c
File metadata and controls
172 lines (140 loc) · 3.36 KB
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
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#include <gmssl/sm2.h>
#include <gmssl/sm3.h>
#include <gmssl/mem.h>
#include <gmssl/asn1.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include <gmssl/sm2_commit.h>
#define SM2_COMMIT_SEED "GmSSL SM2 Pederson Commitment Generator H"
// C = rG + xH
int sm2_commit_generate(const uint8_t x[32], uint8_t r[32], uint8_t commit[65], size_t *commitlen)
{
SM2_POINT H;
SM2_POINT C;
SM2_BN r_;
if (sm2_point_from_hash(&H, (uint8_t *)SM2_COMMIT_SEED, sizeof(SM2_COMMIT_SEED)-1) != 1) {
error_print();
return -1;
}
do {
sm2_fn_rand(r_);
} while (sm2_bn_is_zero(r_));
sm2_bn_to_bytes(r_, r);
gmssl_secure_clear(r_, sizeof(r_));
// C = xH + rG
sm2_point_mul_sum(&C, x, &H, r);
sm2_point_to_compressed_octets(&C, commit);
*commitlen = 33;
return 1;
}
int sm2_commit_open(const uint8_t x[32], const uint8_t r[32], const uint8_t *commit, size_t commitlen)
{
SM2_POINT H;
SM2_POINT C;
SM2_POINT C_;
if (sm2_point_from_octets(&C, commit, commitlen) != 1) {
error_print();
return -1;
}
if (sm2_point_from_hash(&H, (uint8_t *)SM2_COMMIT_SEED, sizeof(SM2_COMMIT_SEED)-1) != 1) {
error_print();
return -1;
}
// C' = xH + rG
if (sm2_point_mul_sum(&C_, x, &H, r) != 1) {
error_print();
return -1;
}
if (memcmp(&C, &C_, sizeof(SM2_POINT)) != 0) {
error_print();
return 0;
}
return 1;
}
// C = r*G + x1*H1 + x2*H2 + ...
int sm2_commit_vector_generate(const sm2_bn_t *x, size_t count, uint8_t r[32], uint8_t commit[65], size_t *commitlen)
{
SM2_POINT H;
SM2_POINT C;
SM2_Fn r_;
size_t i;
if (count < 1) {
error_print();
return -1;
}
if (sm2_point_from_hash(&H, (uint8_t *)SM2_COMMIT_SEED, sizeof(SM2_COMMIT_SEED)-1) != 1) {
error_print();
return -1;
}
do {
sm2_fn_rand(r_);
} while (sm2_bn_is_zero(r_));
sm2_bn_to_bytes(r_, r);
gmssl_secure_clear(r_, sizeof(r_));
if (sm2_point_mul_sum(&C, x[0], &H, r) != 1) {
error_print();
return -1;
}
for (i = 1; i < count; i++) {
SM2_POINT xH;
if (sm2_point_from_hash(&H, (uint8_t *)&H, sizeof(H)) != 1
|| sm2_point_mul(&xH, x[i], &H) != 1
|| sm2_point_add(&C, &C, &xH) != 1) {
error_print();
return -1;
}
}
sm2_point_to_compressed_octets(&C, commit);
*commitlen = 33;
return 1;
}
int sm2_commit_vector_open(const sm2_bn_t *x, size_t count, const uint8_t r[32], const uint8_t *commit, size_t commitlen)
{
SM2_POINT H;
SM2_POINT C;
SM2_POINT C_;
size_t i;
if (count < 1) {
error_print();
return -1;
}
if (sm2_point_from_octets(&C, commit, commitlen) != 1) {
error_print();
return -1;
}
if (sm2_point_from_hash(&H, (uint8_t *)SM2_COMMIT_SEED, sizeof(SM2_COMMIT_SEED)-1) != 1) {
error_print();
return -1;
}
if (sm2_point_mul_sum(&C_, x[0], &H, r) != 1) {
error_print();
return -1;
}
for (i = 1; i< count; i++) {
SM2_POINT xH;
if (sm2_point_from_hash(&H, (uint8_t *)&H, sizeof(H)) != 1
|| sm2_point_mul(&xH, x[i], &H) != 1
|| sm2_point_add(&C_, &C_, &xH) != 1) {
error_print();
return -1;
}
}
if (memcmp(&C, &C_, sizeof(SM2_POINT)) != 0) {
error_print();
return -1;
}
return 1;
}