Skip to content

Commit

Permalink
Just a comment about -g2 if const fn never materializes
Browse files Browse the repository at this point in the history
  • Loading branch information
¨Jeff committed Mar 13, 2023
1 parent 36425dc commit a5b3f91
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions nugget_bls/any_tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "any_tools"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions nugget_bls/any_tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Very unfinsihed, but if const fn were going to take too long
then ideas here combined with lazy_static gives a substitute.

44 changes: 44 additions & 0 deletions nugget_bls/any_tools/src/lib-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

use ark_std::any::Any;

// use std::{any::Any};

pub struct AnyLinkedList {
fence:
start: AnyLLPtr,
}

pub type AnyLLPtr = AnyLL<dyn Any>;

pub struct AnyLL<T: ?Sized + 'static> {
next: AnyLLPtr,
any: T,
}

impl AnyLL<dyn Any> {
pub fn try_fetch<'a,T: 'static>(&'a self) -> Option<&'a T> {
let mut selfy: &'a Self = self;
loop {
if let Some(r) = selfy.any.downcast_ref::<T>() { return Some(r); }
if selfy.next.is_none() { return None; }
selfy = selfy.next.as_ref().unwrap()
}
}

pub fn fetch_or_extend<'a,T: 'static,F: FnOnce() -> T>(&'a mut self, init: F) -> &'a T {
let mut selfy: &'a mut Self = self;
loop {
if let Some(r) = selfy.any.downcast_ref::<T>() { return r; }
if selfy.next.is_none() {
let b = Box::new(AnyLL { next: None, any: init(), });
let new = unsafe { core::mem::transmute(&b.any) };
selfy.next = Some(b as Box<AnyLL<dyn Any>>);
return new;
// We deduce the above code is safe because you could comment
// the two lines containing new and uncomment the line below:
// return selfy.next.as_mut().unwrap().any.downcast_ref::<T>().unwrap();
}
selfy = selfy.next.as_mut().unwrap()
}
}
}
57 changes: 57 additions & 0 deletions nugget_bls/any_tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@


use ark_std::any::Any;

// use std::{any::Any};

pub struct AnyLinkedList {
fence:
start: AnyLLPtr,
}

pub type AnyLLPtr = Cell<Option<Box<AnyLL<dyn Any>>>>;

pub struct AnyLL<T: ?Sized + 'static> {
next: AnyLLPtr,
any: T,
}

impl AnyLL<dyn Any> {
pub fn try_fetch<'a,T: 'static>(&'a self) -> Option<&'a T> {
let mut selfy: &'a Self = self;
loop {
if let Some(r) = selfy.any.downcast_ref::<T>() { return Some(r); }
if selfy.next.is_none() { return None; }
selfy = selfy.next.as_ref().unwrap()
}
}

pub fn fetch_or_extend<'a,T: 'static,F: FnOnce() -> T>(&'a mut self, init: F) -> &'a T {
let mut selfy: &'a mut Self = self;
loop {
if let Some(r) = selfy.any.downcast_ref::<T>() { return r; }
if selfy.next.is_none() {
let b = Box::new(AnyLL { next: None, any: init(), });
let new = unsafe { core::mem::transmute(&b.any) };
selfy.next = Some(b as Box<AnyLL<dyn Any>>);
return new;
// We deduce the above code is safe because you could comment
// the two lines containing new and uncomment the line below:
// return selfy.next.as_mut().unwrap().any.downcast_ref::<T>().unwrap();
}
selfy = selfy.next.as_mut().unwrap()
}
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
4 changes: 4 additions & 0 deletions nugget_bls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub struct AggregateSignature<P: Pairing> {
agg_pk_g2: <P as Pairing>::G2Affine,
}

// TODO: We could precomute g2_minus_generator using lazy_static and
// the AnyLinkedList trick in nugget_bls/any_tools/src, but const fn
// should work eventually, so likely overkill now.

fn g2_minus_generator<P: Pairing>() -> <P as Pairing>::G2Prepared {
prepare_g2::<P>(- pk_in::<P>().0.into_group())
}
Expand Down

0 comments on commit a5b3f91

Please sign in to comment.