Skip to content

Commit

Permalink
Rename to_array to append
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Dec 22, 2023
1 parent 7e5cd34 commit f245d46
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 85 deletions.
16 changes: 8 additions & 8 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cairo_verifier::common::{
flip_endiannes::FlipEndiannessTrait, to_array::ToArrayTrait, blake2s::blake2s
flip_endiannes::FlipEndiannessTrait, array_append::ArrayAppendTrait, blake2s::blake2s
};
use poseidon::poseidon_hash_span;
use core::integer::BoundedU128;
Expand Down Expand Up @@ -28,8 +28,8 @@ impl ChannelImpl of ChannelTrait {

fn random_uint256_to_prover(ref self: Channel) -> u256 {
let mut hash_data = ArrayTrait::<u32>::new();
self.digest.to_array_be(ref hash_data);
self.counter.to_array_be(ref hash_data);
hash_data.append_big_endian(self.digest);
hash_data.append_big_endian(self.counter);
self.counter += 1;
blake2s(hash_data).flip_endiannes()
}
Expand Down Expand Up @@ -61,8 +61,8 @@ impl ChannelImpl of ChannelTrait {
let mut hash_data = ArrayTrait::<u32>::new();

assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1');
(self.digest + 1).to_array_be(ref hash_data);
value_u256.to_array_be(ref hash_data);
hash_data.append_big_endian(self.digest + 1);
hash_data.append_big_endian(value_u256);

self.digest = blake2s(hash_data).flip_endiannes();
self.counter = 0;
Expand All @@ -77,15 +77,15 @@ impl ChannelImpl of ChannelTrait {
let mut hash_data = ArrayTrait::<u32>::new();

assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1');
(self.digest + 1).to_array_be(ref hash_data);
hash_data.append_big_endian(self.digest + 1);

let mut i = 0;
loop {
if i == values.len() {
break;
};
let value_u256: u256 = (*values[i] * MONTGOMERY_R).into();
value_u256.to_array_be(ref hash_data);
hash_data.append_big_endian(value_u256);
i += 1;
};

Expand All @@ -97,7 +97,7 @@ impl ChannelImpl of ChannelTrait {
let mut hash_data = ArrayTrait::<u32>::new();

assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1');
(self.digest + 1).to_array_be(ref hash_data);
hash_data.append_big_endian(self.digest + 1);

let low: u32 = (value % 0x100000000).try_into().unwrap();
let high: u32 = (value / 0x100000000).try_into().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod blake2s;
mod flip_endiannes;
mod from_span;
mod horner_eval;
mod to_array;
mod array_append;
mod array_print;

#[cfg(test)]
Expand Down
63 changes: 63 additions & 0 deletions src/common/array_append.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use cairo_verifier::common::flip_endiannes::FlipEndiannessU32;

// 2^8 = 256
const U128maxU8: u128 = 256;
const U64maxU8: u64 = 256;
const U32maxU8: u32 = 256;
const U16maxU8: u16 = 256;

// 2^16 = 65536
const U128maxU16: u128 = 65536;
const U64maxU16: u64 = 65536;
const U32maxU16: u32 = 65536;

// 2^32 = 4294967296
const U128maxU32: u128 = 4294967296;
const U64maxU32: u64 = 4294967296;

trait ArrayAppendTrait<ArrayElement, AppendElement> {
fn append_little_endian(ref self: Array<ArrayElement>, element: AppendElement);
fn append_big_endian(ref self: Array<ArrayElement>, element: AppendElement);
}

impl ArrayU32AppendU256 of ArrayAppendTrait<u32, u256> {
fn append_little_endian(ref self: Array<u32>, element: u256) {
self.append_little_endian(element.low);
self.append_little_endian(element.high);
}

fn append_big_endian(ref self: Array<u32>, element: u256) {
self.append_big_endian(element.high);
self.append_big_endian(element.low);
}
}

impl ArrayU32AppendU128 of ArrayAppendTrait<u32, u128> {
fn append_little_endian(ref self: Array<u32>, mut element: u128) {
let mut i = 4;
loop {
if i != 0 {
i -= 1;
let (q, r) = DivRem::div_rem(element, U128maxU32.try_into().unwrap());
self.append(r.try_into().unwrap());
element = q;
} else {
break;
}
}
}

fn append_big_endian(ref self: Array<u32>, mut element: u128) {
let mut array = ArrayTrait::<u32>::new();
array.append_little_endian(element);
let mut i = array.len();
loop {
if i != 0 {
i -= 1;
self.append((*array.at(i)).flip_endiannes());
} else {
break;
}
}
}
}
6 changes: 3 additions & 3 deletions src/common/flip_endiannes.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cairo_verifier::common::{from_span::FromSpanTrait, to_array::ToArrayTrait};
use cairo_verifier::common::{from_span::FromSpanTrait, array_append::ArrayAppendTrait};

trait FlipEndiannessTrait<F> {
fn flip_endiannes(self: F) -> F;
Expand All @@ -16,15 +16,15 @@ impl FlipEndiannessU32 of FlipEndiannessTrait<u32> {
impl FlipEndiannessU256 of FlipEndiannessTrait<u256> {
fn flip_endiannes(self: u256) -> u256 {
let mut data = ArrayTrait::<u32>::new();
self.to_array_be(ref data);
data.append_big_endian(self);
data.span().from_span_le()
}
}

impl FlipEndiannessU128 of FlipEndiannessTrait<u128> {
fn flip_endiannes(self: u128) -> u128 {
let mut data = ArrayTrait::<u32>::new();
self.to_array_be(ref data);
data.append_big_endian(self);
data.span().from_span_le()
}
}
2 changes: 1 addition & 1 deletion src/common/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod test_blake2s;
mod test_flip_endiannes;
mod test_from_span;
mod test_horner_eval;
mod test_to_array;
mod test_array_append;
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
use cairo_verifier::common::to_array::ToArrayTrait;
use cairo_verifier::common::array_append::ArrayAppendTrait;

#[test]
#[available_gas(9999999999)]
fn test_to_array_le_1() {
fn test_array_append_le_1() {
let value = 1827398791_u128; // = 0x6cebe487
let mut result = ArrayTrait::<u32>::new();
value.to_array_le(ref result);
result.append_little_endian(value);
assert((*result[0]) == 0x6cebe487, 'Invalid value');
assert((*result[1]) == 0x00000000, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_to_array_le_2() {
fn test_array_append_le_2() {
let value = 18273987910128309_u128; // = 0x40ec185e0352b5
let mut result = ArrayTrait::<u32>::new();
value.to_array_le(ref result);
result.append_little_endian(value);
assert((*result[0]) == 0x5e0352b5, 'Invalid value');
assert((*result[1]) == 0x0040ec18, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_to_array_le_3() {
fn test_array_append_le_3() {
let value =
182739879101283091827398791012830918273987910128309_u256; // = 0x7d09239523c3e2d590e3500b26c941117ab49552b5
let mut result = ArrayTrait::<u32>::new();
value.to_array_le(ref result);
result.append_little_endian(value);
assert((*result[0]) == 0xb49552b5, 'Invalid value');
assert((*result[1]) == 0xc941117a, 'Invalid value');
assert((*result[2]) == 0xe3500b26, 'Invalid value');
Expand All @@ -37,11 +37,11 @@ fn test_to_array_le_3() {

#[test]
#[available_gas(9999999999)]
fn test_to_array_le_4() {
fn test_array_append_le_4() {
let value =
18273987910112830918273987910128309910128309182739879101283099101283091827399_u256; // = 0x2866b5eb89ac02463b066d1405c0e20f5a19ba600c90767713f2ae1b6f254ac7
let mut result = ArrayTrait::<u32>::new();
value.to_array_le(ref result);
result.append_little_endian(value);
assert((*result[0]) == 0x6f254ac7, 'Invalid value');
assert((*result[1]) == 0x13f2ae1b, 'Invalid value');
assert((*result[2]) == 0x0c907677, 'Invalid value');
Expand Down
63 changes: 0 additions & 63 deletions src/common/to_array.cairo

This file was deleted.

0 comments on commit f245d46

Please sign in to comment.