Skip to content

Commit

Permalink
Merge pull request #1534 from AztecProtocol/defi-bridge-project
Browse files Browse the repository at this point in the history
Hotfix release prep
  • Loading branch information
joss-aztec authored Sep 27, 2022
2 parents 17ee1fc + 15808f6 commit 27e63e9
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 449 deletions.
26 changes: 0 additions & 26 deletions src/aztec/ecc/curves/bn254/fq.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,29 +471,3 @@ TEST(fq, pow_regression_check)
EXPECT_EQ(zero.pow(uint256_t(0)), one);
}
// 438268ca91d42ad f1e7025a7b654e1f f8d9d72e0438b995 8c422ec208ac8a6e

TEST(fq, sqr_regression)
{
uint256_t values[] = { uint256_t(0xbdf876654b0ade1b, 0x2c3a66c64569f338, 0x2cd8bf2ec1fe55a3, 0x11c0ea9ee5693ede),
uint256_t(0x551b14ec34f2151c, 0x62e472ed83a2891e, 0xf208d5e5c9b5b3fb, 0x14315aeaf6027d8c),
uint256_t(0xad39959ae8013750, 0x7f1d2c709ab84cbb, 0x408028b80a60c2f1, 0x1dcd116fc26f856e),
uint256_t(0x95e967d30dcce9ce, 0x56139274241d2ea1, 0x85b19c1c616ec456, 0x1f1780cf9bf045b4),
uint256_t(0xbe841c861d8eb80e, 0xc5980d67a21386c0, 0x5fd1f1afecddeeb5, 0x24dbb8c1baea0250),
uint256_t(0x3ae4b3a27f05d6e3, 0xc5f6785b12df8d29, 0xc3a6c5f095103046, 0xd6b94cb2cc1fd4b),
uint256_t(0xc003c71932a6ced5, 0x6302a413f68e26e9, 0x2ed4a9b64d69fad, 0xfe61ffab1ae227d) };
for (auto& value : values) {
fq element(value);
EXPECT_EQ(element.sqr(), element * element);
}
}

TEST(fq, neg_and_self_neg_0_cmp_regression)
{
fq a = 0;
fq a_neg = -a;
EXPECT_EQ((a == a_neg), true);
a = 0;
a_neg = 0;
a_neg.self_neg();
EXPECT_EQ((a == a_neg), true);
}
12 changes: 0 additions & 12 deletions src/aztec/ecc/curves/secp256k1/secp256k1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,4 @@ TEST(secp256k1, derive_generators)
}
}
*/

