Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a851d08a46 | ||
|
|
19bbada388 | ||
|
|
bdfb0fb928 | ||
|
|
9941aac2f2 | ||
|
|
3c2b566e68 | ||
|
|
15eadfe50c | ||
|
|
a1d4d1dca7 | ||
|
|
d6e6a3b3f4 | ||
|
|
e3eb834322 | ||
|
|
01a5530213 | ||
|
|
50a725c174 | ||
|
|
c4ce4cd946 | ||
|
|
5bf52cda57 | ||
|
|
95abdd06d5 | ||
|
|
9f69fab7c6 | ||
|
|
109cdf4056 | ||
|
|
79d30349b4 | ||
|
|
e3cd0e5aa7 | ||
|
|
86e970b888 | ||
|
|
29e162383d | ||
|
|
97b870b725 |
17
README.md
17
README.md
@@ -1,4 +1,17 @@
|
||||
# cirpedersen
|
||||
# CircomLib
|
||||
|
||||
Pedersen Hash and Exponentiation circuits using Baby Jub Curve in circom language
|
||||
## Description
|
||||
|
||||
- This repository contains a library of circuit templates.
|
||||
- All files are copyrighted under 2018 0KIMS association and part of the free software [circom](https://github.com/iden3/circom) (Zero Knowledge Circuit Compiler).
|
||||
|
||||
## Organisation
|
||||
|
||||
This respository contains 5 folders:
|
||||
- `circuits`: it contains the implementation of different cryptographic primitives in circom language.
|
||||
- `calcpedersenbases`: set of functions in JavaScript used to find a set of points in [Baby Jubjub](https://github.com/barryWhiteHat/baby_jubjub) elliptic curve that serve as basis for the [Pedersen Hash](https://github.com/zcash/zcash/issues/2234).
|
||||
- `doc`: it contains some circuit schemes in ASCII (must be opened with Monodraw, an ASCII art editor for Mac).
|
||||
- `src`: it contains similar implementation of circuits in JavaScript.
|
||||
- `test`: tests.
|
||||
|
||||
A description of the specific circuit templates for the `circuit` folder will be soon updated.
|
||||
@@ -1,3 +1,19 @@
|
||||
# CircomLib/Circuits
|
||||
|
||||
## Description
|
||||
|
||||
- This folder contains circuit templates for standard operations and many cryptographic primitives.
|
||||
- Below you can find specifications of each function. In the representation of elements, there are three tyes:
|
||||
- Binary
|
||||
- String
|
||||
- Field element (the field is specified in each case. We consider 2 possible fields: Fp and Fr, where p... and r... .)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
[TOC]
|
||||
|
||||
## Jordi
|
||||
|
||||
* compconstant - Returns 1 if `in` (expanded to binary array) > `ct`
|
||||
* aliascheck - check if `in` (expanded to binary array) oveflowed its 254 bits (<= -1)
|
||||
* babyjub - twisted Edwards curve 168700.x^2 + y^2 = 1 + 168696.x^2.y^2
|
||||
@@ -11,4 +27,804 @@
|
||||
* zcash/zcash#2233
|
||||
* smt - Sparse Merkle Tree
|
||||
* https://ethresear.ch/t/optimizing-sparse-merkle-trees/3751
|
||||
* montgomery https://en.wikipedia.org/wiki/Montgomery_curve
|
||||
* montgomery https://en.wikipedia.org/wiki/Montgomery_curve
|
||||
|
||||
## Circuits
|
||||
|
||||
### sha256
|
||||
|
||||
Folder containing the implementation of sha256 hash circuit.
|
||||
|
||||
### smt
|
||||
|
||||
Folder containing the circuit implementation of Sparse Merkle Trees.
|
||||
|
||||
### aliascheck
|
||||
|
||||
- `AliasCheck()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### babyjub
|
||||
|
||||
Arithmetic on [Baby Jubjub elliptic curve](https://github.com/barryWhiteHat/baby_jubjub) in twisted Edwards form. (TODO: Expose here the characteristics of the curve?)
|
||||
|
||||
|
||||
- `BabyAdd()`
|
||||
|
||||
- DESCRIPTION
|
||||
|
||||
It adds two points on the Baby Jubjub curve. More specifically, given two points P1 = (`x1`, `y1`) and P2 = (`x2`, `y2`) it returns a point P3 = (`xout`, `yout`) such that
|
||||
|
||||
(`xout`, `yout`) = (`x1`,`y1`) + (`x2`,`y2`)
|
||||
= ((`x1y2`+`y1x2`)/(1+`dx1x2y1y2`)),(`y1y2`-`ax1x2`)/(1-`dx1x2y1y2`))
|
||||
|
||||
- SCHEMA
|
||||
```
|
||||
var a var d
|
||||
| |
|
||||
| |
|
||||
______v_________v_______
|
||||
input x1 ----> | |
|
||||
input y1 ----> | BabyAdd() | ----> output xout
|
||||
input x2 ----> | | ----> output yout
|
||||
input y2 ----> |________________________|
|
||||
```
|
||||
|
||||
- INPUTS
|
||||
|
||||
| Input | Representation | Description | |
|
||||
| ------------- | ------------- | ------------- | ------------- |
|
||||
| `x1` | Bigint | Field element of Fp | First coordinate of a point (x1, y1) on E. |
|
||||
| `y1` | Bigint | Field element of Fp | Second coordinate of a point (x1, y1) on E. |
|
||||
| `x2` | Bigint | Field element of Fp | First coordinate of a point (x2, y2) on E. |
|
||||
| `y2` | Bigint | Field element of Fp | Second coordinate of a point (x2, y2) on E. |
|
||||
|
||||
Requirement: at least `x1`!=`x2` or `y1`!=`y2`.
|
||||
|
||||
- OUTPUT
|
||||
|
||||
| Input | Representation | Description | |
|
||||
| ------------- | ------------- | ------------- | ------------- |
|
||||
| `xout` | Bigint | Field element of Fp | First coordinate of the addition point (xout, yout) = (x1, y1) + (x2, y2). |
|
||||
| `yout` | Bigint | Field element of Fp | Second coordinate of the addition point (xout, yout) = (x1, y1) + (x2, y2). |
|
||||
|
||||
- BENCHMARKS (constraints)
|
||||
|
||||
- EXAMPLE
|
||||
|
||||
- `BabyDbl()`
|
||||
- DESCRIPTION : doubles a point (`xout`,`yout`) = 2*(`x`,`y`).
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `BabyCheck()`
|
||||
|
||||
- DESCRIPTION : checks if a given point is in the curve.
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `BabyPbk()`
|
||||
|
||||
- DESCRIPTION: : given a private key, it returns the associated public key.
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
|
||||
### binsub
|
||||
|
||||
- `BinSub(n)`
|
||||
|
||||
- DESCRIPTION: binary substraction.
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### binsum
|
||||
|
||||
- `nbits(a)`
|
||||
|
||||
- DESCRIPTION : binary sum.
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `BinSum(n, ops)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### bitify
|
||||
|
||||
- `Num2Bits()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Num2Bits_strict()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Bits2Num()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Bits2Num_strict()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Num2BitsNeg()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### comparators
|
||||
|
||||
- `IsZero() `
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `IsEqual()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `ForceEqualIfEnabled()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `LessThan()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `GreaterThan()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `GreaterEqThan()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### compconstant
|
||||
|
||||
- `CompConstant(ct)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### eddsa
|
||||
|
||||
Edwards Digital Signature Algorithm in Baby Jubjbub (link a eddsa)
|
||||
|
||||
- `EdDSAVerifier(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### eddsamimc
|
||||
|
||||
- `EdDSAMiMCVerifier()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### eddsamimcsponge
|
||||
|
||||
- `EdDSAMiMCSpongeVerifier()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### eddsaposeidon
|
||||
|
||||
- `EdDSAPoseidonVerifier()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### escalarmul
|
||||
|
||||
- `EscalarMulWindow(base, k)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `EscalarMul(n, base)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### escalarmulany
|
||||
|
||||
- `Multiplexor2()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `BitElementMulAny()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `SegmentMulAny(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `EscalarMulAny(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### escalarmulfix
|
||||
|
||||
- `WindowMulFix()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `SegmentMulFix(nWindows)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `EscalarMulFix(n, BASE)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### escalarmulw4table
|
||||
|
||||
- `pointAdd`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `EscalarMulW4Table`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### gates
|
||||
|
||||
- `XOR`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `AND`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `OR`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `NOT`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `NAND`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `NOR`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `MultiAND`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### mimc
|
||||
|
||||
Implementation of MiMC-7 hash in Fp being... (link to description of the hash)
|
||||
|
||||
- `MiMC7(nrounds)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `MultiMiMC7(nInputs, nRounds)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### mimcsponge
|
||||
|
||||
- `MiMCSponge(nInputs, nRounds, nOutputs)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `MiMCFeistel(nrounds)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### montgomery
|
||||
|
||||
- `Edwards2Montgomery()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Montgomery2Edwards()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `MontgomeryAdd()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `MontgomeryDouble()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### multiplexer
|
||||
|
||||
- `log2(a)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `EscalarProduct(w)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Decoder(w)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Multiplexer(wIn, nIn)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### mux1
|
||||
|
||||
- `MultiMux1(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Mux1()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### mux2
|
||||
|
||||
- `MultiMux2(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Mux2()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### mux3
|
||||
|
||||
- `MultiMux3(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Mux3()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### mux4
|
||||
|
||||
- `MultiMux4(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Mux4()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### pedersen_old
|
||||
|
||||
Old version of the Pedersen hash (do not use any
|
||||
more?).
|
||||
|
||||
### pedersen
|
||||
|
||||
- `Window4()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Segment(nWindows)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Pedersen(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### pointbits
|
||||
|
||||
- `sqrt(n)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Bits2Point()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Bits2Point_Strict()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Point2Bits`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Point2Bits_Strict`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### poseidon
|
||||
|
||||
Implementation of Poseidon hash function (LINK)
|
||||
|
||||
- `Sigma()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Ark(t, C)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Mix(t, M)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
- `Poseidon(nInputs, t, nRoundsF, nRoundsP)`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### sign
|
||||
|
||||
- `Sign()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
### switcher
|
||||
|
||||
- `Switcher()`
|
||||
|
||||
- DESCRIPTION
|
||||
- SCHEMA
|
||||
- INPUT
|
||||
- OUTPUT
|
||||
- BENCHMARKS
|
||||
- EXAMPLE
|
||||
|
||||
@@ -47,7 +47,7 @@ template EdDSAPoseidonVerifier() {
|
||||
snum2bits.out[i] ==> compConstant.in[i];
|
||||
}
|
||||
compConstant.in[253] <== 0;
|
||||
compConstant.out === 0;
|
||||
compConstant.out*enabled === 0;
|
||||
|
||||
// Calculate the h = H(R,A, msg)
|
||||
|
||||
@@ -79,7 +79,7 @@ template EdDSAPoseidonVerifier() {
|
||||
// We check that A is not zero.
|
||||
component isZero = IsZero();
|
||||
isZero.in <== dbl3.x;
|
||||
isZero.out === 0;
|
||||
isZero.out*enabled === 0;
|
||||
|
||||
component mulAny = EscalarMulAny(254);
|
||||
for (i=0; i<254; i++) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
Copyright 2018 0KIMS association.
|
||||
|
||||
This file is part of circom (Zero Knowledge Circuit Compiler).
|
||||
@@ -71,7 +71,7 @@ template EscalarMulWindow(base, k) {
|
||||
signal input sel[4];
|
||||
signal output out[2];
|
||||
|
||||
component table;
|
||||
var table;
|
||||
component mux;
|
||||
component adder;
|
||||
|
||||
@@ -86,8 +86,8 @@ template EscalarMulWindow(base, k) {
|
||||
}
|
||||
|
||||
for (i=0; i<16; i++) {
|
||||
table.out[i][0] ==> mux.c[0][i];
|
||||
table.out[i][1] ==> mux.c[1][i];
|
||||
mux.c[0][i] <== table[i][0];
|
||||
mux.c[1][i] <== table[i][1];
|
||||
}
|
||||
|
||||
in[0] ==> adder.x1;
|
||||
|
||||
@@ -27,8 +27,8 @@ function pointAdd(x1,y1,x2,y2) {
|
||||
return res;
|
||||
}
|
||||
|
||||
template EscalarMulW4Table(base, k) {
|
||||
signal output out[16][2];
|
||||
function EscalarMulW4Table(base, k) {
|
||||
var out[16][2];
|
||||
|
||||
var i;
|
||||
var p[2];
|
||||
@@ -39,11 +39,13 @@ template EscalarMulW4Table(base, k) {
|
||||
dbl = pointAdd(dbl[0], dbl[1], dbl[0], dbl[1]);
|
||||
}
|
||||
|
||||
out[0][0] <== 0;
|
||||
out[0][1] <== 1;
|
||||
out[0][0] = 0;
|
||||
out[0][1] = 1;
|
||||
for (i=1; i<16; i++) {
|
||||
p = pointAdd(out[i-1][0], out[i-1][1], dbl[0], dbl[1]);
|
||||
out[i][0] <== p[0];
|
||||
out[i][1] <== p[1];
|
||||
out[i][0] = p[0];
|
||||
out[i][1] = p[1];
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ template MiMCSponge(nInputs, nRounds, nOutputs) {
|
||||
}
|
||||
}
|
||||
|
||||
outs[0] = S[nInputs - 1].xL_out;
|
||||
outs[0] <== S[nInputs - 1].xL_out;
|
||||
|
||||
for (var i = 0; i < nOutputs - 1; i++) {
|
||||
S[nInputs + i] = MiMCFeistel(nRounds);
|
||||
@@ -39,8 +39,8 @@ template MiMCFeistel(nrounds) {
|
||||
signal output xL_out;
|
||||
signal output xR_out;
|
||||
|
||||
var c = [
|
||||
0,
|
||||
// doesn't contain the first and last round constants, which are always zero
|
||||
var c_partial = [
|
||||
7120861356467848435263064379192047478074060781135320967663101236819528304084,
|
||||
5024705281721889198577876690145313457398658950011302225525409148828000436681,
|
||||
17980351014018068290387269214713820287804403312720763401943303895585469787384,
|
||||
@@ -258,8 +258,7 @@ template MiMCFeistel(nrounds) {
|
||||
18224457394066545825553407391290108485121649197258948320896164404518684305122,
|
||||
274945154732293792784580363548970818611304339008964723447672490026510689427,
|
||||
11050822248291117548220126630860474473945266276626263036056336623671308219529,
|
||||
2119542016932434047340813757208803962484943912710204325088879681995922344971,
|
||||
0
|
||||
2119542016932434047340813757208803962484943912710204325088879681995922344971
|
||||
];
|
||||
|
||||
var t;
|
||||
@@ -268,13 +267,19 @@ template MiMCFeistel(nrounds) {
|
||||
signal xL[nrounds-1];
|
||||
signal xR[nrounds-1];
|
||||
|
||||
var c;
|
||||
for (var i=0; i<nrounds; i++) {
|
||||
t = (i==0) ? k+xL_in : k + xL[i-1] + c[i];
|
||||
if ((i == 0) || (i == nrounds - 1)) {
|
||||
c = 0;
|
||||
} else {
|
||||
c = c_partial[i - 1];
|
||||
}
|
||||
t = (i==0) ? k+xL_in : k + xL[i-1] + c;
|
||||
t2[i] <== t*t;
|
||||
t4[i] <== t2[i]*t2[i];
|
||||
if (i<nrounds-1) {
|
||||
xL[i] <== ((i==0) ? xR_in : xR[i-1]) + t4[i]*t;
|
||||
xR[i] = (i==0) ? xL_in : xL[i-1];
|
||||
xR[i] <== (i==0) ? xL_in : xL[i-1];
|
||||
} else {
|
||||
xR_out <== xR[i-1] + t4[i]*t;
|
||||
xL_out <== xL[i-1];
|
||||
|
||||
3
index.js
3
index.js
@@ -4,3 +4,6 @@ exports.mimc7 = require("./src/mimc7");
|
||||
exports.mimcsponge = require("./src/mimcsponge");
|
||||
exports.babyJub = require("./src/babyjub");
|
||||
exports.pedersenHash = require("./src/pedersenHash");
|
||||
exports.SMT = require("./src/smt").SMT;
|
||||
exports.SMTMemDB = require("./src/smt_memdb");
|
||||
exports.poseidon = require("./src/poseidon");
|
||||
|
||||
31
package-lock.json
generated
31
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circomlib",
|
||||
"version": "0.0.15",
|
||||
"version": "0.0.21",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -348,9 +348,9 @@
|
||||
}
|
||||
},
|
||||
"big-integer": {
|
||||
"version": "1.6.44",
|
||||
"resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.44.tgz",
|
||||
"integrity": "sha512-7MzElZPTyJ2fNvBkPxtFQ2fWIkVmuzw41+BZHSzpEq3ymB2MfeKp1+yXl/tS75xCx+WnyV+yb0kp+K1C3UNwmQ=="
|
||||
"version": "1.6.46",
|
||||
"resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.46.tgz",
|
||||
"integrity": "sha512-Vj2TNtZ8Y0XaL6HCkzJiEqfykjtv/9wVCWIutMe+QVIXLPe2tCLEzULtYvcX9WRtmNIj3Jqi5tNjIsR0N4QOsg=="
|
||||
},
|
||||
"binary-extensions": {
|
||||
"version": "1.13.1",
|
||||
@@ -601,10 +601,9 @@
|
||||
}
|
||||
},
|
||||
"circom": {
|
||||
"version": "0.0.28",
|
||||
"resolved": "https://registry.npmjs.org/circom/-/circom-0.0.28.tgz",
|
||||
"integrity": "sha512-cYivdFVPUAVsGFgx3/W3BQe50fwiu+w8Mq9rWA/UYLWwjAgY0Ctk+obpxGL5v9ZHJpO6pvmczLnOXmV/KqiB5g==",
|
||||
"dev": true,
|
||||
"version": "0.0.35",
|
||||
"resolved": "https://registry.npmjs.org/circom/-/circom-0.0.35.tgz",
|
||||
"integrity": "sha512-MWsJPYPH+s9wN2I5abEHUIAyFVsgTCy+UzJh///WnflXfh3c1tlbv8zt1VV+YHHREpyS+WF5ZBr7TujpaVFu5g==",
|
||||
"requires": {
|
||||
"big-integer": "^1.6.32",
|
||||
"optimist": "^0.6.1",
|
||||
@@ -3863,7 +3862,6 @@
|
||||
"version": "0.6.1",
|
||||
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz",
|
||||
"integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"minimist": "~0.0.1",
|
||||
"wordwrap": "~0.0.2"
|
||||
@@ -3872,8 +3870,7 @@
|
||||
"wordwrap": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
|
||||
"integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc=",
|
||||
"dev": true
|
||||
"integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -3929,9 +3926,9 @@
|
||||
"integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg=="
|
||||
},
|
||||
"p-limit": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz",
|
||||
"integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==",
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz",
|
||||
"integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==",
|
||||
"requires": {
|
||||
"p-try": "^2.0.0"
|
||||
}
|
||||
@@ -4703,9 +4700,9 @@
|
||||
}
|
||||
},
|
||||
"snarkjs": {
|
||||
"version": "0.1.18",
|
||||
"resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.1.18.tgz",
|
||||
"integrity": "sha512-JWZMBvPPIL424+QCY0PtreIiHTRoX4h4qYvKv3nqKPVZl0T7sw3B/ZeizrCVRz7Jr9vrwZxmzJ0XIg6D1yC8Mg==",
|
||||
"version": "0.1.20",
|
||||
"resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.1.20.tgz",
|
||||
"integrity": "sha512-tYmWiVm1sZiB44aIh5w/3HUaTntTUC4fv+CWs4rR0gfkt2KbHTpArOqZW++/Lxujrn9IypXVhdKVUr/eE6Hxfg==",
|
||||
"requires": {
|
||||
"big-integer": "^1.6.43",
|
||||
"chai": "^4.2.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circomlib",
|
||||
"version": "0.0.15",
|
||||
"version": "0.0.21",
|
||||
"description": "Basic circuits library for Circom",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
@@ -26,12 +26,12 @@
|
||||
"dependencies": {
|
||||
"blake-hash": "^1.1.0",
|
||||
"blake2b": "^2.1.3",
|
||||
"snarkjs": "^0.1.19",
|
||||
"circom": "0.0.35",
|
||||
"snarkjs": "^0.1.20",
|
||||
"typedarray-to-buffer": "^3.1.5",
|
||||
"web3": "^1.0.0-beta.55"
|
||||
},
|
||||
"devDependencies": {
|
||||
"circom": "0.0.28",
|
||||
"eslint-plugin-mocha": "^5.2.0",
|
||||
"ganache-cli": "^6.4.4",
|
||||
"mocha": "^5.2.0"
|
||||
|
||||
@@ -36,6 +36,8 @@ exports.getMatrix = (t, seed, nRounds) => {
|
||||
if (typeof seed === "undefined") seed = SEED;
|
||||
if (typeof nRounds === "undefined") nRounds = NROUNDSF + NROUNDSP;
|
||||
if (typeof t === "undefined") t = T;
|
||||
assert(t<=6); // Force the same matrix for all.
|
||||
t=6;
|
||||
let nonce = "0000";
|
||||
let cmatrix = getPseudoRandom(seed+"_matrix_"+nonce, t*2);
|
||||
while (!allDifferent(cmatrix)) {
|
||||
@@ -95,7 +97,7 @@ exports.createHash = (t, nRoundsF, nRoundsP, seed) => {
|
||||
const M = exports.getMatrix(t, seed, nRoundsF + nRoundsP);
|
||||
return function(inputs) {
|
||||
let state = [];
|
||||
assert(inputs.length < t);
|
||||
assert(inputs.length <= t);
|
||||
assert(inputs.length > 0);
|
||||
for (let i=0; i<inputs.length; i++) state[i] = bigInt(inputs[i]);
|
||||
for (let i=inputs.length; i<t; i++) state[i] = F.zero;
|
||||
|
||||
@@ -121,7 +121,7 @@ function createCode(t, nRoundsF, nRoundsP, seed) {
|
||||
// We ignore the pointer and the length and just load 6 values to the state
|
||||
// (Stack positions 0-5) If the array is shorter, we just set zeros.
|
||||
for (let i=0; i<t; i++) {
|
||||
C.push(0x44+(0x20*(5-i)));
|
||||
C.push(0x44+(0x20*(t-1-i)));
|
||||
C.calldataload();
|
||||
}
|
||||
|
||||
|
||||
@@ -309,3 +309,5 @@ async function newMemEmptyTrie() {
|
||||
|
||||
module.exports.loadFromFile = loadFromFile;
|
||||
module.exports.newMemEmptyTrie = newMemEmptyTrie;
|
||||
module.exports.SMT = SMT;
|
||||
module.exports.SMTMemDB = SMTMemDB;
|
||||
|
||||
@@ -27,6 +27,14 @@ class SMTMemDb {
|
||||
return this.nodes[keyS];
|
||||
}
|
||||
|
||||
async multiGet(keys) {
|
||||
const promises = [];
|
||||
for (let i=0; i<keys.length; i++) {
|
||||
promises.push(this.get(keys[i]));
|
||||
}
|
||||
return await Promise.all(promises);
|
||||
}
|
||||
|
||||
async setRoot(rt) {
|
||||
this.root = rt;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ describe("Aliascheck test", () => {
|
||||
circuit.calculateWitness({in: inp});
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert.equal(err.message, "Constraint doesn't match: 1 != 0");
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
}
|
||||
});
|
||||
|
||||
@@ -67,7 +67,7 @@ describe("Aliascheck test", () => {
|
||||
circuit.calculateWitness({in: inp});
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert.equal(err.message, "Constraint doesn't match: 1 != 0");
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -100,11 +100,11 @@ describe("Baby Jub test", function () {
|
||||
circuitTest.calculateWitness({x: 1, y: 0});
|
||||
assert(false, "Should be a valid point");
|
||||
} catch(err) {
|
||||
assert.equal(err.message, "Constraint doesn't match: 168700 != 1");
|
||||
assert(/Constraint\sdoesn't\smatch(.*)168700\s!=\s1/.test(err.message) );
|
||||
}
|
||||
});
|
||||
|
||||
it("Should extract the public key from the private one", async () => {
|
||||
it("Should extract the public key from the private one", async () => {
|
||||
|
||||
const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
|
||||
const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
|
||||
|
||||
@@ -23,7 +23,7 @@ describe("Sum test", () => {
|
||||
it("Should create a sum circuit", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "sum_test.circom"));
|
||||
assert.equal(cirDef.nVars, 101);
|
||||
assert.equal(cirDef.nVars, 97); // 32 (in1) + 32(in2) + 32(out) + 1 (carry)
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ template Main() {
|
||||
var i;
|
||||
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203]
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
component escalarMul = EscalarMul(256, base);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ template Main() {
|
||||
signal output out[2];
|
||||
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203]
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
|
||||
component n2b = Num2Bits(253);
|
||||
|
||||
@@ -8,7 +8,7 @@ template Main() {
|
||||
var i;
|
||||
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203]
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
component escalarMul = EscalarMul(256, base);
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ template Main() {
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
component escalarMul = EscalarMulW4Table(base, 0);
|
||||
var escalarMul = EscalarMulW4Table(base, 0);
|
||||
for (var i=0; i<16; i++) {
|
||||
out[i][0] <== escalarMul.out[i][0]*in;
|
||||
out[i][1] <== escalarMul.out[i][1]*in;
|
||||
out[i][0] <== escalarMul[i][0]*in;
|
||||
out[i][1] <== escalarMul[i][1]*in;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ template Main() {
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
component escalarMul = EscalarMulW4Table(base, 3);
|
||||
var escalarMul = EscalarMulW4Table(base, 3);
|
||||
for (var i=0; i<16; i++) {
|
||||
out[i][0] <== escalarMul.out[i][0]*in;
|
||||
out[i][1] <== escalarMul.out[i][1]*in;
|
||||
out[i][0] <== escalarMul[i][0]*in;
|
||||
out[i][1] <== escalarMul[i][1]*in;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
3
test/circuits/poseidon3_test.circom
Normal file
3
test/circuits/poseidon3_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/poseidon.circom"
|
||||
|
||||
component main = Poseidon(2, 3, 8, 57);
|
||||
@@ -67,7 +67,7 @@ describe("EdDSA MiMC test", function () {
|
||||
M: msg});
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert.equal(err.message, "Constraint doesn't match: 1 != 0");
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ describe("EdDSA Poseidon test", function () {
|
||||
M: msg});
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert.equal(err.message, "Constraint doesn't match: 1 != 0");
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ describe("Exponentioation test", () => {
|
||||
|
||||
const w = circuit.calculateWitness({in: 1});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
let g = [bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
|
||||
|
||||
@@ -46,12 +48,12 @@ describe("Exponentioation test", () => {
|
||||
for (let i=0; i<16; i++) {
|
||||
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
|
||||
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
|
||||
/*
|
||||
console.log(xout1.toString());
|
||||
console.log(yout1.toString());
|
||||
console.log(dbl[0]);
|
||||
console.log(dbl[1]);
|
||||
*/
|
||||
|
||||
// console.log(xout1.toString());
|
||||
// console.log(yout1.toString());
|
||||
// console.log(dbl[0]);
|
||||
// console.log(dbl[1]);
|
||||
|
||||
assert(xout1.equals(dbl[0]));
|
||||
assert(yout1.equals(dbl[1]));
|
||||
|
||||
@@ -74,6 +76,8 @@ describe("Exponentioation test", () => {
|
||||
|
||||
const w = circuit.calculateWitness({in: 1});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
|
||||
|
||||
@@ -87,12 +91,12 @@ describe("Exponentioation test", () => {
|
||||
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
|
||||
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
|
||||
|
||||
/*
|
||||
console.log(xout1.toString());
|
||||
console.log(yout1.toString());
|
||||
console.log(dbl[0]);
|
||||
console.log(dbl[1]);
|
||||
*/
|
||||
|
||||
// console.log(xout1.toString());
|
||||
// console.log(yout1.toString());
|
||||
// console.log(dbl[0]);
|
||||
// console.log(dbl[1]);
|
||||
|
||||
assert(xout1.equals(dbl[0]));
|
||||
assert(yout1.equals(dbl[1]));
|
||||
|
||||
@@ -102,7 +106,7 @@ describe("Exponentioation test", () => {
|
||||
});
|
||||
|
||||
it("Should exponentiate g^31", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test.circom"));
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test.circom"), {reduceConstraints: true});
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
@@ -146,12 +150,12 @@ describe("Exponentioation test", () => {
|
||||
c = addPoint(c,c);
|
||||
}
|
||||
c = addPoint(c,g);
|
||||
/*
|
||||
console.log(xout2.toString());
|
||||
console.log(yout2.toString());
|
||||
console.log(c[0].toString());
|
||||
console.log(c[1].toString());
|
||||
*/
|
||||
|
||||
// console.log(xout2.toString());
|
||||
// console.log(yout2.toString());
|
||||
// console.log(c[0].toString());
|
||||
// console.log(c[1].toString());
|
||||
|
||||
assert(xout2.equals(c[0]));
|
||||
assert(yout2.equals(c[1]));
|
||||
|
||||
|
||||
@@ -43,6 +43,8 @@ describe("Mux4 test", () => {
|
||||
for (let i=0; i<16; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
@@ -96,6 +98,8 @@ describe("Mux4 test", () => {
|
||||
for (let i=0; i<4; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
@@ -118,6 +122,8 @@ describe("Mux4 test", () => {
|
||||
for (let i=0; i<2; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
|
||||
@@ -18,43 +18,74 @@ describe("Blake2b version test", function() {
|
||||
});
|
||||
|
||||
describe("Poseidon Circuit test", function () {
|
||||
let circuit;
|
||||
let circuit6;
|
||||
let circuit3;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "poseidon_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("Poseidon constraints: " + circuit.nConstraints);
|
||||
const cirDef6 = await compiler(path.join(__dirname, "circuits", "poseidon6_test.circom"));
|
||||
circuit6 = new snarkjs.Circuit(cirDef6);
|
||||
console.log("Poseidon6 constraints: " + circuit6.nConstraints);
|
||||
const cirDef3 = await compiler(path.join(__dirname, "circuits", "poseidon3_test.circom"));
|
||||
circuit3 = new snarkjs.Circuit(cirDef3);
|
||||
console.log("Poseidon3 constraints: " + circuit3.nConstraints);
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([1, 2])", async () => {
|
||||
const w = circuit.calculateWitness({inputs: [1, 2]});
|
||||
it("Should check constrain of hash([1, 2]) t=6", async () => {
|
||||
const w = circuit6.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const res = w[circuit.getSignalIdx("main.out")];
|
||||
const res = w[circuit6.getSignalIdx("main.out")];
|
||||
|
||||
const hash = poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
assert.equal('12242166908188651009877250812424843524687801523336557272219921456462821518061', res2.toString());
|
||||
assert.equal("12242166908188651009877250812424843524687801523336557272219921456462821518061", res2.toString());
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
assert(circuit.checkWitness(w));
|
||||
assert(circuit6.checkWitness(w));
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([3, 4])", async () => {
|
||||
const w = circuit.calculateWitness({inputs: [3, 4]});
|
||||
it("Should check constrain of hash([3, 4]) t=6", async () => {
|
||||
const w = circuit6.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const res = w[circuit.getSignalIdx("main.out")];
|
||||
const res = w[circuit6.getSignalIdx("main.out")];
|
||||
|
||||
const hash = poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([3, 4]);
|
||||
assert.equal('17185195740979599334254027721507328033796809509313949281114643312710535000993', res2.toString());
|
||||
assert.equal("17185195740979599334254027721507328033796809509313949281114643312710535000993", res2.toString());
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
assert(circuit6.checkWitness(w));
|
||||
});
|
||||
|
||||
|
||||
it("Should check constrain of hash([1, 2]) t=3", async () => {
|
||||
const w = circuit3.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const res = w[circuit3.getSignalIdx("main.out")];
|
||||
|
||||
const hash = poseidon.createHash(3, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
assert.equal("2104035019328376391822106787753454168168617545136592089411833517434990977743", res2.toString());
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
assert(circuit3.checkWitness(w));
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([3, 4]) t=3", async () => {
|
||||
const w = circuit3.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const res = w[circuit3.getSignalIdx("main.out")];
|
||||
|
||||
const hash = poseidon.createHash(3, 8, 57);
|
||||
|
||||
const res2 = hash([3, 4]);
|
||||
assert.equal("12456141564250880945411182508630957604732712316993112736876413121277158512223", res2.toString());
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
|
||||
assert(circuit3.checkWitness(w));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,13 +8,13 @@ const bigInt = require("snarkjs").bigInt;
|
||||
const assert = chai.assert;
|
||||
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
||||
|
||||
const SEED = "mimc";
|
||||
|
||||
describe("Poseidon Smart contract test", () => {
|
||||
describe("Poseidon Smart contract test", function () {
|
||||
let testrpc;
|
||||
let web3;
|
||||
let mimc;
|
||||
let poseidon6;
|
||||
let poseidon3;
|
||||
let accounts;
|
||||
this.timeout(100000);
|
||||
|
||||
before(async () => {
|
||||
web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 });
|
||||
@@ -24,17 +24,23 @@ describe("Poseidon Smart contract test", () => {
|
||||
it("Should deploy the contract", async () => {
|
||||
const C = new web3.eth.Contract(poseidonGenContract.abi);
|
||||
|
||||
mimc = await C.deploy({
|
||||
data: poseidonGenContract.createCode()
|
||||
poseidon6 = await C.deploy({
|
||||
data: poseidonGenContract.createCode(6)
|
||||
}).send({
|
||||
gas: 2500000,
|
||||
from: accounts[0]
|
||||
});
|
||||
poseidon3 = await C.deploy({
|
||||
data: poseidonGenContract.createCode(3)
|
||||
}).send({
|
||||
gas: 2500000,
|
||||
from: accounts[0]
|
||||
});
|
||||
});
|
||||
|
||||
it("Shold calculate the mimic correctly", async () => {
|
||||
it("Shold calculate the poseidon correctly t=6", async () => {
|
||||
|
||||
const res = await mimc.methods.poseidon([1,2]).call();
|
||||
const res = await poseidon6.methods.poseidon([1,2]).call();
|
||||
|
||||
// console.log("Cir: " + bigInt(res.toString(16)).toString(16));
|
||||
|
||||
@@ -45,5 +51,19 @@ describe("Poseidon Smart contract test", () => {
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
});
|
||||
it("Shold calculate the poseidon correctly t=3", async () => {
|
||||
|
||||
const res = await poseidon3.methods.poseidon([1,2]).call();
|
||||
|
||||
// console.log("Cir: " + bigInt(res.toString(16)).toString(16));
|
||||
|
||||
const hash = Poseidon.createHash(3, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
// console.log("Ref: " + bigInt(res2).toString(16));
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
23
test/rawsmt3.circom
Normal file
23
test/rawsmt3.circom
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
include "../circuits/smt/smtverifier.circom";
|
||||
template SMT(nLevels) {
|
||||
signal input root;
|
||||
signal input mtp[nLevels];
|
||||
signal input hi;
|
||||
signal input hv;
|
||||
|
||||
component smtClaimExists = SMTVerifier(nLevels);
|
||||
smtClaimExists.enabled <== 1;
|
||||
smtClaimExists.fnc <== 0;
|
||||
smtClaimExists.root <== root;
|
||||
for (var i=0; i<nLevels; i++) {
|
||||
smtClaimExists.siblings[i] <== mtp[i];
|
||||
}
|
||||
smtClaimExists.oldKey <== 0;
|
||||
smtClaimExists.oldValue <== 0;
|
||||
smtClaimExists.isOld0 <== 0;
|
||||
|
||||
smtClaimExists.key <== hi;
|
||||
smtClaimExists.value <== hv;
|
||||
}
|
||||
component main = SMT(4);
|
||||
@@ -80,11 +80,11 @@ describe("SHA256 test", () => {
|
||||
console.log("Vars: "+circuit.nVars);
|
||||
console.log("Constraints: "+circuit.nConstraints);
|
||||
|
||||
/*
|
||||
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
|
||||
const b = Buffer.from(testStr, 'utf8');
|
||||
*/
|
||||
// const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
|
||||
// const b = Buffer.from(testStr, 'utf8');
|
||||
|
||||
const b = new Buffer.alloc(64);
|
||||
for (let i=0; i<64; i++) {
|
||||
b[i] = i+1;
|
||||
@@ -95,7 +95,7 @@ describe("SHA256 test", () => {
|
||||
.digest("hex");
|
||||
|
||||
const arrIn = buffer2bitArray(b);
|
||||
const witness = circuit.calculateWitness({ "in": arrIn } /*, {logOutput: true} */);
|
||||
const witness = circuit.calculateWitness({ "in": arrIn }, {logOutput: false});
|
||||
|
||||
const arrOut = witness.slice(1, 257);
|
||||
const hash2 = bitArray2buffer(arrOut).toString("hex");
|
||||
@@ -104,7 +104,6 @@ describe("SHA256 test", () => {
|
||||
|
||||
}).timeout(1000000);
|
||||
|
||||
|
||||
it("Should calculate a hash of 2 compressor", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test448.circom"), {reduceConstraints:false} );
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
@@ -112,20 +111,19 @@ describe("SHA256 test", () => {
|
||||
console.log("Vars: "+circuit.nVars);
|
||||
console.log("Constraints: "+circuit.nConstraints);
|
||||
|
||||
|
||||
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
|
||||
const b = Buffer.from(testStr, 'utf8');
|
||||
for (let i=0; i<64; i++) {
|
||||
b[i] = i+1;
|
||||
}
|
||||
const b = Buffer.from(testStr, "utf8");
|
||||
// for (let i=0; i<64; i++) {
|
||||
// b[i] = i+1;
|
||||
// }
|
||||
|
||||
const hash = crypto.createHash("sha256")
|
||||
.update(b)
|
||||
.digest("hex");
|
||||
|
||||
const arrIn = buffer2bitArray(b);
|
||||
const witness = circuit.calculateWitness({ "in": arrIn } /*, {logOutput: true} */);
|
||||
const witness = circuit.calculateWitness({ "in": arrIn } , {logOutput: false});
|
||||
|
||||
const arrOut = witness.slice(1, 257);
|
||||
const hash2 = bitArray2buffer(arrOut).toString("hex");
|
||||
|
||||
@@ -111,5 +111,28 @@ describe("SMT test", function () {
|
||||
assert(circuit.checkWitness(w));
|
||||
});
|
||||
|
||||
it("Check inclussion Adria case", async () => {
|
||||
const e1_hi= bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839");
|
||||
const e1_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
|
||||
|
||||
const e2ok_hi= bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262");
|
||||
const e2ok_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
|
||||
|
||||
const e2fail_hi= bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579");
|
||||
const e2fail_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
|
||||
|
||||
const tree1 = await smt.newMemEmptyTrie();
|
||||
await tree1.insert(e1_hi,e1_hv);
|
||||
await tree1.insert(e2ok_hi,e2ok_hv);
|
||||
|
||||
await testInclusion(tree1, e2ok_hi, circuit);
|
||||
|
||||
const tree2 = await smt.newMemEmptyTrie();
|
||||
await tree2.insert(e1_hi,e1_hv);
|
||||
await tree2.insert(e2fail_hi,e2fail_hv);
|
||||
|
||||
await testInclusion(tree2, e2fail_hi, circuit);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
98
test/smtverifier_adria.js
Normal file
98
test/smtverifier_adria.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const fs = require("fs")
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const smt = require("../src/smt.js");
|
||||
|
||||
const circuitSource = `
|
||||
include "../circuits/smt/smtverifier.circom";
|
||||
template SMT(nLevels) {
|
||||
signal input root;
|
||||
signal input mtp[nLevels];
|
||||
signal input hi;
|
||||
signal input hv;
|
||||
|
||||
component smtClaimExists = SMTVerifier(nLevels);
|
||||
smtClaimExists.enabled <== 1;
|
||||
smtClaimExists.fnc <== 0;
|
||||
smtClaimExists.root <== root;
|
||||
for (var i=0; i<nLevels; i++) {
|
||||
smtClaimExists.siblings[i] <== mtp[i];
|
||||
}
|
||||
smtClaimExists.oldKey <== 0;
|
||||
smtClaimExists.oldValue <== 0;
|
||||
smtClaimExists.isOld0 <== 0;
|
||||
|
||||
smtClaimExists.key <== hi;
|
||||
smtClaimExists.value <== hv;
|
||||
}
|
||||
component main = SMT(4);
|
||||
`;
|
||||
|
||||
describe("smt3test", function () {
|
||||
this.timeout(200000);
|
||||
|
||||
let circuitFileName;
|
||||
|
||||
before( async () => {
|
||||
circuitFileName = path.join(__dirname, ".", "rawsmt3.circom");
|
||||
fs.writeFileSync(circuitFileName,circuitSource);
|
||||
});
|
||||
|
||||
const levels = 4;
|
||||
async function testsmt3(e1, e2) {
|
||||
let tree = await smt.newMemEmptyTrie();
|
||||
|
||||
// insert e1, e2
|
||||
await tree.insert(e1.hi, e1.hv);
|
||||
await tree.insert(e2.hi, e2.hv);
|
||||
|
||||
// generate proof for e1
|
||||
const findInfo = await tree.find(e1.hi);
|
||||
const siblings = findInfo.siblings;
|
||||
while (siblings.length < levels) siblings.push(bigInt(0));
|
||||
|
||||
const input = {
|
||||
root: tree.root,
|
||||
mtp: siblings,
|
||||
hi: e1.hi,
|
||||
hv: e1.hv,
|
||||
};
|
||||
|
||||
const compiledCircuit = await compiler(
|
||||
circuitFileName,
|
||||
{ reduceConstraints: false }
|
||||
);
|
||||
|
||||
const circuit = new snarkjs.Circuit(compiledCircuit);
|
||||
const witness = circuit.calculateWitness(input);
|
||||
circuit.checkWitness(witness);
|
||||
}
|
||||
|
||||
it("TestSmts", async () => {
|
||||
|
||||
const e1 = {
|
||||
hi: bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"),
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
|
||||
};
|
||||
|
||||
const e2ok = {
|
||||
hi: bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262"),
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
|
||||
};
|
||||
|
||||
const e2fail = {
|
||||
hi: bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579"),
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
|
||||
};
|
||||
|
||||
console.log("test e1, e2ok");
|
||||
await testsmt3(e1, e2ok);
|
||||
|
||||
console.log("test e1, e2fail");
|
||||
await testsmt3(e1, e2fail);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user