기본 콘텐츠로 건너뛰기

Circom SHA256 해시 예제

Circom SHA256 해시 예제

아래 문서의 예제를 약간 변형해서 SHA256 해시 검증을 실습하였습니다.

회로 생성

  1. 프로젝트 디렉토리에서 회로 파일 program.circom 작성

    pragma circom 2.0.0;
    include "./circomlib/circuits/sha256/sha256.circom";
    
    template program() {
        signal input in[8];
        signal output out[256];
    
        component SHA = Sha256(8);
        SHA.in <== in;
        out <== SHA.out;
    }
    
    component main = program();
    
  2. 프로젝트 디렉토리에서 Git 저장소 circomlib 복제

    git clone https://github.com/iden3/circomlib.git
    
  3. 회로 컴파일

    circom program.circom --r1cs --wasm --sym --c
    

신뢰 설정 - Powers of Tau

  1. 프로젝트 디렉토리에서 ‘powers of tau’ ceremony 시작

    snarkjs powersoftau new bn128 15 pot15_0000.ptau -v
    
  2. 첫 번째 기여

    snarkjs powersoftau contribute pot15_0000.ptau pot15_0001.ptau --name="First contribution" -v
    

신뢰 설정 - Phase 2

  1. program_js 디렉토리에서 Phase 2 준비 (시간이 많이 걸림)

    snarkjs powersoftau prepare phase2 ..\pot15_0001.ptau pot15_final.ptau -v
    
  2. .r1cs 파일과 연관된 .zkey 파일 생성

    snarkjs groth16 setup ..\program.r1cs pot15_final.ptau program_0000.zkey
    
  3. 첫 번째 기여

    snarkjs zkey contribute program_0000.zkey program_0001.zkey --name="1st Contributor Name" -v
    
  4. 검증키 내보내기

    snarkjs zkey export verificationkey program_0001.zkey verification_key.json
    

증명 성공 예시

  1. program_js 디렉토리에 input.json 파일 생성

    정수 1의 big-endian 비트 표현

    {"in":["0","0","0","0","0","0","0","1"]}
    
  2. program_js 디렉토리에서 witness 생성

    node generate_witness.js program.wasm input.json witness.wtns
    
  3. 증명 생성

    snarkjs groth16 prove program_0001.zkey witness.wtns proof.json public.json
    

    public.json 파일의 내용

    [
     "0", "1", "0", "0", "1", "0", "1", "1",
     "1", "1", "1", "1", "0", "1", "0", "1",
     "0", "0", "0", "1", "0", "0", "1", "0",
     "0", "0", "1", "0", "1", "1", "1", "1",
     "0", "0", "1", "1", "0", "1", "0", "0",
     "0", "1", "0", "0", "0", "1", "0", "1",
     ...
    ]
    
  4. 증명 검증

    snarkjs groth16 verify verification_key.json public.json proof.json
    

Python 코드로 입출력 검증

스크립트 실행

input.json 파일로부터 big-endian 비트 배열을 읽어서 바이트 배열로 변환하고 SHA256 해시 생성하여 output.txt에 저장

python calc_sha256_python.py --input-filepath input.json --output-dir .

output.txt

Input Bytes:
   1

Output Bits (Little Endian):
1 1 0 1  0 0 1 0      1 0 1 0  1 1 1 1
0 1 0 0  1 0 0 0      1 1 1 1  0 1 0 0
0 0 1 0  1 1 0 0      1 0 1 0  0 0 1 0
0 0 1 0  1 0 1 0      1 0 1 0  0 0 1 1
1 1 0 1  1 1 0 0      0 1 1 1  1 0 1 1
0 1 1 1  0 1 0 0      1 1 0 1  1 1 0 1
0 0 1 1  0 0 0 1      0 1 0 0  1 0 1 1
1 1 1 0  1 1 0 1      1 1 0 0  0 1 1 1
1 0 0 0  1 0 1 1      0 0 0 0  0 1 1 0
0 1 0 1  0 0 0 0      0 1 1 0  1 0 1 1
1 0 0 0  1 1 0 0      1 1 0 0  0 0 1 1
1 0 1 0  0 0 0 1      1 0 1 0  0 1 0 1
1 1 1 0  1 0 1 1      0 0 1 1  0 0 1 1
0 1 0 0  0 1 1 1      0 0 1 1  1 1 0 0
1 1 1 0  1 1 1 0      1 0 1 0  0 0 0 1
1 0 1 0  0 0 1 0      0 1 0 1  1 0 0 1