TEST(secp256k1, neg_and_self_neg_0_cmp_regression)
{
secp256k1::fq a = 0;
secp256k1::fq a_neg = -a;
EXPECT_EQ((a == a_neg), true);
a = 0;
a_neg = 0;
a_neg.self_neg();
EXPECT_EQ((a == a_neg), true);
}

} // namespace test_secp256k1
20 changes: 6 additions & 14 deletions src/aztec/ecc/fields/asm_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"mulxq %[modulus_1], %%rdi, %%rcx \n\t" /* (t[2], t[3]) <- (modulus[1] * k) */ \
"adcq %%rcx, %%r10 \n\t" /* r[2] += t[3] + flag_c */ \
"adcq $0, %%r11 \n\t" /* r[4] += flag_c */ \
/* Partial fix "adcq $0, %%r12 \n\t"*/ /* r[4] += flag_c */ \
/* Partial fix "adcq $0, %%r12 \n\t"*/ /* r[4] += flag_c */ \
"addq %%rdi, %%r9 \n\t" /* r[1] += t[2] */ \
"mulxq %[modulus_2], %%rdi, %%rcx \n\t" /* (t[0], t[1]) <- (modulus[3] * k) */ \
"mulxq %[modulus_3], %%r8, %%rdx \n\t" /* (t[2], t[3]) <- (modulus[2] * k) */ \
Expand Down Expand Up @@ -540,7 +540,6 @@
"adcxq %%rcx, %%r13 \n\t" /* r[5] += t[4] + flag_o */ \
"adoxq %[zero_reference], %%r13 \n\t" /* r[5] += flag_o */ \
"adcxq %[zero_reference], %%r14 \n\t" /* r[6] += flag_c */ \
"adoxq %[zero_reference], %%r14 \n\t" /* r[6] += flag_o */ \
\
/* double result registers */ \
"adoxq %%r9, %%r9 \n\t" /* r[1] = 2r[1] */ \
Expand Down Expand Up @@ -574,12 +573,10 @@
"mulxq %[modulus_0], %%rdi, %%rcx \n\t" /* (t[0], t[1]) <- (modulus[0] * k) */ \
"adoxq %%rdi, %%r8 \n\t" /* r[0] += t[0] (%r8 now free) */ \
"mulxq %[modulus_3], %%r8, %%rdi \n\t" /* (t[2], t[3]) <- (modulus[2] * k) */ \
"adcxq %%rdi, %%r12 \n\t" /* r[4] += t[3] + flag_c */ \
"adoxq %%rcx, %%r9 \n\t" /* r[1] += t[1] + flag_o */ \
"adcxq %[zero_reference], %%r13 \n\t" /* r[5] += flag_c */ \
"adcxq %[zero_reference], %%r14 \n\t" /* r[6] += flag_c */ \
"adcxq %%rdi, %%r12 \n\t" /* r[4] += t[3] + flag_o */ \
"adoxq %%rcx, %%r9 \n\t" /* r[1] += t[1] + flag_c */ \
"adcxq %[zero_reference], %%r13 \n\t" /* r[5] += flag_o */ \
"mulxq %[modulus_1], %%rdi, %%rcx \n\t" /* (t[2], t[3]) <- (modulus[1] * k) */ \
"adcxq %[zero_reference], %%r15 \n\t" /* r[7] += flag_c */ \
"adoxq %%rcx, %%r10 \n\t" /* r[2] += t[3] + flag_o */ \
"adcxq %%rdi, %%r9 \n\t" /* r[1] += t[2] */ \
"adoxq %%r8, %%r11 \n\t" /* r[3] += t[2] + flag_o */ \
Expand All @@ -597,9 +594,6 @@
"adoxq %%rcx, %%r13 \n\t" /* r[5] += t[3] + flag_o */ \
"adcxq %[zero_reference], %%r13 \n\t" /* r[5] += flag_c */ \
"adoxq %[zero_reference], %%r14 \n\t" /* r[6] += flag_o */ \
"adcxq %[zero_reference], %%r14 \n\t" /* r[6] += flag_c */ \
"adoxq %[zero_reference], %%r15 \n\t" /* r[7] += flag_o */ \
"adcxq %[zero_reference], %%r15 \n\t" /* r[7] += flag_c */ \
"mulxq %[modulus_0], %%r8, %%rcx \n\t" /* (t[0], t[1]) <- (modulus[0] * k) */ \
"adcxq %%r8, %%r9 \n\t" /* r[1] += t[0] (%r9 now free) */ \
"adoxq %%rcx, %%r10 \n\t" /* r[2] += t[1] + flag_c */ \
Expand All @@ -620,14 +614,12 @@
"adcxq %%r8, %%r13 \n\t" /* r[5] += t[2] + flag_c */ \
"adoxq %%r9, %%r14 \n\t" /* r[6] += t[3] + flag_c */ \
"adcxq %[zero_reference], %%r14 \n\t" /* r[6] += flag_o */ \
"adoxq %[zero_reference], %%r15 \n\t" /* r[7] += flag_o */ \
"adcxq %[zero_reference], %%r15 \n\t" /* r[7] += flag_c */ \
"adoxq %[zero_reference], %%r15 \n\t" /* r[7] += flag_c */ \
"mulxq %[modulus_0], %%r8, %%r9 \n\t" /* (t[0], t[1]) <- (modulus[0] * k) */ \
"adcxq %%r8, %%r10 \n\t" /* r[2] += t[0] (%r10 now free) */ \
"adoxq %%r9, %%r11 \n\t" /* r[3] += t[1] + flag_c */ \
"adcxq %%rdi, %%r11 \n\t" /* r[3] += t[2] */ \
"adoxq %[zero_reference], %%r12 \n\t" /* r[4] += flag_o */ \
"adoxq %[zero_reference], %%r13 \n\t" /* r[5] += flag_o */ \
"adoxq %[zero_reference], %%r12 \n\t" /* r[4] += flag_c */ \
\
/* perform modular reduction: r[3] */ \
"movq %%r11, %%rdx \n\t" /* move r11 into %rdx */ \
Expand Down
1 change: 0 additions & 1 deletion src/aztec/ecc/fields/field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
namespace barretenberg {
template <class Params> struct alignas(32) field {
public:
// We don't initialize data by default since we'd lose a lot of time on pointless initializations.
field() noexcept {}

constexpr field(const uint256_t& input) noexcept
Expand Down
10 changes: 5 additions & 5 deletions src/aztec/ecc/fields/field_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ template <class T> constexpr field<T> field<T>::operator-() const noexcept
return p - *this; // modulus - *this;
}
constexpr field p{ twice_modulus.data[0], twice_modulus.data[1], twice_modulus.data[2], twice_modulus.data[3] };
return (p - *this).reduce_once(); // modulus - *this;
return p - *this; // modulus - *this;
}

