├── Contracts ├── Cert_Status_SC.sol ├── Cert_ZK_Proof.sol ├── VerifySignature.sol └── cert_isused_sc.sol ├── README.md └── ZoKrates ├── Cert_ZK_Proof.zok ├── Cert_ZK_Proof_SC.sol └── proof.json /Contracts/Cert_Status_SC.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.8.1; 2 | 3 | contract Cert_Status_SC{ 4 | 5 | mapping(address => bool) legal_IDP; 6 | 7 | mapping(bytes32 => bool) Cert_Status; 8 | 9 | mapping(bytes32 => bytes32) Ethereum_Hash_2_ZoKrates_Hash; 10 | 11 | //IDP_Address=0x6E819b34c53Dc81400D95ab87BFdBE3Ae80E2EA2 12 | 13 | //cert_Ethereum_Hash=0x3d903698a1337cd13e37a43671352b865e495d214a7df21aa00ed11b5cf9837c 14 | 15 | //Ethereum_signature=0x202b28672ae94adda39a495d138c8cb56807963d843785dae83a21dde8473cd4276bfb67f42b310c7579f9ad07f5768ae6a3325657ba7f8e20ddb9fe449478bf1b 16 | 17 | //cert_ZoKrates_Hash=0x31f1904100000000d1b06df10000000027e42fac00000000b227f24b00000000 18 | 19 | 20 | address Authority_Address=0x6E819b34c53Dc81400D95ab87BFdBE3Ae80E2EA2; 21 | 22 | function legal_IDP_register(address IDP_DID) public { 23 | 24 | require ( msg.sender==Authority_Address,"Your identity is illegal"); 25 | legal_IDP[IDP_DID]=true; 26 | 27 | 28 | } 29 | 30 | 31 | address account_address=0x6E819b34c53Dc81400D95ab87BFdBE3Ae80E2EA2; //IDP Ether Account Address 32 | 33 | function Set_Status_Active(bytes32 Sign_H) public { 34 | 35 | require ( legal_IDP[msg.sender]==true,"Your identity is illegal"); 36 | Cert_Status[Sign_H]=true; 37 | } 38 | 39 | function Set_Status_Revocation(bytes32 Sign_H) public { 40 | require (legal_IDP[msg.sender]==true,"Your identity is illegal"); 41 | Cert_Status[Sign_H]=false; 42 | } 43 | 44 | function read_Status(bytes32 Sign_H) public view returns(bool){ 45 | return Cert_Status[Sign_H]; 46 | } 47 | 48 | /////////////////////////////////////////////////////////////////// 49 | /////////////////////////////////////////////////////////////////// 50 | function Set_Ethereum_Hash_2_ZoKrates_Hash(bytes32 Ethereum_Hash,bytes32 ZoKrates_Hash) public { 51 | 52 | require (msg.sender == account_address,"Your identity is illegal"); 53 | Ethereum_Hash_2_ZoKrates_Hash[ZoKrates_Hash]=Ethereum_Hash; 54 | } 55 | 56 | function read_Ethereum_Hash_2_ZoKrates_Hash(bytes32 ZoKrates_Hash) public view returns(bytes32){ 57 | return Ethereum_Hash_2_ZoKrates_Hash[ZoKrates_Hash]; 58 | 59 | } 60 | 61 | } 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Contracts/Cert_ZK_Proof.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: LGPL-3.0-only 2 | // This file is LGPL3 Licensed 3 | pragma solidity ^0.8.0; 4 | 5 | /** 6 | * @title Elliptic curve operations on twist points for alt_bn128 7 | * @author Mustafa Al-Bassam (mus@musalbas.com) 8 | * @dev Homepage: https://github.com/musalbas/solidity-BN256G2 9 | */ 10 | 11 | library BN256G2 { 12 | uint256 internal constant FIELD_MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47; 13 | uint256 internal constant TWISTBX = 0x2b149d40ceb8aaae81be18991be06ac3b5b4c5e559dbefa33267e6dc24a138e5; 14 | uint256 internal constant TWISTBY = 0x9713b03af0fed4cd2cafadeed8fdf4a74fa084e52d1852e4a2bd0685c315d2; 15 | uint internal constant PTXX = 0; 16 | uint internal constant PTXY = 1; 17 | uint internal constant PTYX = 2; 18 | uint internal constant PTYY = 3; 19 | uint internal constant PTZX = 4; 20 | uint internal constant PTZY = 5; 21 | 22 | /** 23 | * @notice Add two twist points 24 | * @param pt1xx Coefficient 1 of x on point 1 25 | * @param pt1xy Coefficient 2 of x on point 1 26 | * @param pt1yx Coefficient 1 of y on point 1 27 | * @param pt1yy Coefficient 2 of y on point 1 28 | * @param pt2xx Coefficient 1 of x on point 2 29 | * @param pt2xy Coefficient 2 of x on point 2 30 | * @param pt2yx Coefficient 1 of y on point 2 31 | * @param pt2yy Coefficient 2 of y on point 2 32 | * @return (pt3xx, pt3xy, pt3yx, pt3yy) 33 | */ 34 | function ECTwistAdd( 35 | uint256 pt1xx, uint256 pt1xy, 36 | uint256 pt1yx, uint256 pt1yy, 37 | uint256 pt2xx, uint256 pt2xy, 38 | uint256 pt2yx, uint256 pt2yy 39 | ) public view returns ( 40 | uint256, uint256, 41 | uint256, uint256 42 | ) { 43 | if ( 44 | pt1xx == 0 && pt1xy == 0 && 45 | pt1yx == 0 && pt1yy == 0 46 | ) { 47 | if (!( 48 | pt2xx == 0 && pt2xy == 0 && 49 | pt2yx == 0 && pt2yy == 0 50 | )) { 51 | assert(_isOnCurve( 52 | pt2xx, pt2xy, 53 | pt2yx, pt2yy 54 | )); 55 | } 56 | return ( 57 | pt2xx, pt2xy, 58 | pt2yx, pt2yy 59 | ); 60 | } else if ( 61 | pt2xx == 0 && pt2xy == 0 && 62 | pt2yx == 0 && pt2yy == 0 63 | ) { 64 | assert(_isOnCurve( 65 | pt1xx, pt1xy, 66 | pt1yx, pt1yy 67 | )); 68 | return ( 69 | pt1xx, pt1xy, 70 | pt1yx, pt1yy 71 | ); 72 | } 73 | 74 | assert(_isOnCurve( 75 | pt1xx, pt1xy, 76 | pt1yx, pt1yy 77 | )); 78 | assert(_isOnCurve( 79 | pt2xx, pt2xy, 80 | pt2yx, pt2yy 81 | )); 82 | 83 | uint256[6] memory pt3 = _ECTwistAddJacobian( 84 | pt1xx, pt1xy, 85 | pt1yx, pt1yy, 86 | 1, 0, 87 | pt2xx, pt2xy, 88 | pt2yx, pt2yy, 89 | 1, 0 90 | ); 91 | 92 | return _fromJacobian( 93 | pt3[PTXX], pt3[PTXY], 94 | pt3[PTYX], pt3[PTYY], 95 | pt3[PTZX], pt3[PTZY] 96 | ); 97 | } 98 | 99 | /** 100 | * @notice Multiply a twist point by a scalar 101 | * @param s Scalar to multiply by 102 | * @param pt1xx Coefficient 1 of x 103 | * @param pt1xy Coefficient 2 of x 104 | * @param pt1yx Coefficient 1 of y 105 | * @param pt1yy Coefficient 2 of y 106 | * @return (pt2xx, pt2xy, pt2yx, pt2yy) 107 | */ 108 | function ECTwistMul( 109 | uint256 s, 110 | uint256 pt1xx, uint256 pt1xy, 111 | uint256 pt1yx, uint256 pt1yy 112 | ) public view returns ( 113 | uint256, uint256, 114 | uint256, uint256 115 | ) { 116 | uint256 pt1zx = 1; 117 | if ( 118 | pt1xx == 0 && pt1xy == 0 && 119 | pt1yx == 0 && pt1yy == 0 120 | ) { 121 | pt1xx = 1; 122 | pt1yx = 1; 123 | pt1zx = 0; 124 | } else { 125 | assert(_isOnCurve( 126 | pt1xx, pt1xy, 127 | pt1yx, pt1yy 128 | )); 129 | } 130 | 131 | uint256[6] memory pt2 = _ECTwistMulJacobian( 132 | s, 133 | pt1xx, pt1xy, 134 | pt1yx, pt1yy, 135 | pt1zx, 0 136 | ); 137 | 138 | return _fromJacobian( 139 | pt2[PTXX], pt2[PTXY], 140 | pt2[PTYX], pt2[PTYY], 141 | pt2[PTZX], pt2[PTZY] 142 | ); 143 | } 144 | 145 | /** 146 | * @notice Get the field modulus 147 | * @return The field modulus 148 | */ 149 | function GetFieldModulus() public pure returns (uint256) { 150 | return FIELD_MODULUS; 151 | } 152 | 153 | function submod(uint256 a, uint256 b, uint256 n) internal pure returns (uint256) { 154 | return addmod(a, n - b, n); 155 | } 156 | 157 | function _FQ2Mul( 158 | uint256 xx, uint256 xy, 159 | uint256 yx, uint256 yy 160 | ) internal pure returns (uint256, uint256) { 161 | return ( 162 | submod(mulmod(xx, yx, FIELD_MODULUS), mulmod(xy, yy, FIELD_MODULUS), FIELD_MODULUS), 163 | addmod(mulmod(xx, yy, FIELD_MODULUS), mulmod(xy, yx, FIELD_MODULUS), FIELD_MODULUS) 164 | ); 165 | } 166 | 167 | function _FQ2Muc( 168 | uint256 xx, uint256 xy, 169 | uint256 c 170 | ) internal pure returns (uint256, uint256) { 171 | return ( 172 | mulmod(xx, c, FIELD_MODULUS), 173 | mulmod(xy, c, FIELD_MODULUS) 174 | ); 175 | } 176 | 177 | function _FQ2Add( 178 | uint256 xx, uint256 xy, 179 | uint256 yx, uint256 yy 180 | ) internal pure returns (uint256, uint256) { 181 | return ( 182 | addmod(xx, yx, FIELD_MODULUS), 183 | addmod(xy, yy, FIELD_MODULUS) 184 | ); 185 | } 186 | 187 | function _FQ2Sub( 188 | uint256 xx, uint256 xy, 189 | uint256 yx, uint256 yy 190 | ) internal pure returns (uint256 rx, uint256 ry) { 191 | return ( 192 | submod(xx, yx, FIELD_MODULUS), 193 | submod(xy, yy, FIELD_MODULUS) 194 | ); 195 | } 196 | 197 | function _FQ2Div( 198 | uint256 xx, uint256 xy, 199 | uint256 yx, uint256 yy 200 | ) internal view returns (uint256, uint256) { 201 | (yx, yy) = _FQ2Inv(yx, yy); 202 | return _FQ2Mul(xx, xy, yx, yy); 203 | } 204 | 205 | function _FQ2Inv(uint256 x, uint256 y) internal view returns (uint256, uint256) { 206 | uint256 inv = _modInv(addmod(mulmod(y, y, FIELD_MODULUS), mulmod(x, x, FIELD_MODULUS), FIELD_MODULUS), FIELD_MODULUS); 207 | return ( 208 | mulmod(x, inv, FIELD_MODULUS), 209 | FIELD_MODULUS - mulmod(y, inv, FIELD_MODULUS) 210 | ); 211 | } 212 | 213 | function _isOnCurve( 214 | uint256 xx, uint256 xy, 215 | uint256 yx, uint256 yy 216 | ) internal pure returns (bool) { 217 | uint256 yyx; 218 | uint256 yyy; 219 | uint256 xxxx; 220 | uint256 xxxy; 221 | (yyx, yyy) = _FQ2Mul(yx, yy, yx, yy); 222 | (xxxx, xxxy) = _FQ2Mul(xx, xy, xx, xy); 223 | (xxxx, xxxy) = _FQ2Mul(xxxx, xxxy, xx, xy); 224 | (yyx, yyy) = _FQ2Sub(yyx, yyy, xxxx, xxxy); 225 | (yyx, yyy) = _FQ2Sub(yyx, yyy, TWISTBX, TWISTBY); 226 | return yyx == 0 && yyy == 0; 227 | } 228 | 229 | function _modInv(uint256 a, uint256 n) internal view returns (uint256 result) { 230 | bool success; 231 | assembly { 232 | let freemem := mload(0x40) 233 | mstore(freemem, 0x20) 234 | mstore(add(freemem,0x20), 0x20) 235 | mstore(add(freemem,0x40), 0x20) 236 | mstore(add(freemem,0x60), a) 237 | mstore(add(freemem,0x80), sub(n, 2)) 238 | mstore(add(freemem,0xA0), n) 239 | success := staticcall(sub(gas(), 2000), 5, freemem, 0xC0, freemem, 0x20) 240 | result := mload(freemem) 241 | } 242 | require(success); 243 | } 244 | 245 | function _fromJacobian( 246 | uint256 pt1xx, uint256 pt1xy, 247 | uint256 pt1yx, uint256 pt1yy, 248 | uint256 pt1zx, uint256 pt1zy 249 | ) internal view returns ( 250 | uint256 pt2xx, uint256 pt2xy, 251 | uint256 pt2yx, uint256 pt2yy 252 | ) { 253 | uint256 invzx; 254 | uint256 invzy; 255 | (invzx, invzy) = _FQ2Inv(pt1zx, pt1zy); 256 | (pt2xx, pt2xy) = _FQ2Mul(pt1xx, pt1xy, invzx, invzy); 257 | (pt2yx, pt2yy) = _FQ2Mul(pt1yx, pt1yy, invzx, invzy); 258 | } 259 | 260 | function _ECTwistAddJacobian( 261 | uint256 pt1xx, uint256 pt1xy, 262 | uint256 pt1yx, uint256 pt1yy, 263 | uint256 pt1zx, uint256 pt1zy, 264 | uint256 pt2xx, uint256 pt2xy, 265 | uint256 pt2yx, uint256 pt2yy, 266 | uint256 pt2zx, uint256 pt2zy) internal pure returns (uint256[6] memory pt3) { 267 | if (pt1zx == 0 && pt1zy == 0) { 268 | ( 269 | pt3[PTXX], pt3[PTXY], 270 | pt3[PTYX], pt3[PTYY], 271 | pt3[PTZX], pt3[PTZY] 272 | ) = ( 273 | pt2xx, pt2xy, 274 | pt2yx, pt2yy, 275 | pt2zx, pt2zy 276 | ); 277 | return pt3; 278 | } else if (pt2zx == 0 && pt2zy == 0) { 279 | ( 280 | pt3[PTXX], pt3[PTXY], 281 | pt3[PTYX], pt3[PTYY], 282 | pt3[PTZX], pt3[PTZY] 283 | ) = ( 284 | pt1xx, pt1xy, 285 | pt1yx, pt1yy, 286 | pt1zx, pt1zy 287 | ); 288 | return pt3; 289 | } 290 | 291 | (pt2yx, pt2yy) = _FQ2Mul(pt2yx, pt2yy, pt1zx, pt1zy); // U1 = y2 * z1 292 | (pt3[PTYX], pt3[PTYY]) = _FQ2Mul(pt1yx, pt1yy, pt2zx, pt2zy); // U2 = y1 * z2 293 | (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1zx, pt1zy); // V1 = x2 * z1 294 | (pt3[PTZX], pt3[PTZY]) = _FQ2Mul(pt1xx, pt1xy, pt2zx, pt2zy); // V2 = x1 * z2 295 | 296 | if (pt2xx == pt3[PTZX] && pt2xy == pt3[PTZY]) { 297 | if (pt2yx == pt3[PTYX] && pt2yy == pt3[PTYY]) { 298 | ( 299 | pt3[PTXX], pt3[PTXY], 300 | pt3[PTYX], pt3[PTYY], 301 | pt3[PTZX], pt3[PTZY] 302 | ) = _ECTwistDoubleJacobian(pt1xx, pt1xy, pt1yx, pt1yy, pt1zx, pt1zy); 303 | return pt3; 304 | } 305 | ( 306 | pt3[PTXX], pt3[PTXY], 307 | pt3[PTYX], pt3[PTYY], 308 | pt3[PTZX], pt3[PTZY] 309 | ) = ( 310 | 1, 0, 311 | 1, 0, 312 | 0, 0 313 | ); 314 | return pt3; 315 | } 316 | 317 | (pt2zx, pt2zy) = _FQ2Mul(pt1zx, pt1zy, pt2zx, pt2zy); // W = z1 * z2 318 | (pt1xx, pt1xy) = _FQ2Sub(pt2yx, pt2yy, pt3[PTYX], pt3[PTYY]); // U = U1 - U2 319 | (pt1yx, pt1yy) = _FQ2Sub(pt2xx, pt2xy, pt3[PTZX], pt3[PTZY]); // V = V1 - V2 320 | (pt1zx, pt1zy) = _FQ2Mul(pt1yx, pt1yy, pt1yx, pt1yy); // V_squared = V * V 321 | (pt2yx, pt2yy) = _FQ2Mul(pt1zx, pt1zy, pt3[PTZX], pt3[PTZY]); // V_squared_times_V2 = V_squared * V2 322 | (pt1zx, pt1zy) = _FQ2Mul(pt1zx, pt1zy, pt1yx, pt1yy); // V_cubed = V * V_squared 323 | (pt3[PTZX], pt3[PTZY]) = _FQ2Mul(pt1zx, pt1zy, pt2zx, pt2zy); // newz = V_cubed * W 324 | (pt2xx, pt2xy) = _FQ2Mul(pt1xx, pt1xy, pt1xx, pt1xy); // U * U 325 | (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt2zx, pt2zy); // U * U * W 326 | (pt2xx, pt2xy) = _FQ2Sub(pt2xx, pt2xy, pt1zx, pt1zy); // U * U * W - V_cubed 327 | (pt2zx, pt2zy) = _FQ2Muc(pt2yx, pt2yy, 2); // 2 * V_squared_times_V2 328 | (pt2xx, pt2xy) = _FQ2Sub(pt2xx, pt2xy, pt2zx, pt2zy); // A = U * U * W - V_cubed - 2 * V_squared_times_V2 329 | (pt3[PTXX], pt3[PTXY]) = _FQ2Mul(pt1yx, pt1yy, pt2xx, pt2xy); // newx = V * A 330 | (pt1yx, pt1yy) = _FQ2Sub(pt2yx, pt2yy, pt2xx, pt2xy); // V_squared_times_V2 - A 331 | (pt1yx, pt1yy) = _FQ2Mul(pt1xx, pt1xy, pt1yx, pt1yy); // U * (V_squared_times_V2 - A) 332 | (pt1xx, pt1xy) = _FQ2Mul(pt1zx, pt1zy, pt3[PTYX], pt3[PTYY]); // V_cubed * U2 333 | (pt3[PTYX], pt3[PTYY]) = _FQ2Sub(pt1yx, pt1yy, pt1xx, pt1xy); // newy = U * (V_squared_times_V2 - A) - V_cubed * U2 334 | } 335 | 336 | function _ECTwistDoubleJacobian( 337 | uint256 pt1xx, uint256 pt1xy, 338 | uint256 pt1yx, uint256 pt1yy, 339 | uint256 pt1zx, uint256 pt1zy 340 | ) internal pure returns ( 341 | uint256 pt2xx, uint256 pt2xy, 342 | uint256 pt2yx, uint256 pt2yy, 343 | uint256 pt2zx, uint256 pt2zy 344 | ) { 345 | (pt2xx, pt2xy) = _FQ2Muc(pt1xx, pt1xy, 3); // 3 * x 346 | (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1xx, pt1xy); // W = 3 * x * x 347 | (pt1zx, pt1zy) = _FQ2Mul(pt1yx, pt1yy, pt1zx, pt1zy); // S = y * z 348 | (pt2yx, pt2yy) = _FQ2Mul(pt1xx, pt1xy, pt1yx, pt1yy); // x * y 349 | (pt2yx, pt2yy) = _FQ2Mul(pt2yx, pt2yy, pt1zx, pt1zy); // B = x * y * S 350 | (pt1xx, pt1xy) = _FQ2Mul(pt2xx, pt2xy, pt2xx, pt2xy); // W * W 351 | (pt2zx, pt2zy) = _FQ2Muc(pt2yx, pt2yy, 8); // 8 * B 352 | (pt1xx, pt1xy) = _FQ2Sub(pt1xx, pt1xy, pt2zx, pt2zy); // H = W * W - 8 * B 353 | (pt2zx, pt2zy) = _FQ2Mul(pt1zx, pt1zy, pt1zx, pt1zy); // S_squared = S * S 354 | (pt2yx, pt2yy) = _FQ2Muc(pt2yx, pt2yy, 4); // 4 * B 355 | (pt2yx, pt2yy) = _FQ2Sub(pt2yx, pt2yy, pt1xx, pt1xy); // 4 * B - H 356 | (pt2yx, pt2yy) = _FQ2Mul(pt2yx, pt2yy, pt2xx, pt2xy); // W * (4 * B - H) 357 | (pt2xx, pt2xy) = _FQ2Muc(pt1yx, pt1yy, 8); // 8 * y 358 | (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1yx, pt1yy); // 8 * y * y 359 | (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt2zx, pt2zy); // 8 * y * y * S_squared 360 | (pt2yx, pt2yy) = _FQ2Sub(pt2yx, pt2yy, pt2xx, pt2xy); // newy = W * (4 * B - H) - 8 * y * y * S_squared 361 | (pt2xx, pt2xy) = _FQ2Muc(pt1xx, pt1xy, 2); // 2 * H 362 | (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1zx, pt1zy); // newx = 2 * H * S 363 | (pt2zx, pt2zy) = _FQ2Mul(pt1zx, pt1zy, pt2zx, pt2zy); // S * S_squared 364 | (pt2zx, pt2zy) = _FQ2Muc(pt2zx, pt2zy, 8); // newz = 8 * S * S_squared 365 | } 366 | 367 | function _ECTwistMulJacobian( 368 | uint256 d, 369 | uint256 pt1xx, uint256 pt1xy, 370 | uint256 pt1yx, uint256 pt1yy, 371 | uint256 pt1zx, uint256 pt1zy 372 | ) internal pure returns (uint256[6] memory pt2) { 373 | while (d != 0) { 374 | if ((d & 1) != 0) { 375 | pt2 = _ECTwistAddJacobian( 376 | pt2[PTXX], pt2[PTXY], 377 | pt2[PTYX], pt2[PTYY], 378 | pt2[PTZX], pt2[PTZY], 379 | pt1xx, pt1xy, 380 | pt1yx, pt1yy, 381 | pt1zx, pt1zy); 382 | } 383 | ( 384 | pt1xx, pt1xy, 385 | pt1yx, pt1yy, 386 | pt1zx, pt1zy 387 | ) = _ECTwistDoubleJacobian( 388 | pt1xx, pt1xy, 389 | pt1yx, pt1yy, 390 | pt1zx, pt1zy 391 | ); 392 | 393 | d = d / 2; 394 | } 395 | } 396 | } 397 | // This file is MIT Licensed. 398 | // 399 | // Copyright 2017 Christian Reitwiessner 400 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 401 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 402 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 403 | pragma solidity ^0.8.0; 404 | library Pairing { 405 | struct G1Point { 406 | uint X; 407 | uint Y; 408 | } 409 | // Encoding of field elements is: X[0] * z + X[1] 410 | struct G2Point { 411 | uint[2] X; 412 | uint[2] Y; 413 | } 414 | /// @return the generator of G1 415 | function P1() pure internal returns (G1Point memory) { 416 | return G1Point(1, 2); 417 | } 418 | /// @return the generator of G2 419 | function P2() pure internal returns (G2Point memory) { 420 | return G2Point( 421 | [10857046999023057135944570762232829481370756359578518086990519993285655852781, 422 | 11559732032986387107991004021392285783925812861821192530917403151452391805634], 423 | [8495653923123431417604973247489272438418190587263600148770280649306958101930, 424 | 4082367875863433681332203403145435568316851327593401208105741076214120093531] 425 | ); 426 | } 427 | /// @return the negation of p, i.e. p.addition(p.negate()) should be zero. 428 | function negate(G1Point memory p) pure internal returns (G1Point memory) { 429 | // The prime q in the base field F_q for G1 430 | uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; 431 | if (p.X == 0 && p.Y == 0) 432 | return G1Point(0, 0); 433 | return G1Point(p.X, q - (p.Y % q)); 434 | } 435 | /// @return r the sum of two points of G1 436 | function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) { 437 | uint[4] memory input; 438 | input[0] = p1.X; 439 | input[1] = p1.Y; 440 | input[2] = p2.X; 441 | input[3] = p2.Y; 442 | bool success; 443 | assembly { 444 | success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60) 445 | // Use "invalid" to make gas estimation work 446 | switch success case 0 { invalid() } 447 | } 448 | require(success); 449 | } 450 | /// @return r the sum of two points of G2 451 | function addition(G2Point memory p1, G2Point memory p2) internal view returns (G2Point memory r) { 452 | (r.X[0], r.X[1], r.Y[0], r.Y[1]) = BN256G2.ECTwistAdd(p1.X[0],p1.X[1],p1.Y[0],p1.Y[1],p2.X[0],p2.X[1],p2.Y[0],p2.Y[1]); 453 | } 454 | /// @return r the product of a point on G1 and a scalar, i.e. 455 | /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p. 456 | function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) { 457 | uint[3] memory input; 458 | input[0] = p.X; 459 | input[1] = p.Y; 460 | input[2] = s; 461 | bool success; 462 | assembly { 463 | success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60) 464 | // Use "invalid" to make gas estimation work 465 | switch success case 0 { invalid() } 466 | } 467 | require (success); 468 | } 469 | /// @return the result of computing the pairing check 470 | /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1 471 | /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should 472 | /// return true. 473 | function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) { 474 | require(p1.length == p2.length); 475 | uint elements = p1.length; 476 | uint inputSize = elements * 6; 477 | uint[] memory input = new uint[](inputSize); 478 | for (uint i = 0; i < elements; i++) 479 | { 480 | input[i * 6 + 0] = p1[i].X; 481 | input[i * 6 + 1] = p1[i].Y; 482 | input[i * 6 + 2] = p2[i].X[1]; 483 | input[i * 6 + 3] = p2[i].X[0]; 484 | input[i * 6 + 4] = p2[i].Y[1]; 485 | input[i * 6 + 5] = p2[i].Y[0]; 486 | } 487 | uint[1] memory out; 488 | bool success; 489 | assembly { 490 | success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20) 491 | // Use "invalid" to make gas estimation work 492 | switch success case 0 { invalid() } 493 | } 494 | require(success); 495 | return out[0] != 0; 496 | } 497 | /// Convenience method for a pairing check for two pairs. 498 | function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) { 499 | G1Point[] memory p1 = new G1Point[](2); 500 | G2Point[] memory p2 = new G2Point[](2); 501 | p1[0] = a1; 502 | p1[1] = b1; 503 | p2[0] = a2; 504 | p2[1] = b2; 505 | return pairing(p1, p2); 506 | } 507 | /// Convenience method for a pairing check for three pairs. 508 | function pairingProd3( 509 | G1Point memory a1, G2Point memory a2, 510 | G1Point memory b1, G2Point memory b2, 511 | G1Point memory c1, G2Point memory c2 512 | ) internal view returns (bool) { 513 | G1Point[] memory p1 = new G1Point[](3); 514 | G2Point[] memory p2 = new G2Point[](3); 515 | p1[0] = a1; 516 | p1[1] = b1; 517 | p1[2] = c1; 518 | p2[0] = a2; 519 | p2[1] = b2; 520 | p2[2] = c2; 521 | return pairing(p1, p2); 522 | } 523 | /// Convenience method for a pairing check for four pairs. 524 | function pairingProd4( 525 | G1Point memory a1, G2Point memory a2, 526 | G1Point memory b1, G2Point memory b2, 527 | G1Point memory c1, G2Point memory c2, 528 | G1Point memory d1, G2Point memory d2 529 | ) internal view returns (bool) { 530 | G1Point[] memory p1 = new G1Point[](4); 531 | G2Point[] memory p2 = new G2Point[](4); 532 | p1[0] = a1; 533 | p1[1] = b1; 534 | p1[2] = c1; 535 | p1[3] = d1; 536 | p2[0] = a2; 537 | p2[1] = b2; 538 | p2[2] = c2; 539 | p2[3] = d2; 540 | return pairing(p1, p2); 541 | } 542 | } 543 | 544 | 545 | import "cert_isused_sc.sol"; 546 | 547 | 548 | contract Cert_ZK_Proof_SC { 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | cert_isused_sc cert_isused_sc_ok; 557 | 558 | 559 | 560 | function set_cert_isused_sc_addr(address cert_isused_sc_addr)public{ 561 | cert_isused_sc_ok=cert_isused_sc(cert_isused_sc_addr); 562 | } 563 | 564 | function read_cert_isused_sc(bytes32 Hash_P) public returns(bool){ 565 | bool cert_isused_sc_status= cert_isused_sc_ok.read_Status(Hash_P); 566 | return cert_isused_sc_status; 567 | } 568 | 569 | 570 | function set_cert_isused_sc_status(bytes32 Hash_P) public{ 571 | cert_isused_sc_ok.set_cert_isused_sc_Status(Hash_P); 572 | } 573 | 574 | 575 | 576 | function cert_vr( 577 | uint[2] memory a, 578 | uint[2][2] memory b, 579 | uint[2] memory c, uint[7] memory input, bytes32 Hash_P 580 | ) public returns (bool r) { 581 | 582 | Proof memory proof; 583 | proof.a = Pairing.G1Point(a[0], a[1]); 584 | proof.b = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); 585 | proof.c = Pairing.G1Point(c[0], c[1]); 586 | 587 | bool cert_isUsed=read_cert_isused_sc(Hash_P); 588 | require(cert_isUsed!=true,"Cert has been used"); 589 | 590 | bool hold_prdid=verifyTx(proof,input); 591 | require(hold_prdid==true,"user identity is valid"); 592 | 593 | set_cert_isused_sc_status(Hash_P); 594 | 595 | return true; 596 | 597 | 598 | } 599 | 600 | 601 | using Pairing for *; 602 | struct VerifyingKey { 603 | Pairing.G1Point alpha; 604 | Pairing.G2Point beta; 605 | Pairing.G2Point gamma; 606 | Pairing.G2Point delta; 607 | Pairing.G1Point[] gamma_abc; 608 | } 609 | struct Proof { 610 | Pairing.G1Point a; 611 | Pairing.G2Point b; 612 | Pairing.G1Point c; 613 | } 614 | function verifyingKey() pure internal returns (VerifyingKey memory vk) { 615 | vk.alpha = Pairing.G1Point(uint256(0x1c2967c21d410dd1f4163f0bc80e3db9c83553590f8d6e350de26225434d5da3), uint256(0x2305d418002cbb3b8d2ebed972d505041cb3aafe3cfdffbe0518ad506af197b1)); 616 | vk.beta = Pairing.G2Point([uint256(0x0844b98e5db244fb7464eabfed9a4985647f6d6ddf8236f70e0d722e88990cb7), uint256(0x26346d136edf4953bbb7391bb19e1b502273460259e10c55a2da3149f7e7c582)], [uint256(0x0e921cb0f2fc48b3249b08993e17cb338964cf2cda203e8467bfb7920356f2ea), uint256(0x232c990d23514c54509d1484796b7bf6c83274a62ba6166be4d754c5761d821d)]); 617 | vk.gamma = Pairing.G2Point([uint256(0x26bd8ea01bf3714a815eb543d54175de88b5dd17b14722358c089cef5d82e929), uint256(0x023fa4ce3293db5817e3913dd0b2f75ac9295e6194954740503ade5da4d4a23a)], [uint256(0x0caf708febc000edd407ac3f948f2529dec3085b0cff78ab954f9c20be98fa1d), uint256(0x1fed1e83790e55f9f4de8ccc4ace68d191ce2c750622fd7189de30f30d2ca660)]); 618 | vk.delta = Pairing.G2Point([uint256(0x07c2e2371bfc4fac506ac4134a18e859cf6346bd5f6b70552c09ad2c0f540c6d), uint256(0x116df72768971f642f10fd868a0fe1a4732a2bc85a6d5000a657966444efae6d)], [uint256(0x038070c6e7572887b5006a8dfdc51384e745b16fb546b3b4ee684e0f01acb9f2), uint256(0x287a95ce3a4196703674bb23fafdb8b2c3da111dbb12607eed74ef1d1ce9c291)]); 619 | vk.gamma_abc = new Pairing.G1Point[](8); 620 | vk.gamma_abc[0] = Pairing.G1Point(uint256(0x050561a076d184f85efcadaa31afd8d65243d1909dbe6fcf54ae669c84a45e35), uint256(0x1009e7313aaf24d0dd9d51d0294d91f1cbfcec80509afbeb1daea16e85a5153b)); 621 | vk.gamma_abc[1] = Pairing.G1Point(uint256(0x0d605fb86f7cd2b4066367126d044839c4b04a5c7ae0bae73d1656cc06df9a8a), uint256(0x08adbdd82969a69bc6f7c0b2828824ededda0bb702913d48bfded5092656e15c)); 622 | vk.gamma_abc[2] = Pairing.G1Point(uint256(0x260b825875166fcb465770a9c94679f5b7b199578bb90b8243383de214ae9656), uint256(0x15ad6d5d6fab1ed8fedca53aa040f3005482339bcb0001a6837b80e964607fc2)); 623 | vk.gamma_abc[3] = Pairing.G1Point(uint256(0x08d810982b89a18ad93ff01a01e385e08e98f1889a0959cf456ee8279e0c268d), uint256(0x1458c4907e5b3eee9b4b35366db667c6782c1fa8429f1b702c55f333c27c5922)); 624 | vk.gamma_abc[4] = Pairing.G1Point(uint256(0x2b658d3aab6ae7bc45d4809a4e8ba2b34e42cc179d4a27b5621861413996bea9), uint256(0x2196d89116314f2ed75316c31211f0d4eb07bba8ca35d9e365ef1224953d2eb5)); 625 | vk.gamma_abc[5] = Pairing.G1Point(uint256(0x2d5adddef7b0ddbce8e540670e91d3ed29853f8ccc94f464bf97ac98c34f3800), uint256(0x271290acbfbc42871700b161d9cbdf5ee580fcb56be9afe7a91cec8f42ee4875)); 626 | vk.gamma_abc[6] = Pairing.G1Point(uint256(0x04f3dcdf4e76496a4c6d94c527c1926cea37eab22d96a9c7a58ac5531713bcc1), uint256(0x16214d48222e38ab11089e2be608a41061d11dffe7ba60e727c5026244c9b4b9)); 627 | vk.gamma_abc[7] = Pairing.G1Point(uint256(0x037d19e09f975b79cc1ab4c0e8f5fb3e6988634a0bb08f7c9abf05d1c1b009e1), uint256(0x0af315b9704ebda00c152a1b2609dde932998a332a6e6c5c1b70ac52ce4dec05)); 628 | } 629 | function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { 630 | uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; 631 | VerifyingKey memory vk = verifyingKey(); 632 | require(input.length + 1 == vk.gamma_abc.length); 633 | // Compute the linear combination vk_x 634 | Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); 635 | for (uint i = 0; i < input.length; i++) { 636 | require(input[i] < snark_scalar_field); 637 | vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.gamma_abc[i + 1], input[i])); 638 | } 639 | vk_x = Pairing.addition(vk_x, vk.gamma_abc[0]); 640 | if(!Pairing.pairingProd4( 641 | proof.a, proof.b, 642 | Pairing.negate(vk_x), vk.gamma, 643 | Pairing.negate(proof.c), vk.delta, 644 | Pairing.negate(vk.alpha), vk.beta)) return 1; 645 | return 0; 646 | } 647 | function verifyTx( 648 | Proof memory proof, uint[7] memory input 649 | ) public view returns (bool r) { 650 | uint[] memory inputValues = new uint[](7); 651 | 652 | for(uint i = 0; i < input.length; i++){ 653 | inputValues[i] = input[i]; 654 | } 655 | if (verify(inputValues, proof) == 0) { 656 | return true; 657 | } else { 658 | return false; 659 | } 660 | } 661 | } 662 | -------------------------------------------------------------------------------- /Contracts/VerifySignature.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.10; 2 | 3 | contract VerifySignature{ 4 | 5 | 6 | function verifyByHashAndSig(bytes32 Sign_H, bytes IDP_signature) public view returns (address){ 7 | bytes memory signedString = IDP_signature; 8 | uint8 v; 9 | 10 | bytes32 r = bytesToBytes32(slice(signedString, 0, 32)); 11 | bytes32 s = bytesToBytes32(slice(signedString, 32, 32)); 12 | byte v1 = slice(signedString, 64, 1)[0]; 13 | // if(uint8(v1)<28){ 14 | v = uint8(v1);//} 15 | // else{ 16 | // v = uint8(v1); 17 | // } 18 | return ecrecoverDirect(Sign_H, r, s, v); 19 | } 20 | 21 | 22 | function slice(bytes memory data, uint start, uint len) returns (bytes){ 23 | bytes memory b = new bytes(len); 24 | 25 | for(uint i = 0; i < len; i++){ 26 | b[i] = data[i + start]; 27 | } 28 | return b; 29 | } 30 | 31 | 32 | function bytesToBytes32(bytes memory source) returns (bytes32 result) { 33 | assembly { 34 | result := mload(add(source, 32)) 35 | } 36 | } 37 | 38 | 39 | function ecrecoverDirect(bytes32 hash, bytes32 r, bytes32 s, uint8 v) returns (address addr){ 40 | /* prefix might be needed for geth only 41 | * https://github.com/ethereum/go-ethereum/issues/3731 42 | */ 43 | bytes memory prefix = "\x19Ethereum Signed Message:\n32"; 44 | hash = sha3(prefix, hash); 45 | 46 | addr = ecrecover(hash, v, r, s); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Contracts/cert_isused_sc.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.8.1; 2 | 3 | 4 | contract cert_isused_sc{ 5 | 6 | mapping(bytes32 => bool) cert_Status; 7 | 8 | // address cert_vr_sc_address=0xCF64b74efbfC345eA574A35a87f4d1da4618AC43; // Address of Cert_ZK_Proof.sol 9 | 10 | address Authority_Address=0x6E819b34c53Dc81400D95ab87BFdBE3Ae80E2EA2; 11 | 12 | mapping(address => bool) legal_SC; 13 | 14 | function legal_IDP_register(address legal_SC_Addr) public { 15 | require ( msg.sender==Authority_Address,"Your identity is illegal"); 16 | legal_SC[legal_SC_Addr]=true; 17 | 18 | } 19 | 20 | 21 | function set_cert_isused_sc_Status(bytes32 Hash_P) public { 22 | 23 | require (legal_SC[msg.sender]==true,"Your identity is illegal"); 24 | cert_Status[Hash_P]=true; 25 | 26 | } 27 | 28 | 29 | function read_Status(bytes32 Hash_P) public returns(bool){ 30 | 31 | require (legal_SC[msg.sender]==true,"Your identity is illegal"); 32 | return cert_Status[Hash_P]; 33 | 34 | } 35 | 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zero-knowledge-proof-VC 2 | ## Note 3 | This repository contains some corresponding smart contracts and programs of zero knowledge proof of ZoKrates, and they can be used for the proposed digital identity management frame based blockchain's smart contracts of zero knowledge proof. 4 | 5 | ***●*** The document, Contracts, contain four smart contracts: 6 | (1) Cert_Status_SC.sol: The smart contract of status managment of verifiable certificates. 7 | (2) cert_isused_sc.sol: The smart contract of status managment of zk_proof. 8 | (3) Cert_ZK_Proof.sol: The smart contract of verifying whether zk_proof is valid. 9 | (4) VerifySignature.sol: The smart contract of verifying whether the digital signature of IDP is valid. 10 | 11 | ***●*** The document, ZoKrates, contain some files related with zero knowlwdge proof: 12 | (1) Cert_ZK_Proof.zok: The zero knowledge proof program of verifiable certificate. 13 | (2) Cert_ZK_Proof.sol: The original smart contract of verifying whether zk_proof is valid and it is generated by Cert_ZK_Proof.zok. 14 | (3) proof.json: The zero knowledge proof, zk_proof, and its format is JSON. 15 | 16 | On the other hand, the development manual of ZoKrates can be found in https://zokrates.github.io/gettingstarted.html#one-line-install. 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /ZoKrates/Cert_ZK_Proof.zok: -------------------------------------------------------------------------------- 1 | import "ecc/babyjubjubParams" as context 2 | from "ecc/babyjubjubParams" import BabyJubJubParams 3 | import "ecc/proofOfOwnership" as proofOfOwnership 4 | import "ecc/edwardsScalarMult" as multiply 5 | 6 | import "utils/pack/bool/unpack" as unpack 7 | import "utils/casts/u64_from_bits" as u64_from_bits 8 | import "hashes/keccak/256bit" as keccak256 9 | 10 | 11 | 12 | def testOwnershipTrue(private field sk, public field[2] pk) -> bool: 13 | BabyJubJubParams context = context() 14 | field[2] G = [context.Gu, context.Gv] 15 | 16 | bool out = proofOfOwnership(pk, sk, context) 17 | 18 | assert(out) 19 | return true 20 | 21 | 22 | def bool_to_u64_arr(bool[N] bits) -> u64[P]: 23 | u64[P] res = [0; P] 24 | for u32 i in 0..P do 25 | res[i] = u64_from_bits(bits[64 * i..64 * (i + 1)]) 26 | endfor 27 | return res 28 | 29 | 30 | 31 | def main(private field user_age,private field user_sk, private field[2] user_pk,public field[2] user_DID,public field[2] IDP_DID,public field[2] cert_Hash)->field: 32 | 33 | assert(testOwnershipTrue(user_sk,user_pk)) 34 | 35 | bool[254] pk0_t=unpack::<254>(user_pk[0]) 36 | bool[256] pk0=[false,false,...pk0_t] 37 | u64[4] pk0_u64 = bool_to_u64_arr::<256,4>(pk0) 38 | 39 | 40 | bool[254] pk1_t=unpack::<254>(user_pk[1]) 41 | bool[256] pk1=[false,false,...pk1_t] 42 | u64[4] pk1_u64 = bool_to_u64_arr::<256,4>(pk1) 43 | 44 | 45 | u64[8] PK_sigma=[pk0_u64[0],pk0_u64[1],pk0_u64[2],pk0_u64[3],pk1_u64[0],pk1_u64[1],pk1_u64[2],pk1_u64[3]] // pk 46 | 47 | 48 | u64[4] user_DID_1P=keccak256::<8>(PK_sigma) //PrDID'=keccak256(PK||sigma) 49 | 50 | 51 | bool[128] user_DID_t=unpack::<128>(user_DID[0]) 52 | bool[128] user_DID_t1=unpack::<128>(user_DID[1]) 53 | bool[256] user_DID_t2=[...user_DID_t,...user_DID_t1] 54 | u64[4] user_DID_u64 = bool_to_u64_arr::<256,4>(user_DID_t2) 55 | 56 | assert(user_DID_1P==user_DID_u64) 57 | 58 | 59 | 60 | 61 | bool[128] IDP_DID_t=unpack::<128>(IDP_DID[0]) 62 | bool[128] IDP_DID_t1=unpack::<128>(IDP_DID[1]) 63 | bool[256] IDP_DID_t2=[...IDP_DID_t,...IDP_DID_t1] 64 | u64[4] IDP_DID_u64 = bool_to_u64_arr::<256,4>(IDP_DID_t2) 65 | 66 | 67 | bool[64] user_age_t=unpack::<64>(user_age) 68 | u64[1] user_age_u64 = bool_to_u64_arr::<64,1>(user_age_t) 69 | 70 | 71 | u64[9] cert_Hash_temp_u64=[user_age_u64[0],user_DID_u64[0],user_DID_u64[1],user_DID_u64[2],user_DID_u64[3],IDP_DID_u64[0],IDP_DID_u64[1],IDP_DID_u64[2],IDP_DID_u64[3]] 72 | 73 | 74 | 75 | u64[4] cert_Hash_temp=keccak256::<9>(cert_Hash_temp_u64) 76 | 77 | 78 | 79 | bool[128] cert_Hash_t=unpack::<128>(cert_Hash[0]) 80 | bool[128] cert_Hash_t1=unpack::<128>(cert_Hash[1]) 81 | bool[256] cert_Hash_t2=[...cert_Hash_t,...cert_Hash_t1] 82 | u64[4] cert_Hash_u64 = bool_to_u64_arr::<256,4>(cert_Hash_t2) 83 | 84 | assert(cert_Hash_temp==cert_Hash_u64) 85 | 86 | 87 | 88 | field valid_age_max=60 89 | field valid_age_min=18 90 | 91 | 92 | assert(user_age>valid_age_min && user_age