Skip to content

Commit

Permalink
Stone v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Sep 16, 2024
1 parent ba85f37 commit 1414a54
Show file tree
Hide file tree
Showing 41 changed files with 18,405 additions and 11,271 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ COPY install_deps.sh /app/
RUN /app/install_deps.sh

# Install Cairo0 for end-to-end test.
RUN pip install cairo-lang==0.12.0
RUN pip install cairo-lang==0.13.2

COPY docker_common_deps.sh /app/
WORKDIR /app/
Expand Down Expand Up @@ -42,6 +42,7 @@ RUN cairo-run \
--air_private_input=fibonacci_private_input.json \
--trace_file=fibonacci_trace.json \
--memory_file=fibonacci_memory.json \
--min_steps=512 \
--print_output \
--proof_mode

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ These things need to be checked externally.
## Creating and verifying a proof of a CairoZero program

To run and prove the example program `fibonacci.cairo`,
install `cairo-lang` version 0.12.0 (see further instructions in the
[cairo-lang repository](https://github.com/starkware-libs/cairo-lang/tree/v0.12.0)):
install `cairo-lang` version 0.13.2 (see further instructions in the
[cairo-lang repository](https://github.com/starkware-libs/cairo-lang/tree/v0.13.2)):

```bash
pip install cairo-lang==0.12.0
pip install cairo-lang==0.13.2
```

Navigate to the example test directory (`e2e_test/CairoZero`):
Expand Down
14 changes: 14 additions & 0 deletions e2e_test/Cairo/fibonacci.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 StarkWare Industries Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.starkware.co/open-source-license/
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions
// and limitations under the License.

use core::felt252;

fn main() -> Array<felt252> {
Expand Down
6 changes: 5 additions & 1 deletion src/starkware/air/cpu/board/cpu_air.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ class CpuAir : public CpuAirDefinition<FieldElementT, LayoutId> {
private:
constexpr uint64_t PedersenRatio() const;
constexpr uint64_t RangeCheckRatio() const;
constexpr uint64_t RangeCheck96Ratio() const;
constexpr uint64_t RangeCheck96NInstances() const;
constexpr uint64_t EcdsaRatio() const;
constexpr uint64_t BitwiseRatio() const;
constexpr uint64_t EcOpRatio() const;
constexpr uint64_t KeccakRatio() const;
constexpr uint64_t PoseidonRatio() const;
constexpr uint64_t AddModNInstances() const;
constexpr uint64_t MulModNInstances() const;
constexpr bool UsesPedersenBuiltin() const;
constexpr bool UsesRangeCheckBuiltin() const;
constexpr bool UsesRangeCheck96Builtin() const;
Expand All @@ -153,6 +155,8 @@ class CpuAir : public CpuAirDefinition<FieldElementT, LayoutId> {
constexpr bool UsesEcOpBuiltin() const;
constexpr bool UsesKeccakBuiltin() const;
constexpr bool UsesPoseidonBuiltin() const;
constexpr bool UsesAddModBuiltin() const;
constexpr bool UsesMulModBuiltin() const;

/*
Writes public_memory virtual column in the trace.
Expand Down
134 changes: 118 additions & 16 deletions src/starkware/air/cpu/board/cpu_air.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "starkware/air/cpu/builtin/ec/ec_op_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/hash/hash_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/keccak/keccak_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/modulo/add_mod_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/modulo/mod_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/modulo/mul_mod_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/poseidon/poseidon_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/range_check/range_check_builtin_prover_context.h"
#include "starkware/air/cpu/builtin/signature/signature_builtin_prover_context.h"
Expand Down Expand Up @@ -57,18 +60,20 @@ constexpr uint64_t CpuAir<FieldElementT, LayoutId>::RangeCheckRatio() const {
}

template <typename FieldElementT, int LayoutId>
constexpr uint64_t CpuAir<FieldElementT, LayoutId>::RangeCheck96Ratio() const {
constexpr uint64_t CpuAir<FieldElementT, LayoutId>::RangeCheck96NInstances() const {
uint64_t row_ratio = 0, cpu_component_step = 0;
if constexpr (CpuAir::kIsDynamicAir) { // NOLINT: clang-tidy if constexpr bug.
return SafeDiv(
this->dynamic_params_[CpuAir::kRangeCheck96RowRatioDynamicParam],
CpuAir::kCpuComponentHeight * this->dynamic_params_[CpuAir::kCpuComponentStepDynamicParam]);
} else { // NOLINT: clang-tidy if constexpr bug.
row_ratio = this->dynamic_params_[CpuAir::kRangeCheck96BuiltinRowRatioDynamicParam];
cpu_component_step = this->dynamic_params_[CpuAir::kCpuComponentStepDynamicParam];
} else {
if constexpr (CpuAir::kHasRangeCheck96Builtin) { // NOLINT: clang-tidy if constexpr bug.
return CpuAir::kRangeCheck96BuiltinRatio;
row_ratio = CpuAir::kRangeCheck96BuiltinRowRatio;
cpu_component_step = CpuAir::kCpuComponentStep;
} else { // NOLINT: clang-tidy if constexpr bug.
return 0;
}
}
return SafeDiv(n_steps_ * cpu_component_step * CpuAir::kCpuComponentHeight, row_ratio);
}

template <typename FieldElementT, int LayoutId>
Expand Down Expand Up @@ -146,6 +151,40 @@ constexpr uint64_t CpuAir<FieldElementT, LayoutId>::PoseidonRatio() const {
}
}

template <typename FieldElementT, int LayoutId>
constexpr uint64_t CpuAir<FieldElementT, LayoutId>::AddModNInstances() const {
uint64_t row_ratio = 0, cpu_component_step = 0;
if constexpr (CpuAir::kIsDynamicAir) { // NOLINT: clang-tidy if constexpr bug.
row_ratio = this->dynamic_params_[CpuAir::kAddModRowRatioDynamicParam];
cpu_component_step = this->dynamic_params_[CpuAir::kCpuComponentStepDynamicParam];
} else {
if constexpr (CpuAir::kHasAddModBuiltin) { // NOLINT: clang-tidy if constexpr bug.
row_ratio = CpuAir::kAddModRowRatio;
cpu_component_step = CpuAir::kCpuComponentStep;
} else { // NOLINT: clang-tidy if constexpr bug.
return 0;
}
}
return SafeDiv(n_steps_ * cpu_component_step * CpuAir::kCpuComponentHeight, row_ratio);
}

template <typename FieldElementT, int LayoutId>
constexpr uint64_t CpuAir<FieldElementT, LayoutId>::MulModNInstances() const {
uint64_t row_ratio = 0, cpu_component_step = 0;
if constexpr (CpuAir::kIsDynamicAir) { // NOLINT: clang-tidy if constexpr bug.
row_ratio = this->dynamic_params_[CpuAir::kMulModRowRatioDynamicParam];
cpu_component_step = this->dynamic_params_[CpuAir::kCpuComponentStepDynamicParam];
} else {
if constexpr (CpuAir::kHasMulModBuiltin) { // NOLINT: clang-tidy if constexpr bug.
row_ratio = CpuAir::kMulModRowRatio;
cpu_component_step = CpuAir::kCpuComponentStep;
} else { // NOLINT: clang-tidy if constexpr bug.
return 0;
}
}
return SafeDiv(n_steps_ * cpu_component_step * CpuAir::kCpuComponentHeight, row_ratio);
}

template <typename FieldElementT, int LayoutId>
constexpr bool CpuAir<FieldElementT, LayoutId>::UsesPedersenBuiltin() const {
if constexpr (CpuAir::kIsDynamicAir) { // NOLINT: clang-tidy if constexpr bug.
Expand Down Expand Up @@ -218,6 +257,24 @@ constexpr bool CpuAir<FieldElementT, LayoutId>::UsesPoseidonBuiltin() const {
}
}

template <typename FieldElementT, int LayoutId>
constexpr bool CpuAir<FieldElementT, LayoutId>::UsesAddModBuiltin() const {
if constexpr (CpuAir::kIsDynamicAir) { // NOLINT: clang-tidy if constexpr bug.
return this->dynamic_params_[CpuAir::kUsesAddModBuiltinDynamicParam];
} else { // NOLINT: clang-tidy if constexpr bug.
return CpuAir::kHasAddModBuiltin;
}
}

template <typename FieldElementT, int LayoutId>
constexpr bool CpuAir<FieldElementT, LayoutId>::UsesMulModBuiltin() const {
if constexpr (CpuAir::kIsDynamicAir) { // NOLINT: clang-tidy if constexpr bug.
return this->dynamic_params_[CpuAir::kUsesMulModBuiltinDynamicParam];
} else { // NOLINT: clang-tidy if constexpr bug.
return CpuAir::kHasMulModBuiltin;
}
}

template <typename FieldElementT, int LayoutId>
void CpuAir<FieldElementT, LayoutId>::BuildPeriodicColumns(
const FieldElementT& gen, Builder* builder) const {
Expand Down Expand Up @@ -286,22 +343,27 @@ template <typename FieldElementT, int LayoutId>
CpuAir<FieldElementT, LayoutId> CpuAir<FieldElementT, LayoutId>::WithInteractionElementsImpl(
gsl::span<const FieldElementT> interaction_elms) const {
CpuAir new_air(*this);
ASSERT_RELEASE(
interaction_elms.size() == (CpuAir::kHasDilutedPool ? 6 : 3),
"Interaction element vector is of wrong size.");
new_air.memory__multi_column_perm__perm__interaction_elm_ = interaction_elms[0];
new_air.memory__multi_column_perm__hash_interaction_elm0_ = interaction_elms[1];
new_air.range_check16__perm__interaction_elm_ = interaction_elms[2];
unsigned int i = 0;
new_air.memory__multi_column_perm__perm__interaction_elm_ = interaction_elms[i++];
new_air.memory__multi_column_perm__hash_interaction_elm0_ = interaction_elms[i++];
new_air.range_check16__perm__interaction_elm_ = interaction_elms[i++];
new_air.memory__multi_column_perm__perm__public_memory_prod_ = new_air.GetPublicMemoryProd();
if constexpr (CpuAir::kHasDilutedPool) { // NOLINT: clang-tidy if constexpr bug.
new_air.diluted_check__permutation__interaction_elm_ = interaction_elms[3];
new_air.diluted_check__interaction_z_ = interaction_elms[4];
new_air.diluted_check__interaction_alpha_ = interaction_elms[5];
new_air.diluted_check__permutation__interaction_elm_ = interaction_elms[i++];
new_air.diluted_check__interaction_z_ = interaction_elms[i++];
new_air.diluted_check__interaction_alpha_ = interaction_elms[i++];
new_air.diluted_check__final_cum_val_ =
DilutedCheckComponentProverContext1<FieldElementT>::ExpectedFinalCumulativeValue(
CpuAir::kDilutedSpacing, CpuAir::kDilutedNBits, new_air.diluted_check__interaction_z_,
new_air.diluted_check__interaction_alpha_);
}
if constexpr (CpuAir::kHasAddModBuiltin) {
new_air.add_mod__interaction_elm_ = interaction_elms[i++];
}
if constexpr (CpuAir::kHasMulModBuiltin) {
new_air.mul_mod__interaction_elm_ = interaction_elms[i++];
}
ASSERT_RELEASE(interaction_elms.size() == i, "Interaction element vector is of wrong size.");
return new_air;
}

Expand Down Expand Up @@ -366,7 +428,7 @@ std::pair<CpuAirProverContext1<FieldElementT>, Trace> CpuAir<FieldElementT, Layo
if (UsesRangeCheck96Builtin()) {
rc96_prover.emplace(
"range_check96_builtin", ctx_, &memory_pool, &rc16_pool, this->range_check96_begin_addr_,
SafeDiv(n_steps_, RangeCheck96Ratio()), CpuAir::kRangeCheck96NParts, CpuAir::kOffsetBits,
RangeCheck96NInstances(), CpuAir::kRangeCheck96NParts, CpuAir::kOffsetBits,
RangeCheckBuiltinProverContext<FieldElementT>::ParsePrivateInput(
private_input["range_check96"]));

Expand Down Expand Up @@ -458,6 +520,46 @@ std::pair<CpuAirProverContext1<FieldElementT>, Trace> CpuAir<FieldElementT, Layo
}
}

// AddMod builtin.
if constexpr (CpuAir::kHasAddModBuiltin) { // NOLINT: clang-tidy if constexpr bug.
if (UsesAddModBuiltin()) {
ProfilingBlock add_mod_builtin_block("AddMod builtin");
AddModBuiltinProverContext<FieldElementT, CpuAir::kAddModNWords>(
/*name=*/"add_mod",
/*ctx=*/ctx_,
/*memory_pool=*/&memory_pool,
/*begin_addr=*/this->add_mod_begin_addr_,
/*n_instances=*/AddModNInstances(),
/*batch_size=*/CpuAir::kAddModBatchSize,
/*word_bit_len=*/CpuAir::kAddModWordBitLen,
/*inputs=*/
ModBuiltinProverContext<FieldElementT, CpuAir::kAddModNWords>::ParsePrivateInput(
private_input["add_mod"], CpuAir::kAddModBatchSize))
.WriteTrace(trace_spans);
}
}

// MulMod builtin.
if constexpr (CpuAir::kHasMulModBuiltin) { // NOLINT: clang-tidy if constexpr bug.
if (UsesMulModBuiltin()) {
ProfilingBlock mul_mod_builtin_block("MulMod builtin");
MulModBuiltinProverContext<FieldElementT, CpuAir::kMulModNWords>(
/*name=*/"mul_mod",
/*ctx=*/ctx_,
/*memory_pool=*/&memory_pool,
/*rc_pool=*/&rc16_pool,
/*begin_addr=*/this->mul_mod_begin_addr_,
/*n_instances=*/MulModNInstances(),
/*batch_size=*/CpuAir::kMulModBatchSize,
/*word_bit_len=*/CpuAir::kMulModWordBitLen,
/*bits_per_part=*/16,
/*inputs=*/
ModBuiltinProverContext<FieldElementT, CpuAir::kMulModNWords>::ParsePrivateInput(
private_input["mul_mod"], CpuAir::kMulModBatchSize))
.WriteTrace(trace_spans);
}
}

// Finalize.
rc16_pool.Finalize(rc_min_, rc_max_, trace_spans);
if (rc_prover.has_value()) {
Expand Down
17 changes: 17 additions & 0 deletions src/starkware/air/cpu/board/cpu_air_definition0.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class CpuAirDefinition<FieldElementT, 0> : public Air {
static constexpr bool kHasPoseidonBuiltin = false;
static constexpr bool kHasRangeCheck96Builtin = false;
static constexpr bool kHasAddModBuiltin = false;
static constexpr bool kHasMulModBuiltin = false;
static constexpr char kLayoutName[] = "small";
static constexpr BigInt<4> kLayoutCode = 0x736d616c6c_Z;
static constexpr uint64_t kConstraintDegree = 2;
Expand Down Expand Up @@ -589,6 +590,10 @@ class CpuAirDefinition<FieldElementT, 0> : public Air {
kHasKeccakBuiltin ? GetSegment(mem_segment_addresses, "keccak").begin_addr : 0),
poseidon_begin_addr_(
kHasPoseidonBuiltin ? GetSegment(mem_segment_addresses, "poseidon").begin_addr : 0),
add_mod_begin_addr_(
kHasAddModBuiltin ? GetSegment(mem_segment_addresses, "add_mod").begin_addr : 0),
mul_mod_begin_addr_(
kHasMulModBuiltin ? GetSegment(mem_segment_addresses, "mul_mod").begin_addr : 0),
dynamic_params_(ParseDynamicParams(dynamic_params)),

range_check_min_(rc_min),
Expand Down Expand Up @@ -640,6 +645,14 @@ class CpuAirDefinition<FieldElementT, 0> : public Air {
const CompileTimeOptional<FieldElementT, kHasPoseidonBuiltin> initial_poseidon_addr_ =
FieldElementT::FromUint(ExtractHiddenMemberValue(poseidon_begin_addr_));

const CompileTimeOptional<uint64_t, kHasAddModBuiltin> add_mod_begin_addr_;
const CompileTimeOptional<FieldElementT, kHasAddModBuiltin> add_mod__initial_mod_addr_ =
FieldElementT::FromUint(ExtractHiddenMemberValue(add_mod_begin_addr_));

const CompileTimeOptional<uint64_t, kHasMulModBuiltin> mul_mod_begin_addr_;
const CompileTimeOptional<FieldElementT, kHasMulModBuiltin> mul_mod__initial_mod_addr_ =
FieldElementT::FromUint(ExtractHiddenMemberValue(mul_mod_begin_addr_));

// Flat vector of dynamic_params, used for efficient computation of the composition polynomial.
// See ParseDynamicParams.
CompileTimeOptional<std::vector<uint64_t>, kIsDynamicAir> dynamic_params_;
Expand All @@ -660,6 +673,10 @@ class CpuAirDefinition<FieldElementT, 0> : public Air {
FieldElementT::Uninitialized();
CompileTimeOptional<FieldElementT, kHasDilutedPool> diluted_check__interaction_alpha_ =
FieldElementT::Uninitialized();
CompileTimeOptional<FieldElementT, kHasAddModBuiltin> add_mod__interaction_elm_ =
FieldElementT::Uninitialized();
CompileTimeOptional<FieldElementT, kHasMulModBuiltin> mul_mod__interaction_elm_ =
FieldElementT::Uninitialized();

FieldElementT memory__multi_column_perm__perm__public_memory_prod_ =
FieldElementT::Uninitialized();
Expand Down
17 changes: 17 additions & 0 deletions src/starkware/air/cpu/board/cpu_air_definition1.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class CpuAirDefinition<FieldElementT, 1> : public Air {
static constexpr bool kHasPoseidonBuiltin = false;
static constexpr bool kHasRangeCheck96Builtin = false;
static constexpr bool kHasAddModBuiltin = false;
static constexpr bool kHasMulModBuiltin = false;
static constexpr char kLayoutName[] = "dex";
static constexpr BigInt<4> kLayoutCode = 0x646578_Z;
static constexpr uint64_t kConstraintDegree = 2;
Expand Down Expand Up @@ -585,6 +586,10 @@ class CpuAirDefinition<FieldElementT, 1> : public Air {
kHasKeccakBuiltin ? GetSegment(mem_segment_addresses, "keccak").begin_addr : 0),
poseidon_begin_addr_(
kHasPoseidonBuiltin ? GetSegment(mem_segment_addresses, "poseidon").begin_addr : 0),
add_mod_begin_addr_(
kHasAddModBuiltin ? GetSegment(mem_segment_addresses, "add_mod").begin_addr : 0),
mul_mod_begin_addr_(
kHasMulModBuiltin ? GetSegment(mem_segment_addresses, "mul_mod").begin_addr : 0),
dynamic_params_(ParseDynamicParams(dynamic_params)),

range_check_min_(rc_min),
Expand Down Expand Up @@ -636,6 +641,14 @@ class CpuAirDefinition<FieldElementT, 1> : public Air {
const CompileTimeOptional<FieldElementT, kHasPoseidonBuiltin> initial_poseidon_addr_ =
FieldElementT::FromUint(ExtractHiddenMemberValue(poseidon_begin_addr_));

const CompileTimeOptional<uint64_t, kHasAddModBuiltin> add_mod_begin_addr_;
const CompileTimeOptional<FieldElementT, kHasAddModBuiltin> add_mod__initial_mod_addr_ =
FieldElementT::FromUint(ExtractHiddenMemberValue(add_mod_begin_addr_));

const CompileTimeOptional<uint64_t, kHasMulModBuiltin> mul_mod_begin_addr_;
const CompileTimeOptional<FieldElementT, kHasMulModBuiltin> mul_mod__initial_mod_addr_ =
FieldElementT::FromUint(ExtractHiddenMemberValue(mul_mod_begin_addr_));

// Flat vector of dynamic_params, used for efficient computation of the composition polynomial.
// See ParseDynamicParams.
CompileTimeOptional<std::vector<uint64_t>, kIsDynamicAir> dynamic_params_;
Expand All @@ -656,6 +669,10 @@ class CpuAirDefinition<FieldElementT, 1> : public Air {
FieldElementT::Uninitialized();
CompileTimeOptional<FieldElementT, kHasDilutedPool> diluted_check__interaction_alpha_ =
FieldElementT::Uninitialized();
CompileTimeOptional<FieldElementT, kHasAddModBuiltin> add_mod__interaction_elm_ =
FieldElementT::Uninitialized();
CompileTimeOptional<FieldElementT, kHasMulModBuiltin> mul_mod__interaction_elm_ =
FieldElementT::Uninitialized();

FieldElementT memory__multi_column_perm__perm__public_memory_prod_ =
FieldElementT::Uninitialized();
Expand Down
Loading

0 comments on commit 1414a54

Please sign in to comment.