template <class T> constexpr field<T> field<T>::operator-=(const field& other) noexcept
Expand All @@ -179,7 +179,7 @@ template <class T> constexpr void field<T>::self_neg() noexcept
*this = p - *this;
} else {
constexpr field p{ twice_modulus.data[0], twice_modulus.data[1], twice_modulus.data[2], twice_modulus.data[3] };
*this = (p - *this).reduce_once();
*this = p - *this;
}
}

Expand Down Expand Up @@ -236,9 +236,9 @@ template <class T> constexpr field<T> field<T>::to_montgomery_form() const noexc
constexpr field r_squared{ T::r_squared_0, T::r_squared_1, T::r_squared_2, T::r_squared_3 };

field result = *this;
result.self_reduce_once();
result.self_reduce_once();
result.self_reduce_once();
result.reduce_once();
result.reduce_once();
result.reduce_once();
return (result * r_squared).reduce_once();
}

Expand Down
13 changes: 1 addition & 12 deletions src/aztec/ecc/groups/affine_element.test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <ecc/curves/bn254/g1.hpp>
#include <ecc/curves/secp256k1/secp256k1.hpp>
#include <common/test.hpp>
#include <fstream>
#include <common/serialize.hpp>
Expand Down Expand Up @@ -29,21 +28,11 @@ TEST(affine_element, read_write_buffer)

// Regression test to ensure that the point at infinity is not equal to its coordinate-wise reduction, which may lie
// on the curve, depending on the y-coordinate.
TEST(affine_element, infinity_equality_regression)
TEST(affine_element, infinity_regression)
{
g1::affine_element P;
P.self_set_infinity();
g1::affine_element R(0, P.y);
ASSERT_FALSE(P == R);
}