Output Bits (Big Endian):
0 1 0 0  1 0 1 1      1 1 1 1  0 1 0 1
0 0 0 1  0 0 1 0      0 0 1 0  1 1 1 1
0 0 1 1  0 1 0 0      0 1 0 0  0 1 0 1
0 1 0 1  0 1 0 0      1 1 0 0  0 1 0 1
0 0 1 1  1 0 1 1      1 1 0 1  1 1 1 0
0 0 1 0  1 1 1 0      1 0 1 1  1 0 1 1
1 0 0 0  1 1 0 0      1 1 0 1  0 0 1 0
1 0 1 1  0 1 1 1      1 1 1 0  0 0 1 1
1 1 0 1  0 0 0 1      0 1 1 0  0 0 0 0
0 0 0 0  1 0 1 0      1 1 0 1  0 1 1 0
0 0 1 1  0 0 0 1      1 1 0 0  0 0 1 1
1 0 0 0  0 1 0 1      1 0 1 0  0 1 0 1
1 1 0 1  0 1 1 1      1 1 0 0  1 1 0 0
1 1 1 0  0 0 1 0      0 0 1 1  1 1 0 0
0 1 1 1  0 1 1 1      1 0 0 0  0 1 0 1
0 1 0 0  0 1 0 1      1 0 0 1  1 0 1 0

Output Bytes:
  75  245   18   47   52   69   84  197   59  222   46  187  140  210  183  227
 209   96   10  214   49  195  133  165  215  204  226   60  119  133   69  154

SHA-256 Hash: 4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a

참고 자료

  1. Zero-Knowledge Proof of SHA256 Hash using zkSNARK, Binod, Medium
  2. Circom SHA256, Jonas Pauli, Medium
  3. circom-sha256, jonas089, GitHub

Written with StackEdit.

댓글

이 블로그의 인기 게시물

Intel MKL 예제를 Microsoft Visual C++로 빌드하기

Intel MKL 예제를 Microsoft Visual C++로 빌드하기 인텔 프로세서 시스템에서 아래의 영역에 해당하는 수학 계산을 빠르게 수행하고자 한다면 Intel MKL 라이브러리를 사용할 수 있습니다. Linear Algebra Fast Fourier Transforms (FFT) Vector Statistics & Data Fitting Vector Math & Miscellaneous Solvers 이 문서는 Intel MKL 이 제공하는 예제 파일을 Microsoft Visual C++ 로 컴파일하고 링크하여 실행 파일을 만드는 과정을 소개합니다. 빌드 환경 다음은 이 문서를 작성하는 과정에서 Intel MKL 예제를 빌드하기 위하여 사용한 환경입니다. 시스템 운영체제: Windows 10 (64비트) 프로세서: Intel Core i7 설치 제품 IDE: Microsoft Visual Studio Community 2019 (version 16) 라이브러리: Intel Math Kernel Library 2019 Update 5 환경 변수 명령 프롬프트 창을 엽니다. 아래 스크립트를 실행하여 환경 변수 INCLUDE , LIB , 그리고 PATH 를 설정합니다. @echo off set CPRO_PATH=C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows set MKLROOT=%CPRO_PATH%\mkl set REDIST=%CPRO_PATH%\redist set INCLUDE=%MKLROOT%\include;%INCLUDE% set LIB=%MKLROOT%\lib\intel64;%LIB% set PATH=%REDIST%\intel64\mkl;%PATH% REM for OpenMP intel thread set LIB=%CPRO_PATH%\compiler\lib...

Llama 3.2로 문장 생성 및 챗팅 완성 실습

Llama 3.2로 문장 생성 및 챗팅 완성 실습 Running Meta Llama on Linux 문서의 내용을 참고하여 Llama 3.2 1B 모델로 다음 두 가지 기능을 실습합니다. 문장 완성 챗팅 완성 실습 환경 Ubuntu 20.04.6 LTS Python 3.12.7 Llama3.2-1B, Llama3.2-1B-Instruct rustc 1.83.0 NVIDIA RTX 4090 24GB 프로그램 준비 실습에서 사용할 wget , md5sum 설치 sudo apt-get install wget sudo apt-get install md5sum NVIDIA GPU 설치 여부 확인 nvidia-smi 실습 디렉토리 만들기 mkdir llama3-demo cd llama3-demo git clone https://github.com/meta-llama/llama3.git Python 3.10 이상의 버전으로 가상환경 만들고 활성화 python -m venv llama-venv . llama-venv/bin/activate Rust 컴파일러 설치 How To Install Rust on Ubuntu 20.04 문서를 참고하여 Rust 컴파일러를 설치합니다. curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh 위 명령을 실행하면 아래와 같이 세 가지 선택 옵션이 나타나는데 그냥 엔터를 쳐서 1번 옵션으로 진행합니다. ... 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation 아래 명령을 실행하여 현재 쉘에 반영하고 설치된 컴파일러 버전을 확인합니다. source $HOME/.cargo/env rustc --version 의존 라이브러리 설치 pip install ...