Skip to content

Commit

Permalink
chore: sync upstream (#35)
Browse files Browse the repository at this point in the history
* Make GateRef value public

* Better document constraints on addcy carries (0xPolygonZero#1139)

* Add missing constraints on addcy carries.

* Remove bit-checks; make documentation clearer.

* Constrain keccak general

* Reuse set_public_value_targets

* Fix endianness in benefiary limbs

* Convert to u32 instead of u64

* Clippy

* Silence Poseidon warnings for ARM targets

* Remove unused attributes

* Fix trait import. (0xPolygonZero#1163)

* Error instead of panicking for missing preprocessed circuits (0xPolygonZero#1159)

* Set exception flag to 1.

* Connect public values in aggregation circuit (0xPolygonZero#1169)

* Connect public values in aggregation circuit

* Minor

* Write trie roots to memory before kernel bootstrapping  (0xPolygonZero#1172)

* Write trie roots

* Remove CPU trace length

* Update hash_initial/final_tries

* Fix tests

* Minor

* PR feedback

* Connect SHL/SHR operations to the Arithmetic table (0xPolygonZero#1166)

* Add corresponding arithmetic operations to shift ones

* Include SHL/SHR in the arithmetic CTL

* Prevent overflow

* Expand documentation for ctl_data_ternops()

* Combine AND and OR flags in CpuStark

* Reduce reallocations

* Address review

* Apply Nicholas comment

* Revert changes in cyclic_subgroup_unknown_order

* Fix logic CTL

* Combine all logic flags together

* Comment

* Combine EQ and ISZERO flags

* Combine jump flags

* Add guidance for external contributors to README.md

* Use Keccak config in simple tests

* Remove is_cpu_cycle

* Remove is_bootstrap_kernel column

* Implement inverse from Fermat little theorem (0xPolygonZero#1176)

* Add inverse from Fermat little theorem

* Remove inlining for goldilocks try_inverse method

* Remove copy on write for mpt_insert and mpt_delete

* Combine a few constraints

* Reduce overconstraining in decode module

* Remove filtering in membus

* Observe public values

* Update tests to have a blockgaslimit fitting u32s

* Update BlockBaseFee to fit in 2 limbs

* Refactor

* Apply comment

* Combine get_context and set_context into one flag

* Remove unnecessary changes in the Operation enum

* Apply comment

* Patched plonky2 to use a patch for eth_trie_utils

* Implement receipts and logs

Co-authored-by: Hamy Ratoanina <[email protected]>
Co-authored-by: Linda Guiga <[email protected]>

* Cleanup

* Fix tests and address comments

* Change receipts_trie in basic_smart_contract and self_balance_gas_cost

* Cleanup

* Clippy

* Made `PublicValues` serializable

- Needed by the prover scheduler.

* make generate partial_witness pub

* Update range from ReceiptTrie PR

* fix: constrain higher bits of reg_preimage

* Keccak STARK: constraint preimage to equal A on first round

* Constrain next row's stack length

* Add missing links between public values

* Implement receipts of types 1 and 2

* Apply comments

* Fix comment in `proof.rs`

* Apply comments

* Replace genesis state trie check with TODO

* feat: serde for targets

* Display actual trace lengths instead of number of ops

* Remove duplicate code

* Remove filter column for KeccakStark

* Added mock feature flag and test

* remove spurious

* Add blockhash sys opcode

* Apply comments

* Change h256_ulimbs

* Fix overflow check and test. Remove [..8] when using h256_limbs.

* Clippy

* Fix memop reads, from_prover_inputs and cleanup.

* Fix Clippy

* Removed mock feature flag and added mock_build

* clippy

* Now builds on the latest nightly

* Also included clippy fixes introduced by new nightly

* Apply Nick's comment

* Apply Nick's comment

* Now refers to sub-crates using paths (and removed `patch` section)

- Previously refered to specific `crates.io` versions.
- Motivation for this is to allow external projects to be able use
  specific revisions of this repo. Without this, a `[patch]` section
  is almost always required in the external project in order to force
  the internal plonky2 sub-crates to the same version, an approach which
  comes with its own issues.

* Fixes

* clippy

* Made visibilities outside of crate to allow for forking partial witness gen outside of crate

* latest nightly in CI and rust-toolchain

* suppress incorrect Clippy error

* fmt

* clippy suggestions

* clippy suggestions

* clippy suggestions

* clippy suggestions

* clippy suggestions

* more clippy suggestions

* Move byte packing / unpacking to a distinct table (0xPolygonZero#1212)

* Duplicate Memory trace into BytePacking one

* Add mload_32bytes instruction

* Use dedicated ops for byte packing trace

* Change witness generation to reduce memory reads for MLOAD_32BYTES

* Remove segments

* Fix stack

* Fix extra product when fixing CTL for byte_packing

* Write output value in trace

* Add constraints for BYTE_PACKING table

* Add recursive constraints for BYTE_PACKING table

* Fix constraints

* Add address in trace and constraints

* Add timestamp and batch inputs into BytePackingOp struct

* Add extra column

* Fix BytePackingStark CTL

* Tiny fix in witness generation

* Fix the Memory CTL

* Add constraints for the new columns

* Remove 1 column

* Remove limb columns

* Fix

* Fix recursive circuit of BytePackingTable

* Fix constraints

* Fix endianness

* Add MSTORE_32BYTES instruction and move decomposition to packing table

* Add missing constraint

* Add range-check for all bytes

* Add extra constraint

* Cleanup

* Remove REMAINING_LEN column

* Add corresponding implementations in interpreter

* Fix recursive version

* Remove debug assertion because of CI

* Remove FILTER column

* Update new test from rebasing

* Reorder STARK modules to match TraceCheckPoint ordering

* Address comments

* Pacify clippy

* Add documentation to the packing module

* Fix doctest

* Swap ordering in stack macro (0xPolygonZero#1230)

* Swap ordering in stack macro

* Update comment

* Fix self_balance_gas_cost and basic_smart_contract. (0xPolygonZero#1227)

* Fix self_balance_gas_cost and basic_smart_contract.

* Fix Clippy

* Combine arithmetic flags on the CPU side (0xPolygonZero#1187)

* Combine FP254 flags

* Combine basic binary ops together and do CTL with opcode value

* Combine ternary ops together

* Combine MUL DIV and MOD

* Combine shift operations

* Combine byte with other binary ops

* Fix tests

* Clean leftover comment

* Update from latest main

* Put the 'is_simulated' flag inside the Operation enum

* Cleaner way to handle "simulated" operations SHL and SHR.

* Fix comments.

* Minor: suggestion for re-expressing `combined_ops`.

* Update comment

---------

Co-authored-by: Hamish Ivey-Law <[email protected]>

* Remove redundant Keccak sponge cols (0xPolygonZero#1233)

* Rename columns in KeccakSponge for clarity

* Remove redundant columns

* Apply comments

* Combine mstore_general and mload_general into one flag (0xPolygonZero#1188)

* Combine mstore_general and mload_general into one flag

* Add comments and make stack constraints cleaner.

* Fix number of native instructions

* Ordering

* Cleanup

* Update calls to stack eval from latest main

---------

Co-authored-by: Robin Salen <[email protected]>

---------

Co-authored-by: Dimo99 <[email protected]>
Co-authored-by: Hamish Ivey-Law <[email protected]>
Co-authored-by: Robin Salen <[email protected]>
Co-authored-by: Robin Salen <[email protected]>
Co-authored-by: Jacqueline Nabaglo <[email protected]>
Co-authored-by: Nicholas Ward <[email protected]>
Co-authored-by: Linda Guiga <[email protected]>
Co-authored-by: wborgeaud <[email protected]>
Co-authored-by: Hamy Ratoanina <[email protected]>
Co-authored-by: BGluth <[email protected]>
Co-authored-by: Linda Guiga <[email protected]>
Co-authored-by: John Guibas <[email protected]>
Co-authored-by: Daniel Lubarov <[email protected]>
Co-authored-by: Ayush Shukla <[email protected]>
Co-authored-by: Uma Roy <[email protected]>
Co-authored-by: Hamish Ivey-Law <[email protected]>
  • Loading branch information
17 people authored Sep 15, 2023
1 parent 27fe692 commit bd622e0
Show file tree
Hide file tree
Showing 34 changed files with 340 additions and 198 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-06-30
toolchain: nightly
override: true

- name: rust-cache
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-06-30
toolchain: nightly
override: true
components: rustfmt, clippy

Expand Down
7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,3 @@ incremental = true

[profile.bench]
opt-level = 3

[patch.crates-io]
plonky2_field = { path = "field" }
plonky2_maybe_rayon = { path = "maybe_rayon" }
plonky2 = { path = "plonky2" }
starky = { path = "starky" }
plonky2_util = { path = "util" }
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,108 @@ static GLOBAL: Jemalloc = Jemalloc;
Jemalloc is known to cause crashes when a binary compiled for x86 is run on an Apple silicon-based Mac under [Rosetta 2](https://support.apple.com/en-us/HT211861). If you are experiencing crashes on your Apple silicon Mac, run `rustc --print target-libdir`. The output should contain `aarch64-apple-darwin`. If the output contains `x86_64-apple-darwin`, then you are running the Rust toolchain for x86; we recommend switching to the native ARM version.


## Guidance for external contributors

Do you feel keen and able to help with Plonky2? That's great! We
encourage external contributions!

We want to make it easy for you to contribute, but at the same time we
must manage the burden of reviewing external contributions. We are a
small team, and the time we spend reviewing external contributions is
time we are not developing ourselves.

We also want to help you to avoid inadvertently duplicating work that
is already underway, or building something that we will not
want to incorporate.

First and foremost, please keep in mind that this is a highly
technical piece of software and contributing is only suitable for
experienced mathematicians, cryptographers and software engineers.

The Polygon Zero Team reserves the right to accept or reject any
external contribution for any reason, including a simple lack of time
to maintain it (now or in the future); we may even decline to review
something that is not considered a sufficiently high priority for us.

To avoid disappointment, please communicate your intention to
contribute openly, while respecting the limited time and availability
we have to review and provide guidance for external contributions. It
is a good idea to drop a note in our public Discord #development
channel of your intention to work on something, whether an issue, a
new feature, or a performance improvement. This is probably all that's
really required to avoid duplication of work with other contributors.

What follows are some more specific requests for how to write PRs in a
way that will make them easy for us to review. Deviating from these
guidelines may result in your PR being rejected, ignored or forgotten.


### General guidance for your PR

Obviously PRs will not be considered unless they pass our Github
CI. The Github CI is not executed for PRs from forks, but you can
simulate the Github CI by running the commands in
`.github/workflows/ci.yml`.

Under no circumstances should a single PR mix different purposes: Your
PR is either a bug fix, a new feature, or a performance improvement,
never a combination. Nor should you include, for example, two
unrelated performance improvements in one PR. Please just submit
separate PRs. The goal is to make reviewing your PR as simple as
possible, and you should be thinking about how to compose the PR to
minimise the burden on the reviewer.

Also note that any PR that depends on unstable features will be
automatically rejected. The Polygon Zero Team may enable a small
number of unstable features in the future for our exclusive use;
nevertheless we aim to minimise the number of such features, and the
number of uses of them, to the greatest extent possible.

Here are a few specific guidelines for the three main categories of
PRs that we expect:


#### The PR fixes a bug

In the PR description, please clearly but briefly describe

1. the bug (could be a reference to a GH issue; if it is from a
discussion (on Discord/email/etc. for example), please copy in the
relevant parts of the discussion);
2. what turned out to the cause the bug; and
3. how the PR fixes the bug.

Wherever possible, PRs that fix bugs should include additional tests
that (i) trigger the original bug and (ii) pass after applying the PR.


#### The PR implements a new feature

If you plan to contribute an implementation of a new feature, please
double-check with the Polygon Zero team that it is a sufficient
priority for us that it will be reviewed and integrated.

In the PR description, please clearly but briefly describe

1. what the feature does
2. the approach taken to implement it

All PRs for new features must include a suitable test suite.


#### The PR improves performance

Performance improvements are particularly welcome! Please note that it
can be quite difficult to establish true improvements for the
workloads we care about. To help filter out false positives, the PR
description for a performance improvement must clearly identify

1. the target bottleneck (only one per PR to avoid confusing things!)
2. how performance is measured
3. characteristics of the machine used (CPU, OS, #threads if appropriate)
4. performance before and after the PR


## Licenses

As this is a monorepo, see the individual crates within for license information.
Expand Down
2 changes: 1 addition & 1 deletion field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2021"
anyhow = { version = "1.0.40", default-features = false }
itertools = { version = "0.11.0", default-features = false, features = ["use_alloc"] }
num = { version = "0.4", default-features = false, features = ["alloc", "rand"] }
plonky2_util = { version = "0.1.0", default-features = false }
plonky2_util = { path = "../util", default-features = false }
rand = { version = "0.8.5", default-features = false, features = ["getrandom"] }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }
static_assertions = { version = "1.1.0", default-features = false }
Expand Down
58 changes: 55 additions & 3 deletions field/src/goldilocks_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use num::{BigUint, Integer};
use plonky2_util::{assume, branch_hint};
use serde::{Deserialize, Serialize};

use crate::inversion::try_inverse_u64;
use crate::ops::Square;
use crate::types::{Field, Field64, PrimeField, PrimeField64, Sample};

const EPSILON: u64 = (1 << 32) - 1;
Expand Down Expand Up @@ -95,9 +95,55 @@ impl Field for GoldilocksField {
Self::order()
}

#[inline(always)]
/// Returns the inverse of the field element, using Fermat's little theorem.
/// The inverse of `a` is computed as `a^(p-2)`, where `p` is the prime order of the field.
///
/// Mathematically, this is equivalent to:
/// $a^(p-1) = 1 (mod p)$
/// $a^(p-2) * a = 1 (mod p)$
/// Therefore $a^(p-2) = a^-1 (mod p)$
///
/// The following code has been adapted from winterfell/math/src/field/f64/mod.rs
/// located at https://github.com/facebook/winterfell.
fn try_inverse(&self) -> Option<Self> {
try_inverse_u64(self)
if self.is_zero() {
return None;
}

// compute base^(P - 2) using 72 multiplications
// The exponent P - 2 is represented in binary as:
// 0b1111111111111111111111111111111011111111111111111111111111111111

// compute base^11
let t2 = self.square() * *self;

// compute base^111
let t3 = t2.square() * *self;

// compute base^111111 (6 ones)
// repeatedly square t3 3 times and multiply by t3
let t6 = exp_acc::<3>(t3, t3);

// compute base^111111111111 (12 ones)
// repeatedly square t6 6 times and multiply by t6
let t12 = exp_acc::<6>(t6, t6);

// compute base^111111111111111111111111 (24 ones)
// repeatedly square t12 12 times and multiply by t12
let t24 = exp_acc::<12>(t12, t12);

// compute base^1111111111111111111111111111111 (31 ones)
// repeatedly square t24 6 times and multiply by t6 first. then square t30 and
// multiply by base
let t30 = exp_acc::<6>(t24, t6);
let t31 = t30.square() * *self;

// compute base^111111111111111111111111111111101111111111111111111111111111111
// repeatedly square t31 32 times and multiply by t31
let t63 = exp_acc::<32>(t31, t31);

// compute base^1111111111111111111111111111111011111111111111111111111111111111
Some(t63.square() * *self)
}

fn from_noncanonical_biguint(n: BigUint) -> Self {
Expand Down Expand Up @@ -402,6 +448,12 @@ pub(crate) unsafe fn reduce160(x_lo: u128, x_hi: u32) -> GoldilocksField {
GoldilocksField(t2)
}

/// Squares the base N number of times and multiplies the result by the tail value.
#[inline(always)]
fn exp_acc<const N: usize>(base: GoldilocksField, tail: GoldilocksField) -> GoldilocksField {
base.exp_power_of_2(N) * tail
}

#[cfg(test)]
mod tests {
use crate::{test_field_arithmetic, test_prime_field_arithmetic};
Expand Down
136 changes: 0 additions & 136 deletions field/src/inversion.rs

This file was deleted.

2 changes: 0 additions & 2 deletions field/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

extern crate alloc;

mod inversion;

pub(crate) mod arch;

pub mod batch_util;
Expand Down
6 changes: 3 additions & 3 deletions plonky2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ hashbrown = { version = "0.14.0", default-features = false, features = ["ahash",
keccak-hash = { version = "0.10.0", default-features = false }
itertools = { version = "0.11.0", default-features = false }
log = { version = "0.4.14", default-features = false }
plonky2_maybe_rayon = { version = "0.1.1", default-features = false }
plonky2_maybe_rayon = { path = "../maybe_rayon", default-features = false }
num = { version = "0.4", default-features = false, features = ["rand"] }
plonky2_field = { version = "0.1.1", default-features = false }
plonky2_util = { version = "0.1.1", default-features = false }
plonky2_field = { path = "../field", default-features = false }
plonky2_util = { path = "../util", default-features = false }
rand = { version = "0.8.4", default-features = false }
rand_chacha = { version = "0.3.1", optional = true, default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive", "rc"] }
Expand Down
Loading

0 comments on commit bd622e0

Please sign in to comment.