// Regression test to ensure that the point at infinity is not equal to its coordinate-wise reduction, which may lie
// on the curve, depending on the y-coordinate.
TEST(affine_element, infinity_ordering_regression)
{
secp256k1::g1::affine_element P(0, 1), Q(0, 1);

P.self_set_infinity();
EXPECT_NE(P < Q, Q < P);
}
} // namespace test_affine_element
7 changes: 0 additions & 7 deletions src/aztec/ecc/groups/affine_element_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,6 @@ constexpr bool affine_element<Fq, Fr, T>::operator==(const affine_element& other
template <class Fq, class Fr, class T>
constexpr bool affine_element<Fq, Fr, T>::operator>(const affine_element& other) const noexcept
{
// We are setting point at infinity to always be the lowest element
if (is_point_at_infinity()) {
return false;
} else if (other.is_point_at_infinity()) {
return true;
}

if (x > other.x) {
return true;
} else if (x == other.x && y > other.y) {
Expand Down
3 changes: 1 addition & 2 deletions src/aztec/numeric/uint256/uint256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ class alignas(32) uint256_t {

uint64_t data[4];

constexpr std::pair<uint256_t, uint256_t> divmod(const uint256_t& b) const;

private:
constexpr std::pair<uint64_t, uint64_t> mul_wide(const uint64_t a, const uint64_t b) const;
constexpr std::pair<uint64_t, uint64_t> addc(const uint64_t a, const uint64_t b, const uint64_t carry_in) const;
Expand All @@ -164,6 +162,7 @@ class alignas(32) uint256_t {
const uint64_t b,
const uint64_t c,
const uint64_t carry_in) const;
constexpr std::pair<uint256_t, uint256_t> divmod(const uint256_t& b) const;
};

inline std::ostream& operator<<(std::ostream& os, uint256_t const& a)
Expand Down
6 changes: 0 additions & 6 deletions src/aztec/stdlib/primitives/bigfield/bigfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ template <typename Composer, typename T> class bigfield {
maximum_value = DEFAULT_MAXIMUM_LIMB;
}
}
friend std::ostream& operator<<(std::ostream& os, const Limb& a)
{
os << "{ " << a.element << " < " << a.maximum_value << " }";
return os;
}
Limb(const Limb& other) = default;
Limb(Limb&& other) = default;
Limb& operator=(const Limb& other) = default;
Expand Down Expand Up @@ -217,7 +212,6 @@ template <typename Composer, typename T> class bigfield {
const std::vector<bigfield>& to_sub,
bool enable_divisor_nz_check = false);

static bigfield sum(const std::vector<bigfield>& terms);
static bigfield internal_div(const std::vector<bigfield>& numerators,
const bigfield& denominator,
bool check_for_zero);
Expand Down
36 changes: 1 addition & 35 deletions src/aztec/stdlib/primitives/bigfield/bigfield.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ template <typename Composer> class stdlib_bigfield : public testing::Test {
typedef typename bn254::witness_ct witness_ct;

public:
// The bug happens when we are applying the CRT formula to a*b < r, which can happen when using the division
// operator
static void test_fuzzer_bug()
{
auto composer = Composer();
uint256_t value(2);
fq_ct tval = fq_ct::create_from_u512_as_witness(&composer, value);
fq_ct tval1 = tval - tval;
fq_ct tval2 = tval1 / tval;
(void)tval2;
auto prover = composer.create_prover();
auto verifier = composer.create_verifier();
waffle::plonk_proof proof = prover.construct_proof();
bool proof_result = verifier.verify_proof(proof);
EXPECT_EQ(proof_result, true);
}
static void test_bad_mul()
{

Expand Down Expand Up @@ -780,17 +764,6 @@ template <typename Composer> class stdlib_bigfield : public testing::Test {
bool proof_result = verifier.verify_proof(proof);
EXPECT_EQ(proof_result, true);
}

static void test_conditional_select_regression()
{
auto composer = Composer();
barretenberg::fq a(0);
barretenberg::fq b(1);
fq_ct a_ct(&composer, a);
fq_ct b_ct(&composer, b);
fq_ct selected = a_ct.conditional_select(b_ct, typename bn254::bool_ct(&composer, true));
EXPECT_EQ(barretenberg::fq((selected.get_value() % uint512_t(barretenberg::fq::modulus)).lo), b);
}
};

// Define types for which the above tests will be constructed.
Expand All @@ -801,10 +774,7 @@ typedef testing::Types<waffle::StandardComposer,
ComposerTypes;
// Define the suite of tests.
TYPED_TEST_SUITE(stdlib_bigfield, ComposerTypes);
TYPED_TEST(stdlib_bigfield, fuzzer_bug)
{
TestFixture::test_fuzzer_bug();
}

TYPED_TEST(stdlib_bigfield, badmul)
{
TestFixture::test_bad_mul();
Expand Down Expand Up @@ -870,10 +840,6 @@ TYPED_TEST(stdlib_bigfield, quotient_completeness_regression)
TestFixture::test_quotient_completeness();
}

TYPED_TEST(stdlib_bigfield, conditional_select_regression)
{
TestFixture::test_conditional_select_regression();
}
// // This test was disabled before the refactor to use TYPED_TEST's/
// TEST(stdlib_bigfield, DISABLED_test_div_against_constants)
// {
Expand Down
Loading

0 comments on commit 27e63e9

Please sign in to comment.