Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into fix/vhnat/small-dom…
Browse files Browse the repository at this point in the history
…ain-hotfix
  • Loading branch information
VitaliiH authored and VitaliiH committed Jul 25, 2024
2 parents 0fcbb58 + 8411ed1 commit b33965a
Show file tree
Hide file tree
Showing 31 changed files with 147 additions and 280 deletions.
6 changes: 3 additions & 3 deletions docs/docs/contributor-guide.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributor's Guide

We welcome all contributions with open arms. At Ingonyama we take a village approach, believing it takes many hands and minds to build a ecosystem.
We welcome all contributions with open arms. At Ingonyama we take a village approach, believing it takes many hands and minds to build an ecosystem.

## Contributing to ICICLE

Expand All @@ -14,9 +14,9 @@ We welcome all contributions with open arms. At Ingonyama we take a village appr
When opening a [pull request](https://github.com/ingonyama-zk/icicle/pulls) please keep the following in mind.

- `Clear Purpose` - The pull request should solve a single issue and be clean of any unrelated changes.
- `Clear description` - If the pull request is for a new feature describe what you built, why you added it and how its best that we test it. For bug fixes please describe the issue and the solution.
- `Clear description` - If the pull request is for a new feature describe what you built, why you added it and how it's best that we test it. For bug fixes please describe the issue and the solution.
- `Consistent style` - Rust and Golang code should be linted by the official linters (golang fmt and rust fmt) and maintain a proper style. For CUDA and C++ code we use [`clang-format`](https://github.com/ingonyama-zk/icicle/blob/main/.clang-format), [here](https://github.com/ingonyama-zk/icicle/blob/605c25f9d22135c54ac49683b710fe2ce06e2300/.github/workflows/main-format.yml#L46) you can see how we run it.
- `Minimal Tests` - please add test which cover basic usage of your changes .
- `Minimal Tests` - please add test which cover basic usage of your changes.

## Questions?

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/icicle/golang-bindings/ecntt.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type NTTConfig[T any] struct {
- **`areInputsOnDevice`**: Indicates if input scalars are located on the device.
- **`areOutputsOnDevice`**: Indicates if results are stored on the device.
- **`IsAsync`**: Controls whether the NTT operation runs asynchronously.
- **`NttAlgorithm`**: Explicitly select the NTT algorithm. ECNTT supports running on `Radix2` algoruithm.
- **`NttAlgorithm`**: Explicitly select the NTT algorithm. ECNTT supports running on `Radix2` algorithm.

### Default Configuration

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/icicle/golang-bindings/msm.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ cfg.Ctx.IsBigTriangle = true

Toggling between MSM modes occurs automatically based on the number of results you are expecting from the `MSM` function.

The number of results is interpreted from the size of `var out core.DeviceSlice`. Thus its important when allocating memory for `var out core.DeviceSlice` to make sure that you are allocating `<number of results> X <size of a single point>`.
The number of results is interpreted from the size of `var out core.DeviceSlice`. Thus it's important when allocating memory for `var out core.DeviceSlice` to make sure that you are allocating `<number of results> X <size of a single point>`.

```go
...
Expand Down Expand Up @@ -168,7 +168,7 @@ import (
)
```

This package include `G2Projective` and `G2Affine` points as well as a `G2Msm` method.
This package includes `G2Projective` and `G2Affine` points as well as a `G2Msm` method.

```go
package main
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/icicle/polynomials/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Polynomial& add_monomial_inplace(Coeff monomial_coeff, uint64_t monomial = 0);
Polynomial& sub_monomial_inplace(Coeff monomial_coeff, uint64_t monomial = 0);
```
The ability to add or subtract monomials directly and in-place is an efficient way to manipualte polynomials.
The ability to add or subtract monomials directly and in-place is an efficient way to manipulate polynomials.
Example:
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/icicle/primitives/msm.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MSM - Multi scalar multiplication

MSM stands for Multi scalar multiplication, its defined as:
MSM stands for Multi scalar multiplication, it's defined as:

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mi>M</mi>
Expand Down Expand Up @@ -43,7 +43,7 @@ $a_0, \ldots, a_n$ - Scalars

$MSM(a, G) \in G$ - a single EC (elliptic curve) point

In words, MSM is the sum of scalar and EC point multiplications. We can see from this definition that the core operations occurring are Modular Multiplication and Elliptic curve point addition. Its obvious that multiplication can be computed in parallel and then the products summed, making MSM inherently parallelizable.
In words, MSM is the sum of scalar and EC point multiplications. We can see from this definition that the core operations occurring are Modular Multiplication and Elliptic curve point addition. It's obvious that multiplication can be computed in parallel and then the products summed, making MSM inherently parallelizable.

Accelerating MSM is crucial to a ZK protocol's performance due to the [large percent of run time](https://hackmd.io/@0xMonia/SkQ6-oRz3#Hardware-acceleration-in-action) they take when generating proofs.

Expand Down Expand Up @@ -131,7 +131,7 @@ Large buckets exist in two cases:
2. When `c` does not divide the scalar bit-size.
`large_bucket_factor` that is equal to 10 yields good results for most cases, but it's best to fine tune this parameter per `c` and per scalar distribution.
The two most important parameters for performance are `c` and the `precompute_factor`. They affect the number of EC additions as well as the memory size. When the points are not known in advance we cannot use precomputation. In this case the best `c` value is usually around $log_2(msmSize) - 4$. However, in most protocols the points are known in advanced and precomputation can be used unless limited by memory. Usually it's best to use maximum precomputation (such that we end up with only a single bucket module) combined we a `c` value around $log_2(msmSize) - 1$.
The two most important parameters for performance are `c` and the `precompute_factor`. They affect the number of EC additions as well as the memory size. When the points are not known in advance we cannot use precomputation. In this case the best `c` value is usually around $log_2(msmSize) - 4$. However, in most protocols the points are known in advance and precomputation can be used unless limited by memory. Usually it's best to use maximum precomputation (such that we end up with only a single bucket module) combined with a `c` value around $log_2(msmSize) - 1$.
## Memory usage estimation
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/icicle/primitives/ntt.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Choosing an algorithm is heavily dependent on your use case. For example Cooley-

NTT also supports two different modes `Batch NTT` and `Single NTT`

Deciding weather to use `batch NTT` vs `single NTT` is highly dependent on your application and use case.
Deciding whether to use `batch NTT` vs `single NTT` is highly dependent on your application and use case.

#### Single NTT

Expand Down
8 changes: 4 additions & 4 deletions docs/docs/icicle/primitives/poseidon.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Poseidon

[Poseidon](https://eprint.iacr.org/2019/458.pdf) is a popular hash in the ZK ecosystem primarily because its optimized to work over large prime fields, a common setting for ZK proofs, thereby minimizing the number of multiplicative operations required.
[Poseidon](https://eprint.iacr.org/2019/458.pdf) is a popular hash in the ZK ecosystem primarily because it's optimized to work over large prime fields, a common setting for ZK proofs, thereby minimizing the number of multiplicative operations required.

Poseidon has also been specifically designed to be efficient when implemented within ZK circuits, Poseidon uses far less constraints compared to other hash functions like Keccak or SHA-256 in the context of ZK circuits.

Expand Down Expand Up @@ -42,7 +42,7 @@ To generate a secure hash output, the algorithm goes through a series of "full r

**Linear Transformation and Round Constants:** A linear transformation is performed and round constants are added. The linear transformation in partial rounds can be designed to be less computationally intensive (this is done by using a sparse matrix) than in full rounds, further optimizing the function's efficiency.

The user of Poseidon can often choose how many partial or full rounds he wishes to apply; more full rounds will increase security but degrade performance. The choice and balance is highly dependent on the use case.
The user of Poseidon can often choose how many partial or full rounds he wishes to apply; more full rounds will increase security but degrade performance. The choice and balance are highly dependent on the use case.

## Using Poseidon

Expand All @@ -60,7 +60,7 @@ So for Poseidon of arity 2 and input of size 1024 * 2, we would expect 1024 elem

Poseidon is extremely customizable and using different constants will produce different hashes, security levels and performance results.

We support pre-calculated and optimized constants for each of the [supported curves](../core#supported-curves-and-operations).The constants can be found [here](https://github.com/ingonyama-zk/icicle/tree/main/icicle/include/poseidon/constants) and are labeled clearly per curve `<curve_name>_poseidon.h`.
We support pre-calculated and optimized constants for each of the [supported curves](../core#supported-curves-and-operations). The constants can be found [here](https://github.com/ingonyama-zk/icicle/tree/main/icicle/include/poseidon/constants) and are labeled clearly per curve `<curve_name>_poseidon.h`.

If you wish to generate your own constants you can use our python script which can be found [here](https://github.com/ingonyama-zk/icicle/tree/main/icicle/include/poseidon/constants/generate_parameters.py).

Expand Down Expand Up @@ -135,7 +135,7 @@ impl<'a> Default for HashConfig<'a> {
}
```

In the example above `Poseidon::load(arity, &ctx).unwrap();` is used which will load the correct constants based on arity and curve. Its possible to [generate](#constants) your own constants and load them.
In the example above `Poseidon::load(arity, &ctx).unwrap();` is used which will load the correct constants based on arity and curve. It's possible to [generate](#constants) your own constants and load them.

```rust
let ctx = get_default_device_context();
Expand Down
5 changes: 5 additions & 0 deletions icicle/src/curves/extern.cu
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ extern "C" void CONCAT_EXPAND(CURVE, to_affine)(projective_t* point, affine_t* p
*point_out = projective_t::to_affine(*point);
}

extern "C" void CONCAT_EXPAND(CURVE, from_affine)(affine_t* point, projective_t* point_out)
{
*point_out = projective_t::from_affine(*point);
}

extern "C" void CONCAT_EXPAND(CURVE, generate_projective_points)(projective_t* points, int size)
{
projective_t::rand_host_many(points, size);
Expand Down
5 changes: 5 additions & 0 deletions icicle/src/curves/extern_g2.cu
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ extern "C" void CONCAT_EXPAND(CURVE, g2_to_affine)(g2_projective_t* point, g2_af
*point_out = g2_projective_t::to_affine(*point);
}

extern "C" void CONCAT_EXPAND(CURVE, g2_from_affine)(g2_affine_t* point, g2_projective_t* point_out)
{
*point_out = g2_projective_t::from_affine(*point);
}

extern "C" void CONCAT_EXPAND(CURVE, g2_generate_projective_points)(g2_projective_t* points, int size)
{
g2_projective_t::rand_host_many(points, size);
Expand Down
30 changes: 0 additions & 30 deletions wrappers/golang/core/internal/mock_curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@ func (p *MockProjective) FromLimbs(x, y, z []uint32) MockProjective {
return *p
}

func (p *MockProjective) FromAffine(a MockAffine) MockProjective {
z := MockBaseField{}
z.One()

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}

type MockAffine struct {
X, Y MockBaseField
}
Expand All @@ -68,18 +53,3 @@ func (a *MockAffine) FromLimbs(x, y []uint32) MockAffine {

return *a
}

func (a MockAffine) ToProjective() MockProjective {
var z MockBaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p MockProjective
return p.Zero()
}

return MockProjective{
X: a.X,
Y: a.Y,
Z: z.One(),
}
}
31 changes: 9 additions & 22 deletions wrappers/golang/curves/bls12377/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,10 @@ func (p *Projective) FromLimbs(x, y, z []uint32) Projective {
}

func (p *Projective) FromAffine(a Affine) Projective {
z := BaseField{}
z.One()

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(p))
C.bls12_377_from_affine(cA, cP)
return *p
}

Expand All @@ -65,7 +58,7 @@ func (p *Projective) ProjectiveToAffine() Affine {
var a Affine

cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(&p))
cP := (*C.projective_t)(unsafe.Pointer(p))
C.bls12_377_to_affine(cP, cA)
return a
}
Expand Down Expand Up @@ -111,18 +104,12 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
}

func (a Affine) ToProjective() Projective {
var z BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p Projective
return p.Zero()
}
var p Projective

return Projective{
X: a.X,
Y: a.Y,
Z: z.One(),
}
cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(&p))
C.bls12_377_from_affine(cA, cP)
return p
}

func AffineFromProjective(p *Projective) Affine {
Expand Down
31 changes: 9 additions & 22 deletions wrappers/golang/curves/bls12377/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,10 @@ func (p *G2Projective) FromLimbs(x, y, z []uint32) G2Projective {
}

func (p *G2Projective) FromAffine(a G2Affine) G2Projective {
z := G2BaseField{}
z.One()

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
cP := (*C.g2_projective_t)(unsafe.Pointer(p))
C.bls12_377_g2_from_affine(cA, cP)
return *p
}

Expand All @@ -65,7 +58,7 @@ func (p *G2Projective) ProjectiveToAffine() G2Affine {
var a G2Affine

cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
cP := (*C.g2_projective_t)(unsafe.Pointer(p))
C.bls12_377_g2_to_affine(cP, cA)
return a
}
Expand Down Expand Up @@ -111,18 +104,12 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
}

func (a G2Affine) ToProjective() G2Projective {
var z G2BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p G2Projective
return p.Zero()
}
var p G2Projective

return G2Projective{
X: a.X,
Y: a.Y,
Z: z.One(),
}
cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
C.bls12_377_g2_from_affine(cA, cP)
return p
}

func G2AffineFromProjective(p *G2Projective) G2Affine {
Expand Down
1 change: 1 addition & 0 deletions wrappers/golang/curves/bls12377/g2/include/curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext;

bool bls12_377_g2_eq(g2_projective_t* point1, g2_projective_t* point2);
void bls12_377_g2_to_affine(g2_projective_t* point, g2_affine_t* point_out);
void bls12_377_g2_from_affine(g2_affine_t* point, g2_projective_t* point_out);
void bls12_377_g2_generate_projective_points(g2_projective_t* points, int size);
void bls12_377_g2_generate_affine_points(g2_affine_t* points, int size);
cudaError_t bls12_377_g2_affine_convert_montgomery(g2_affine_t* points, size_t n, bool is_into, DeviceContext* ctx);
Expand Down
1 change: 1 addition & 0 deletions wrappers/golang/curves/bls12377/include/curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext;

bool bls12_377_eq(projective_t* point1, projective_t* point2);
void bls12_377_to_affine(projective_t* point, affine_t* point_out);
void bls12_377_from_affine(affine_t* point, projective_t* point_out);
void bls12_377_generate_projective_points(projective_t* points, int size);
void bls12_377_generate_affine_points(affine_t* points, int size);
cudaError_t bls12_377_affine_convert_montgomery(affine_t* points, size_t n, bool is_into, DeviceContext* ctx);
Expand Down
31 changes: 9 additions & 22 deletions wrappers/golang/curves/bls12381/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,10 @@ func (p *Projective) FromLimbs(x, y, z []uint32) Projective {
}

func (p *Projective) FromAffine(a Affine) Projective {
z := BaseField{}
z.One()

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(p))
C.bls12_381_from_affine(cA, cP)
return *p
}

Expand All @@ -65,7 +58,7 @@ func (p *Projective) ProjectiveToAffine() Affine {
var a Affine

cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(&p))
cP := (*C.projective_t)(unsafe.Pointer(p))
C.bls12_381_to_affine(cP, cA)
return a
}
Expand Down Expand Up @@ -111,18 +104,12 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
}

func (a Affine) ToProjective() Projective {
var z BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p Projective
return p.Zero()
}
var p Projective

return Projective{
X: a.X,
Y: a.Y,
Z: z.One(),
}
cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(&p))
C.bls12_381_from_affine(cA, cP)
return p
}

func AffineFromProjective(p *Projective) Affine {
Expand Down
Loading

0 comments on commit b33965a

Please sign in to comment.